pom文件引入
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-javanica</artifactId>
<version>1.5.18</version>
</dependency>
添加配置
spring.cloud.circuitbreaker.hystrix.enabled=true
spring.cloud.circuitbreaker.enabled=true
HystrixCommand 进行注解
@HystrixCommand(fallbackMethod = "getErrorInfo",commandProperties = {
@HystrixProperty(name ="execution.isolation.thread.timeoutInMilliseconds",value = "3000")
})
@Override
public List<Integer> queryPage(TestBizQuery query) {
return this.queryA(query);
}
public List<Integer> queryA(TestBizQuery query){
String url = "http://127.0.0.1:8001/v1/tests?page="+query.getPage();
RestTemplate restTemplate1 = new RestTemplate();
String aa= restTemplate1.getForObject(url, String.class);
return new ArrayList<>();
}
public List<Integer> getErrorInfo(TestBizQuery query, Throwable throwable){
ArrayList<Integer> objects = new ArrayList<>();
objects.add(1);
objects.add(2);
return objects;
}
execution.isolation.thread.timeoutInMilliseconds 这个表示超时多少进入fallbackmethod方法
至此配置结束
Hystrix不生效问题排查
这是最早的时候代码,因为外部调用类的调用的queryPage方法,而spring的APO使用的CGI代理,会生成子类来实现方法。
@Override
public List<Integer> queryPage(TestBizQuery query) {
return this.queryA(query);
}
@HystrixCommand(fallbackMethod = "getErrorInfo",commandProperties = {
@HystrixProperty(name ="execution.isolation.thread.timeoutInMilliseconds",value = "3000")
})
public List<Integer> queryA(TestBizQuery query){
String url = "http://127.0.0.1:8001/v1/tests?page="+query.getPage();
RestTemplate restTemplate1 = new RestTemplate();
String aa= restTemplate1.getForObject(url, String.class);
return new ArrayList<>();
}
public List<Integer> getErrorInfo(TestBizQuery query, Throwable throwable){
ArrayList<Integer> objects = new ArrayList<>();
objects.add(1);
objects.add(2);
return objects;
}
他是spring所有注解基本都存在的问题无论是spring 事务 或者cache注解都会存在。
AOP的实现有两类,一类是基于接口的采用动态代理,生成一个代理类,一类是基于类的,会采用CGLIB生成子类,然后在子类中扩展父类的方法。问题出在了CGLIB生成了代理,代理内部方法失效
原因:
-
cglib代理的对象是类,是将代理类在执行期间动态生成一个子类,重写所有不是final的方法,它代理的不是方法;
-
当使用method()方式调用时,他生成的代理子类中会使用super.method()去调用,所以最后调用的就是原方法;
-
当使用this.method()方式调用时,代理也会使用super.method()去调用,所以最后调用的就是原方法;
-
使用new MyObject().method()方式调用,那更是会创建原对象,所以最后调用的也是原来的方法;
-
AOP只会对spring bean(也就是spring 容器中的bean) 进行扫描,判断是否需要代理,所以只有使用spring容器中获取到的bean实例去调用目标方法时并且目标方法是有aop切入配置,才会触发调用代理类的子类,所以当你使用spring bean去调用代理类时,AOP会判断是都需要代理,如有代理则将spring bean换成代理子类对象;这样在子类对象中如果再互相调用,那就走的都是子类对象的方法,如上2、3、4所述。
原文的地址:点击查看