基于AsyncRestTemplate异步HTTP请求的一种轻量级技术实现

本文介绍了如何利用Spring的AsyncRestTemplate实现批量异步HTTP请求,通过定义业务标识、请求、响应和异步调用处理,提高代码执行效率,减少响应时间。文章详细阐述了每个步骤,并给出了示例代码。
摘要由CSDN通过智能技术生成

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/51428562


Ⅰ、前言

          在上一篇博客中讲述ListenableFuture通过异步回调机制来实现请求的非阻塞。通常情况下,客户端获取数据并不会只发送一次http请求,可能会有多个http请求。这样,使用上一篇博客中的方法,就会产生大量的冗余代码,因为请求处理的代码除了一些参数不同外,其它地方都大致相同。我们发现不同请求之间的区别在于:请求地址的不同、响应类型的不同,可能还会有额外请求参数的不同。我们可以将请求数据和响应数据进行封装,这样,只需要一个字段来标识每一次http请求属于哪一个业务就可以实现批量发送http请求,整个过程是异步非阻塞的,一旦获取到数据就会触发回调函数,进而获取到响应数据,最后再进行业务逻辑相关处理。



Ⅱ、RestTemplate简介

1、定义

          RestTemplate是Spring3.0中出现的新类,其可以简化HTTP服务器通信,处理HTTP连接,使应用程序代码通过提供url和响应类型(可能的模板变量)便可提取结果。


2、方法

//get方法
//其中url为请求地址,responseType为响应类(需要自己依据响应格式来确定)
//urlVariables为数组变量
public <T> T getForObject(String url, Class<T> responseType, Object... urlVariables) throws RestClientException 

//urlVariables为Map类型变量,其中key为请求字段名,value为请求字段值
public <T> T getForObject(String url, Class<T> responseType, Map<String, ?> urlVariables)

public <T> T getForObject(URI url, Class<T> responseType) throws RestClientException

//ResponseEntity
public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... urlVariables) throws RestClientException

public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String, ?> urlVariables) throws RestClientException

public <T> ResponseEntity<T> getForEntity(URI url, Class<T> responseType) throws RestClientException 


//post
public <T> T postForObject(String url, Object request, Class<T> responseType, Object... uriVariables) throws RestClientException

public <T> T postForObject(String url, Object request, Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException

public <T> T postForObject(URI url, Object request, Class<T> responseType) throws RestClientException 

public <T> ResponseEntity<T> postForEntity(String url, Object request, Class<T> responseType, Object... uriVariables) throws RestClientException

public <T> ResponseEntity<T> postForEntity(String url, Object request, Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException

public <T> ResponseEntity<T> postForEntity(URI url, Object request, Class<T> responseType) throws RestClientException

3、说明

          Spring提供的RestTemplate可用于访问Rest服务的客户端,其提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率,但其并没有实现异步调用的功能。下面将引入Spring4.0提供的AsyncRestTemplate,该类可实现异步非阻塞处理http请求。



Ⅲ、AsyncRestTemplate简介

1、定义

          AsyncRestTemplate是在Spring4.0中对RestTemplate进行扩展产生的新类,其为客户端提供了异步http请求处理的一种机制,通过返回ListenableFuture对象生成回调机制,以达到异步非阻塞发送http请求。


2、方法

//get
public <T> ListenableFuture<ResponseEntity<T>> getForEntity(String url, Class<T> responseType, Object... uriVariables) throws RestClientException

public <T> ListenableFuture<ResponseEntity<T>> getForEntity(String url, Class<T> responseType, Map<String, ?> urlVariables) throws RestClientException

public <T> ListenableFuture<ResponseEntity<T>> getForEntity(URI url, Class<T> responseType) throws RestClientException


//post
public <T> ListenableFuture<ResponseEntity<T>> postForEntity(String url, HttpEntity<?> request, Class<T> responseType, Object... uriVariables) throws RestClientException

public <T> ListenableFuture<ResponseEntity<T>> postForEntity(String url, HttpEntity<?> request, Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException 

public <T> ListenableFuture<ResponseEntity<T>> postForEntity(URI url, HttpEntity<?> request, Class<T> responseType) throws RestClientException 

3、说明

          相比于RestTemplate,AsyncRestTemplate通过回调机制能够很好地异步处理多个http请求,使得客户端在主方法中不必等待服务器响应,而是继续执行后续代码,这样就较大地提高了代码的执行效率,减少响应时间。

  • 12
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
异步RestTemplate是Spring提供的一种并发处理能力较强的HttpClient,它能够在发送HTTP请求时自动创建新的线程池,充分发挥多CPU核心下的并行处理能力。在处理高并发请求时,异步RestTemplate的效率远高于同步方式。 在使用异步RestTemplate实现HTTP异步请求通用类Utils时,我们可以定义一个静态方法,该方法接收URL、请求方式、请求参数和回调函数等参数,将这些参数封装成RequestEntity对象,并使用异步RestTemplate进行异步HTTP请求。 具体实现方式如下: 1. 引入异步RestTemplate依赖,如下所示: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ``` 2. 定义异步HTTP请求通用类Utils,如下所示: ``` @Component public class HttpUtils { private final AsyncRestTemplate asyncRestTemplate; public HttpUtils(AsyncRestTemplate asyncRestTemplate) { this.asyncRestTemplate = asyncRestTemplate; } public static void asyncRequest(String url, HttpMethod method, HttpHeaders headers, Object requestBody, ParameterizedTypeReference responseType, AsyncCallback callback) { RequestEntity request = new RequestEntity(requestBody, headers, method, URI.create(url)); ListenableFuture<ResponseEntity> future = asyncRestTemplate.exchange(request, responseType); future.addCallback(callback); } } ``` 3. 在需要进行异步HTTP请求的位置调用HttpUtils类的asyncRequest方法即可,如下所示: ``` HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); JSONObject requestBody = new JSONObject(); requestBody.put("username", "admin"); requestBody.put("password", "123456"); ParameterizedTypeReference responseType = new ParameterizedTypeReference<ApiResponse<Object>>() {}; HttpUtils.asyncRequest("http://localhost:8080/login", HttpMethod.POST, headers, requestBody, responseType, new AsyncCallback() { @Override public void onSuccess(ResponseEntity responseEntity) { ApiResponse<Object> response = responseEntity.getBody(); System.out.println(response); } @Override public void onFailure(Throwable throwable) { throwable.printStackTrace(); } }); ``` 以上就是使用异步RestTemplate实现HTTP异步请求通用类Utils的步骤和详细说明。通过封装通用的异步HTTP请求方法,我们可以在各个业务模块中灵活调用,提高代码的复用性和可读性,同时也能够快速地响应请求,提高系统的并发处理能力。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值