1、替换 tomcat
首先,把 tomcat 换成 undertow,这个性能在 Jmeter 的压测下,undertow 比 tomcat 高一倍
第一步,pom 修改去除tomcat
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-undertow</artifactId> </dependency>
|
第二步,配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| server: undertow: max-http-post-size: 0
io-threads: 4
worker-threads: 32
buffer-size: 1024
direct-buffers: true
|
2、替换 HTTPClient
第一步,加依赖
1 2 3 4
| <dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-httpclient</artifactId> </dependency>
|
第二部,在 application.yml或者 bootstrap.yml 里面配置
1 2 3 4 5 6 7 8 9 10
| feign: hystrix: enabled: true httpclient: enabled: true okhttp: enabled: false
|
第三步,配置 HTTPClient Bean
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.TimeUnit;
import org.apache.http.client.HttpClient; import org.apache.http.client.config.RequestConfig; import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;
@Configuration public class HttpPool {
@Bean public HttpClient httpClient(){ System.out.println("===== Apache httpclient 初始化连接池开始===" ); RequestConfig.Builder requestConfigBuilder = RequestConfig.custom(); requestConfigBuilder.setSocketTimeout(5 * 1000); requestConfigBuilder.setConnectTimeout(5 * 1000); RequestConfig defaultRequestConfig = requestConfigBuilder.build(); final PoolingHttpClientConnectionManager pollingConnectionManager = new PoolingHttpClientConnectionManager(30, TimeUnit.MILLISECONDS); pollingConnectionManager.setMaxTotal(1000); pollingConnectionManager.setDefaultMaxPerRoute(100);
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); httpClientBuilder.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy()); httpClientBuilder.setConnectionManager(pollingConnectionManager); httpClientBuilder.setDefaultRequestConfig(defaultRequestConfig); HttpClient client = httpClientBuilder.build();
Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { System.out.println("=====closeIdleConnections==="); pollingConnectionManager.closeExpiredConnections(); pollingConnectionManager.closeIdleConnections(5, TimeUnit.SECONDS); } }, 10 * 1000, 5 * 1000); System.out.println("===== Apache httpclient 初始化连接池完毕===");
return client; } }
|
3、配置 Hystrix
第一步,依赖
1 2 3 4
| <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency>
|
第二步,配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| hystrix: threadpool: default: coreSize: 500 command: default: fallback: enabled: true execution: isolation: thread: timeoutInMilliseconds: 30000
|
原文链接:https://www.jianshu.com/p/fe1c4412de7f