feign 整合 httpclien 连接池
时间: 2023-10-01 20:11:56 浏览: 219
Feign HTTP Client 使用指南 - Tower.zip
在 Feign 中使用 HttpClient 连接池可以提高性能和资源利用率,避免重复创建和关闭连接。下面是整合的步骤:
1. 添加依赖
在 pom.xml 中添加以下依赖:
```xml
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.12</version>
</dependency>
```
2. 配置 HttpClient
在 Spring Boot 中,可以使用 HttpClientBuilder 来构建 HttpClient,并配置连接池参数:
```java
@Configuration
public class HttpClientConfig {
@Value("${httpclient.max-total}")
private int maxTotal;
@Value("${httpclient.default-max-per-route}")
private int defaultMaxPerRoute;
@Bean
public CloseableHttpClient httpClient() {
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setMaxTotal(maxTotal);
connectionManager.setDefaultMaxPerRoute(defaultMaxPerRoute);
return HttpClientBuilder.create()
.setConnectionManager(connectionManager)
.build();
}
}
```
其中,maxTotal 表示连接池最大连接数,defaultMaxPerRoute 表示每个路由(域名)最大连接数。
3. 配置 Feign
在 Feign 中配置 HttpClient,需要实现 HttpClient 的 Client 接口,具体如下:
```java
public class HttpClientFeignClient implements Client {
private final CloseableHttpClient httpClient;
public HttpClientFeignClient(CloseableHttpClient httpClient) {
this.httpClient = httpClient;
}
@Override
public Response execute(Request request, Request.Options options) throws IOException {
HttpClientContext context = HttpClientContext.create();
HttpUriRequest httpUriRequest = toHttpUriRequest(request);
CloseableHttpResponse httpResponse = httpClient.execute(httpUriRequest, context);
return toResponse(httpResponse);
}
private HttpUriRequest toHttpUriRequest(Request request) {
String method = request.method();
String url = request.url();
Request.Body body = request.body();
HttpEntityEnclosingRequestBase httpUriRequest;
switch (method) {
case "GET":
httpUriRequest = new HttpGet(url);
break;
case "POST":
httpUriRequest = new HttpPost(url);
break;
case "PUT":
httpUriRequest = new HttpPut(url);
break;
case "DELETE":
httpUriRequest = new HttpDelete(url);
break;
default:
throw new UnsupportedOperationException("unsupported http method: " + method);
}
if (body != null) {
httpUriRequest.setEntity(new ByteArrayEntity(body.asBytes()));
}
return httpUriRequest;
}
private Response toResponse(CloseableHttpResponse httpResponse) throws IOException {
int statusCode = httpResponse.getStatusLine().getStatusCode();
String reasonPhrase = httpResponse.getStatusLine().getReasonPhrase();
Map<String, Collection<String>> headers = new HashMap<>();
for (Header header : httpResponse.getAllHeaders()) {
String name = header.getName();
String value = header.getValue();
headers.computeIfAbsent(name, k -> new ArrayList<>()).add(value);
}
Response.Builder builder = Response.builder()
.status(statusCode)
.reason(reasonPhrase)
.headers(headers);
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
InputStream inputStream = entity.getContent();
byte[] bodyData = ByteStreams.toByteArray(inputStream);
builder.body(bodyData);
}
return builder.build();
}
}
```
最后,在 Feign 的配置类中,使用上述实现类作为 Client:
```java
@Configuration
public class FeignConfig {
@Autowired
private CloseableHttpClient httpClient;
@Bean
public Client feignClient() {
return new HttpClientFeignClient(httpClient);
}
}
```
这样就完成了 Feign 整合 HttpClient 连接池的配置。
阅读全文