SpringCloud Feign能夠進行服務消費,並且內置了Hystrix,可以進行熔斷。
Feign能夠經過fallback指定熔斷回調的類。代碼示例及講解可見:
https://www.cnblogs.com/expiator/p/10826852.html
可是,有時候咱們還須要記錄異常信息,能夠經過fallbackFactory實現。html
示例以下:java
@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,屬性fallbackFactory指定實現類,以下:git
/** * 使用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的實現類,須要重寫create()方法,這個方法的參數爲Throwable異常類,能夠藉此記錄異常信息。
create()返回進行服務熔斷/降級的Hystrix類。github
@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; // } // }; } }
Hystrix類以下所示:app
@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對應的接口,結果以下:
ide
訪問另外一個接口,返回結果以下:
異常信息以下所示:測試
====================================》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,就是咱們在服務提供者中拋出的異常。firefox
https://github.com/firefoxer1992/SpringCloudProject/tree/master/eureka-consumer-feign/src/main/java/com/example/democode