環境:SpringCloud3.1.5
Spring Cloud OpenFeign是一種聲明式、模板化的HTTP客戶端,它簡化了遠程調用的編程體驗。在Spring Cloud中使用OpenFeign,開發者可以像調用本地方法一樣使用HTTP請求訪問遠程服務,而無需感知這是在調用遠程方法。
OpenFeign會根據帶有注解的函數信息構建出網絡請求的模板,在發送網絡請求之前,OpenFeign會將函數的參數值設置到這些請求模板中。
在項目啟動階段,OpenFeign框架會自動的掃包流程,從指定的目錄下掃描并加載所有被@FeignClient注解修飾的接口。OpenFeign會針對每一個FeignClient接口生成一個動態代理(JDK)對象,這個動態代理對象會被添加到Spring上下文中,并注入到對應的服務里。
總的來說,Spring Cloud OpenFeign是一種強大的工具,它可以幫助開發者更輕松地實現微服務的遠程調用。
引入依賴
groupId: org.springframework.cloudartifactId: spring-cloud-starter-openfeign
開啟
@SpringBootApplication@EnableFeignClientspublic class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }}
示例
@FeignClient(name = "demoService")public interface DemoFeign { @GetMapping("/info/{id}") public Object info(@PathVariable("id") Integer id) ;}
在 @FeignClient 注解中,name="demoService"是一個客戶端名稱,用于創建 Spring Cloud LoadBalancer 客戶端。你也可以設置 url 屬性(絕對值或主機名)指定一個 URL。
通過指定@FeignClient configuration屬性實現自定義配置
@FeignClient(name = "demoService", configuration=DemoConfiguration.class)public interface DemoFeign {}
默認情況下Spring Cloud OpenFeign會提供如下的默認Bean:
Decoder feignDecoder:ResponseEntityDecoder 響應內容解碼
Encoder feignEncoder: SpringEncoder
Logger feignLogger: Slf4jLogger
MicrometerCapability micrometerCapability: 如果 feign-micrometer 位于類路徑上,且 MeterRegistry 可用
CachingCapability cachingCapability: 如果使用 @EnableCaching 注解。可通過 feign.cache.enabled 關閉。
Contract feignContract: SpringMvcContract這個的作用就是用來接下@FeignClient注解類中的相應MVC注解
Feign.Builder feignBuilder: FeignCircuitBreaker.Builder構建Feign客戶端的,比如環境中使用了CircuitBreaker,那么就會在調用接口時應用斷路器功能
Client feignClient: 如果 Spring Cloud LoadBalancer 位于類路徑上,則使用 FeignBlockingLoadBalancerClient。如果它們都不在類路徑上,則使用默認的 Feign Client。默認的Client使用jdk自定的網絡請求 URLConnection。
我們可以在自定義的DemoConfiguration中自定義自己的實現,在DemoConfiguration中你可以自定義如下的Bean:注意:這些bean是 OpenFeign沒有提供的,除了上面介紹的你可以覆蓋自定義實現,你還可以定義下面的bean
Logger.LevelRetryerErrorDecoderRequest.OptionsCollection<RequestInterceptor>SetterFactoryQueryMapEncoderCapability (MicrometerCapability and CachingCapability are provided by default)
接下來會介紹常用的一些自定義配置
@FeignClient(name = "${pack.demo.name}", url="${pack.demo.url}")public interface DemoFeign {}
配置文件
pack: demo: name: demoService url: http://localhost:8088/demos
feign: client: config: default: connectTimeout: 5000 readTimeout: 5000
默認所有的接口請求超時都是5s。
如果啟用了 Feign 客戶端刷新,則每個 Feign 客戶端都會以 feign.Request.Options 作為 refresh-scoped Bean 來創建。這意味著 connectTimeout 和 readTimeout 等屬性可以通過 POST /actuator/refresh 針對任何 Feign 客戶端實例進行刷新。值需要進行如下配置接口
feign: client: refresh-enabled: true
配置文件中設置
logging: level: '[com.pack.feign.test.DemoFeign]': debug
配置日志基本,兩種方式:1 配置文件;2 定義Bean
feign: client: config: demoService: logger-level: full
在自定義配置類中定義為bean
@Beanpublic Logger.Level loggerLevel() { return Logger.Level.FULL ;}
默認情況下,當請求的接口出現異常不會進行重試,可以通過定義下面bean進行重試
@Beanpublic Retryer feignRetryer() { Retryer.Default retryer = new Retryer.Default(100, SECONDS.toMillis(1), 2); return retryer ;}
默認OpenFeign不提供任何攔截器,如果你需要在請求前進行處理,比如添加請求header,那么你可以自定義攔截器實現
@Beanpublic RequestInterceptor headerRequestInterceptor() { return template -> { template.header("X-API-TOKEN", "666666") ; } ;}
也可以通過配置文件配置
feign: client: config: demoService: request-interceptors: - com.pack.feign.HeaderRequestInterceptor
在某些情況下,可能需要自定義 Feign 客戶端,你可以使用 Feign Builder API 創建客戶端。下面的示例創建了兩個具有相同接口的 Feign Client,但每個客戶端都配置了單獨的請求攔截器。
public class DemoController { private DemoFeign demoFeign ; @Autowired public DemoController(Client client, Encoder encoder, Decoder decoder) { this.fooClient = Feign.builder().client(client) .encoder(encoder) .decoder(decoder) .requestInterceptor(new HeaderRequestInterceptor("X-API-TOKEN", "666666")) .target(FooClient.class, "http://localhost:8088/demos"); }}
當你的類路徑下有了Spring Cloud CircuitBreaker后服務降級fallback才會生效。
@FeignClient( url = "http://localhost:8088/demos", name = "demoService", configuration = DemoFeignConfiguration.class, fallback = DemoFeignFallback.class)public interface DemoFeign { @GetMapping("/info/{id}") public Object info(@PathVariable("id") Integer id) ;}
服務降級類必須實現對應FeignClient的接口
public class DemoFeignFallback implements DemoFeign { public Object info(Integer id) { return "default - " + id; }}
如果需要訪問觸發回退的原因,可以使用 @FeignClient 內的 fallbackFactory 屬性。
@FeignClient( url = "http://localhost:8088/demos", name = "demoService", configuration = DemoFeignConfiguration.class, fallbackFactory = DemoFeignFallbackFactory.class,)public interface DemoFeign { @GetMapping("/info/{id}") public Object info(@PathVariable("id") Integer id) ;}
工廠類
public class DemoFeignFallbackFactory implements FallbackFactory<DemoFeignFallback> { static class DemoFeignFallback implements DemoFeign { private Throwable cause ; public DemoFeignFallback(Throwable cause) { this.cause = cause ; } @Override public Object info(Integer id) { return "接口調用異常 - " + this.cause.getMessage() ; } } @Override public DemoFeignFallback create(Throwable cause) { return new DemoFeignFallback(cause) ; }}
如果使用 @EnableCaching 注解,則會創建并注冊一個 CachingCapability Bean,以便 Feign 客戶端識別其接口上的 @Cache* 相關注解:
@FeignClient( url = "http://localhost:8088/demos", name = "demoService", configuration = DemoFeignConfiguration.class, fallbackFactory = DemoFeignFallbackFactory.class,)public interface DemoFeign { @GetMapping("/info/{id}") @Cacheable(cacheNames = "demo-cache", key = "#id") public Object info(@PathVariable("id") Integer id) ;}
Feign 通過單繼承接口支持模板應用程序。這樣就可以將常用操作歸類到方便的基礎接口中。
@FeignClient( url = "http://localhost:8088/users", name = "userService")public interface UserService { @GetMapping("/{id}") User getUser(@PathVariable("id") Long id) ;}@FeignClient("users")public interface UserClient extends UserService {}
注意:@FeignClient 接口不應在服務器和客戶端之間共享,并且不再支持在類級使用 @RequestMapping 對 @FeignClient 接口進行注解。
你可以考慮為你的 Feign 請求啟用請求或響應 GZIP 壓縮。你可以啟用其中一個屬性:
feign: compression: request enabled: true response: enabled: true
控制壓縮MeidaType類型和最小請求閾值長度
feign: compression: request: mime-types: text/xml,application/xml,application/json min-request-size: 2048
由于 OpenFeign 項目目前不支持 Spring WebClient 等反應式客戶端,因此 Spring Cloud OpenFeign 也不支持反應式客戶端。
目前官方推薦的響應式庫:feign-reactive
<dependency> <groupId>com.playtika.reactivefeign</groupId> <artifactId>feign-reactor-webclient</artifactId> <version>3.3.0</version></dependency>
我目前使用的是3.3.0版本,除了注解適當調整,其它使用基本一致,非常爽。
@ReactiveFeignClient( url = "http://localhost:8088/demos", name = "demoReactorFeign", fallback = DemoReactorFeignFallback.class, configuration = {DemoReactorFeignConfig.class})public interface DemoReactorFeign { @GetMapping("/info/{id}") public Mono<Object> info(@PathVariable("id") Integer id) ;}
OpenFeign是一個非常有用的工具,它為開發者提供了一種簡單而強大的方式來處理遠程服務調用。通過使用OpenFeign,開發者可以專注于業務邏輯,而無需花費太多精力在復雜的網絡編程細節上。
OpenFeign的聲明式編程風格使得我們能夠以一種高度抽象和簡潔的方式與遠程服務進行交互,它簡化了網絡請求的構建和發送過程,降低了開發的復雜度。
總的來說,Spring Cloud OpenFeign是一個強大而靈活的工具,它可以幫助開發者更高效地構建和維護微服務應用。通過使用OpenFeign,我們可以更好地專注于業務邏輯,而無需擔心網絡調用的實現細節。
完畢!!!
本文鏈接:http://www.tebozhan.com/showinfo-26-35260-0.html全網最詳細的OpenFeign講解,肯定有你不知道的
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com
上一篇: 消息隊列批量收發消息,請避開這五個坑!
下一篇: Python的三元表達式,你知道嗎?