目录
前言
最近比较忙好久没更新博客了,正好项目中使用到了WebClient,之前也项目中也没有实际使用过。所以最近也一直在学习和整理WebClient的相关文档。
一、WebClient是什么?
WebClient 被称作响应式 web 客户端,如何理解响应式,其实就是快速响应用户。传统的 RestTemplate 正好和其相对,它不是快速响应用户,它会阻塞代码,直到 http 请求返回响应才会继续运行下去,而 WebClient 则是异步执行,不阻塞代码。所以,WebClient 的出现就是为了替换掉 RestTemplate 或者 AsyncRestTemplate,它能够以少量的线程数处理高并发的 Http 请求。
官方的文档也有说明: NOTE: As of 5.0, the non-blocking, reactive org.springframework.web.reactive.client.WebClient offers a modern alternative to the RestTemplate with efficient support for both sync and async, as well as streaming scenarios. The RestTemplate will be deprecated in a future version and will not have major new features added going forward.
需要注意的是 webclient 方式,并不能提升程序的性能,它的价值在于用有限的资源提高系统的吞吐量和伸缩性。
二、使用步骤
1.引入依赖库
pom文件如下(示例):
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <dependency> <groupId>org.projectreactor</groupId> <artifactId>reactor-spring</artifactId> <version>1.0.1.RELEASE</version> </dependency>
2. 如何创建WebClient使用
创建 WebClient 实例的 3 种方式:
第一种,使用 WebClient 接口的默认方法
第二种,使用给出 URI 参数
第三种也是最复杂的一种,使用 DefaultWebClientBuilder 类
WebClient webClient = WebClient .builder() .baseUrl("http://localhost:8080") .defaultCookie("cookieKey", "cookieValue") .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) .defaultUriVariables(Collections.singletonMap("url", "http://localhost:8080")) .build();
WebClient 发起请求的代码如下:
指定Http方法 webclient .method(HttpMethod.POST); 或 webclient .post(HttpMethod.POST); Java
输入请求地址:
WebClient.RequestBodySpec uri1 = webclient .method(HttpMethod.POST) .uri("/resource");
我们可以为请求设置请求体,cookie,headers 等,例如:
设置 Content-Type,Post Json 数据,requestType 会被序列化成 Json 数据。
Mono<RestfulResponse> re = webclient .post().uri("/checkToken") .contentType(MediaType.APPLICATION_JSON_UTF8) .body(Mono.just(requestType), MyRequestType.class)
如果要 Post 原生的 Json 串,也容易,上述代码改成: Mono<RestfulResponse> re = webclient .post().uri("/checkToken") .contentType(MediaType.APPLICATION_JSON_UTF8) .body(BodyInserters.fromObject("{\"name\":\"zhangsan\"}"))
带上 Cookie
Mono<String> resp = WebClient.create() .method(HttpMethod.GET) .uri("http://localhost:8080") .cookie("name","zhang") .cookie("id","99999") .retrieve() .bodyToMono(String.class);
WebClient 发送请求的方法是异步执行的,在发送完毕后,程序继续往下运行,但也可以通过阻塞程序来等待请求的响应。
获得响应有 2 种方法,exchange 和 retrieve 方法。两者返回类型不同,exchange 返回的内容更多,包含了响应头信息,Cookie,状态码等信息,它的类型本质上是 ClientResponse。retrieve 方法则是获得响应 body 的快捷方式。由于响应的得到是异步的,所以可以调用 block 方法来阻塞当前程序,等待获得响应的结果。
String response2 = request1.exchange() .block() .bodyToMono(String.class) .block();
String response3 = request2 .retrieve() .bodyToMono(String.class) .block();
我们还可以通过lamda表达式,采用函数式编程的方式,注册相应的事件响应。例如,可以使用 onStatus 处理响应状态,doOnError,onErrorReturn 进行错误处理。
WebClient 获得响应有 2 个方法 bodyToMono 和 bodyToFlux,这两者返回的类型分别是 Mono<T> 和 Flux<T>。两者的区别在于 Mono<T> 的响应结果,仅包含 0-1 个结果,而 Flux<T> 可以包含多个结果。
总结
WebClient 使用简单友好,熟悉 http 协议的人会很容易上手,一旦熟练掌握后,会提高开发效率和程序的吞吐量和伸缩性。