池化HttpClient,拿去就能用
1 import lombok.extern.slf4j.Slf4j;
2 import org.apache.http.HttpEntity;
3 import org.apache.http.HttpResponse;
4 import org.apache.http.HttpStatus;
5 import org.apache.http.NameValuePair;
6 import org.apache.http.client.HttpRequestRetryHandler;
7 import org.apache.http.client.config.RequestConfig;
8 import org.apache.http.client.entity.UrlEncodedFormEntity;
9 import org.apache.http.client.methods.CloseableHttpResponse;
10 import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
11 import org.apache.http.client.methods.HttpPost;
12 import org.apache.http.client.methods.HttpRequestBase;
13 import org.apache.http.config.Registry;
14 import org.apache.http.config.RegistryBuilder;
15 import org.apache.http.conn.socket.ConnectionSocketFactory;
16 import org.apache.http.conn.socket.PlainConnectionSocketFactory;
17 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
18 import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
19 import org.apache.http.entity.StringEntity;
20 import org.apache.http.impl.client.CloseableHttpClient;
21 import org.apache.http.impl.client.HttpClients;
22 import org.apache.http.impl.client.StandardHttpRequestRetryHandler;
23 import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
24 import org.apache.http.message.BasicNameValuePair;
25 import org.apache.http.ssl.SSLContextBuilder;
26 import org.apache.http.util.EntityUtils;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import java.io.IOException;
31 import java.io.UnsupportedEncodingException;
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Set;
36 import java.util.concurrent.ScheduledExecutorService;
37 import java.util.concurrent.ScheduledThreadPoolExecutor;
38 import java.util.concurrent.TimeUnit;
39
40 /**
41 * 可为每个域名设置单独的连接池数量
42 * connectionManager.setMaxPerRoute(new HttpRoute(new HttpHost("www.baidu.com")), 1);
43 * setConnectTimeout表示设置建立连接的超时时间
44 * setConnectionRequestTimeout表示从连接池中拿连接的等待超时时间
45 * setSocketTimeout表示发出请求后等待对端应答的超时时间
46 */
47
48 @Slf4j
49 public class HttpConnectionPoolUtils {
50
51 private static final Logger LOGGER = LoggerFactory.getLogger(HttpConnectionPoolUtils.class);
52
53 private static final String ENCODING = "UTF-8";
54
55 private static PoolingHttpClientConnectionManager connectionManager = null;
56
57 private static CloseableHttpClient httpClient;
58
59 static {
60 LOGGER.info("初始化http connection 连接池 ...");
61 try {
62 // 配置同时支持 HTTP 和 HTPPS
63 SSLContextBuilder builder = new SSLContextBuilder();
64 builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
65 SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(builder.build());
66 Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslConnectionSocketFactory).build();
67 connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
68 }catch (Exception e){
69 LOGGER.error("初始化http 连接池异常",e);
70 connectionManager = new PoolingHttpClientConnectionManager();
71 }
72 // 总连接池数量
73 connectionManager.setMaxTotal(20);
74 connectionManager.setDefaultMaxPerRoute(100);
75 RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(1000).setConnectionRequestTimeout(2000).setSocketTimeout(3000).build();
76 HttpRequestRetryHandler retryHandler = new StandardHttpRequestRetryHandler();
77
78 httpClient = HttpClients.custom().setConnectionManager(connectionManager).setDefaultRequestConfig(requestConfig).setRetryHandler(retryHandler).build();
79
80 ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(1);
81 scheduledExecutorService.scheduleWithFixedDelay(() -> {
82 connectionManager.closeExpiredConnections();
83 connectionManager.closeIdleConnections(20,TimeUnit.SECONDS);
84 LOGGER.info("回收过期的http连接完成 status:{}",connectionManager.getTotalStats());
85 },30,120,TimeUnit.SECONDS);
86
87 Runtime.getRuntime().addShutdownHook(new Thread(() -> {
88 log.info("关闭 httpClient 连接");
89 try {
90 if(httpClient != null){
91 httpClient.close();
92 }
93 } catch (IOException e) {
94 log.error("关闭 httpClient 异常",e);
95 }
96 }));
97 }
98
99 public static HttpClientResult doPost(String url, Map<String, String> params) throws IOException {
100 return doPost(url, null, params);
101 }
102
103 public static HttpClientResult doPost(String url, Map<String, String> headers, Map<String, String> params) throws IOException {
104
105 log.info("http post 请求:url={}", url);
106 log.info("http post 请求:params = {}", params);
107 // 创建httpClient对象
108 HttpPost httpPost = new HttpPost(url);
109 packageHeader(headers, httpPost);
110 // 封装请求参数
111 try {
112 packageParam(params, httpPost);
113 } catch (UnsupportedEncodingException e) {
114 throw e;
115 }
116
117 // 创建httpResponse对象
118 CloseableHttpResponse httpResponse = null;
119
120 try {
121 // 执行请求并获得响应结果
122 long startTime = System.currentTimeMillis();
123 HttpClientResult httpClientResult = getHttpClientResult(httpResponse, httpClient, httpPost);
124 long endTime = System.currentTimeMillis();
125 log.info("http post 请求相应时间:{}", (endTime-startTime));
126 log.info("http post 请求返回结果:{}", httpClientResult);
127 return httpClientResult;
128 } catch (Exception e) {
129 throw e;
130 } finally {
131 // 释放资源
132 if (httpResponse != null) {
133 httpResponse.close();
134 }
135 }
136 }
137
138 public static void packageHeader(Map<String, String> params, HttpRequestBase httpMethod) {
139 // 封装请求头
140 if (params != null) {
141 Set<Map.Entry<String, String>> entrySet = params.entrySet();
142 for (Map.Entry<String, String> entry : entrySet) {
143 // 设置到请求头到HttpRequestBase对象中
144 httpMethod.setHeader(entry.getKey(), entry.getValue());
145 }
146 }
147 }
148
149 public static void packageParam(Map<String, String> params, HttpEntityEnclosingRequestBase httpMethod)
150 throws UnsupportedEncodingException {
151 // 封装请求参数
152 if (params != null) {
153 List<NameValuePair> nvps = new ArrayList<NameValuePair>();
154 Set<Map.Entry<String, String>> entrySet = params.entrySet();
155 for (Map.Entry<String, String> entry : entrySet) {
156 nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
157 }
158
159 // 设置到请求的http对象中
160 httpMethod.setEntity(new UrlEncodedFormEntity(nvps, ENCODING));
161 }
162 }
163
164 public static HttpClientResult getHttpClientResult(CloseableHttpResponse httpResponse,
165 CloseableHttpClient httpClient, HttpRequestBase httpMethod) throws IOException {
166 // 执行请求
167 httpResponse = httpClient.execute(httpMethod);
168
169 // 获取返回结果
170 if (httpResponse != null && httpResponse.getStatusLine() != null) {
171 String content = "";
172 if (httpResponse.getEntity() != null) {
173 content = EntityUtils.toString(httpResponse.getEntity(), ENCODING);
174 }
175 return new HttpClientResult(httpResponse.getStatusLine().getStatusCode(), content);
176 }
177 return new HttpClientResult(HttpStatus.SC_INTERNAL_SERVER_ERROR);
178 }
179
180 public static String doPost(String url,String postBody) throws IOException {
181 HttpPost httpPost = new HttpPost(url);
182 StringEntity entity = new StringEntity(postBody,"utf-8");
183 entity.setContentEncoding("UTF-8");
184 entity.setContentType("application/json");
185 httpPost.setEntity(entity);
186 HttpResponse resp = httpClient.execute(httpPost);
187
188 String respContent = "";
189 if(resp.getStatusLine().getStatusCode() == 200) {
190 HttpEntity he = resp.getEntity();
191 respContent = EntityUtils.toString(he,"UTF-8");
192 }
193 return respContent;
194 }
195 }
196
池化HttpClient,拿去就能用的更多相关文章
- Deep Learning 学习随记(七)Convolution and Pooling --卷积和池化
图像大小与参数个数: 前面几章都是针对小图像块处理的,这一章则是针对大图像进行处理的.两者在这的区别还是很明显的,小图像(如8*8,MINIST的28*28)可以采用全连接的方式(即输入层和隐含层直接 ...
- 对象池化技术 org.apache.commons.pool
恰当地使用对象池化技术,可以有效地减少对象生成和初始化时的消耗,提高系统的运行效率.Jakarta Commons Pool组件提供了一整套用于实现对象池化的框架,以及若干种各具特色的对象池实现,可以 ...
- 第十三节,使用带有全局平均池化层的CNN对CIFAR10数据集分类
这里使用的数据集仍然是CIFAR-10,由于之前写过一篇使用AlexNet对CIFAR数据集进行分类的文章,已经详细介绍了这个数据集,当时我们是直接把这些图片的数据文件下载下来,然后使用pickle进 ...
- TensorFlow池化层-函数
池化层的作用如下-引用<TensorFlow实践>: 池化层的作用是减少过拟合,并通过减小输入的尺寸来提高性能.他们可以用来对输入进行降采样,但会为后续层保留重要的信息.只使用tf.nn. ...
- 【TensorFlow】tf.nn.max_pool实现池化操作
max pooling是CNN当中的最大值池化操作,其实用法和卷积很类似 有些地方可以从卷积去参考[TensorFlow]tf.nn.conv2d是怎样实现卷积的? tf.nn.max_pool(va ...
- tf入门-池化函数 tf.nn.max_pool 的介绍
转载自此大神 http://blog.csdn.net/mao_xiao_feng/article/details/53453926 max pooling是CNN当中的最大值池化操作,其实用法和卷积 ...
- 基于深度学习和迁移学习的识花实践——利用 VGG16 的深度网络结构中的五轮卷积网络层和池化层,对每张图片得到一个 4096 维的特征向量,然后我们直接用这个特征向量替代原来的图片,再加若干层全连接的神经网络,对花朵数据集进行训练(属于模型迁移)
基于深度学习和迁移学习的识花实践(转) 深度学习是人工智能领域近年来最火热的话题之一,但是对于个人来说,以往想要玩转深度学习除了要具备高超的编程技巧,还需要有海量的数据和强劲的硬件.不过 Tens ...
- 卷积和池化的区别、图像的上采样(upsampling)与下采样(subsampled)
1.卷积 当从一个大尺寸图像中随机选取一小块,比如说 8x8 作为样本,并且从这个小块样本中学习到了一些特征,这时我们可以把从这个 8x8 样本中学习到的特征作为探测器,应用到这个图像的任意地方中去. ...
- 深入解析CNN pooling 池化层原理及其作用
原文地址:https://blog.csdn.net/CVSvsvsvsvs/article/details/90477062 池化层作用机理我们以最简单的最常用的max pooling最大池化层为例 ...
随机推荐
- tune kubernetes eviction parameter
Highlight 本文会介绍kubernetes中关于集群驱逐的相关参数, 合理设置驱逐速率的考虑因素, 但是不会涉及node层面资源的驱逐阈值的设置. Basic 在kubernetes中, 如果 ...
- Caffe源码-SyncedMemory类
SyncedMemory类简介 最近在阅读caffe源码,代码来自BVLC/caffe,基本是参照网络上比较推荐的 Blob-->Layer-->Net-->Solver 的顺序来分 ...
- 简单介绍托管执行和 CLI
目录 CIL 和 ILDASM 查看 myApp.dll 的 CIL 输出 使用 ILSpy 查看 myApp.dll 反编译后的代码 处理器不能直接解释程序集.程序集用的是另一种语言,即公共中间语言 ...
- JS---最终版本--封装缓动(变速)动画函数---增加任意多个属性&回调函数&层级&透明度
封装缓动(变速)动画函数---增加任意多个属性&回调函数&层级&透明度 相较之前的,增加了2个判断,第一个判断是不是透明度,第二个判断是不是zindex, 都不是,就只是普通属 ...
- git中的SSL certificate problem: unable to get local issuer certificate错误的解决办法
我们在使用git初始化一个项目时,尤其是通过git submodule update --init --remote初始化子模块时,可能会遇到下面这个错误: fatal: unable to acce ...
- Kafka实战(七) - 优雅地部署 Kafka 集群
既然是集群,必然有多个Kafka节点,只有单节点构成的Kafka伪集群只能用于日常测试,不可能满足线上生产需求. 真正的线上环境需要考量各种因素,结合自身的业务需求而制定.看一些考虑因素(以下顺序,可 ...
- 同步IO, 异步IO的理解
1. 什么是IO? 在计算机中无时无刻不存在着对数据的访问和读取(数据都存储在物理的媒介上,例如寄存器,高速缓存,内存,磁盘,网卡等等),这些操作被称为IO. 2. 阻塞IO (1)当用户线程发起IO ...
- C# Monitor and transfer or copy the changed or created file to a new location
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...
- 挖掘Dark Sky Maps(热的要死后,疯传的一个气温地图网站)
最近,各种朋友圈,社会媒体,都在疯传一张图,这张图显示的全球的气温图,本没有什么特别的,但是这张图的网站来源所展示的数据与气象局或者各种天气预报的温度值相差倒是不少,引来一片网友的吐槽. 但是,作为专 ...
- Unable to connect to the server: x509: certificate signed by unknown authority
0x00 Problem 在使用二进制搭建 k8s 集群的过程中,使用 kubectl get 等操作时始终显示 x509: certificate signed by unknown authori ...