1、zookeeper宕机与dubbo直连
现象:zookeeper注册中心宕机,还可以消费dubbo暴露的服务。
原因:
健壮性1.监控中心宕掉不影响使用,只是丢失部分采样数据2.数据库宕掉后,注册中心仍能通过缓存提供服务列表查询,但不能注册新服务3.注册中心对等集群,任意一台宕掉后,将自动切换到另一台4.注册中心全部宕掉后,服务提供者和服务消费者仍能通过本地缓存通讯 // 服务调用者调用后在本地存储了5.服务提供者无状态,任意一台宕掉后,不影响使用6.服务提供者全部宕掉后,服务消费者应用将无法使用,并无限次重连等待服务提供者恢复
高可用:通过设计,减少系统不能提供服务的时间。
@Servicepublic class OrderServiceImpl implements OrderService {@Reference(url = "127.0.0.0:20881", // 直连配置timeout = 3000,retries = 3,version = "1.0.0",stub = "com.lzy.service.impl.UserServiceImpl") // dubbo提供的,去远程发现从而自动注入private UserService userService;@Overridepublic List<UserAdress> initOrder(String userId) {// ......}}
2、负载均衡
https://dubbo.apache.org/zh/docsv2.7/user/examples/loadbalance/
负载均衡策略:
Random LoadBalance
- 随机,按权重设置随机概率。
在一个截面上碰撞的概率高,但调用量越大分布越均匀,而且按概率使用权重后也比较均匀,有利于动态调整提供者权重。
RoundRobin LoadBalance
轮询,按公约后的权重设置轮询比率。
存在慢的提供者累积请求的问题,比如:第二台机器很慢,但没挂,当请求调到第二台时就卡在那,久而久之,所有请求都卡在调到第二台上。
LeastActive LoadBalance
最少活跃调用数,相同活跃数的随机,活跃数指调用前后计数差。
使慢的提供者收到更少请求,因为越慢的提供者的调用前后计数差会越大。
ConsistentHash LoadBalance
一致性 Hash,相同参数的请求总是发到同一提供者。
- 当某一台提供者挂时,原本发往该提供者的请求,基于虚拟节点,平摊到其它提供者,不会引起剧烈变动。
- 算法参见:http://en.wikipedia.org/wiki/Consistent_hashing
- 缺省只对第一个参数 Hash,如果要修改,请配置
- 缺省用 160 份虚拟节点,如果要修改,请配置




测试负载均衡:
启动三个服务:
访问消费者服务:
展示的是默认是随机的。方法API显示:

2.1)客户端负载均衡:roundrobin
@Reference(loadbalance = "roundrobin",timeout = 3000, retries = 3, version = "1.0.0", stub = "com.lzy.service.impl.UserServiceImpl") // dubbo提供的,去远程发现从而自动注入
访问消费者三次:由于三个服务提供者的权重是一样的,所以基于权重的轮询负载均衡结果是一样的。
2.2)客户端负载均衡random
@Reference(loadbalance = "random"
对于服务提供者进行权重设置可以通过控制台也可以通过@Service进行设置。不太灵活,控制台设置好。
@Service(version = "1.0.0",weight = 100) // 暴露服务

3、服务降级
https://dubbo.apache.org/zh/docsv2.7/user/examples/service-downgrade/
可以通过服务降级功能临时屏蔽某个出错的非关键服务,并定义降级后的返回策略。
向注册中心写入动态配置覆盖规则:
RegistryFactory registryFactory = ExtensionLoader.getExtensionLoader(RegistryFactory.class).getAdaptiveExtension();Registry registry = registryFactory.getRegistry(URL.valueOf("zookeeper://10.20.153.10:2181"));registry.register(URL.valueOf("override://0.0.0.0/com.foo.BarService?category=configurators&dynamic=false&application=foo&mock=force:return+null"));
其中:
- mock=force:return+null 表示消费方对该服务的方法调用都直接返回 null 值,不发起远程调用。用来屏蔽不重要服务不可用时对调用方的影响。
- 还可以改为 mock=fail:return+null 表示消费方对该服务的方法调用在失败后,再返回 null 值,不抛异常。用来容忍不重要服务不稳定时对调用方的影响。
控制台就可以直接进行设置:


将order服务屏蔽掉,访问返回空对象(屏蔽):

4、集群容错
https://dubbo.apache.org/zh/docsv2.7/user/examples/fault-tolerent-strategy/
Failover Cluster (缺省)
失败自动切换,当出现失败,重试其它服务器。通常用于读操作,但重试会带来更长延迟。可通过 retries=”2” 来设置重试次数(不含第一次)。
Failfast Cluster
快速失败,只发起一次调用,失败立即报错。通常用于非幂等性的写操作,比如新增记录。
Failsafe Cluster
失败安全,出现异常时,直接忽略。通常用于写入审计日志等操作。
Failback Cluster
失败自动恢复,后台记录失败请求,定时重发。通常用于消息通知操作。
Forking Cluster
并行调用多个服务器,只要一个成功即返回。通常用于实时性要求较高的读操作,但需要浪费更多服务资源。可通过 forks=”2” 来设置最大并行数。
Broadcast Cluster
广播调用所有提供者,逐个调用,任意一台报错则报错。通常用于通知所有提供者更新缓存或日志等本地资源信息。
5、集群容错:整合hystrix
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId><version>1.4.1.RELEASE</version></dependency>
@EnableHystrix // 开启集群容错
@Service(version = "1.0.0",weight = 100) // 暴露服务@Componentpublic class UserServiceImpl implements UserService {@HystrixCommand // 代理@Overridepublic List<UserAdress> getUserAddressList(String userId) {System.out.println("UserServiceImpl 3");// 模拟出现异常double random = Math.random();if(random > 0.5) throw new RuntimeException();UserAdress userAdress1 = new UserAdress(1, "长沙", "1", "李老师", "199", "是");UserAdress userAdress2 = new UserAdress(2, "长沙", "2", "谭老师", "198", "是");return Arrays.asList(userAdress1, userAdress2);}}
消费者引入依赖,开启Hystrix
@Servicepublic class OrderServiceImpl implements OrderService {@Reference(loadbalance = "random",timeout = 3000, retries = 3, version = "1.0.0", stub = "com.lzy.service.impl.UserServiceImpl") // dubbo提供的,去远程发现从而自动注入private UserService userService;@HystrixCommand(fallbackMethod = "hello") // 调用出现异常,调用hello方法@Overridepublic List<UserAdress> initOrder(String userId) {// 1. 查询用户的地址System.out.println("用户id:" + userId);return userAddressList;}public List<UserAdress> hello(String userId) {return Arrays.asList(new UserAdress(10, "测试地址", "10", "测试收货人", "测试电话", "测试默认"));}}
5.1)依赖问题
之前使用的dubbo依赖是alibaba,然后去仓库中查看是使用的是apache,所以将其改掉,改掉是因为启动发送没有对应的某个方法。
完整的依赖如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><packaging>pom</packaging><modules><module>user-service-provider</module><module>order-service-consumer</module><module>gmail-interface</module></modules><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.7.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>dubbo-demo</artifactId><version>0.0.1-SNAPSHOT</version><name>dubbo-demo</name><description>dubbo-demo</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter --><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-spring-boot-starter</artifactId><version>2.7.8</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId><version>2.2.1.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-dependencies-zookeeper --><dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-dependencies-zookeeper</artifactId><version>2.7.15</version><type>pom</type></dependency></dependencies></project>
测试结果:

