Hystrix不生效问题研究

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生成了代理,代理内部方法失效

原因:

  1. cglib代理的对象是类,是将代理类在执行期间动态生成一个子类,重写所有不是final的方法,它代理的不是方法;

  2. 当使用method()方式调用时,他生成的代理子类中会使用super.method()去调用,所以最后调用的就是原方法;

  3. 当使用this.method()方式调用时,代理也会使用super.method()去调用,所以最后调用的就是原方法;

  4. 使用new MyObject().method()方式调用,那更是会创建原对象,所以最后调用的也是原来的方法;

  5. AOP只会对spring bean(也就是spring 容器中的bean) 进行扫描,判断是否需要代理,所以只有使用spring容器中获取到的bean实例去调用目标方法时并且目标方法是有aop切入配置,才会触发调用代理类的子类,所以当你使用spring bean去调用代理类时,AOP会判断是都需要代理,如有代理则将spring bean换成代理子类对象;这样在子类对象中如果再互相调用,那就走的都是子类对象的方法,如上2、3、4所述。
    原文的地址:点击查看

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值