Spring Cloud Feign 之Fallback


轉自:https://www.jianshu.com/p/d84768f7996d

Spring Cloud Feign 之Fallback

環境信息: java 1.8、Spring boot 1.5.10.RELEASE、spring cloud-Edgware.SR3、maven 3.3+

在網絡請求時,可能會出現異常請求,如果還想再異常情況下使系統可用,那么就需要容錯處理,比如:網絡請求超時時給用戶提示“稍后重試”或使用本地快照數據等等。

Spring Cloud Feign就是通過Fallback實現的,有兩種方式:

1、@FeignClient.fallback = UserFeignFallback.class指定一個實現Feign接口的實現類。

2、@FeignClient.fallbackFactory = UserFeignFactory.class指定一個實現FallbackFactory<T>工廠接口類

因為Fallback是通過Hystrix實現的, 所以需要開啟Hystrix,spring boot application.properties文件配置feign.hystrix.enabled=true,這樣就開啟了Fallback

Fallback-實現Feign接口

UserFeignFallback回調實現,由spring創建使用@Component(其他的注冊也可以)注解

HystrixTargeter.targetWithFallback方法實現了@FeignClient.fallback處理邏輯,通過源碼可以知道UserFeignFallback回調類是從Spring容器中獲取的,所以UserFeignFallback由spring創建。

UserFeign配置:

package com.example.feign; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import java.util.List; @FeignClient(name = "user",url = "${user.url}",fallback = UserFeignFallback.class /*fallbackFactory = UserFeignFactory.class*/) public interface UserFeign { @PostMapping void save(User user); @GetMapping("/{id}") User getUserByID(@PathVariable("id") String id); @GetMapping List<User> findAll(); } 

UserFeignFallback類:

package com.example.feign; import org.springframework.stereotype.Component; import java.util.List; @Component public class UserFeignFallback implements UserFeign { @Override public void save(User user) { } @Override public User getUserByID(String id) { User user = new User(); user.setId("100"); user.setName("fallback 回調用戶"); return user; } @Override public List<User> findAll() { return null; } } 

為了模擬回調失敗服務提供方,拋出500錯誤。

 @GetMapping("/{id}") public User getUserByID(@PathVariable("id") String id) { // return userMap.get(id); throw new RuntimeException("服務端測試異常!"); } 

運行單元測試UserFeignTest.getUserByID控制台輸出結果:

2018-08-18 11:47:59.800 INFO 8660 --- [ hystrix-user-1] com.example.feign.UserFeign : [UserFeign#getUserByID] ---> GET http://localhost:8080/user/1 HTTP/1.1 2018-08-18 11:47:59.800 INFO 8660 --- [ hystrix-user-1] com.example.feign.UserFeign : [UserFeign#getUserByID] ---> END HTTP (0-byte body) 2018-08-18 11:47:59.828 INFO 8660 --- [ hystrix-user-1] com.example.feign.UserFeign : [UserFeign#getUserByID] <--- HTTP/1.1 500 (27ms) 2018-08-18 11:47:59.828 INFO 8660 --- [ hystrix-user-1] com.example.feign.UserFeign : [UserFeign#getUserByID] connection: close 2018-08-18 11:47:59.828 INFO 8660 --- [ hystrix-user-1] com.example.feign.UserFeign : [UserFeign#getUserByID] content-type: application/json;charset=UTF-8 2018-08-18 11:47:59.828 INFO 8660 --- [ hystrix-user-1] com.example.feign.UserFeign : [UserFeign#getUserByID] date: Sat, 18 Aug 2018 03:47:59 GMT 2018-08-18 11:47:59.828 INFO 8660 --- [ hystrix-user-1] com.example.feign.UserFeign : [UserFeign#getUserByID] transfer-encoding: chunked 2018-08-18 11:47:59.828 INFO 8660 --- [ hystrix-user-1] com.example.feign.UserFeign : [UserFeign#getUserByID] 2018-08-18 11:47:59.829 INFO 8660 --- [ hystrix-user-1] com.example.feign.UserFeign : [UserFeign#getUserByID] {"timestamp":1534564079825,"status":500,"error":"Internal Server Error","exception":"java.lang.RuntimeException","message":"服務端測試異常!","path":"/user/1"} 2018-08-18 11:47:59.829 INFO 8660 --- [ hystrix-user-1] com.example.feign.UserFeign : [UserFeign#getUserByID] <--- END HTTP (167-byte body) User{id='100', name='fallback 回調用戶'} 

服務提供方拋出的500錯誤代碼,但是客戶端程序還可以正常運行輸出了UserFeignFallback.getUserByID方法返回的結果。

FallbackFactory<T>工廠

上面的實現方式簡單,但是獲取不到HTTP請求錯誤狀態碼和信息 ,這時就可以使用工廠模式來實現Fallback

同樣工廠實現類也要交由spring管理,同時結合UserFeignFallback使用,這里需要注意的create方法返回值類型一定要實現Feign接口,否則會報錯。

UserFeignFactory只做了打印異常處理:

package com.example.feign; import feign.hystrix.FallbackFactory; import org.springframework.stereotype.Component; @Component public class UserFeignFactory implements FallbackFactory<UserFeign> { private final UserFeignFallback userFeignFallback; public UserFeignFactory(UserFeignFallback userFeignFallback) { this.userFeignFallback = userFeignFallback; } @Override public UserFeign create(Throwable cause) { //打印下異常 cause.printStackTrace(); return userFeignFallback; } } 

UserFeign:

package com.example.feign; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import java.util.List; @FeignClient(name = "user", url = "${user.url}", /*fallback = UserFeignFallback.class*/ fallbackFactory = UserFeignFactory.class) public interface UserFeign { @PostMapping void save(User user); @GetMapping("/{id}") User getUserByID(@PathVariable("id") String id); @GetMapping List<User> findAll(); } 

運行單元測試UserFeignTest.getUserByID可以看到控制台打印的異常feign.FeignException更多信息省略。

ErrorDecoder接口處理請求錯誤信息,默認實現ErrorDecoder.Default拋出FeignException異常

FeignException.status 方法返回HTTP狀態碼,FallbackFactory.create默認情況下可以強制轉換成FeignException異常這樣就可以獲取到HTTP狀態碼了。

自定義ErrorDecoder

第一種:application.properties

全局配置,通過application.properties配置文件

feign.client.default-config=my-config
feign.client.config.my-config.error-decoder=com.example.feign.MyErrorDecoder

錯誤解碼實現類MyErrorDecoder

package com.example.feign; import feign.Response; import feign.codec.ErrorDecoder; public class MyErrorDecoder implements ErrorDecoder { @Override public Exception decode(String methodKey, Response response) { return new MyFeignException(methodKey,response); } } 

自定義異常MyFeignException

package com.example.feign; import feign.Response; public class MyFeignException extends RuntimeException { private final String methodKey; private Response response; MyFeignException(String methodKey, Response response) { this.methodKey = methodKey; this.response = response; } public Response getResponse() { return response; } public String getMethodKey() { return methodKey; } } 

第二種:@EnableFeignClients

全局配置,@EnableFeignClients.defaultConfiguration注解

package com.example; import com.example.feign.FeignClientsConfig; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.feign.EnableFeignClients; /** * 啟動類 * * @author: sunshaoping * @date: Create by in 上午10:47 2018/8/7 */ @EnableFeignClients( defaultConfiguration = FeignClientsConfig.class ) @SpringBootApplication public class FeignApplication { public static void main(String[] args) { SpringApplication.run(FeignApplication.class, args); } } 

第三種:@FeignClient

作用范圍是Feign接口,優先級要高於上面兩種,@FeignClient.configuration 注解

package com.example.feign; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import java.util.List; @FeignClient(name = "user", url = "${user.url}", /*fallback = UserFeignFallback.class*/ decode404 = true, fallbackFactory = UserFeignFactory.class, configuration = FeignClientsConfig.class ) public interface UserFeign { @PostMapping void save(User user); @GetMapping("/{id}") User getUserByID(@PathVariable("id") String id); @GetMapping List<User> findAll(); } 

總結

本章節講了如下內容

Spring Cloud Feign HTTP請求異常Fallback容錯機制,它是基於Hystrix實現的,所以要通過配置參數feign.hystrix.enabled=true開啟該功能,及其兩種實現方式。

Fallback工廠方式引出了ErrorDecoder錯誤解碼自定義處理,有三種方式,可根據實際請求選擇,舉一反三其他自定義配置也可以通過這種方式實現如:Decoder、Encoder、Logger(第二、三章有介紹)。

如果開啟的Hystrix就不要用feign的超時配置了,單位是毫秒

feign.client.config.defalut.connect-timeout=10000

defalut是默認配置名稱,可以使用feign.client.default-config替換自定義名稱

feign.client.default-config=my-config
feign.client.config.my-config.connect-timeout=10000

請使用如下屬性配置超時時間,單位毫秒

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=20000

樣例地址 spring-cloud-feign 分支 Spring-Cloud-Feign-之fallback



作者:孫平平
鏈接:https://www.jianshu.com/p/d84768f7996d
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM