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添加依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>

2.2 发送请求的内容

  • 请求方式: GETPOSTDELETExxxx
  • 请求路径: /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。注意不要请求太频繁

package com.hh.springboot3test.service;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.util.HashMap;
import java.util.Map;

@Service
public class IpService {

    public Mono<String> queryIp(String ip) {
        // 创建WebClient
        // WebClient.create(String baseUrl)
        WebClient client = WebClient.create();

        // 准备数据
        Map<String, String> params = new HashMap<>();
        params.put("ip", ip);

        // 定义发请求行为, 类似CompletableFuture的异步发送
        Mono<String> mono = client.get()
                .uri("http://whois.pconline.com.cn/ipJson.jsp?ip={ip}}", params)
                .accept(MediaType.APPLICATION_JSON)    // 定义响应的内容类型
                .retrieve()
                .bodyToMono(String.class);

        // 直接返回给RestContoller,返回给前端,可以正常显示
        return mono;
    }

}
  • 可以通过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添加依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>

3.2 使用示例

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

package com.hh.springboot3test.service;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.service.annotation.GetExchange;
import reactor.core.publisher.Mono;


public interface IpInterface {

    @GetExchange(url = "/ipJson.jsp", accept = "application/json")
        // 方法接收ip,然后通过@RequestParam将ip传递给url的ip参数
        // 也可以通过@RequestHeader("header1")将值传递给请求的header1
    Mono<String> queryIp(@RequestParam("ip") String ip);
}

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

package com.hh.springboot3test.service;

import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.support.WebClientAdapter;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
import reactor.core.publisher.Mono;

@Service
public class IpService {


    public Mono<String> queryIp(String ip) {

        // 创建客户端
        WebClient client = WebClient.builder()
                .baseUrl("http://whois.pconline.com.cn")
                .codecs(clientCodecConfigurer -> {
                    clientCodecConfigurer
                            .defaultCodecs()
                            .maxInMemorySize(256 * 1024 * 1024);
                    // 响应数据量太大有可能会超出BufferSize,所以这里设置的大一点
                })
                .build();

        // 创建工厂
        HttpServiceProxyFactory factory = HttpServiceProxyFactory
                .builder(WebClientAdapter.forClient(client)).build();

        // 获取代理对象
        IpInterface ipApi = factory.createClient(IpInterface.class);

        return ipApi.queryIp(ip);


    }

}

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>