使用最新的Spring Boot2.0.3和 Spring Cloud F 系列進(jìn)行微服務(wù)架構(gòu)開發(fā)硝枉,在做壓測(cè)的時(shí)候遇到了很多問題抚笔,經(jīng)過一日一夜的源碼和 google苟翻,終于弄出一套方案衰倦。
關(guān)鍵字:com.netflix.hystrix.exception.HystrixRuntimeException花鹅; timeout; could not be queued for execution
1交掏、替換 tomcat
首先妆偏,把 tomcat 換成 undertow,這個(gè)性能在 Jmeter 的壓測(cè)下盅弛,undertow 比 tomcat 高一倍
第一步钱骂,pom 修改
<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>
第二步,配置
server:
undertow:
max-http-post-size: 0
# 設(shè)置IO線程數(shù), 它主要執(zhí)行非阻塞的任務(wù),它們會(huì)負(fù)責(zé)多個(gè)連接, 默認(rèn)設(shè)置每個(gè)CPU核心一個(gè)線程,數(shù)量和CPU 內(nèi)核數(shù)目一樣即可
io-threads: 4
# 阻塞任務(wù)線程池, 當(dāng)執(zhí)行類似servlet請(qǐng)求阻塞操作, undertow會(huì)從這個(gè)線程池中取得線程,它的值設(shè)置取決于系統(tǒng)的負(fù)載 io-threads*8
worker-threads: 32
# 以下的配置會(huì)影響buffer,這些buffer會(huì)用于服務(wù)器連接的IO操作,有點(diǎn)類似netty的池化內(nèi)存管理
# 每塊buffer的空間大小,越小的空間被利用越充分
buffer-size: 1024
# 每個(gè)區(qū)分配的buffer數(shù)量 , 所以pool的大小是buffer-size * buffers-per-region
# buffers-per-region: 1024 # 這個(gè)參數(shù)不需要寫了
# 是否分配的直接內(nèi)存
direct-buffers: true
2熊尉、替換 HTTPClient
其次把普通的 HTTPURLConnection 替換為使用 HTTPClient
第一步 pom 增加對(duì)應(yīng)的包
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
第二部罐柳,在 application.yml或者 bootstrap.yml 里面配置
# feign配置
feign:
hystrix:
# 在feign中開啟hystrix功能,默認(rèn)情況下feign不開啟hystrix功能
enabled: true
## 配置httpclient線程池
httpclient:
enabled: true
okhttp:
enabled: false
第三步狰住,配置 HTTPClient Bean
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 初始化連接池開始===" );
// 生成默認(rèn)請(qǐng)求配置
RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
// 超時(shí)時(shí)間
requestConfigBuilder.setSocketTimeout(5 * 1000);
// 連接時(shí)間
requestConfigBuilder.setConnectTimeout(5 * 1000);
RequestConfig defaultRequestConfig = requestConfigBuilder.build();
// 連接池配置
// 長(zhǎng)連接保持30秒
final PoolingHttpClientConnectionManager pollingConnectionManager = new PoolingHttpClientConnectionManager(30, TimeUnit.MILLISECONDS);
// 總連接數(shù)
pollingConnectionManager.setMaxTotal(1000);
// 同路由的并發(fā)數(shù)
pollingConnectionManager.setDefaultMaxPerRoute(100);
// httpclient 配置
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
// 保持長(zhǎng)連接配置墅垮,需要在頭添加Keep-Alive
httpClientBuilder.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy());
httpClientBuilder.setConnectionManager(pollingConnectionManager);
httpClientBuilder.setDefaultRequestConfig(defaultRequestConfig);
HttpClient client = httpClientBuilder.build();
// 啟動(dòng)定時(shí)器从媚,定時(shí)回收過期的連接
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
第一步刻帚,還是導(dǎo)入包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
第二步,配置
# 配置hystrix的參數(shù)
hystrix:
threadpool:
# default: 默認(rèn)參數(shù)创南,作用的所有的hystrix的客戶端,如果需要對(duì)某個(gè)具體的接口伦忠,可以寫接口+方法名稱
default:
coreSize: 500
command:
default:
fallback:
# 是否開啟回退方法
enabled: true
execution:
isolation:
thread:
timeoutInMilliseconds: 30000 #缺省為1000