用 WebClient 代替 RestTemplate
RestTemplate是用于执行 HTTP 请求的同步客户端,通过底层 HTTP 客户端库(例如 JDK HttpURLConnection、Apache HttpComponents 等)公开一个简单的模板方法 API。
RestTemplate 通过 HTTP 方法为常见场景提供模板
注意:从Spring 5.0开始,该类处于维护模式,只有较小的更改请求和错误被接受。 请考虑使用 org.springframework.web.reactive.client.WebClient,它具有更现代的 API 并支持同步、异步和流式处理方案。

WebClient是用于执行 HTTP 请求的非阻塞、响应式客户端,通过底层 HTTP 客户端库(如 Reactor Netty)公开流畅的响应式 API。

平时在Spring Boot项目开发过程中,可以多使用WebClient来进行异步远程调用
举个例子:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
</dependencies>
package com.example.demo413;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
/**
* @Author ChengJianSheng
* @Date 2022/4/13
*/
@RestController
public class HelloController {
private String url = "http://localhost:8093/soas-enterprise/hello/hi?name=zhangsan"; // Thread.sleep(5000)
/**
* WebClient
*/
@GetMapping("/hello")
public String hello() {
Mono resp = WebClient.create().get().uri(url).retrieve().bodyToMono(String.class);
resp.subscribe(e-> System.out.println(e)); // 后执行
System.out.println(1); // 先执行
return "hello";
}
/**
* RestTemplate
*/
@GetMapping("/hi")
public String hi() {
RestTemplate restTemplate = new RestTemplate();
String resp = restTemplate.getForEntity(url, String.class).getBody();
System.out.println(resp); // 先执行
System.out.println(1); // 后执行
return resp;
}
}
观察控制台,看代码执行顺序
用 WebClient 代替 RestTemplate的更多相关文章
- Spring WebClient vs. RestTemplate
1. 简介 本教程中,我们将对比 Spring 的两种 Web 客户端实现 -- RestTemplate 和 Spring 5 中全新的 Reactive 替代方案 WebClient. 2. 阻塞 ...
- Spring5 of WebClient(转载)
前言 Spring5带来了新的响应式web开发框架WebFlux,同时,也引入了新的HttpClient框架WebClient.WebClient是Spring5中引入的执行 HTTP 请求的非阻塞. ...
- Feign Client 原理和使用
Feign Client 原理和使用 一块石头 公众号:好奇心森林 关注他 创作声明:内容包含虚构创作 6 人赞同了该文章 最近一个新项目在做后端HTTP库技术选型的时候对比了Spring We ...
- Spring Cloud Alibaba基础教程:支持的几种服务消费方式(RestTemplate、WebClient、Feign)
通过<Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现>一文的学习,我们已经学会如何使用Nacos来实现服务的注册与发现,同时也介绍如何通过LoadBal ...
- SpringBoot26 RestTemplate、WebClient
1 RestTemplate RestTemplate是在客户端访问 Restful 服务的一个核心类:RestTemplate通过提供回调方法和允许配置信息转换器来实现个性化定制RestTempla ...
- SpringBoot系列: RestTemplate 快速入门
====================================相关的文章====================================SpringBoot系列: 与Spring R ...
- RestTemplate的使用介绍汇总
一 常用方法 https://blog.csdn.net/u012843361/article/details/79893638 二 关于client的选择和设置(通过设置ClientHttpRequ ...
- SpringCloud03 Ribbon知识点、 Feign知识点、利用RestTemplate+Ribbon调用远程服务提供的资源、利用feign调用远程服务提供的资源、熔断
1 远程服务资源的调用 1.1 古老的套路 在微服务出现之前,所有的远程服务资源必须通过RestTemplate或者HttpClient进行:但是这两者仅仅实现了远程服务资源的调用,并未提供负载均衡实 ...
- RestTemplate最详解
目录 1. RestTemplate简单使用 2. 一些其他设置 3. 简单总结 在项目中,当我们需要远程调用一个HTTP接口时,我们经常会用到RestTemplate这个类.这个类是Spring框架 ...
- Spring框架spring-web模块中的RestTemplate类详解
RestTemplate类是spring-web模块中进行HTTP访问的REST客户端核心类.RestTemplate请求使用阻塞式IO,适合低并发的应用场景. 1. RestTemplate类提供了 ...
随机推荐
- 数字IC设计全流程介绍
数字IC设计全流程设计 掌握数字集成电路设计的流程 数字设计流程中每个阶段主要做哪些工作? 数字设计流程中每个阶段使用的主要EDA工具? 数字电路常用软件公司Mentor(questasim),Syn ...
- [转帖]Spring为啥不推荐使用@Autowired注解?
https://my.oschina.net/u/5079097/blog/5289666 引言 使用IDEA开发时,同组小伙伴都喜欢用@Autowired注入,代码一片warning,看着很不舒 ...
- [转帖] 请求量突增一下,系统有效QPS为何下降很多?
https://www.cnblogs.com/codelogs/p/17056485.html 原创:扣钉日记(微信公众号ID:codelogs),欢迎分享,转载请保留出处. 简介# 最近我观察到一 ...
- [转帖]chrome历史版本及重大变化(维基百科)
Google Chrome是Google LLC开发的免费 网络浏览器.开发过程分为不同的"发布渠道",每个发布渠道都在单独的开发阶段进行构建.Chrome提供了4种渠道:稳定版, ...
- [转帖]Python基础之函数(四)
https://www.jianshu.com/p/168e341fb81c 一.函数定义 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段:比如常用的print(),就是内建函数:通 ...
- [转帖]学习linux必须知道的命令
https://www.cnblogs.com/aibeier/p/15315487.html 基础不牢,地动山摇.在linux命令行下查看命令帮助man用于查看命令的帮助信息 man cp--hel ...
- 批量删除一个月为tag的镜像的办法
第一步获取镜像列表 这是一个最简单的列转行. docker images |grep 20220401 |awk 'BEGIN{ORS=","}{print $1}' 第二步执行双 ...
- JDK发布版本的总结
https://www.oracle.com/java/technologies/javase/8all-relnotes.html 从官网总结一下每个版本的发布日期 Java SE 8u141 Ad ...
- 两千字讲明白java中instanceof关键字的使用!
写在开头 在过往的内容中,我们讲了不少的Java关键字,比如final.static.this.super等等,Java中的关键字非常之多,下图是整理的关键字集合 而我们今天要学习的就是其中的inst ...
- 基于eBPF的微服务网络安全(Cilium 1)
基于eBPF的微服务网络安全 翻译自:Network security for microservices with eBPF 一些开源的kubernetes工具已经开始使用eBPF,这些工具大多数与 ...