Dubbo 概述 Dubbo概念
Dubbo架构
节点角色说明
Demo地址 Dubbo-demo[https://github.com/raineddown/Dubbo-demo ]
Dubbo快速入门项目结构:
实现步骤
①启动zookeeper
②创建服务提供者Provider模块
③创建服务消费者Consumer模块
④在服务提供者模块编写 UserServiceImpl 提供服务
⑤在服务消费者中的 UserController 远程调用UserServiceImpl 提供的服务
⑥配置zookeeper地址,分别启动两个服务,测试
代码:
client:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 import com.itheima.pojo.User;import com.itheima.service.UserService;import org.apache.dubbo.config.annotation.Reference;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController @RequestMapping ("/user" )public class UserController { @Reference private UserService userService; @RequestMapping ("/sayHello" ) public String sayHello () { return userService.sayHello(); } @RequestMapping ("/find" ) public User find (int id) { return userService.findUserById(id); } }
server:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 import com.itheima.pojo.User;import com.itheima.service.UserService;import org.apache.dubbo.config.annotation.Service;@Service public class UserServiceImpl implements UserService { public String sayHello () { return "hello dubbo hello!~" ; } public User findUserById (int id) { User user = new User(1 ,"zhangsan" ,"123" ); return user; } }
Dubbo高级特性 Dubbo高级特性超时与重试
服务消费者在调用服务提供者的时候发生了阻塞、等待的情形,这个时候,服务消费者会一直等待下去。
在某个峰值时刻,大量的请求都在同时请求服务消费者,会造成线程的大量堆积,势必会造成雪崩。
dubbo 利用超时机制来解决这个问题,设置一个超时时间,在这个时间段内,无法完成服务访问,则自动断开连接。
使用timeout属性配置超时时间,默认值1000,单位毫秒。
client:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 @RestController @RequestMapping ("/user" )public class UserController { @Reference (timeout = 1000 ) private UserService userService; @RequestMapping ("/sayHello" ) public String sayHello () { return userService.sayHello(); } int i = 1 ; @RequestMapping ("/find" ) public User find (int id) { new Thread(new Runnable() { public void run () { while (true ){ System.out.println(i++); try { Thread.sleep(1000 ); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); return userService.findUserById(id); } }
server:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 import org.apache.dubbo.config.annotation.Service;@Service (timeout = 3000 ,retries = 2 )public class UserServiceImpl implements UserService { int i = 1 ; public String sayHello () { return "hello dubbo hello!~" ; } public User findUserById (int id) { System.out.println("服务被调用了:" +i++); User user = new User(1 ,"zhangsan" ,"123" ); try { Thread.sleep(5000 ); } catch (InterruptedException e) { e.printStackTrace(); } return user; } }
客户端,@Reference(timeout = 1000)注解存在,控制台输出1后报错,1秒超时。客户端超时配置会覆盖服务端配置(服务端3秒配置被覆盖)
服务端,若客户端@Reference没有超时配置,控制台输出3后超时报错。
重试
控制台输出:
服务被调用了: 1
服务被调用了: 2
服务被调用了: 3
服务被调用三次,retries不写默认为2。
Dubbo高级特性多版本
server1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 import org.apache.dubbo.config.annotation.Service;@Service (version = "v1.0" )public class UserServiceImpl implements UserService { public String sayHello () { return "hello dubbo hello!~" ; } public User findUserById (int id) { System.out.println("old..." ); User user = new User(1 ,"zhangsan" ,"123" ); return user; } }
server2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 import org.apache.dubbo.config.annotation.Service;@Service (version = "v2.0" )public class UserServiceImpl2 implements UserService { public String sayHello () { return "hello dubbo hello!~" ; } public User findUserById (int id) { System.out.println("new...." ); User user = new User(1 ,"zhangsan" ,"123" ); return user; } }
client:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 @RestController @RequestMapping ("/user" )public class UserController { @Reference (version = "v2.0" ) private UserService userService; @RequestMapping ("/sayHello" ) public String sayHello () { return userService.sayHello(); } @RequestMapping ("/find" ) public User find (int id) { return userService.findUserById(id); } }
根据版本选择对应的接口实现类,类似于spring @Qualifier(“Version”)和@Resource(name=”Version”)注解。
Dubbo高级特性负载均衡 负载均衡策略(4种):
•Random :按权重随机,默认使用该策略。按权重设置随机概率。
•RoundRobin :按权重轮询。
•LeastActive:最少活跃调用数,相同活跃数的随机。
•ConsistentHash:一致性 Hash,相同参数的请求总是发到同一提供者。
client:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 @RestController @RequestMapping ("/user" )public class UserController { @Reference (loadbalance = "random" ) private UserService userService; @RequestMapping ("/sayHello" ) public String sayHello () { return userService.sayHello(); } @RequestMapping ("/find" ) public User find (int id) { return userService.findUserById(id); } }
server:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 @Service (weight = 100 ) public class UserServiceImpl implements UserService { public String sayHello () { return "3......" ; } public User findUserById (int id) { System.out.println("old..." ); User user = new User(1 ,"zhangsan" ,"123" ); return user; } }
Dubbo高级特性集群容错
client:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 @RestController @RequestMapping ("/user" )public class UserController { @Reference (cluster = "failover" ) private UserService userService; @RequestMapping ("/sayHello" ) public String sayHello () { return userService.sayHello(); } @RequestMapping ("/find" ) public User find (int id) { return userService.findUserById(id); } }
server:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 @Service public class UserServiceImpl implements UserService { public String sayHello () { return "hello dubbo hello!~" ; } public User findUserById (int id) { System.out.println("3..." ); User user = new User(1 ,"zhangsan" ,"123" ); return user; } }
Dubbo服务降级 官网地址[https://dubbo.apache.org/zh/docs/advanced/service-downgrade/ ]
mock=force:return null
表示消费方对该服务的方法调用都直接返回 null 值,不发起远程调用。用来屏蔽不重要服务不可用时对调用方的影响。
还可以改为 mock=fail:return null
表示消费方对该服务的方法调用在失败后,再返回 null 值,不抛异常。用来容忍不重要服务不稳定时对调用方的影响。
client:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 @RestController @RequestMapping ("/user" )public class UserController { @Reference (mock=force:return null ) private UserService userService; @RequestMapping ("/sayHello" ) public String sayHello () { return userService.sayHello(); } @RequestMapping ("/find" ) public User find (int id) { return userService.findUserById(id); } }
This is copyright.