环境: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.28at.com/showinfo-26-54152-0.html深入ReactiveFeign:反应式远程接口调用的最佳实践
声明:本网页内容旨在传播知识,不代表本站观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。