赞
踩
以前有sdk(Software Development Kit)工具包,导入jar包,直接调用功能即可。而远程过程调用由服务提供者提供API(接口: Application Programming Interface)服务,服务消费者通过连接对方服务器进行请求\响应交互,来实现调用效果
有两种应用场景:
<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>
可以使用WebClient.builder()配置更多参数项:
也可以请求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 -> {......})
定义错误处理SpringBoot允许我们通过定义接口的方式,给任意位置发送http请求,实现远程调用
<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>
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); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。