文章目录
  1. 1.不基于框架的开发
    1. 1.1 创建父模块 dubbo-demo
    2. 1.2 创建子模块 dubbo-common(公共接口)
    3. 1.3 创建子模块 dubbo-provider(服务提供者)
      1. 1.3.1 指定依赖库 pom.xml
      2. 1.3.2 配置日志(非必须)
      3. 1.3.3 配置 Dubbo
      4. 1.3.4 新增服务的实现类(实现 dubbo-common 中接口)
      5. 1.3.5 启动服务提供者
    4. 1.4 创建子模块 dubbo-consumer(服务消费者)
      1. 1.4.1 指定依赖库 pom.xml
      2. 1.4.2 配置日志(非必须)
      3. 1.4.3 配置 Dubbo
      4. 1.4.4 启动服务提供者
    5. 1.5 启动
      1. 1.5.1 启动服务注册中心(ZooKeeper)
      2. 1.5.2 启动服务提供者
      3. 1.5.3 启动服务消费者
  2. 2.基于 SpringBoot 开发

上篇文章 Dubbo 原理与部署 讲述了 Dubbo 注册中心(ZooKeeper)的搭建,但其中的服务提供者和消费者需要开发者实现,本文详述两者的实现方法。各方部署完毕后,一个基本的微服务架构就完成了。传统上是以 Spring 实现,现在也有 starter 帮助以 SpringBoot 的方式实现。

作者:王克锋
出处:https://kefeng.wang/2017/12/12/dubbo-development/
版权:自由转载-非商用-非衍生-保持署名,转载请标明作者和出处。

1.不基于框架的开发

1.1 创建父模块 dubbo-demo

创建 Maven 工程(不依赖于任何 Maven 框架),创建后删除 src 目录。

1.2 创建子模块 dubbo-common(公共接口)

新增服务接口 HelloService.java,服务的提供方和消费方必须使用相同的该接口。

1
2
3
public interface HelloService {
String sayHello(String name);
}

1.3 创建子模块 dubbo-provider(服务提供者)

1.3.1 指定依赖库 pom.xml

指定刚创建的 dubbo-common,还有ZooKeeper客户端、dubbo(内含 Spring)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<dependencies>
<dependency>
<artifactId>dubbo-common</artifactId>
<groupId>wang.kefeng</groupId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<dependency><!-- containts Spring 4.3.10.RELEASE -->
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.0</version><!-- 2018-01-07 -->
<exclusions>
<exclusion>
<artifactId>netty</artifactId>
<groupId>org.jboss.netty</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

1.3.2 配置日志(非必须)

resources/log4j.properties

1
2
3
4
log4j.rootLogger=INFO, STDOUT
log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern=%d{HH:mm:ss.SSS} %p [%F:%L] - %m%n

1.3.3 配置 Dubbo

resources/spring.xml,包括应用名称、注册中心(ZooKeeper)的地址、服务提供者协议及其选项、服务提供者的服务及其参数,注意 beans 节点参数 xmlns:dubbo/xsi:schemaLocation 中 dubbo 相关内容。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?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">

<!-- 当前应用名称,显示于注册中心 -->
<dubbo:application name="dubbo-provider"/>

<!-- 服务注册中心(可定义多条) -->
<dubbo:registry address="zookeeper://localhost:2181?backup=localhost:2182,localhost:2183"/>

<!-- 服务提供者协议(dubbo,rmi,http,redis/...)及其端口,最大连接数 -->
<dubbo:protocol name="dubbo" port="20880" accepts="256"/>

<!-- 服务接口及其实现、负载均衡策略为(random, roundrobin, leastactive)、容错策略、版本号(保证接口一致) -->
<dubbo:service interface="wang.kefeng.HelloService" class="wang.kefeng.HelloServiceImpl" loadbalance="roundrobin" cluster="failover" version="1.0"/>

</beans>

1.3.4 新增服务的实现类(实现 dubbo-common 中接口)

HelloServiceImpl.java

1
2
3
4
5
public class HelloServiceImpl implements HelloService {
public String sayHello(String name) {
return "Hello " + name;
}
}

1.3.5 启动服务提供者

DubboProvider.java

1
2
3
4
5
6
7
8
9
public class DubboProvider {
public static void main(String[] args) throws IOException {
try (ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml")) {
context.start();
System.out.println("***** [dubbo-provider] is ready.");
System.in.read(); // press any key to exit
}
}
}

1.4 创建子模块 dubbo-consumer(服务消费者)

1.4.1 指定依赖库 pom.xml

指定刚创建的 dubbo-common,还有ZooKeeper客户端、dubbo(内含 Spring)

1
<!-- 与 dubbo-provider 完全相同 -->

1.4.2 配置日志(非必须)

resources/log4j.properties

1
## 与 dubbo-provider 完全相同

1.4.3 配置 Dubbo

resources/spring.xml,包括应用名称、注册中心(ZooKeeper)的地址、服务消费者选项、服务消费者引用的服务的接口,注意 beans 节点参数 xmlns:dubbo/xsi:schemaLocation 中 dubbo 相关内容。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?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">

<!-- 当前应用名称,显示于注册中心 -->
<dubbo:application name="dubbo-consumer"/>

<!-- 服务注册中心(可定义多条),不存在时不报错 -->
<dubbo:registry address="zookeeper://localhost:2181?backup=localhost:2182,localhost:2183" check="false"/>

<!-- http://dubbo.io/books/dubbo-user-book/references/xml/dubbo-consumer.html -->
<!-- 设置 dubbo:reference 的缺省配置: 负载均衡策略为(random, roundrobin, leastactive),启动时不检查 -->
<dubbo:consumer loadbalance="roundrobin" check="false"/>

<!-- 服务接口及其 beanId、版本号(保证接口一致) -->
<dubbo:reference id="helloService" interface="wang.kefeng.HelloService" version="1.0"/>

</beans>

1.4.4 启动服务提供者

DubboConsumer.java

1
2
3
4
5
6
7
8
9
public class DubboConsumer {
public static void main(String[] args) {
try (ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml")) {
HelloService helloService = context.getBean("helloService", HelloService.class);
String result = helloService.sayHello("kefeng.wang");
System.out.println("***** [dubbo-consumer] done: " + result);
}
}
}

1.5 启动

1.5.1 启动服务注册中心(ZooKeeper)

开启了三个节点:localhost:{2181,2182,2183}

1.5.2 启动服务提供者

运行 dubbo-provider 下的 DubboProvider.java,从日志可看到服务 HelloService 注册到注册中心。

12:57:38.024 INFO [AbstractRegistry.java:272] - [DUBBO] Register: dubbo://192.168.1.101:20880/wang.kefeng.HelloService?accepts=256&anyhost=true&application=dubbo-provider&class=wang.kefeng.HelloServiceImpl&cluster=failover&dubbo=2.6.0&generic=false&interface=wang.kefeng.HelloService&loadbalance=roundrobin&methods=sayHello&pid=10000&revision=1.0&side=provider&timestamp=1516510657150&version=1.0, dubbo version: 2.6.0, current host: 127.0.0.1

12:57:38.320 INFO [AbstractRegistry.java:295] - [DUBBO] Subscribe: provider://192.168.1.101:20880/wang.kefeng.HelloService?accepts=256&anyhost=true&application=dubbo-provider&category=configurators&check=false&class=wang.kefeng.HelloServiceImpl&cluster=failover&dubbo=2.6.0&generic=false&interface=wang.kefeng.HelloService&loadbalance=roundrobin&methods=sayHello&pid=10000&revision=1.0&side=provider&timestamp=1516510657150&version=1.0, dubbo version: 2.6.0, current host: 127.0.0.1

* [dubbo-provider] is ready.

1.5.3 启动服务消费者

运行 dubbo-consumer 下的 DubboConsumer.java

12:58:39.977 INFO [AbstractRegistry.java:272] - [DUBBO] Register: consumer://192.168.1.101/wang.kefeng.HelloService?application=dubbo-consumer&category=consumers&check=false&default.check=false&default.loadbalance=roundrobin&dubbo=2.6.0&interface=wang.kefeng.HelloService&methods=sayHello&pid=10116&revision=1.0&side=consumer&timestamp=1516510719415&version=1.0, dubbo version: 2.6.0, current host: 192.168.1.101

12:58:40.320 INFO [AbstractRegistry.java:295] - [DUBBO] Subscribe: consumer://192.168.1.101/wang.kefeng.HelloService?application=dubbo-consumer&category=providers,configurators,routers&default.check=false&default.loadbalance=roundrobin&dubbo=2.6.0&interface=wang.kefeng.HelloService&methods=sayHello&pid=10116&revision=1.0&side=consumer&timestamp=1516510719415&version=1.0, dubbo version: 2.6.0, current host: 192.168.1.101

* [dubbo-consumer] done: Hello kefeng.wang

2.基于 SpringBoot 开发

spring-boot-start-dubbo,以 spring boot 的方式接入 dubbo,使得 dubbo 更容易地使用。与原生 dubbo 一样,推荐使用 zookeeper 作为注册中心。

官网: https://gitee.com/reger/spring-boot-starter-dubbo
文档很全面,内容包括示例、SpringBoot形式的配置参数项(包括注册中心、监控中心、服务提供者、服务消费者、应用等),文档很完整,这里不再重复。

Maven 依赖(最新版本号查看 这里):

1
2
3
4
5
<dependency>
<groupId>com.gitee.reger</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>1.0.10</version>
</dependency>

文章目录
  1. 1.不基于框架的开发
    1. 1.1 创建父模块 dubbo-demo
    2. 1.2 创建子模块 dubbo-common(公共接口)
    3. 1.3 创建子模块 dubbo-provider(服务提供者)
      1. 1.3.1 指定依赖库 pom.xml
      2. 1.3.2 配置日志(非必须)
      3. 1.3.3 配置 Dubbo
      4. 1.3.4 新增服务的实现类(实现 dubbo-common 中接口)
      5. 1.3.5 启动服务提供者
    4. 1.4 创建子模块 dubbo-consumer(服务消费者)
      1. 1.4.1 指定依赖库 pom.xml
      2. 1.4.2 配置日志(非必须)
      3. 1.4.3 配置 Dubbo
      4. 1.4.4 启动服务提供者
    5. 1.5 启动
      1. 1.5.1 启动服务注册中心(ZooKeeper)
      2. 1.5.2 启动服务提供者
      3. 1.5.3 启动服务消费者
  2. 2.基于 SpringBoot 开发