springcloud ribbon集成retry:
1.修改配置application.yml:
spring:
application:
name: retry-service
##类似@EnableRetry注释,此处代表启动springcloud ribbon retry
cloud:
loadbalancer:
retry:
enabled: true
server:
context-path: /
port: 7004
eureka:
client:
service-url:
defaultZone: http://eureka1:8001/eureka
##设置断路器超时时间,默认为1秒(必须设置此配置),在springcloud E版本以后,对于ribbon无需此设置了;
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 10000
##配置http请求的超时时间
custom:
requestFactory:
connect-timeout: 10000
connection-request-timeout: 10000
read-timeout: 20000
##针对于某一个微服务进行重试策略配置
provider-service: ##表示针对provider-service的微服务进行重试策略,如果不加微服务名称,只设置以下内容表示全局
ribbon:
OkToRetryOnAllOperations: true ##对所有的请求都进行重试
MaxAutoRetriesNextServer: 1 ##切换实例的次数,如果请求异常,在两个provider间切换
MaxAutoRetries: 2 ##对当前实例重试的次数
2.创建api和实体类:
package com.cc.springcloud.entity;
import java.io.Serializable;
public class User implements Serializable{
private static final long serialVersionUID = 1L;
private String id;
private String name;
public User(String id, String name) {
super();
this.id = id;
this.name = name;
}
public User() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.cc.springcloud.api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.cc.springcloud.entity.User;
@RestController
public class RetryController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping(value="/retry")
public String retry() {
//表达是传参
//restTemplate.getForObject("http://provider-service/getUser?id={1}&name={2}", User.class,"001","张三");
User user = restTemplate.getForObject("http://provider-service/getUser?id={1}", User.class,"001");
System.out.println("username:"+user.getName());
return "retry success!";
}
}
3.Application:
package com.cc.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.web.client.RestTemplate;
@EnableDiscoveryClient //标示是一个具体的服务,需要向注册中心注册
@SpringBootApplication //springboot 核心配置
@EnableRetry //使用spring原生retry必须开启此注解,即开启重试
public class Application {
@Bean
@ConfigurationProperties(prefix="cunstom.requestFactory")
public HttpComponentsClientHttpRequestFactory customHttpRequestFactory() {
return new HttpComponentsClientHttpRequestFactory();
}
@Bean
@LoadBalanced //用于实现内部的服务负载均衡机制:service-id或service-name
public RestTemplate restTemplate() {
return new RestTemplate(customHttpRequestFactory());
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
4.启动微服务测试:
Retry-service已经注册到Eureka;
5.访问测试:访问两次,同时测试负载均衡;
访问测试与负载完成;
6.测试重试策略:
-修改缩短读超时:
-修改其中一个实例,设置方法调用等待时间超过上面设置时间,此处修改provider-1:
启动访问测试:当访问provider-1时,重试策略被启动
流程:retry-service调用provider-1,counter=1,超时启动重试在请求counter=2,最后一次请求counter=3,然后切换请求provider-2成功返回结果;一次请求两次重试一次切换,总耗时为7秒钟,注意:断路器的超时时间一定要高于重试的耗时时间hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 10000,如果低于那么就会抛出异常(老版本),新版本已经不再有此问题,断路器的此种配置失效。