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.请求命令方式
- 目前没发现方法,如果有大佬知道方法,还请在评论区不吝赐教