#Hystrix:HystrixCommand #异步调用、异常处理 @FDDLC

先修课:#Hystrix:服务熔断、服务降级 #自定义熔断: @FDDLC

 

一、自定义HystrixCommand中的异常处理

1、provider:

    @RequestMapping("/hystrix")
    public String hystrix() throws Exception{
        //Thread.sleep(10000);
        int i=1/0;
        return "I'm from the provider. If you can see me, it means the connection works well.";
    }

上面的 int i = 1/0; 会抛出异常!

 

2、consumer中自定义的HystrixCommand:

package cn.consumer.config;


import com.netflix.hystrix.HystrixCommand;
import org.springframework.web.client.RestTemplate;

//注意:有两个HystrixCommand:一个是注解,一个是抽象类。千万不要导错了包!
public class MyHystrixCommand extends HystrixCommand<String> {
    private RestTemplate restTemplate;

    public MyHystrixCommand(Setter setter,RestTemplate restTemplate) {
        super(setter);
        this.restTemplate=restTemplate;
    }

    @Override
    protected String run() throws Exception {
        return restTemplate.getForObject("http://provider/hystrix", String.class);
    }

    @Override
    protected String getFallback() {
        System.out.println("super.getExecutionException().getMessage():"+super.getExecutionException().getMessage());
        return "getFallback()";
    }
}

 

3、consumer中的controller:

    //自定义熔断
    @RequestMapping("/myhystrix")
    public String myhystrix(){
        MyHystrixCommand myHystrixCommand=new MyHystrixCommand(
                com.netflix.hystrix.HystrixCommand.Setter.withGroupKey(
                        HystrixCommandGroupKey.Factory.asKey("")
                ), restTemplate);
        return myHystrixCommand.execute();
    }

 

4、请求结果:

 

5、consumer控制台输出:

 

 

 

二、HystrixCommand的异常调用

1、provider:

    @RequestMapping("/hystrix")
    public String hystrix() throws Exception{
        //Thread.sleep(10000);
        //int i=1/0;
        return "I'm from the provider. If you can see me, it means the connection works well.";
    }

 

2、consumer中的MyHystrixCommand:

package cn.consumer.config;


import com.netflix.hystrix.HystrixCommand;
import org.springframework.web.client.RestTemplate;

//注意:有两个HystrixCommand:一个是注解,一个是抽象类。千万不要导错了包!
public class MyHystrixCommand extends HystrixCommand<String> {
    private RestTemplate restTemplate;

    public MyHystrixCommand(Setter setter,RestTemplate restTemplate) {
        super(setter);
        this.restTemplate=restTemplate;
    }

    @Override
    protected String run() throws Exception {
        return restTemplate.getForObject("http://provider/hystrix", String.class);
    }

    @Override
    protected String getFallback() {
        System.out.println("super.getExecutionException().getMessage():"+super.getExecutionException().getMessage());
        return "getFallback()";
    }
}

 

3、consumer中的controller:

    //自定义熔断
    @RequestMapping("/myhystrix")
    public String myhystrix()throws Exception{
        MyHystrixCommand myHystrixCommand=new MyHystrixCommand(
                com.netflix.hystrix.HystrixCommand.Setter.withGroupKey(
                        HystrixCommandGroupKey.Factory.asKey("")
                ), restTemplate);
        Future<String> future = myHystrixCommand.queue(); //异步调用
        //可以先处理其他逻辑,之后再去future中拿结果

        //有两个get方法:
        //V get(long timeout, TimeUnit unit):带参的get方法可以指定超时时间
        return future.get(); //无参的get方法
    }

核心就是上面的queue和get!

 

4、请求:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值