用 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类提供了 ...
随机推荐
- 新建Maven工程没有src/main...目录?
0.必看:详细的Maven项目介绍 1.问题 我新建的Maven项目的pom.xml为空,且无法被识别,同时项目目录没有src/main等等 2.解决 这里设置的JDK版本不对,我选用了JDK19 但 ...
- [转帖]Kafka故障之磁盘打满
https://www.jianshu.com/p/095e820361ae 问:磁盘打满扩容后能正常重启吗?答:不一定 要看文件格式是否损坏(log.index等).如果损坏会报错:index fi ...
- [转帖]构建 TiFlash 副本
https://docs.pingcap.com/zh/tidb/stable/create-tiflash-replicas#%E6%8C%89%E8%A1%A8%E6%9E%84%E5%BB%BA ...
- [转帖]Kafka 性能优化与问题深究
Kafka 性能优化与问题深究 一.Kafka深入探究 1.1 kafka整体介绍 1. 1.1 Kafka 如何做到高吞吐.低延迟的呢? Kafka是一个分布式高吞吐量的消息系统,这里提下 Kafk ...
- [转帖]神秘的backlog参数与TCP连接队列
https://www.cnblogs.com/codelogs/p/16060820.html 简介# 这要从一次压测项目说起,那是我们公司的系统与另几家同行公司的系统做性能比拼,性能数据会直接影响 ...
- awk中的NR,FNR ,NF,$NF,RS,ORS,FS,OFS
awk中的NR,FNR ,NF,$NF,RS,ORS,FS,OFS https://www.cnblogs.com/zhangqingsh/archive/2013/04/24/3040801.htm ...
- CentOS7升级Glibc到超过2.17版本无法启动的解决办法
CentOS7升级Glibc到超过2.17版本无法启动的解决办法 背景 今天有同事告知服务器宕机无法启动. 提示信息为: [sda] Assuming drive cache: write throu ...
- 依据HTML标准再探Javascript事件循环及其与浏览器渲染的关系
Javascript的一些基础概念 JavaScript执行引擎在宿主环境中是单线程的,这意味着在同一时间内只能执行一个任务.在Javascript运行期间,引擎会创建和维护相应的堆(heap)和栈( ...
- 【小测试】rust中的无符号整数溢出
作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢! cnblogs博客 zhihu Github 公众号:一本正经的瞎扯 1.在编译阶段就可以识别出来的溢出 fn main(){ ...
- NetCore高级系列文章03---中间件
.net web 一路发展过来,经历了 .net webfrom. .net Mvc ..net core不论哪种架构,都会对用户的请求到达服务器后经历一系列类似于管道的处理.在.net webfro ...