spring-cloud-square-retrofit 定义
微服务远程调用框架,用于弥补 OpenFeign 对于响应式调用的缺失
场景
响应式微服务之间的调用,例如基于 Spring WebFlux 提供的 http 接口服务。尽管可以通过 WebClient 手动配置 ReactorLoadBalancerExchangeFilterFunction 实例来实现负载。如下案例,但无法像 OpenFeign 那样通过调用方法的方式调用远程服务,而且无法兼容非响应式的 http 接口服务。
| private final WebClient.Builder loadBalancedWebClientBuilder; |
| |
| public DemoConsumerController(WebClient.Builder loadBalancedWebClientBuilder) { |
| this.loadBalancedWebClientBuilder = loadBalancedWebClientBuilder; |
| } |
| |
| @LoadBalanced |
| @Bean |
| WebClient.Builder webClientBuilder() { |
| return WebClient.builder(); |
| } |
| |
| |
| private final String SERVICE = "http://silky-demo-webflux"; |
| @RequestMapping("/mono") |
| public Mono<String> mono(@RequestParam(value = "name", defaultValue = "Mary") String name) { |
| return loadBalancedWebClientBuilder.build().get().uri(SERVICE + "/producer/mono") |
| .retrieve().bodyToMono(String.class) |
| .map(greeting -> String.format("%s, %s!", greeting, name)); |
| } |
使用案例
web 案例
| |
| |
| |
| @SpringBootApplication |
| @EnableEurekaClient |
| @EnableRetrofitClients |
| public class SquareWebServer { |
| public static void main(String[] args) { |
| SpringApplication.run(SquareWebServer.class, args); |
| } |
| } |
| |
| @Configuration |
| public class OkHttpConfig { |
| |
| @Bean |
| @LoadBalanced |
| public OkHttpClient.Builder builder() { |
| return new OkHttpClient.Builder(); |
| } |
| |
| } |
| |
| @RetrofitClient("silky-demo-web") |
| public interface SilkyWebClient { |
| @GET("/producer/sleep/{seconds}") |
| Call<String> sleep(@Path("seconds") int seconds); |
| } |
| |
| dependencies { |
| implementation 'org.springframework.boot:spring-boot-starter-web' |
| implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap' |
| implementation 'org.springframework.cloud:spring-cloud-starter-config' |
| implementation 'org.springframework.cloud:spring-cloud-starter-loadbalancer' |
| implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client' |
| |
| implementation 'org.springframework.cloud:spring-cloud-square-okhttp:0.4.0' |
| implementation 'org.springframework.cloud:spring-cloud-square-retrofit:0.4.0' |
| implementation 'org.projectlombok:lombok' |
| annotationProcessor 'org.projectlombok:lombok' |
| testImplementation 'org.springframework.boot:spring-boot-starter-test' |
| } |
web flux 案例
| |
| |
| |
| @SpringBootApplication |
| @EnableEurekaClient |
| @EnableRetrofitClients |
| public class SquareWebServer { |
| public static void main(String[] args) { |
| SpringApplication.run(SquareWebServer.class, args); |
| } |
| } |
| |
| @Configuration |
| public class WebClientConfig { |
| |
| @Bean |
| @LoadBalanced |
| public WebClient.Builder builder() { |
| return new WebClientConfig().builder(); |
| } |
| |
| } |
| |
| @RetrofitClient("silky-demo-webflux") |
| public interface SilkyWebFluxClient { |
| @GET("/port/{name}") |
| Mono<String> port(@Path("name") int name); |
| } |
| |
| dependencies { |
| implementation 'org.springframework.boot:spring-boot-starter-web' |
| implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap' |
| implementation 'org.springframework.cloud:spring-cloud-starter-config' |
| implementation 'org.springframework.cloud:spring-cloud-starter-loadbalancer' |
| implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client' |
| |
| implementation 'org.springframework.cloud:spring-cloud-square-retrofit-webclient:0.4.0' |
| implementation 'org.projectlombok:lombok' |
| annotationProcessor 'org.projectlombok:lombok' |
| testImplementation 'org.springframework.boot:spring-boot-starter-test' |
| } |
完整项目源码地址
gitee:
基础启动说明
- 以 native 环境启动 silky-eureka-server
- 以 native 环境启动 silky-config
- 以 native 环境启动两个 silky-demo-web ,由于配置文件端口写的 0,所以无需专门配置端口
- 以 native 环境启动两个 silky-demo-webflux ,由于配置文件端口写的 0,所以无需专门配置端口
测试
在 silky-demo-square-web 与 silky-demo-square-webflux 都写了响应的单元测试,可直接验证
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律