OpenFeign是Spring Cloud的一部分,它基于Feign實現了聲明式服務調用和負載均衡。以下是OpenFeign的一些主要特性:
總的來說,OpenFeign是一個功能強大的聲明式服務調用和負載均衡工具,它可以提高服務調用的效率和靈活性,并可以幫助用戶更好地管理他們的分布式系統。
但是OpenFeign并不支持反應式客戶端,如Spring WebClient,Spring Cloud OpenFeign也不支持。
feign-reactor是Spring Cloud的feign的擴展,它提供了對Reactor Netty的支持,可以更好地處理HTTP請求。具體來說,feign-reactor基于Reactor Netty實現,它支持Reactive編程模型,可以更好地處理異步請求,并且可以更好地利用網絡資源。此外,feign-reactor還提供了一些其他的特性,例如:支持負載均衡、支持熔斷器、支持自定義請求和響應等。
總的來說,feign-reactor可以提升feign在處理HTTP請求時的效率和靈活性。
使用上基本與openfeign一致,就是將相應的注解換了相應的名稱。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId></dependency><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>
feign-reactor-cloud依賴提供了CircuitBreaker + LoadBalancer的支持。
feign-reactor-webclient依賴提供了有關WebClient客戶端相關的實現及配置。
feign-reactor-spring-configuration依賴提供了Spring自動配置。
@SpringBootApplication// 這里與openfeign就是名稱不一樣@EnableReactiveFeignClientspublic class SpringcloudFeignReactorApplication {}
@ReactiveFeignClient( // 目標地址 url = "http://localhost:8088/demos", // 這里沒有走服務發現機制,隨意 name = "demoReactorFeign", // 回退;當發生異?;虺瑫r調用,這里與openfeign一樣都需要實現當前feign接口 fallback = DemoReactorFeignFallback.class, // 配置 configuration = {DemoReactorFeignConfig.class})public interface DemoReactorFeign { // 下面這個注解是feign的注解 // @RequestLine("GET /info/{id}") // feign中@PathVariable => @Param // 基于SpringMVC的注解 @GetMapping("/info/{id}") public Mono<Object> info(@PathVariable("id") Integer id) ; }
public class DemoReactorFeignFallback implements DemoReactorFeign { @Override public Mono<Object> info(Integer id) { return Mono.just("請求失敗") ; }}
// 這里沒有添加@Configuration注解,不需要,不過添加了也可以,只是可能會出現問題public class DemoReactorFeignConfig { // 配置上面的回退類 @Bean public DemoReactorFeignFallback demoReactorFeignFallback() { return new DemoReactorFeignFallback() ; } }
以上對feign reactor的使用除了類不一樣外,其它都與openfeign是保持一致的。
@RestController@RequestMapping("/reactor")public class DemoController { @Resource private DemoReactorFeign demoReactorFeign ; @GetMapping("/{id}") public Object info(@PathVariable("id") Integer id) { return this.demoReactorFeign.info(id) ; } }
圖片
成功調用目標接口
超時配置,我們只需要提供配置即可
reactive: feign: client: config: default: options: connectTimeoutMillis: 1000 readTimeoutMillis: 1000
以上是默認配置,對所有的接口都是一樣的超時時間。
由于目標接口模擬了耗時操作,所以調用了回退接口
reactive: feign: client: config: demoReactorFeign: options: connectTimeoutMillis: 2000 readTimeoutMillis: 2000
也可以直接通過編程的方式
public class ProgramReactorFeignMain { @Headers({ "Accept: application/json" }) static interface DemoReactorFeign { @RequestLine("GET /info/{id}") public Mono<Object> info(@Param("id") Integer id) ; } public static void main(String[] args) throws Exception { DemoReactorFeign target = WebReactiveFeign // WebClient based reactive feign //JettyReactiveFeign // Jetty http client based //Java11ReactiveFeign // Java 11 http client based .<DemoReactorFeign>builder() // 指定方法返回值參數化類型 .target(DemoReactorFeign.class, "http://localhost:8088/demos") ; target.info(6666).doOnNext(System.out::println).block() ; } }
完畢!!!
本文鏈接:http://www.tebozhan.com/showinfo-26-11247-0.html高并發下就該使用非阻塞式方式接口調用提高系統整體性能
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com
上一篇: Go 語言史詩級更新-循環Bug修復
下一篇: 分布式微服務架構中的關鍵技術解析