From 31cacf75bc5bdb3c366599c7468e53d7b9abdca4 Mon Sep 17 00:00:00 2001 From: li5454yong <283056051@qq.com> Date: Mon, 6 Feb 2017 18:13:28 +0800 Subject: [PATCH] =?UTF-8?q?SpringBoot=E6=95=B4=E5=90=88HttpClient=E5=AE=9E?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 69 ++++++++++ .../java/com/lxg/springboot/HttpClient.java | 57 +++++++++ .../lxg/springboot/SpringbootApplication.java | 12 ++ .../lxg/springboot/http/HttpAPIService.java | 120 ++++++++++++++++++ .../com/lxg/springboot/http/HttpResult.java | 37 ++++++ .../http/IdleConnectionEvictor.java | 44 +++++++ src/main/resources/application.properties | 0 7 files changed, 339 insertions(+) create mode 100644 pom.xml create mode 100644 src/main/java/com/lxg/springboot/HttpClient.java create mode 100644 src/main/java/com/lxg/springboot/SpringbootApplication.java create mode 100644 src/main/java/com/lxg/springboot/http/HttpAPIService.java create mode 100644 src/main/java/com/lxg/springboot/http/HttpResult.java create mode 100644 src/main/java/com/lxg/springboot/http/IdleConnectionEvictor.java create mode 100644 src/main/resources/application.properties diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..7637da6 --- /dev/null +++ b/pom.xml @@ -0,0 +1,69 @@ + + + 4.0.0 + + com.lxg + springboot-mybatis + 1.0-SNAPSHOT + + + + aliyunRepository + myRepository + http://maven.aliyun.com/nexus/content/groups/public/ + + false + + + + + + org.springframework.boot + spring-boot-starter-parent + 1.3.8.RELEASE + + + + + UTF-8 + UTF-8 + 1.7 + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.springframework.boot + spring-boot-starter-web + + + + org.apache.httpcomponents + httpclient + 4.3.1 + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/src/main/java/com/lxg/springboot/HttpClient.java b/src/main/java/com/lxg/springboot/HttpClient.java new file mode 100644 index 0000000..3a6b096 --- /dev/null +++ b/src/main/java/com/lxg/springboot/HttpClient.java @@ -0,0 +1,57 @@ +package com.lxg.springboot; + +import org.apache.http.client.config.RequestConfig; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import javax.annotation.Resource; + + +/** + * Created by admin on 2017/2/6. + */ +@Configuration +public class HttpClient { + + + + @Bean(name = "httpClientConnectionManager") + public PoolingHttpClientConnectionManager demo(){ + PoolingHttpClientConnectionManager httpClientConnectionManager = new PoolingHttpClientConnectionManager(); + httpClientConnectionManager.setMaxTotal(100); + httpClientConnectionManager.setDefaultMaxPerRoute(20); + return httpClientConnectionManager; + } + + @Bean(name = "httpClientBuilder") + public HttpClientBuilder demo2(@Qualifier("httpClientConnectionManager")PoolingHttpClientConnectionManager httpClientConnectionManager){ + + HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); + + httpClientBuilder.setConnectionManager(httpClientConnectionManager); + + return httpClientBuilder; + } + + @Bean + public CloseableHttpClient demo3(@Qualifier("httpClientBuilder") HttpClientBuilder httpClientBuilder){ + return httpClientBuilder.build(); + } + + + @Bean(name = "builder") + public RequestConfig.Builder demo4(){ + RequestConfig.Builder builder = RequestConfig.custom(); + return builder.setConnectTimeout(1000).setSocketTimeout(10000).setStaleConnectionCheckEnabled(true); + } + + @Bean + public RequestConfig demo5(@Qualifier("builder") RequestConfig.Builder builder){ + return builder.build(); + } + +} diff --git a/src/main/java/com/lxg/springboot/SpringbootApplication.java b/src/main/java/com/lxg/springboot/SpringbootApplication.java new file mode 100644 index 0000000..2614740 --- /dev/null +++ b/src/main/java/com/lxg/springboot/SpringbootApplication.java @@ -0,0 +1,12 @@ +package com.lxg.springboot; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringbootApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringbootApplication.class, args); + } +} diff --git a/src/main/java/com/lxg/springboot/http/HttpAPIService.java b/src/main/java/com/lxg/springboot/http/HttpAPIService.java new file mode 100644 index 0000000..8877a26 --- /dev/null +++ b/src/main/java/com/lxg/springboot/http/HttpAPIService.java @@ -0,0 +1,120 @@ +package com.lxg.springboot.http; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import org.apache.http.NameValuePair; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.utils.URIBuilder; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.util.EntityUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + + +@Service +public class HttpAPIService { + + @Autowired + private CloseableHttpClient httpClient; + + @Autowired + private RequestConfig config; + + /** + * 不带参数的get请求,如果状态码为200,则返回body,如果不为200,则返回null + * + * @param url + * @return + * @throws Exception + */ + public String doGet(String url) throws Exception { + // 声明 http get 请求 + HttpGet httpGet = new HttpGet(url); + + // 装载配置信息 + httpGet.setConfig(config); + + // 发起请求 + CloseableHttpResponse response = this.httpClient.execute(httpGet); + + // 判断状态码是否为200 + if (response.getStatusLine().getStatusCode() == 200) { + // 返回响应体的内容 + return EntityUtils.toString(response.getEntity(), "UTF-8"); + } + return null; + } + + /** + * 带参数的get请求,如果状态码为200,则返回body,如果不为200,则返回null + * + * @param url + * @return + * @throws Exception + */ + public String doGet(String url, Map map) throws Exception { + URIBuilder uriBuilder = new URIBuilder(url); + + if (map != null) { + // 遍历map,拼接请求参数 + for (Map.Entry entry : map.entrySet()) { + uriBuilder.setParameter(entry.getKey(), entry.getValue().toString()); + } + } + + // 调用不带参数的get请求 + return this.doGet(uriBuilder.build().toString()); + + } + + /** + * 带参数的post请求 + * + * @param url + * @param map + * @return + * @throws Exception + */ + public HttpResult doPost(String url, Map map) throws Exception { + // 声明httpPost请求 + HttpPost httpPost = new HttpPost(url); + // 加入配置信息 + httpPost.setConfig(config); + + // 判断map是否为空,不为空则进行遍历,封装from表单对象 + if (map != null) { + List list = new ArrayList(); + for (Map.Entry entry : map.entrySet()) { + list.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString())); + } + // 构造佛from表单对象 + UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(list, "UTF-8"); + + // 把表单放到post里 + httpPost.setEntity(urlEncodedFormEntity); + } + + // 发起请求 + CloseableHttpResponse response = this.httpClient.execute(httpPost); + return new HttpResult(response.getStatusLine().getStatusCode(), EntityUtils.toString( + response.getEntity(), "UTF-8")); + } + + /** + * 不带参数post请求 + * + * @param url + * @return + * @throws Exception + */ + public HttpResult doPost(String url) throws Exception { + return this.doPost(url, null); + } +} \ No newline at end of file diff --git a/src/main/java/com/lxg/springboot/http/HttpResult.java b/src/main/java/com/lxg/springboot/http/HttpResult.java new file mode 100644 index 0000000..973fd8e --- /dev/null +++ b/src/main/java/com/lxg/springboot/http/HttpResult.java @@ -0,0 +1,37 @@ +package com.lxg.springboot.http; + +public class HttpResult { + + // 响应码 + private Integer code; + + // 响应体 + private String body; + + public HttpResult() { + super(); + } + + public HttpResult(Integer code, String body) { + super(); + this.code = code; + this.body = body; + } + + public Integer getCode() { + return code; + } + + public void setCode(Integer code) { + this.code = code; + } + + public String getBody() { + return body; + } + + public void setBody(String body) { + this.body = body; + } + +} \ No newline at end of file diff --git a/src/main/java/com/lxg/springboot/http/IdleConnectionEvictor.java b/src/main/java/com/lxg/springboot/http/IdleConnectionEvictor.java new file mode 100644 index 0000000..080248e --- /dev/null +++ b/src/main/java/com/lxg/springboot/http/IdleConnectionEvictor.java @@ -0,0 +1,44 @@ +package com.lxg.springboot.http; + +import java.util.Date; + +import org.apache.http.conn.HttpClientConnectionManager; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class IdleConnectionEvictor extends Thread { + + @Autowired + private HttpClientConnectionManager connMgr; + + private volatile boolean shutdown; + + public IdleConnectionEvictor() { + super(); + super.start(); + } + + @Override + public void run() { + try { + while (!shutdown) { + synchronized (this) { + wait(5000); + // 关闭失效的连接 + connMgr.closeExpiredConnections(); + } + } + } catch (InterruptedException ex) { + // 结束 + } + } + + //关闭清理无效连接的线程 + public void shutdown() { + shutdown = true; + synchronized (this) { + notifyAll(); + } + } +} \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..e69de29