自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

深入ReactiveFeign:反應(yīng)式遠(yuǎn)程接口調(diào)用的最佳實(shí)踐

開發(fā) 前端
Feign-reactive是一個(gè)非常有用的框架,可以幫助開發(fā)人員輕松地實(shí)現(xiàn)響應(yīng)式微服務(wù),提高應(yīng)用程序的性能和吞吐量。

環(huán)境:SpringBoot2.7.15

1. 簡(jiǎn)介

Feign-reactive是一個(gè)用于在Spring Cloud應(yīng)用程序中實(shí)現(xiàn)響應(yīng)式微服務(wù)的框架。它支持在Spring Cloud應(yīng)用程序中實(shí)現(xiàn)異步和非阻塞的遠(yuǎn)程調(diào)用。Feign-reactive的一些主要特點(diǎn):

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

Feign-reactive是一個(gè)非常有用的框架,可以幫助開發(fā)人員輕松地實(shí)現(xiàn)響應(yīng)式微服務(wù),提高應(yīng)用程序的性能和吞吐量。

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. 實(shí)戰(zhàn)案例

遠(yuǎn)程接口

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

開啟反應(yīng)式功能

@EnableReactiveFeignClients
public class AppFeignReactorApplication {}

基于反應(yīng)式的Feign接口定義

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

以上就完成了一個(gè)非常簡(jiǎn)單的反應(yīng)式feign接口定義,接下來就可以使用了。其實(shí)這里除了注解與openfeign不一樣外,其它都一樣。

測(cè)試調(diào)用

@Resource
private DemoReactorFeign demoReactorFeign ;


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

調(diào)用結(jié)果

圖片

接下來會(huì)介紹更多關(guān)于反應(yīng)式feign的配置

配置降級(jí)

@ReactiveFeignClient(
    url = "http://localhost:8088/demos", 
    name = "demoReactorFeign", 
    fallback = DemoReactorFeignFallback.class,
    configuration = {DemoReactorFeignConfig.class}
)
public interface DemoReactorFeign {

降級(jí)接口定義

public class DemoReactorFeignFallback implements DemoReactorFeign {


  @Override
  public Mono<Object> info(Integer id) {
    return Mono.just("請(qǐng)求失敗") ;
  }


}

自定義配置

public class DemoReactorFeignConfig {


  @Bean
  public DemoReactorFeignFallback demoReactorFeignFallback() {
    return new DemoReactorFeignFallback() ;
  }
  
}

當(dāng)遠(yuǎn)程接口調(diào)用失敗或超時(shí)將會(huì)執(zhí)行上面的fallback。

圖片圖片

超時(shí)配置

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

負(fù)載均衡配置

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>


責(zé)任編輯:武曉燕 來源: Spring全家桶實(shí)戰(zhàn)案例源碼
相關(guān)推薦

2022-08-15 09:00:00

JavaScript前端架構(gòu)

2021-12-05 23:37:21

Java9異步編程

2023-08-31 16:47:05

反應(yīng)式編程數(shù)據(jù)流

2011-08-30 11:19:25

遠(yuǎn)程服務(wù)器數(shù)據(jù)中心遷移服務(wù)器管理工具

2024-12-02 10:56:29

2022-03-29 07:32:38

R2DBC數(shù)據(jù)庫反應(yīng)式

2024-01-31 08:26:44

2023-09-21 08:01:27

SpringR2DBC實(shí)現(xiàn)數(shù)據(jù)庫

2016-11-17 13:39:19

數(shù)據(jù)中心遠(yuǎn)程管理自動(dòng)化網(wǎng)絡(luò)

2021-03-04 11:06:05

自助服務(wù)

2024-01-09 07:25:31

2023-11-29 09:00:55

ReactuseMemo

2023-09-13 08:00:00

JavaScript循環(huán)語句

2016-12-27 08:49:55

API設(shè)計(jì)策略

2015-09-15 16:01:40

混合IT私有云IT架構(gòu)

2023-07-21 01:12:30

Reactfalse?變量

2011-08-18 11:05:21

jQuery

2018-10-24 11:01:53

分布式存儲(chǔ)系統(tǒng)

2012-02-17 16:41:38

云計(jì)算SaaS

2018-04-04 04:26:09

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)