SpringCloud Feign通过FallbackFactory显示异常信息
SpringCloud Feign可以进行服务消费,而且内置了Hystrix,能够进行熔断。
Feign可以通过fallback指定熔断回调的类。代码示例及讲解可见:
https://www.cnblogs.com/expiator/p/10826852.html
但是,有时候我们还需要记录异常信息,可以通过fallbackFactory实现。
服务提供者
示例如下:
@RestController
public class UserController {
@PostMapping("/user/name/{id}")
public JSONObject getUserNameById(@PathVariable("id") Integer id ) throws Exception {
System.out.println("==========================>getUserNameById(),id为:"+id);
//直接抛异常,是为了方便测试服务熔断和降级。
throw new Exception("getUserNameByIdException");
}
@PostMapping("/user")
public User getUserById(@RequestParam("id") Integer id ) throws Exception {
System.out.println("=====================>getUserById(),id为:"+id);
throw new Exception("getUserByIdException");
}
}
服务消费者
FeignClient接口
首先是@FeignClient,属性fallbackFactory指定实现类,如下:
/**
* 使用fallbackFactory捕获异常,并进行服务熔断、服务降级。
*/
@FeignClient(value = "eureka-client",fallbackFactory = UserFeignClientFallbackFactory.class)
public interface UserFeignClient {
@PostMapping(value = "/user/name/{id}")
JSONObject getUserNameById(@PathVariable("id") Integer id);
@PostMapping(value = "/user")
User getUserById(@RequestParam("id") Integer id);
}
FallbackFactory实现类
接下来是FallbackFactory的实现类,需要重写create()方法,这个方法的参数为Throwable异常类,可以借此记录异常信息。
create()返回进行服务熔断/降级的Hystrix类。
@Component
public class UserFeignClientFallbackFactory implements FallbackFactory<UserFeignClient> {
@Override
public UserFeignClient create(Throwable cause) {
System.out.println("=================》fallback reason was: " + cause.getMessage());
return new UserFeignClientHystrix();
//也可以不写UserClientFallbackFactory类,直接用匿名对象写成以下形式:
// return new UserFeignClient(Integer id) {
// @Override
// public JSONObject getUserNameById() {
// JSONObject resultJson = new JSONObject();
// resultJson.put("id", "-1" );
// resultJson.put("name", "null" );
// return resultJson;
// }
// };
}
}
FeignClient实现类(也就是Hystrix类)
Hystrix类如下所示:
@Component
public class UserFeignClientHystrix implements UserFeignClient {
/**
* 服务熔断
* @param id
* @return
*/
@Override
public JSONObject getUserNameById(Integer id) {
System.out.println("=======================>UserFeignClientHystrix ");
JSONObject resultJson = new JSONObject();
resultJson.put("errCode", "0404" );
String description="查询id为"+id+"的用户,服务异常,暂时熔断";
resultJson.put("description", description );
return resultJson;
}
@Override
public User getUserById(Integer id) {
System.out.println("=======================>UserFeignClientHystrix ");
//直接返回id为-1的用户
User user = new User();
user.setId(-1);
return user;
}
}
测试
启动注册中心,服务提供者,服务消费者。
访问服务消费者的接口,就能够得到服务提供者抛出的熔断结果和异常信息。
访问getUserById对应的接口,结果如下:
访问另一个接口,返回结果如下:
异常信息如下所示:
======================》fallback reason was: {} status 500 reading UserFeignClient#getUserNameById(Integer); content:
{"timestamp":1557456567128,"status":500,"error":"Internal Server Error","exception":"java.lang.Exception","message":"getUserNameByIdException","path":"/user/name/2"}
=======================>UserFeignClientHystrix
可以看到message中的异常getUserNameByIdException,就是我们在服务提供者中抛出的异常。
完整代码见Github
分类:
springCloud
, 其他--feign
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了