当前位置: 首页 > news >正文

中国网上购物网站管理的本质是什么

中国网上购物网站,管理的本质是什么,学校网站设计的作用,南阳网站建设哪家好SpringBoot自带的RestTemplate是没有使用连接池的,只是SimpleClientHttpRequestFactory实现了ClientHttpRequestFactory、AsyncClientHttpRequestFactory 2个工厂接口,因此每次调用接口都会创建连接和销毁连接,如果是高并发场景下会大大降低性…

SpringBoot自带的RestTemplate是没有使用连接池的,只是SimpleClientHttpRequestFactory实现了ClientHttpRequestFactory、AsyncClientHttpRequestFactory 2个工厂接口,因此每次调用接口都会创建连接和销毁连接,如果是高并发场景下会大大降低性能。因此,我们可以使用Apache的HttpClient连接池。

pom.xml

		<!-- RestTemplate使用Apache的HttpComponentsClientHttpRequestFactory替换掉Spring SimpleClientHttpRequestFactory 以使用Apache HttpClient的连接池。 --><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId></dependency>

RestTemplate配置类

import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;@Configuration
public class RestTemplateConfig {@Beanpublic RestTemplate restTemplate() {PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();connectionManager.setMaxTotal(50);connectionManager.setDefaultMaxPerRoute(20);RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(5000) // timeout to get connection from pool.setSocketTimeout(5000) // standard connection timeout.setConnectTimeout(5000) // standard connection timeout.build();HttpClient httpClient = HttpClientBuilder.create().setConnectionManager(connectionManager).setDefaultRequestConfig(requestConfig).build();ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);return new RestTemplate(requestFactory);}}

调用

	@Autowired  private RestTemplate restTemplate;  public Res getData(Dto dto) {String url = "https://xxx.com/api/xxx";//封装请求头参数.HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.APPLICATION_JSON);headers.set("Content-Type", "application/json;charset=utf-8");headers.set("自定义请求头key","自定义请求头value");Res res = restTemplate.postForEntity(url, new HttpEntity<>(dto, headers), Res.class).getBody();return res;}

注意

在Spring Boot中,RestTemplate已经过时,建议使用更现代的RestTemplateBuilder和WebClient。

使用RestTemplateBuilder

import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.web.reactive.function.client.WebClient;  @Service  
public class HttpClientService {  private final RestTemplateBuilder restTemplateBuilder;  @Autowired  public HttpClientService(RestTemplateBuilder restTemplateBuilder) {  this.restTemplateBuilder = restTemplateBuilder;  }  public String getData(String url) {  return restTemplateBuilder.build().getForObject(url, String.class);  }
}

使用WebClient自定义连接池

import org.springframework.beans.factory.annotation.Value;  
import org.springframework.http.HttpMethod;  
import org.springframework.stereotype.Service;  
import org.springframework.web.reactive.function.client.WebClient;  @Service  
public class CustomHttpClientService {  private final WebClient webClient;  @Autowired  public CustomHttpClientService(@Value("${custom.pool.size:10}") int poolSize) {  this.webClient = WebClient.builder()  .poolSize(poolSize) // 设置连接池大小等其它参数,这里不在一一赘述。.build();  }  public String getData(String url) {  return webClient.method(HttpMethod.GET).uri(url).retrieve().bodyToMono(String.class).block();  }  
}
http://www.yayakq.cn/news/172779/

相关文章:

  • 外贸网站平台排行榜软件开发需要什么专业
  • php网站开发实例电子版网站建设前期团队建设
  • Ext做网站营销型网站建设方案书
  • 教育网站制作建设银行东航龙卡登录东航网站
  • 装修公司网站该怎么做企业成品网站模板
  • 专做衬衣的网站网站搜索框怎么做
  • 做玄幻封面素材网站海报设计在线生成
  • 台州网站建站服务哪家奿最近热搜新闻事件
  • 网站建设费用如何做账务处理网站建设需要机房服务器
  • 可做笔记的阅读网站贵州省住房和城乡建设厅网站官网
  • 邯郸网站建设网页设计网络推广长春餐饮网站建设
  • 深圳微信网站公司哪家好股市行情app
  • 在家做的手工活哪里有网站wordpress 小米官网主题下载
  • 江西网站开发企业重庆做网站制作公司
  • 专业的营销网站建设公司排名邯郸企业做网站费用
  • 重庆seo整站优化系统房屋租赁合同
  • 淘宝基地网站怎么做做网站域名哪里来
  • 万网网站根目录做俄罗斯外贸的网站设计
  • 虚拟机上做钓鱼网站网站开发的企业
  • 网站开发行业工作交接交接哪些给网站做公正需要带什么
  • 成都温江网站建设怎么给自己制作一个网站
  • 什么网站可以免费做兼职舆情app免费
  • 导航网站超链接如何做商丘三合一网站建设
  • 制作一个买股票的网站怎么做营销网站定制
  • 网站图片用什么格式深圳seo优化公司搜索引擎优化方案
  • 一站式服务就像一个什么营销型网站建设案例
  • 藁城 网站济南网站建设 首选搜点网络
  • 莆田建站培训帮人做网站收费合法吗
  • 固阳网站建设广州调查公司
  • 网站开发能赚钱吗做网站合同范本