環境:SpringBoot2.7.15
Feign-reactive是一個用于在Spring Cloud應用程序中實現響應式微服務的框架。它支持在Spring Cloud應用程序中實現異步和非阻塞的遠程調用。Feign-reactive的一些主要特點:
Feign-reactive是一個非常有用的框架,可以幫助開發人員輕松地實現響應式微服務,提高應用程序的性能和吞吐量。
<dependency> <groupId>com.playtika.reactivefeign</groupId> <artifactId>feign-reactor-spring-configuration</artifactId> <version>3.3.0</version></dependency><dependency> <groupId>com.playtika.reactivefeign</groupId> <artifactId>feign-reactor-cloud</artifactId> <version>3.3.0</version></dependency><dependency> <groupId>com.playtika.reactivefeign</groupId> <artifactId>feign-reactor-webclient</artifactId> <version>3.3.0</version></dependency>
@GetMapping("/demos/info/{id}")public Object info(@PathVariable("id") Integer id) throws Exception { TimeUnit.SECONDS.sleep(3) ; Map<String, Object> result = new HashMap<>() ; result.put("code", 0) ; result.put("data", id) ; result.put("message", "success") ; return result ;}
@EnableReactiveFeignClientspublic class AppFeignReactorApplication {}
@ReactiveFeignClient( url = "http://localhost:8088/demos", name = "demoReactorFeign")public interface DemoReactorFeign { @GetMapping("/info/{id}") public Mono<Object> info(@PathVariable("id") Integer id) ;}
以上就完成了一個非常簡單的反應式feign接口定義,接下來就可以使用了。其實這里除了注解與openfeign不一樣外,其它都一樣。
@Resourceprivate DemoReactorFeign demoReactorFeign ;@GetMapping("/{id}")public Object info(@PathVariable("id") Integer id) { return this.demoReactorFeign.info(id) ;}
調用結果
接下來會介紹更多關于反應式feign的配置
@ReactiveFeignClient( url = "http://localhost:8088/demos", name = "demoReactorFeign", fallback = DemoReactorFeignFallback.class, configuration = {DemoReactorFeignConfig.class})public interface DemoReactorFeign {
public class DemoReactorFeignFallback implements DemoReactorFeign { @Override public Mono<Object> info(Integer id) { return Mono.just("請求失敗") ; }}
public class DemoReactorFeignConfig { @Bean public DemoReactorFeignFallback demoReactorFeignFallback() { return new DemoReactorFeignFallback() ; } }
當遠程接口調用失敗或超時將會執行上面的fallback。
圖片
超時配置
reactive: feign: client: config: demoReactorFeign: options: connectTimeoutMillis: 2000 readTimeoutMillis: 2000
負載均衡配置
reactive: feign: loadbalancer: enabled: true
斷路器配置
reactive: feign: circuit: breaker: enabled: true
要使其生效,必須引入下面的依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-circuitbreaker-reactor-resilience4j</artifactId></dependency>
本文鏈接:http://www.www897cc.com/showinfo-26-54152-0.html深入ReactiveFeign:反應式遠程接口調用的最佳實踐
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com
上一篇: Go 框架 Beego 真的有那么差勁嗎