赞
踩
SpringBoot3.0 正式版发布后,最低支持JDK17并支持使用 GraalVM 将 Spring 的应用程序编译成本地可执行的镜像文件以及Http interface内置声明式的HTTP客户端等许多新特性。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
在接口类上添加注解 @HttpExchange 声明此类是 http interface 端点,并使用如下注解增加方法接口
@GetExchange: for HTTP GET requests.
@PostExchange: for HTTP POST requests.
@PutExchange: for HTTP PUT requests.
@DeleteExchange: for HTTP DELETE requests.
@PatchExchange: for HTTP PATCH requests.
@HttpExchange
public interface RemoteDemoApi {
@GetExchange("/demo")
String demo();
}
通过HttpServiceProxyFactory 携带目标接口的 BaseUrl 实现 webclient 和 http interface 的关联,使用ReactorLoadBalancerExchangeFilterFunction 负载均衡通过服务名从注册中心获取地址。
@Autowired
private ReactorLoadBalancerExchangeFilterFunction reactorLoadBalancerExchangeFilterFunction;
@Bean
public RemoteDemoApi remoteDemoLoadBalancer() {
WebClient client = WebClient.builder().filter(reactorLoadBalancerExchangeFilterFunction).baseUrl("http://UpmsService/").build();
HttpServiceProxyFactory factory = HttpServiceProxyFactory.builder(WebClientAdapter.forClient(client)).build();
return factory.createClient(RemoteDemoApi.class);
}
@RestController
@RequestMapping("/test")
public class TestController {
private final RemoteDemoApi remoteDemoApi;
public TestController(RemoteDemoApi remoteDemoApi) {
this.remoteDemoApi = remoteDemoApi;
}
@GetMapping
public String demo() {
return remoteDemoApi.demo();
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。