SpringBoot整合HttpClient实例
This commit is contained in:
commit
31cacf75bc
|
@ -0,0 +1,69 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.lxg</groupId>
|
||||
<artifactId>springboot-mybatis</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>aliyunRepository</id>
|
||||
<name>myRepository</name>
|
||||
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.3.8.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.7</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>4.3.1</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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<String, Object> map) throws Exception {
|
||||
URIBuilder uriBuilder = new URIBuilder(url);
|
||||
|
||||
if (map != null) {
|
||||
// 遍历map,拼接请求参数
|
||||
for (Map.Entry<String, Object> 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<String, Object> map) throws Exception {
|
||||
// 声明httpPost请求
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
// 加入配置信息
|
||||
httpPost.setConfig(config);
|
||||
|
||||
// 判断map是否为空,不为空则进行遍历,封装from表单对象
|
||||
if (map != null) {
|
||||
List<NameValuePair> list = new ArrayList<NameValuePair>();
|
||||
for (Map.Entry<String, Object> 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);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue