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 发送请求的内容

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

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);


    }

}
  • 23
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值