遷移到CSDN
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
寫在最后
Spring Cloud Feign 系列持續更新中。。。。。歡迎關注
如發現哪些知識點有誤或是沒有看懂,請在評論區提出,博主及時改正。
歡迎轉載請注明出處。