宣告式服務呼叫 Spring Cloud Feign
對Spring Cloud Ribbon和Spring Cloud Hystrix在實踐程序中,這兩個框架的使用幾乎是同時出現的,Spring Cloud Feign就是一個更高層次的封裝來整合這兩個基礎工具以簡化開發,它基于Netflix Feign實作,除了提供這兩者的強大功能之外,它還提供了一種宣告式的Web服務客戶端定義的方式,Spring Cloud Feign在RestTemplate的封裝基礎上做了進一步封裝,由它來幫助定義和實作依賴服務介面的定義,在Spring Cloud Feign的實作下,只需創建一個介面并用注解的方式來配置它,即可完成對服務提供方介面的系結,Spring Cloud Feign具備可插拔的注解支持,包括Feign注解和JAX-RS注解,為了適應Spring用戶,它在Netflix Feign基礎上擴展了對Spring MVC注解的支持,
1. 快速集成 Spring Cloud Feign
基礎服務搭建
-
eureka-server工程:服務注冊中心
-
hello-service工程:服務提供者
-
Spring Cloud Feign Client服務搭建
-
創建Spring Boot 工程 feign-consumer
-
添加依賴: spring-cloud-starter-eureka-server, spring-cloud-starter-feign
-
配置指定服務注冊中心,并定義自身服務名
-
使用
@EnableFeignClients開啟Spring Cloud Feign 的支持功能 -
定義HelloClient介面,使用
@FeignClient注解指定服務名來系結服務,使用SpringMVC注解系結具體該服務提供的REST介面@FeignClient("hello-service") public interface HelloClient { @RequestMapping("/hello") String hello(); } -
在需要呼叫的地方使用
@Autowired注入HelloClient實體,并呼叫該指定服務介面來向該服務發起/hello介面的呼叫
-
2. 引數系結
-
設定Request Header
通過設定Feign攔截器統一設定token,需要實作Feign提供的一個介面
RequestInterceptor@Configuration public class FeignRequestInterceptor implements RequestInterceptor { @Override public void apply(RequestTemplate requestTemplate) { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); if (null != attributes) { HttpServletRequest request = attributes.getRequest(); if (null != request) { //添加token requestTemplate.header("Authorization", request.getHeader("Authorization")); } } } }自定義Feign配置覆寫默認配置
@Configuration public class FeignConfiguration { @Bean public FeignRequestInterceptor feignRequestInterceptor() { return new FeignRequestInterceptor(); } // 超時重試 @Bean public Retryer feignRetryer() { return new Retryer.Default(100, 1000, 5); } }在定義各引數系結時,
@RequestParam,@PathVariable等可以指定引數名稱的注解,value屬性不能缺少,在Feign中系結引數必須通過value屬性指明具體的引數名,否則拋出 IllegalStateException例外, -
接收物件型別請求回應體
接收物件必須有默認的建構式即無參構造器,否則Spring Cloud Feign 根據 JSON 字串轉換物件時拋出例外,
3. 配置
3.1 Ribbon配置
Spring Cloud Feign客戶端負載均衡通過Spring Cloud Ribbon實作,可以直接通過配置Ribbon客戶端的方式自定義各個服務客戶端的呼叫引數,
-
全域配置
使用
ribbon.<key>=<value>的方式設定ribbon的各項引數,如ribbon.ConnectTimeout=500 ribbon.ReadTimeout=5000 -
指定服務配置
針對各個服務客戶端進行個性化配置的方式與全域配置相似,使用
<client>.ribbon.<key>=<value>的方式設定,client可以使用@FeignClient注解中的name或value屬性值來設定對應的ribbon引數,如HELLO-SERVICE.ribbon.ConnectTiomeout=500 HELLO-SERVICE.ribbon.ReadTimeout=5000 HELLO-SERVICE.ribbon.OkToRetryOnAllOperations=true HELLO-SERVICE.ribbon.MaxAutoRetriesNextServer=2 // 嘗試更換實體次數 HELLO-SERVICE.ribbon.MaxAutoRetries=1 // 嘗試訪問首選實體次數 -
重試機制
Spring Cloud Feign中默認實作了請求的重試機制,上面對于HELLO-SERVICE客戶端的配置就是對請求超時及重試機制配置的詳情,
3.2 Hystrix配置
Spring Cloud Feign 還引入了服務保護與容錯的工具Hystrix,默認情況Spring Cloud Feign會為所有Feign客戶端的方法都封裝到Hystrix命令中進行服務保護,
-
全域配置
使用
hystrix.command.default.<key>=<value>的方式配置hystrix各項引數,如hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=5000 -
禁用 Hystrix
-
全域禁用
feign.hystrix.enabled=false -
針對指定服務禁用Hystrix
構建關閉Hystrix配置類
@Configuration public class DisableHystrixConfiguration { @Bean @Scope("prototype") public Feign.Builder feignBuilder() { return Feign.builder(); } }在
@FeignClient注解中通過configuration引數引入配置類@FeignClient(name = "HELLO-SERVICE", configuration = DisableHystrixConfiguration.class) public interface HelliClient {
-
-
指定命令配置
使用hystrix.command.
作為前綴, 默認情況下采用Feign客戶端中的方法名作為標識, 如配置/hello介面的熔斷超時時間:
hystrix.command.hello.execution.isolation.thread.timeoutInMilliseconds=5000 -
服務降級配置
-
為Feign客戶端的定義介面HelloService實作一個具體的實作類HelloServiceFallback
@Component public class HelloServiceFallback implements HelloService { -
在服務系結介面中通過
@FeignClient注解的fallback屬性來指定對應的服務降級實作類@FeignClient(name="HELLO-SERVICE", fallback=HelloServiceFallback.class) public interface HelloService {
-
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/89867.html
標籤:Java
