Spring Cloud 学习笔记 —— Hystrix异常处理

1.Hystrix异常处理

我们知道 hystrix 可以在服务接口调不通、或者请求超时时,会降级处理。但是如果请求的接口没有错,而是本身代码问题抛出异常改怎么处理呢?,其实在之前的 @HystrixCommand 注解方式和 getFallback 请求命令方式也都会自动处理,但还有一些扩展方式,比如我想知道本地报的什么错

(1).请求命令方式

package org.javaboy.hystrix;


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

public class HelloCommand extends HystrixCommand<String> {
    RestTemplate restTemplate;
    String name;

    public HelloCommand(Setter setter, RestTemplate restTemplate, String name) {
        super(setter);
        this.restTemplate = restTemplate;
        this.name = name;
    }

    @Override
    protected String run() throws Exception {
        int i = 1/0;
        return restTemplate.getForObject("http://provider/hello2?name={1}",String.class,name);
    }
    @Override
    protected String getFallback() {
        return "error-extends" + getExecutionException().getMessage();
    }


}

  • 我们在 run 方法中写了一行 int i = 1/0;来模拟代码错误,然后在 getFallback方法中拼接一下 getExecutionException().getMessage()的结果
  • 调用 HelloCommand的接口
@GetMapping("/hello2")
    public String hello2(){
//        HystrixRequestContext cxt = HystrixRequestContext.initializeContext();
        HelloCommand helloCommand = new HelloCommand(
                HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("javaboy"))
                ,restTemplate,"javaboy");
        String s = helloCommand.execute();
        System.out.println(s);
        HelloCommand helloCommand2 = new HelloCommand(
                HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("javaboy"))
                ,restTemplate,"javaboy");
        Future<String> s1 = helloCommand2.queue();
        String s2 = null;
        try {
            s2  = s1.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        System.out.println(s1);
        s = s + "\n</br>" + s2;
//        cxt.close();
        return s;
    }
  • 启动项目访问,访问命令的接口
    在这里插入图片描述
  • 可以看到, 我们捕捉了异常的信息

(2).注解方式

  • 创建使用注解调用目标接口的类HelloService
package org.javaboy.hystrix;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheRemove;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult;
import com.netflix.hystrix.contrib.javanica.command.AsyncResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.Future;

@Service
public class HelloService {
    @Autowired
    RestTemplate restTemplate;
    @HystrixCommand(fallbackMethod = "error")
    public String hello(){
        int i = 1/0;
        return restTemplate.getForObject("http://provider/hello",String.class);
    }

    public String error(Throwable throwable){
        System.out.println(throwable);
        return "It's error" +  throwable.getMessage();
    }
}

  • 创建调用 HelloService的接口
 @GetMapping("/hello")
    public String hello(){
        return helloService.hello();
    }
  • 调用 hello 接口,返回如下信息:
    在这里插入图片描述
  • 注解方式,是在 error 方法的参数中加入 Throwable throwable来进一步完成的,而 error 方法名是在 @HystrixCommand 注解中指定的

2.总结一下

请求命令方式和注解方式都能够自动处理异常,并且可以获取错误的信息

3.如果我不想 hystrix 处理,请直接给我返回错误白板页,怎么办?

(1)注解

  • 只需在刚刚的 HelloService中,的@HystrixCommand的注解上,设置ignoreExceptions = ArithmeticException.class属性即可
@HystrixCommand(fallbackMethod = "error",ignoreExceptions = ArithmeticException.class)
    public String hello(){
        int i = 1/0;
        return restTemplate.getForObject("http://provider/hello",String.class);
    }
  • 请求刚刚的 hello 接口,直接返回白板页:
    在这里插入图片描述
  • 意思是,Hystrix 本来能处理异常,但是给我忽略我声明的异常

2.请求命令方式

  • 目前没发现方法,如果有大佬知道方法,还请在评论区不吝赐教
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值