Dubbo
Dubbo 项目分层
传送门
Duboo 开发环境的搭建
创建项目
1# 创建dubbo项目并删除src目录
2
3# 右键dubbo项目,创建user-api子项目
4
5# 右键dubbo项目,创建user-service子项目
6
7# 右键dubbo项目,创建user-web子项目
parent 父项目限定版本
1<build>
2
3 <plugins>
4 <plugin>
5 <groupId>org.apache.maven.plugins</groupId>
6 <artifactId>maven-compiler-plugin</artifactId>
7 <version>2.3.2</version>
8 <configuration>
9 <source>1.8</source>
10 <target>1.8</target>
11 </configuration>
12 </plugin>
13 </plugins>
14 </build>
添加 Maven 依赖:user-service、user-web
1<!-- spring-boot-starter -->
2 <dependency>
3 <groupId>org.springframework.boot</groupId>
4 <artifactId>spring-boot-starter</artifactId>
5 </dependency>
6 <!-- Dubbo Spring Boot Starter -->
7 <dependency>
8 <groupId>org.apache.dubbo</groupId>
9 <artifactId>dubbo-spring-boot-starter</artifactId>
10 <version>2.7.0</version>
11 </dependency>
12 <dependency>
13 <groupId>org.apache.dubbo</groupId>
14 <artifactId>dubbo</artifactId>
15 </dependency>
16 <!-- spring-boot-starter-web user-web项目 需要引入此依赖-->
17 <dependency>
18 <groupId>org.springframework.boot</groupId>
19 <artifactId>spring-boot-starter-web</artifactId>
20 </dependency>
user-api 项目
假设存在一个 Dubbo RPC API ,由服务提供方为服务消费方暴露接口
1public interface UserService {
2 String sayHello();
3}
user-service 项目
实现 UserService 接口
1import org.apache.dubbo.config.annotation.Service;
2
3@Service(version = "1.0.0")
4public class UserServiceImpl implements UserService {
5 public String sayHello() {
6 return "hello";
7 }
8}
修改 SpringBoot 启动类添(加注解 @EnableAutoConfiguration)
1import org.springframework.boot.SpringApplication;
2import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
3
4public class ServiceApplication {
5 public static void main(String[] args) {
6 SpringApplication.run(ServiceApplication.class);
7 }
8}
添加配置文件 application.yml
1spring:
2 application:
3 name: dubbo-auto-configuration-provider-demo
4
5dubbo:
6 scan:
7 base-packages: com.soulboy.user.service.impl
8 protocol:
9 name: dubbo
10 port: 12345
11 registry:
12 address: N/A
user-web 项目
创建 Controller 层
1import org.apache.dubbo.config.annotation.Reference;
2import org.springframework.web.bind.annotation.RequestMapping;
3import org.springframework.web.bind.annotation.RestController;
4
5@RestController
6public class UserController {
7
8 @Reference(version = "1.0.0", url = "dubbo://127.0.0.1:12345")
9 private UserService userService;
10
11 @RequestMapping("/sayHello")
12 public String sayHello() {
13 return userService.sayHello();
14 }
15}
添加启动类
1import org.springframework.boot.SpringApplication;
2import org.springframework.boot.autoconfigure.SpringBootApplication;
3
4@SpringBootApplication
5public class WebApplication {
6 public static void main(String[] args) {
7 SpringApplication.run(WebApplication.class);
8 }
9}
添加配置文件 application.yml
1spring:
2 application:
3 name: dubbo-auto-configure-consumer-sample
测试
1http://localhost:8080/sayHello
2hello
Dubbo 架构流程
- Provider:暴露服务的服务提供方。
- Consumer:调用远程服务的服务消费方。
- Registry:服务注册与发现的注册中心。
- Monitor:统计服务的调用次数和调用时间的监控中心。
- Container:服务运行容器。
- 服务容器负责启动,加载,运行服务提供者。
- 服务提供者在启动时,向注册中心注册自己提供的服务。
- 服务消费者在启动时,向注册中心订阅自己所需的服务。
- 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
- 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
- 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
Dubbo 常见开发方式
- XML 配置
- 注解配置(2018 年开始支持)
常见注册中心
- simple 注册中心 ----不支持集群,不适用于生产环境
- Multicast 注册中心 ----开发阶段使用
- zookeeper 注册中心 ----目前企业中最常用,也是官方推荐
- Redis 注册中心 ----虽然支持,但是较少使用
- acos 注册中心 ----后起之秀,传送门。
Zookeeper 整合 Dubbo(注解)
Provider:user-service 项目
添加依赖
1<!-- Zookeeper -->
2 <dependency>
3 <groupId>org.apache.zookeeper</groupId>
4 <artifactId>zookeeper</artifactId>
5 </dependency>
6 <dependency>
7 <groupId>org.apache.curator</groupId>
8 <artifactId>curator-framework</artifactId>
9 </dependency>
10 <dependency>
11 <groupId>org.apache.curator</groupId>
12 <artifactId>curator-recipes</artifactId>
13 </dependency>
修改配置文件
1spring:
2 application:
3 name: dubbo-auto-configuration-provider-demo
4
5dubbo:
6 scan:
7 base-packages: com.soulboy.user.service.impl
8 protocol:
9 name: dubbo
10 port: 12345
11 registry:
12 address: zookeeper://192.168.31.220:2181
启动服务
12019-07-25 13:57:05.937 INFO 9492 --- [ main] o.a.c.f.imps.CuratorFrameworkImpl : Default schema
22019-07-25 13:57:10.450 INFO 9492 --- [68.31.220:2181)] org.apache.zookeeper.ClientCnxn : Opening socket connection to server 192.168.31.220/192.168.31.220:2181. Will not attempt to authenticate using SASL (unknown error)
32019-07-25 13:57:10.453 INFO 9492 --- [68.31.220:2181)] org.apache.zookeeper.ClientCnxn : Socket connection established to 192.168.31.220/192.168.31.220:2181, initiating session
42019-07-25 13:57:10.466 INFO 9492 --- [68.31.220:2181)] org.apache.zookeeper.ClientCnxn : Session establishment complete on server 192.168.31.220/192.168.31.220:2181, sessionid = 0x10014aa999f0014, negotiated timeout = 40000
52019-07-25 13:57:10.475 INFO 9492 --- [ain-EventThread] o.a.c.f.state.ConnectionStateManager : State change: CONNECTED
62019-07-25 13:57:11.805 INFO 9492 --- [ main] o.a.c.f.imps.CuratorFrameworkImpl : Starting
72019-07-25 13:57:11.805 INFO 9492 --- [ main] org.apache.zookeeper.ZooKeeper : Initiating client connection, connectString=192.168.31.220:2181 sessionTimeout=60000 watcher=org.apache.curator.ConnectionState@4f8caaf3
82019-07-25 13:57:11.806 INFO 9492 --- [ main] o.a.c.f.imps.CuratorFrameworkImpl : Default schema
92019-07-25 13:57:16.318 INFO 9492 --- [68.31.220:2181)] org.apache.zookeeper.ClientCnxn : Opening socket connection to server 192.168.31.220/192.168.31.220:2181. Will not attempt to authenticate using SASL (unknown error)
102019-07-25 13:57:16.319 INFO 9492 --- [68.31.220:2181)] org.apache.zookeeper.ClientCnxn : Socket connection established to 192.168.31.220/192.168.31.220:2181, initiating session
112019-07-25 13:57:16.331 INFO 9492 --- [68.31.220:2181)] org.apache.zookeeper.ClientCnxn : Session establishment complete on server 192.168.31.220/192.168.31.220:2181, sessionid = 0x10014aa999f0015, negotiated timeout = 40000
122019-07-25 13:57:16.331 INFO 9492 --- [ain-EventThread] o.a.c.f.state.ConnectionStateManager : State change: CONNECTED
132019-07-25 13:57:16.400 INFO 9492 --- [ main] c.x.user.service.ServiceApplication : Started ServiceApplication in 20.668 seconds (JVM running for 21.827)
142019-07-25 13:57:16.404 INFO 9492 --- [pool-1-thread-1] .b.c.e.AwaitingNonWebApplicationListener : [Dubbo] Current Spring Boot Application is await...
Consumer:user-web 项目
添加依赖
1<!-- Zookeeper -->
2 <dependency>
3 <groupId>org.apache.zookeeper</groupId>
4 <artifactId>zookeeper</artifactId>
5 </dependency>
6 <dependency>
7 <groupId>org.apache.curator</groupId>
8 <artifactId>curator-framework</artifactId>
9 </dependency>
10 <dependency>
11 <groupId>org.apache.curator</groupId>
12 <artifactId>curator-recipes</artifactId>
13 </dependency>
修改配置文件
1spring:
2 application:
3 name: dubbo-auto-configure-consumer-sample
4
5dubbo:
6 registry:
7 address: zookeeper://192.168.31.220:2181
修改 Controller 层代码
1import org.apache.dubbo.config.annotation.Reference;
2import org.springframework.web.bind.annotation.RequestMapping;
3import org.springframework.web.bind.annotation.RestController;
4
5@RestController
6public class UserController {
7 @Reference(version = "1.0.0")
8 private UserService userService;
9
10 @RequestMapping("/sayHello")
11 public String sayHello() {
12 return userService.sayHello();
13 }
14}
测试
1http://localhost:8080/sayHello
2hello
使用 zkCli 连接 zookeeper:并查看数据模型
1[zk: 192.168.31.220:2181(CONNECTED) 0] ls /
2[zookeeper, dubbo, soulboy0000000000]
3[zk: 192.168.31.220:2181(CONNECTED) 1] ls /dubbo
4[com.soulboy.user.service.UserService]
5[zk: 192.168.31.220:2181(CONNECTED) 10] ls /dubbo/com.soulboy.user.service.UserService
6
7Node does not exist: /dubbo/com.soulboy.user.service.UserService
8[zk: 192.168.31.220:2181(CONNECTED) 11] ls /dubbo
9[com.xdclass.user.service.UserService]
10[zk: 192.168.31.220:2181(CONNECTED) 12] ls /dubbo/com.soulboy.user.service.UserService
11[consumers, configurators, routers, providers]
12[zk: 192.168.31.220:2181(CONNECTED) 13] ls /dubbo/com.soulboy.user.service.UserService/consumers
13[consumer://169.254.192.38/com.xdclass.user.service.UserService?application=dubbo-auto-configure-consumer-sample&category=consumers&check=false&dubbo=2.0.2&interface=com.xdclass.user.service.UserService&methods=sayHello&pid=10400&qos.enable=false&release=2.7.0&revision=1.0.0&side=consumer×tamp=1564035312365&version=1.0.0]
Zookeeper 整合 Dubbo(XML)
Provider:user-service 项目
修改配置文件 properties.yml
1spring:
2 application:
3 name: dubbo-auto-configuration-provider-demo
新增配置文件 provider.xml
1<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2 xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
3 xmlns="http://www.springframework.org/schema/beans"
4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
5 http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
6 <dubbo:registry address="zookeeper://192.168.31.220:2181"/>
7 <dubbo:protocol name="dubbo" port="12345"/>
8 <bean id="userService" class="com.soulboy.user.service.impl.UserServiceImpl"/>
9 <dubbo:service interface="com.soulboy.user.service.UserService" ref="userService"/>
10</beans>
修改实现类,去掉 @Service 注解
1import org.springframework.stereotype.Service;
2
3public class UserServiceImpl implements UserService {
4 public String sayHello() {
5 return "hello";
6 }
7}
修改启动类,添加 @ImportResource 注解导入 XML 配置文件
1import org.springframework.boot.SpringApplication;
2import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
3import org.springframework.context.annotation.ImportResource;
4
5@EnableAutoConfiguration
6@ImportResource("provider.xml")
7public class ServiceApplication {
8 public static void main(String[] args) {
9 SpringApplication.run(ServiceApplication.class);
10 }
11}
Consumer:user-web 项目
修改配置文件 properties.yml
1spring:
2 application:
3 name: dubbo-auto-configure-consumer-sample
新增配置文件 consumer.xml
1<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2 xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
3 xmlns="http://www.springframework.org/schema/beans"
4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
5 http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
6 <dubbo:registry address="zookeeper://192.168.31.220:2181"/>
7 <dubbo:reference id="userService" check="false" interface="com.soulboy.user.service.UserService"/>
8</beans>
修改 Controller 层
1import org.springframework.beans.factory.annotation.Autowired;
2import org.springframework.web.bind.annotation.RequestMapping;
3import org.springframework.web.bind.annotation.RestController;
4
5@RestController
6public class UserController {
7
8 @Autowired
9 private UserService userService;
10
11 @RequestMapping("/sayHello")
12 public String sayHello() {
13 return userService.sayHello();
14 }
15}
修改启动类,添加 @ImportResource 注解导入 XML 配置文件
1import org.springframework.boot.SpringApplication;
2import org.springframework.boot.autoconfigure.SpringBootApplication;
3import org.springframework.context.annotation.ImportResource;
4
5@SpringBootApplication
6@ImportResource("consumer.xml")
7public class WebApplication {
8 public static void main(String[] args) {
9 SpringApplication.run(WebApplication.class);
10 }
11}
测试
1http://localhost:8080/sayHello
2hello
启动依赖检查
Dubbo 缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止 Spring 初始化完成,以便上线时,能及早发现问题,默认 check="true"。 也就是说 consumer 的启动依赖于 provider。可以通过 check="false" 关闭检查,比如,测试时,有些服务不关心,或者出现了循环依赖,必须有一方先启动。另外,如果你的 Spring 容器是懒加载的,或者通过 API 编程延迟引用服务,请关闭 check,否则服务临时不可用时,会抛出异常,拿到 null 引用,如果 check="false",总是会返回引用,当服务恢复时,能自动连上。
方式一:
1@Reference(version = "1.0.0",check =false)
2 private UserService userService;
方式二:(推荐)
1dubbo
2 registry:
3 address: zookeeper://192.168.31.220:2181
4 reference:
5 com:
6 #接口的全限定名
7 soulboy:
8 user:
9 service:
10 UserService:
11 check: false
各种配置对比
1* dubbo.reference.check=false,强制改变所有 reference 的 check 值,就算配置中有声明,也会被覆盖。
2* dubbo.consumer.check=false,是设置 check 的缺省值,如果配置中有显式的声明,如:<dubbo:reference check="true"/>,不会受影响。
3* dubbo.registry.check=false,前面两个都是指订阅成功,但提供者列表是否为空,是否报错,如果注册订阅失败时,也允许启动,需使用此选项,将在后台定时重试。
配置加载流程
配置优先级是从上往下一次递减,JVM -D 参数是优先级最高的,本地配置文件 dubbo.properties 是优先级最低的。配置来源如下:
- VM System Properties,-D 参数
- Externalized Configuration,外部化配置(2.7 版本)
- ServiceConfig、ReferenceConfig 等编程接口采集的配置
- 本地配置文件 dubbo.properties
超时机制与容错机制
当服务调用失败的时候,默认情况下,dubbo 会进行重试,默认重试次数为 2(不包含第一次),所以一共调用了 3 次。通过 retries="4" 设置重试次数。
1<dubbo:reference id="userService" check="false" interface="com.xdclass.user.service.UserService" retries="4"/>
服务提供方配置
- 方法级别(方法优先级设置高于服务优先级设置)
1<dubbo:service interface="com.xdclass.user.service.UserService" ref="userService" timeout="3000">
2 <dubbo:method name="sayHello" timeout="6000"></dubbo:method>
3 </dubbo:service>
- 服务级别
1<dubbo:service interface="com.xdclass.user.service.UserService" ref="userService" timeout="3000"/>
服务消费方配置
如果服务提供方跟服务消费方同时设置了超时时间,则以服务消费方为准。
- 方法级别
- 服务级别
1<dubbo:reference id="userService" check="false" interface="com.xdclass.user.service.UserService" timeout="6000"/>
集群容错模式
- Failover Cluster:失败自动切换,当出现失败,重试其它服务器通常用于读操作,但重试会带来更长延迟。可通过
retries="2"
来设置重试次数(不含第一次)。 - Failfast Cluster:失败安全,出现异常时,直接忽略。通常用于写入审计日志等操作。
- Failback Cluster:失败自动恢复,后台记录失败请求,定时重发。通常用于消息通知操作。
- Forking Cluster:并行调用多个服务器,只要一个成功即返回。通常用于实时性要求较高的读操作,但需要浪费更多服务资源。可通过
forks="2"
来设置最大并行数。 - Broadcast Cluster:广播调用所有提供者,逐个调用,任意一台报错则报错 。通常用于通知所有提供者更新缓存或日志等本地资源信息。
按照以下示例在服务提供方和消费方配置集群模式:
1<dubbo:service cluster="failsafe" />
2或
3<dubbo:reference cluster="failsafe" />
Dubbo 多协议配置
缺省协议,使用基于 Mina 1.1.7
和 hessian 3.2.1
的 tbremoting 交互。
- 连接个数:单连接
- 连接方式:长连接
- 传输协议:TCP
- 传输方式:NIO 异步传输
- 序列化:Hessian 二进制序列化
- 适用范围:传入传出参数数据包较小(建议小于 100K),消费者比提供者个数多,单一消费者无法压满提供者,尽量不要用 dubbo 协议传输大文件或超大字符串。
- 适用场景:常规远程服务方法调用
多协议配置示例(HTTP)如下:
API:user-api 项目新增接口
1public interface FileService {
2 void upload(String msg);
3}
Provider:user-service 添加实现类
1public class FileServiceImpl implements FileService {
2 @Override
3 public void upload(String msg) {
4 System.out.println("服务被调用" + msg);
5 }
6}
修改配置文件
1<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2 xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
3 xmlns="http://www.springframework.org/schema/beans"
4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
5 http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
6 <dubbo:registry address="zookeeper://192.168.31.220:2181"/>
7 <dubbo:protocol name="dubbo" port="12345"/>
8 <dubbo:protocol name="http" port="8888"/>
9 <bean id="userService" class="com.soulboy.user.service.impl.UserServiceImpl"/>
10 <bean id="fileService" class="com.soulboy.user.service.impl.FileServiceImpl"/>
11 <dubbo:service interface="com.soulboy.user.service.UserService" ref="userService" timeout="3000"/>
12 <dubbo:service interface="com.soulboy.user.service.FileService" ref="fileService" protocol="http"/>
13</beans>
服务提供者加入相应的依赖
1<!-- 多协议配置 http -->
2 <dependency>
3 <groupId>org.eclipse.jetty</groupId>
4 <artifactId>jetty-util</artifactId>
5 </dependency>
6 <dependency>
7 <groupId>javax.servlet</groupId>
8 <artifactId>javax.servlet-api</artifactId>
9 <version>4.0.1</version>
10 </dependency>
11
12 <!-- https://mvnrepository.com/artifact/org.eclipse.jetty/jetty-server -->
13 <dependency>
14 <groupId>org.eclipse.jetty</groupId>
15 <artifactId>jetty-server</artifactId>
16 </dependency>
17
18 <!-- https://mvnrepository.com/artifact/org.eclipse.jetty/jetty-servlet -->
19 <dependency>
20 <groupId>org.eclipse.jetty</groupId>
21 <artifactId>jetty-servlet</artifactId>
22 </dependency>
23
24 <dependency>
25 <groupId>org.springframework.boot</groupId>
26 <artifactId>spring-boot-starter-web</artifactId>
27 </dependency>
修改启动类
1import org.springframework.boot.WebApplicationType;
2import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
3import org.springframework.boot.builder.SpringApplicationBuilder;
4import org.springframework.context.annotation.ImportResource;
5
6@EnableAutoConfiguration
7@ImportResource("provider.xml")
8public class ServiceApplication {
9 public static void main(String[] args) {
10 new SpringApplicationBuilder(ServiceApplication.class).web(WebApplicationType.NONE).run();
11 }
12}
Consumer:user-web 修改配置文件
1<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2 xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
3 xmlns="http://www.springframework.org/schema/beans"
4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
5 http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
6 <dubbo:registry address="zookeeper://192.168.31.220:2181"/>
7 <dubbo:reference id="userService" check="false" interface="com.soulboy.user.service.UserService"/>
8 <dubbo:reference id="fileService" check="false" interface="com.soulboy.user.service.FileService"/>
9</beans>
修改 Controller 层代码
1import org.apache.dubbo.config.annotation.Reference;
2import org.springframework.beans.factory.annotation.Autowired;
3import org.springframework.web.bind.annotation.RequestMapping;
4import org.springframework.web.bind.annotation.RequestParam;
5import org.springframework.web.bind.annotation.RestController;
6
7@RestController
8public class UserController {
9
10 @Autowired
11 private UserService userService;
12
13 @Autowired
14 private FileService fileService;
15
16 @RequestMapping("/sayHello")
17 public String sayHello() {
18 return userService.sayHello();
19 }
20
21 @RequestMapping("/upload")
22 public void upload(@RequestParam(value = "msg") String msg){
23 fileService.upload(msg);
24 }
25}
测试
1http://localhost:8080/upload?msg=123
2user-service控制台输出:
3 服务被调用123
Dubbo 多注册中心
一个 Provider 可以同时想多个注册中心暴露自己的服务。
- 新增 <dubbo:registry address="zookeeper://192.1.1.101:2181"/>
- 多个注册中心使用 id 属性进行区分 id="remote".
- 服务暴露时,多个注册中心使用逗号分隔:registry="local,remote"
1<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2 xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
3 xmlns="http://www.springframework.org/schema/beans"
4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
5 http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
6 <dubbo:registry id="local" address="zookeeper://192.168.31.220:2181"/>
7 <dubbo:registry id="remote" address="zookeeper://192.168.31.240:2181"/>
8 <dubbo:protocol name="dubbo" port="12345"/>
9 <bean id="userService" class="com.soulboy.user.service.impl.UserServiceImpl"/>
10 <dubbo:service interface="com.soulboy.user.service.UserService" ref="userService" timeout="3000"/ registry="local,remote">
11</beans>
Dubbo 服务分组
一个 interface 可能会存在多个实现,这样会导致 Consumer 每次都随机消费同一个接口的不同实现类提供的服务。因此需要 Consumer 对调用做出明确的描述,Provider 可以通过服务分组的概念来解决此类问题。
新增实现类
1public class UserServiceImpl2 implements UserService {
2 @Override
3 public String sayHello() {
4 return "hello 2";
5 }
6}
修改 Provider 配置文件
1<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2 xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
3 xmlns="http://www.springframework.org/schema/beans"
4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
5 http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
6 <dubbo:registry address="zookeeper://192.168.31.220:2181"/>
7 <dubbo:protocol name="dubbo" port="12345"/>
8 <bean id="userService" class="com.soulboy.user.service.impl.UserServiceImpl"/>
9 <dubbo:service group="user1" interface="com.soulboy.user.service.UserService" ref="userService" timeout="3000"/>
10
11 <bean id="userService2" class="com.soulboy.user.service.impl.UserServiceImpl2"/>
12 <dubbo:service group="user2" interface="com.soulboy.user.service.UserService" ref="userService2" timeout="3000"/>
13</beans>
修改 Consumer 配置文件
1<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2 xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
3 xmlns="http://www.springframework.org/schema/beans"
4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
5 http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
6 <dubbo:registry address="zookeeper://192.168.31.220:2181"/>
7 <dubbo:reference id="userService" group="user2" interface="com.soulboy.user.service.UserService"/>
8</beans>
测试
1# 多次刷新依然是消费group="user2" 的接口实现类
2http://localhost:8080/sayHello
3hello 2
Dubbo 服务多版本化
当一个接口实现,出现不兼容升级时,可以用版本号过渡,版本号不同的服务相互间不引用。
- 在低压力时间段,先升级一半提供者为新版本 再将所有消费者升级为新版本 然后将剩下的一半提供者升级为新版本。
- 一旦服务提供者制定了版本,那么所有的服务消费者也是需要去指定相应的版本。
- 建议:暴露服务的时候,最好都指定下服务的版本,方便后续接口的不兼容升级。
1<!-- 老版本服务提供者配置 -->
2<dubbo:service interface="com.foo.BarService" version="1.0.0" />
3<!-- 老版本服务消费者配置 -->
4<dubbo:reference id="barService" interface="com.foo.BarService" version="1.0.0" />
5
6<!-- 新版本服务提供者配置 -->
7<dubbo:service interface="com.foo.BarService" version="2.0.0" />
8<!-- 新版本服务消费者配置 -->
9<dubbo:reference id="barService" interface="com.foo.BarService" version="2.0.0" />
管控台 Dubbo-ops
管控台的主要功能:服务查询,服务治理(包括 Dubbo2.7 中新增的治理规则)以及服务测试、配置管理。
1# 环境准备
2maven npm
3
4# 克隆项目到本地
5[root@localhost test]# git clone https://github.com/apache/incubator-dubbo-ops.git
6
7# 修改配置文件:指定注册中心地址(本地就有zookeeper,因此不修改修改)
8[root@localhost test]# vim incubator-dubbo-ops/dubbo-admin-server/src/main/resources/application.properties
9admin.registry.address=zookeeper://127.0.0.1:2181
10admin.config-center=zookeeper://127.0.0.1:2181
11admin.metadata-report.address=zookeeper://127.0.0.1:2181
12
13# 构建
14[root@localhost test]# cd /test/incubator-dubbo-ops/
15[root@localhost incubator-dubbo-ops]# mvn clean package
16
17# 启动
18[root@localhost test]# mvn --projects dubbo-admin-server spring-boot:run
19或者
20[root@localhost test]# cd dubbo-admin-distribution/target
21 [root@localhost dubbo-admin-distribution]# java -jar dubbo-admin-0.1.jar
22
23# 访问
24http://192.168.31.220:8080
Dubbo-admin
添置条件安装 npm
安装 Dubbo-admin
1# 下载
2git clone https://github.com/apache/dubbo-admin.git
3
4# 配置
5vim dubbo-admin-server/src/main/resources/application.properties
6server.port=7001
7admin.registry.address=zookeeper://127.0.0.1:2181
8admin.config-center=zookeeper://127.0.0.1:2181
9admin.metadata-report.address=zookeeper://127.0.0.1:2181
10
11# build
12mvn clean package
13
14# 启动
15cd dubbo-admin-distribution/target
16nohup java -jar dubbo-admin-0.1.jar &
17
18# 访问
19http://192.168.31.251:7001