Spring Cloud Ribbon
- 客户端负载均衡组件Ribbon 负载均衡就是当client端发送请求得时候会均衡得分发到每个server端
创建spring-cloud-02-ribbon-eureka 就是个服务端 参考前面得copy过来的
创建spring-cloud-02-ribbon-client1 项目
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
@RestController
public class UserController {
@RequestMapping(value = "/getUser", method = {RequestMethod.GET})
public User getUser(@RequestParam("id") String id) {
System.err.println("client1 GET-------------" + id);
return new User(id, "20", "张三");
}
}
spring.application.name=client-service
server.port=1003
server.context-path=/
eureka.client.service-url.defaultZone=http://eureka1:8003/eureka/
spring.output.ansi.enabled=always
创建 spring-cloud-02-ribbon-request
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
spring.application.name=request-service
server.context-path=/
server.port=1005
eureka.client.service-url.defaultZone=http://eureka1:8003/eureka/
spring.output.ansi.enabled=always
在这里在介绍下retry重试机制
spring-cloud-02-ribbon-retry
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
spring.application.name=retry-service
server.context-path=/
server.port=1006
eureka.client.service-url.defaultZone=http://eureka1:8003/eureka/
#启动重试机制
spring.cloud.loadbalancer.retry.enabled=true
##断路器
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=14000
##连接超时时间
#client-service.ribbon.ConnectTimeout=1000
##处理超时时间
#client-service.ribbon.ReadTimeout=1000
req.fact.connect-timeout=1000
req.fact.connection-request-timeout=1000
req.fact.read-timeout=3000
#是否对所有请求都进行重试
client-service.ribbon.OKToRetryOnAllOperations=true
#重试切换实例得次数
client-service.ribbon.MaxAutoRetriesNextServer=1
#重试切次数
client-service.ribbon.MaxAutoRetries=5
client-service.ribbon.ConnectTimeout=1000
client-service.ribbon.ReadTimeout=1000
这样配置没起作用 建议使用RestTemplate原始配置
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds:断路器得超时时间需要大于ribbon得超时时间,不然不会发生重试。
@EnableCircuitBreaker //开启断路器
@EnableRetry //启动Retry框架重试机制
@EnableDiscoveryClient //我是服务 注册到服务中心上
@SpringBootApplication
public class RetryApplication {
@Bean
@ConfigurationProperties(prefix = "req.fact")
public HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory(){
return new HttpComponentsClientHttpRequestFactory();
}
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate(httpComponentsClientHttpRequestFactory());
}
public static void main(String[] args) {
SpringApplication.run(RetryApplication.class, args);
}
}
@RestController
public class IndexController {
@Autowired
RestTemplate restTemplate;
@RequestMapping(value = "/retry", method = {RequestMethod.GET})
@HystrixCommand(fallbackMethod = "retryFailback")
public String retry(){
ResponseEntity<String> response = restTemplate.getForEntity("http://client-service/retry", String.class);
System.err.println("body:" + response.getBody());
return "----- retry ok -----";
}
public String retryFailback(){
System.err.println("-----启动降级策略----");
return "-----启动降级策略----";
}
}
- 在spring-cloud-02-ribbon-client1项目得Controller中添加
@RequestMapping(value = "/retry", method = {RequestMethod.GET})
public String retry() {
System.err.println("client1 1 call-------------");
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "client 1";
}
- 在spring-cloud-02-ribbon-client2项目得Controller中添加
@RequestMapping(value = "/retry", method = {RequestMethod.GET})
public String retry() {
System.err.println("client1 2 call-------------");
return "client 2";
}
- 在启动http://localhost:1006/retry时,通过Ribbon负载均衡会访问client1或者client2项目得retry方法,当访问到client1得retry方法时,配置得读取超时时间为3ms,重试次数为5次,retry方法睡眠为4ms,就是每次访问retry方法时重试,当重试次数6次后,会采用降级策略,因为熔断器配置得为14ms,超过熔断器得时间就会采用降级策略。
- GitHub地址
- hystrix 先入个门