0


SpringBoot远程过程调用RPC(WebClient、HTTP Interface客户端)

目录

1. 远程过程调用介绍

远程过程调用

以前有sdk(Software Development Kit)工具包,导入jar包,直接调用功能即可。而远程过程调用由服务提供者提供API(接口: Application Programming Interface)服务,服务消费者通过连接对方服务器进行请求\响应交互,来实现调用效果

有两种应用场景:

  • 如果是内部微服务,可以通过依赖springcloud、注册中心、openfeign等进行调用。或使用第三方框架Dubbo、gRPC
  • 如果是外部暴露的,可以发送http请求、或遵循外部协议进行调用。SpringBoot提供了轻量级客户端方式: - RestTemplate: 普通开发- WebClient: 非阻塞、响应式编程开发- Http Interface: 声明式编程

2. WebClient

2.1 pom.xml添加依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-webflux</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>io.projectreactor</groupId>
  7. <artifactId>reactor-test</artifactId>
  8. <scope>test</scope>
  9. </dependency>

2.2 发送请求的内容

  • 请求方式: GET\POST\DELETE\xxxx
  • 请求路径: /xxx
  • 请求参数:key1=value1&key2=value2
  • 请求头: header1=value1,header2=value2
  • 请求体:json、文件、流

2.3 WebClient.builder()

可以使用WebClient.builder()配置更多参数项:

  • uriBuilderFactory: 自定义UriBuilderFactory ,定义baseurl
  • defaultUriVariables: 默认uri变量
  • defaultHeader: 每个请求默认头
  • defaultCookie: 每个请求默认cookie
  • defaultRequest: Consumer自定义每个请求
  • filter: 过滤 client发送的每个请求
  • exchangeStrategies: HTTP消息reader/writer自定义
  • clientConnector: HTTP client库设置

2.4 使用示例

也可以请求github提供的免费地址: https://api.github.com/search/users?q=username。注意不要请求太频繁

  1. package com.hh.springboot3test.service;
  2. import org.springframework.http.MediaType;
  3. import org.springframework.stereotype.Service;
  4. import org.springframework.web.reactive.function.client.WebClient;
  5. import reactor.core.publisher.Mono;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. @Service
  9. public class IpService {
  10. public Mono<String> queryIp(String ip) {
  11. // 创建WebClient
  12. // WebClient.create(String baseUrl)
  13. WebClient client = WebClient.create();
  14. // 准备数据
  15. Map<String, String> params = new HashMap<>();
  16. params.put("ip", ip);
  17. // 定义发请求行为, 类似CompletableFuture的异步发送
  18. Mono<String> mono = client.get()
  19. .uri("http://whois.pconline.com.cn/ipJson.jsp?ip={ip}}", params)
  20. .accept(MediaType.APPLICATION_JSON) // 定义响应的内容类型
  21. .retrieve()
  22. .bodyToMono(String.class);
  23. // 直接返回给RestContoller,返回给前端,可以正常显示
  24. return mono;
  25. }
  26. }
  • 可以通过toEntity(User.class)获取响应完整信息,返回Mono<ResponseEntity<User>>类型的数据
  • 可以通过bodyToFlux(User.class)获取stream数据,返回Flux<User>类型的数据
  • 可以通过onStatus(HttpStatus::is4xxClientError, response -> {......})定义错误处理

3. HTTP Interface

SpringBoot允许我们通过定义接口的方式,给任意位置发送http请求,实现远程调用

3.1 pom.xml添加依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-webflux</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>io.projectreactor</groupId>
  7. <artifactId>reactor-test</artifactId>
  8. <scope>test</scope>
  9. </dependency>

3.2 使用示例

IpInterface.java: 定义一个接口,里面说明请求的子url、接收类型、请求参数

  1. package com.hh.springboot3test.service;
  2. import org.springframework.web.bind.annotation.RequestParam;
  3. import org.springframework.web.service.annotation.GetExchange;
  4. import reactor.core.publisher.Mono;
  5. public interface IpInterface {
  6. @GetExchange(url = "/ipJson.jsp", accept = "application/json")
  7. // 方法接收ip,然后通过@RequestParam将ip传递给url的ip参数
  8. // 也可以通过@RequestHeader("header1")将值传递给请求的header1
  9. Mono<String> queryIp(@RequestParam("ip") String ip);
  10. }

IpService.java: 主要提供了baseUrl,然后就可以发送ip参数,获取结果了

  1. package com.hh.springboot3test.service;
  2. import org.springframework.stereotype.Service;
  3. import org.springframework.web.reactive.function.client.WebClient;
  4. import org.springframework.web.reactive.function.client.support.WebClientAdapter;
  5. import org.springframework.web.service.invoker.HttpServiceProxyFactory;
  6. import reactor.core.publisher.Mono;
  7. @Service
  8. public class IpService {
  9. public Mono<String> queryIp(String ip) {
  10. // 创建客户端
  11. WebClient client = WebClient.builder()
  12. .baseUrl("http://whois.pconline.com.cn")
  13. .codecs(clientCodecConfigurer -> {
  14. clientCodecConfigurer
  15. .defaultCodecs()
  16. .maxInMemorySize(256 * 1024 * 1024);
  17. // 响应数据量太大有可能会超出BufferSize,所以这里设置的大一点
  18. })
  19. .build();
  20. // 创建工厂
  21. HttpServiceProxyFactory factory = HttpServiceProxyFactory
  22. .builder(WebClientAdapter.forClient(client)).build();
  23. // 获取代理对象
  24. IpInterface ipApi = factory.createClient(IpInterface.class);
  25. return ipApi.queryIp(ip);
  26. }
  27. }

本文转载自: https://blog.csdn.net/yy8623977/article/details/131951781
版权归原作者 Bulut0907 所有, 如有侵权,请联系我们删除。

“SpringBoot远程过程调用RPC(WebClient、HTTP Interface客户端)”的评论:

还没有评论