一.Ribbon实现客户端负载均衡
构建Spring Boot项目
1.引入pom依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2.application.yml配置
server:
port: 9094
spring:
application:
name: hystrix-client
stores:
ribbon:
listOfServers: www.baidu.com,www.jd.com,www.163.com,www.alibaba.com
3.定义Restful接口
@RestController
@Slf4j
public class RibbonController {
@Autowired
LoadBalancerClient loadBalancerClient;
@Autowired
RestTemplate restTemplate;
@GetMapping("static")
public String stores(){
ServiceInstance instance=loadBalancerClient.choose("stores");
URI storesUri = URI.create(String.format("http://%s:%s", instance.getHost(), instance.getPort()));
log.info(storesUri.toString());
return "static";
}
}
Ribbon的负载均衡策略
默认采用轮询的方式,RoundRobinRule(继承自AbstractLoadBalancerRule)
执行6次的结果
2018-10-29 14:39:49.226 INFO 18991 --- [nio-9094-exec-1] s.h.controller.RibbonController : http://www.baidu.com:80
2018-10-29 14:39:49.328 INFO 18991 --- [nio-9094-exec-3] s.h.controller.RibbonController : http://www.neteasy.com:80
2018-10-29 14:39:49.613 INFO 18991 --- [nio-9094-exec-5] s.h.controller.RibbonController : http://www.alibaba.com:80
2018-10-29 14:39:49.806 INFO 18991 --- [nio-9094-exec-7] s.h.controller.RibbonController : http://www.baidu.com:80
2018-10-29 14:39:49.972 INFO 18991 --- [nio-9094-exec-9] s.h.controller.RibbonController : http://www.neteasy.com:80
2018-10-29 14:39:50.125 INFO 18991 --- [nio-9094-exec-1] s.h.controller.RibbonController : http://www.alibaba.com:80
RandomRule(随机策略) 在index上随机,选择index对应位置的server
在配置文件application.yml加入
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
同时实例化对应的Bean
@Bean
public IRule ribbonRule(){
return new RandomRule();
}
执行6次的结果
2018-10-29 14:58:14.687 INFO 19120 --- [nio-9094-exec-1] s.h.controller.RibbonController : http://www.baidu.com:80
2018-10-29 14:58:14.804 INFO 19120 --- [nio-9094-exec-3] s.h.controller.RibbonController : http://www.neteasy.com:80
2018-10-29 14:58:15.313 INFO 19120 --- [nio-9094-exec-5] s.h.controller.RibbonController : http://www.alibaba.com:80
2018-10-29 14:58:15.743 INFO 19120 --- [nio-9094-exec-7] s.h.controller.RibbonController : http://www.neteasy.com:80
2018-10-29 14:58:26.410 INFO 19120 --- [io-9094-exec-10] s.h.controller.RibbonController : http://www.baidu.com:80
2018-10-29 14:58:27.045 INFO 19120 --- [nio-9094-exec-2] s.h.controller.RibbonController : http://www.alibaba.com:80
Ribbon服务端
将Ribbon服务端注册到Eureka注册中心中,需要引入Eureka的相应Jar包
1.pom文件
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2.application.yml配置
# 9091 server1
server:
port: 9091
spring:
application:
name: ribbon-server
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
instance:
hostname: ${spring.application.name}
instance-id: ${spring.application.name}:${server.port}
appname: ribbon-server
# 9092 server2
server:
port: 9092
spring:
application:
name: ribbon-server
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
instance:
hostname: ${spring.application.name}
instance-id: ${spring.application.name}:${server.port}
appname: ribbon-server
Ribbon客户端
application.yml配置
server:
port: 9094
spring:
application:
name: ribbon-client
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
instance:
hostname: ${spring.application.name}
instance-id: ${spring.application.name}:${server.port}
appname: ribbon-client
启动类配置
@SpringBootApplication
@EnableCircuitBreaker
@EnableEurekaClient
public class HystrixClient2Application {
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(HystrixClient2Application.class, args);
}
}
Ribbon客户端负载均衡测试
.定义如下Restful接口
@RestController
@Slf4j
public class RibbonController {
@Autowired
LoadBalancerClient loadBalancerClient;
@GetMapping("loadInstance")
public String loadInstance(){
ServiceInstance instance=loadBalancerClient.choose("ribbon-server");
log.info(instance.getServiceId()+":"+instance.getHost()+":"+instance.getPort());
return instance.getServiceId()+":"+instance.getHost()+":"+instance.getPort();
}
.浏览器调用http://localhost:9094/loadInstance方法,交替显示如下:
hystrix-server:hystrix-server:9091
hystrix-server:hystrix-server:9092
可以使用下面的配置禁止Eureka对Ribbon服务实例的维护实现
ribbon.eureka.enabled=false
重试机制
由于Spring Cloud Eureka实现的服务治理机制强调了cap原理的ap机制(可用性和可靠性),与zookeeper这类强调cp(一致性,可靠性)服务质量框架最大的区别就是,Eureka为了实现更高的服务可用性,牺牲了一定的一致性,在极端情况下宁愿接受故障实例也不要丢弃"健康"实例。
比如说,当服务注册中心的网络发生故障断开时候,由于所有的服务实例无法维护续约心跳,在强调ap的服务治理中将会把所有服务实例剔除掉,而Eureka则会因为超过85%的实例丢失心跳而触发保护机制,注册中心将会保留此时的所有节点,以实现服务间依然可以进行互相调用的场景,即使其中有部分故障节点,但这样做可以继续保障大多数服务的正常消费。
Spring Cloud整合了spring retry来增强RestTemplate的重试能力,对于我们开发者来说,只需要简单配置,即可完成重试策略。
pom引用
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
通过在application配置,使得客户端在访问到故障请求时,会重试的访问当前实例,达到maxAutoRetries次数后,尝试换一个实例继续请求。
hystrix-server:
ribbon:
ConnectTimeout: 250 #请求连接超时
ReadTimeout: 1000 #请求处理的超时时间
OkToRetryOnAllOperations: true #对所有操作请求都进行重试
MaxAutoRetriesNextServer: 2 #切换实例的重试次数
maxAutoRetries: 2 #对当前实例的重试次数
【参考博客】
1.https://www.jianshu.com/p/f86af82fa782
2.https://www.cnblogs.com/duanxz/p/7504947.html