深入ReactiveFeign:反應(yīng)式遠(yuǎn)程接口調(diào)用的最佳實(shí)踐
環(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):
- 基于Feign的簡(jiǎn)潔風(fēng)格:Feign-reactive繼承了Feign的簡(jiǎn)潔風(fēng)格,使得在編寫基于微服務(wù)架構(gòu)的應(yīng)用程序時(shí),可以更加方便地實(shí)現(xiàn)異步編程。
- 支持Reactive編程模型:Feign-reactive提供對(duì)Reactive編程模型的支持,使得在編寫異步和非阻塞的代碼時(shí)更加容易。
- 異步和非阻塞遠(yuǎn)程調(diào)用:通過Feign-reactive,可以輕松地實(shí)現(xiàn)異步和非阻塞的遠(yuǎn)程調(diào)用,從而提高應(yīng)用程序的響應(yīng)速度和吞吐量。
- 與Spring Cloud集成:Feign-reactive與Spring Cloud集成,使得可以在Spring Cloud應(yīng)用程序中方便地使用Feign-reactive實(shí)現(xiàn)響應(yīng)式微服務(wù)。
- 可擴(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>