日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 軟件

深入ReactiveFeign:反應式遠程接口調用的最佳實踐

來源: 責編: 時間:2023-12-26 09:30:03 403觀看
導讀環境:SpringBoot2.7.151. 簡介Feign-reactive是一個用于在Spring Cloud應用程序中實現響應式微服務的框架。它支持在Spring Cloud應用程序中實現異步和非阻塞的遠程調用。Feign-reactive的一些主要特點:基于Feign的簡潔

環境:SpringBoot2.7.15EzJ28資訊網——每日最新資訊28at.com

1. 簡介

Feign-reactive是一個用于在Spring Cloud應用程序中實現響應式微服務的框架。它支持在Spring Cloud應用程序中實現異步和非阻塞的遠程調用。Feign-reactive的一些主要特點:EzJ28資訊網——每日最新資訊28at.com

  1. 基于Feign的簡潔風格:Feign-reactive繼承了Feign的簡潔風格,使得在編寫基于微服務架構的應用程序時,可以更加方便地實現異步編程。
  2. 支持Reactive編程模型:Feign-reactive提供對Reactive編程模型的支持,使得在編寫異步和非阻塞的代碼時更加容易。
  3. 異步和非阻塞遠程調用:通過Feign-reactive,可以輕松地實現異步和非阻塞的遠程調用,從而提高應用程序的響應速度和吞吐量。
  4. 與Spring Cloud集成:Feign-reactive與Spring Cloud集成,使得可以在Spring Cloud應用程序中方便地使用Feign-reactive實現響應式微服務。
  5. 可擴展性:Feign-reactive具有可擴展性,可以根據需要添加自定義的攔截器、編碼器和解碼器等。

Feign-reactive是一個非常有用的框架,可以幫助開發人員輕松地實現響應式微服務,提高應用程序的性能和吞吐量。EzJ28資訊網——每日最新資訊28at.com

2. 依賴管理

<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>

3. 實戰案例

遠程接口

@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 {}

基于反應式的Feign接口定義

@ReactiveFeignClient(    url = "http://localhost:8088/demos",     name = "demoReactorFeign")public interface DemoReactorFeign {  @GetMapping("/info/{id}")  public Mono<Object> info(@PathVariable("id") Integer id) ;}

以上就完成了一個非常簡單的反應式feign接口定義,接下來就可以使用了。其實這里除了注解與openfeign不一樣外,其它都一樣。EzJ28資訊網——每日最新資訊28at.com

測試調用

@Resourceprivate DemoReactorFeign demoReactorFeign ;@GetMapping("/{id}")public Object info(@PathVariable("id") Integer id) {  return this.demoReactorFeign.info(id) ;}

調用結果EzJ28資訊網——每日最新資訊28at.com

圖片EzJ28資訊網——每日最新資訊28at.com

接下來會介紹更多關于反應式feign的配置EzJ28資訊網——每日最新資訊28at.com

配置降級

@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。
EzJ28資訊網——每日最新資訊28at.com

圖片圖片EzJ28資訊網——每日最新資訊28at.com

超時配置EzJ28資訊網——每日最新資訊28at.com

reactive:  feign:    client:      config:        demoReactorFeign:          options:            connectTimeoutMillis: 2000            readTimeoutMillis: 2000

負載均衡配置EzJ28資訊網——每日最新資訊28at.com

reactive:  feign:    loadbalancer:      enabled: true

斷路器配置EzJ28資訊網——每日最新資訊28at.com

reactive:  feign:    circuit:      breaker:       enabled: true

要使其生效,必須引入下面的依賴EzJ28資訊網——每日最新資訊28at.com

<dependency>  <groupId>org.springframework.cloud</groupId>  <artifactId>spring-cloud-starter-circuitbreaker-reactor-resilience4j</artifactId></dependency>


EzJ28資訊網——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-54152-0.html深入ReactiveFeign:反應式遠程接口調用的最佳實踐

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: Go 框架 Beego 真的有那么差勁嗎

下一篇: LangChain應用開發指南-熟用LCEL語法掌握Chain的精髓

標簽:
  • 熱門焦點
Top 主站蜘蛛池模板: 林周县| 旌德县| 房山区| 甘泉县| 景德镇市| 凤凰县| 石景山区| 静安区| 福泉市| 唐山市| 永新县| 唐河县| 余姚市| 大厂| 临清市| 三都| 五大连池市| 荣昌县| 贵阳市| 修水县| 平江县| 凤阳县| 仪陇县| 太和县| 洛宁县| 宁晋县| 建湖县| 西藏| 邵阳市| 聂拉木县| 达州市| 阳春市| 西和县| 唐河县| 泸定县| 峨眉山市| 紫金县| 隆昌县| 胶南市| 沈阳市| 安乡县|