小工具知识网

当前位置: 首页 > 行业知识 > SpringBoot远程过程调用RPC(WebClient、HTTP Interface客户端)

行业知识

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

2026-11-13 19:40:00 来源:互联网转载

目录

  • 1. 远程过程调用介绍
  • 2. WebClient
    • 2.1 pom.xml添加依赖
    • 2.2 发送请求的内容
    • 2.3 WebClient.builder()
    • 2.4 使用示例
    • 3. HTTP Interface
      • 3.1 pom.xml添加依赖
      • 3.2 使用示例

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

                                org.springframework.boot            spring-boot-starter-webflux                            io.projectreactor            reactor-test            test        

            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;@Servicepublic class IpService {    public Mono queryIp(String ip) {        // 创建WebClient        // WebClient.create(String baseUrl)        WebClient client = WebClient.create();        // 准备数据        Map params = new HashMap();        params.put("ip", ip);        // 定义发请求行为, 类似CompletableFuture的异步发送        Mono 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类型的数据
                • 可以通过bodyToFlux(User.class)获取stream数据,返回Flux类型的数据
                • 可以通过onStatus(HttpStatus::is4xxClientError, response -> {......})定义错误处理

                  3. HTTP Interface

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

                  3.1 pom.xml添加依赖

                                      org.springframework.boot            spring-boot-starter-webflux                            io.projectreactor            reactor-test            test        

                  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 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;@Servicepublic class IpService {    public Mono 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);    }}

上一篇:WPF 防止按钮Click时间多次点击响应

下一篇:怎么自己组装储存服务器?