Spring Boot 整合 Dubbo和Zookeeper

环境介绍

  • zookeeper 安装
  • dubbo-admin 查看管理注册中心服务提供者和消费者

Zookeeper 安装

http://zookeeper.apache.org/releases.html

下载最新安装包,文件格式是tar.gz,可见Apache还是推荐在Linux上使用,但也提供了在windows上使用的脚本。
解压之后,修改数据和日志配置,注意在3.5.3版本上,conf文件夹里不在直接提供zoo.cfg,而是提供了一个example,zoo_sample.cfg。复制重命名为zoo.cfg,然后修改data位置新增log位置。注意,windows目录要用//分割。

dataDir=D:\\software\\zookeeper-3.5.3-beta\\zookeeper\\data
dataLogDir=D:\\software\\zookeeper-3.5.3-beta\\zookeeper\\log
  • 修改zkEnv.cmd
    防止win10 闪退,注意双引号的位置
if not exist "%JAVA_HOME%\bin\Java.exe"

set JAVA="%JAVA_HOME%\bin\java"

启动

使用zkServer.cmd启动Zookeeper即可。注意Zookeeper是运行在8080端口,服务接口是2181

Dubbo admin 搭建

https://github.com/alibaba/dubbo

https://github.com/dangdangdotcom/dubbox

下载dubbo,执行

mvn install -D maven.test.skip=true

然后在dubbo-admin下执行

mvn package -D maven.skip.test=true

打war包,发布到tomcat下,注意修改tomcat端口8081

创建主maven项目

这里写图片描述

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.rencc.dubbo</groupId>
    <artifactId>dubbo-spring-study</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>
        <module>dubbo-provider</module>
        <module>dubbo-consumer</module>
    </modules>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

</project>

创建子spring boot项目

  • dubbo-provider 服务提供者

在application.properties 里设置端口,目前8080和8081已被使用

server.port=8090

添加dubbo的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 配置可参考 http://dubbo.io/User+Guide-zh.htm -->
    <!-- 服务提供方应用名,用于计算依赖关系 -->
    <dubbo:application name="dubbo-provider" owner="dubbo-provider" />

    <!-- 定义 zookeeper 注册中心地址及协议 -->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" client="zkclient" />

    <!-- 定义 Dubbo 协议名称及使用的端口,dubbo 协议缺省端口为 20880,如果配置为 -1 或者没有配置 port,则会分配一个没有被占用的端口 -->
    <dubbo:protocol name="dubbo" port="-1" />

    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.rencc.dubbo.provider.service.DemoService"
        ref="demoService" timeout="10000" />

    <!-- 和本地 bean 一样实现服务 -->
    <bean id="demoService" class="com.rencc.dubbo.provider.serviceImpl.DemoServiceImpl" />

</beans>

DemoService 接口和实现类

package com.rencc.dubbo.provider.service;

public interface DemoService {
    String sayHello(String name);
}





package com.rencc.dubbo.provider.serviceImpl;

import com.rencc.dubbo.provider.service.DemoService;

public class DemoServiceImpl implements DemoService {

    public String sayHello(String name) {
        return "Welcome to Dubbo, Hello " + name;
    }

}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.rencc.dubbo.provider</groupId>
    <artifactId>dubbo-provider</artifactId>
    <packaging>jar</packaging>

    <name>dubbo-provider</name>
    <description>Demo project for Spring Boot</description>

    <parent>  
        <groupId>com.rencc.dubbo</groupId>
        <artifactId>dubbo-spring-study</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.5.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>1.5.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <version>1.5.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.4.10</version>
            <exclusions>
                <exclusion>
                    <artifactId>spring</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-log4j12</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.5.2.RELEASE</version>
            </plugin>
        </plugins>
    </build>


</project>

  • dubbo-consumer 服务消费者
    在此仅贴出dubbo代码配置。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 配置可参考 http://dubbo.io/User+Guide-zh.htm -->
    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="dubbo-consumer" owner="dubbo-consumer" />

    <!-- 定义 zookeeper 注册中心地址及协议 -->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" client="zkclient" />

    <!-- 生成远程服务代理,可以和本地 bean 一样使用 testService -->
    <dubbo:reference id="demoService" interface="com.rencc.dubbo.provider.service.DemoService" />

</beans>
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐