HttpClient高并发下性能优化

1、CloseableHttpClient的使用和优化(案例1)

https://blog.csdn.net/u010285974/article/details/85696239

2、HttpClient连接池设置引发的一次雪崩(案例2)

https://blog.csdn.net/u010889990/article/details/96236617

3、使用httpClient连接池处理get或post请求

https://m.wang1314.com/doc/webapp/topic/20923280.html

创建一个HttpClient

public class HttpClientUtil{
	
    private static RequestConfig requestConfig = null;
	//httpClient
	private static final CloseableHttpClient httpClient;
	
	private static final PoolingHttpClientConnectionManager cm;
	//httpGet方法
//	private static final HttpGet httpget;
	//httpPost方法
//	private static final HttpPost httpPost;
    //响应处理器
//	private static final ResponseHandler<String> responseHandler;
  	static{
        //初始化HTTP请求配置,设置连接池请求、获取数据和建立连接超时时间
        requestConfig = RequestConfig.custom()
			        				 .setConnectionRequestTimeout(40000)
			        				 .setSocketTimeout(40000)
			        				 .setConnectTimeout(40000)
			        				 .build();
        
//        System.setProperty("http.maxConnections","50");//最大活跃连接数
//        System.setProperty("http.keepAlive", "true");//长连接
        
        //连接池配置
        cm = new PoolingHttpClientConnectionManager();
        cm.setMaxTotal(500);//最大连接数
        cm.setDefaultMaxPerRoute(100);//每个路由最大连接数
        //创建定制http客户端
        httpClient = HttpClients.custom()
        						.setConnectionManager(cm)
				                .setRetryHandler(new DefaultHttpRequestRetryHandler(0,false))//设置http client的重试次数,默认是3次;当前是禁用掉(如果项目量不到,这个默认即可)
				                .build();
        //初始化response解析器
//        responseHandler = new BasicResponseHandler();
    }

连接池配置连接数

1、方法一

PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();

cm.setMaxTotal(500);//最大连接数
cm.setDefaultMaxPerRoute(100);//每个路由最大连接数

httpClient = HttpClients.custom().setConnectionManager(cm).build();

2、方法二

System.setProperty("http.maxConnections","50");//最大活跃连接数
 httpClient = HttpClients.custom().useSystemProperties().build();

3、方法三

 HttpClientBuilder builder = HttpClientBuilder.create();
 builder.setMaxConnTotal(500).setMaxConnPerRoute(100);
 HttpClient httpClient = builder.build();

 

 

高并发情况下优化Fegin的性能可以从以下几个方面入手: 1. **连接池配置**: - **MaxConnections**:设置每个路由的最大连接数。可以通过`feign.client.config.default.max-connections`配置项来设置。 - **MaxConnectionsPerRoute**:设置每个路由的最大连接数。可以通过`feign.client.config.default.max-connections-per-route`配置项来设置。 2. **超时设置**: - **连接超时**:设置连接超时时间,可以通过`feign.client.config.default.connect-timeout`配置项来设置。 - **读取超时**:设置读取超时时间,可以通过`feign.client.config.default.read-timeout`配置项来设置。 3. **压缩配置**: - **请求压缩**:启用请求压缩,可以通过`feign.compression.request.enabled=true`配置项来启用。 - **响应压缩**:启用响应压缩,可以通过`feign.compression.response.enabled=true`配置项来启用。 4. **重试机制**: - 启用重试机制,可以通过`feign.client.config.default.retryer`配置项来设置重试策略。 5. **使用HTTP/2**: - 启用HTTP/2协议,可以通过`feign.httpclient.enabled=true`和`feign.httpclient.max-connections`等配置项来配置。 6. **缓存机制**: - 使用缓存机制来减少对下游服务的调用次数。可以通过集成缓存框架(如Redis)来实现。 7. **负载均衡**: - 使用Ribbon或Spring Cloud LoadBalancer进行负载均衡配置,确保请求均匀分布到各个实例上。 8. **异步调用**: - 使用异步调用方式,可以通过`@Async`注解和`CompletableFuture`来实现。 以下是一个示例配置: ```yaml feign: client: config: default: max-connections: 200 max-connections-per-route: 50 connect-timeout: 5000 read-timeout: 10000 compression: request: enabled: true response: enabled: true httpclient: enabled: true max-connections: 200 max-connections-per-route: 50 ``` 通过这些配置,可以有效提升Fegin在高并发情况下的性能。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值