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

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

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

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

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

1. 簡介

Feign-reactive是一個用于在Spring Cloud應用程序中實現響應式微服務的框架。它支持在Spring Cloud應用程序中實現異步和非阻塞的遠程調用。Feign-reactive的一些主要特點:26328資訊網——每日最新資訊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是一個非常有用的框架,可以幫助開發人員輕松地實現響應式微服務,提高應用程序的性能和吞吐量。26328資訊網——每日最新資訊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不一樣外,其它都一樣。26328資訊網——每日最新資訊28at.com

測試調用

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

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

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

接下來會介紹更多關于反應式feign的配置26328資訊網——每日最新資訊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。
26328資訊網——每日最新資訊28at.com

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

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

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

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

reactive:  feign:    loadbalancer:      enabled: true

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

reactive:  feign:    circuit:      breaker:       enabled: true

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

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


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

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

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

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

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

標簽:
  • 熱門焦點
Top 主站蜘蛛池模板: 新乡县| 呼伦贝尔市| 肃宁县| 白城市| 水富县| 滦南县| 宁城县| 五寨县| 潜江市| 朝阳县| 仁布县| 海原县| 乳山市| 镇雄县| 班玛县| 云林县| 梁河县| 海门市| 贺兰县| 罗源县| 华容县| 三穗县| 乐安县| 天镇县| 青龙| 祁东县| 平邑县| 商南县| 淮北市| 托里县| 舞钢市| 汕尾市| 长岭县| 甘谷县| 博罗县| 江油市| 酉阳| 左贡县| 昌平区| 郸城县| 诏安县|