- Feign 配置優化
- hystrix配置 優化
- ribbon 優化
- Servlet 容器 優化
- Zuul配置 優化
文章目錄
- 1.Servlet 容器 優化
- 2.Feign 配置優化
- 3.Zuul配置 優化
- 4.hystrix配置 優化
- 5.ribbon 優化
1.Servlet 容器 優化
默認情況下, Spring Boot 使用 Tomcat 來作為內嵌的 Servlet 容器, 可以將 Web 服務器切換到 Undertow 來提高應用性能, Undertow 是紅帽公司開發的一款基于 NIO 的高性能 Web 嵌入式。
Zuul使用的內置容器默認是Tomcat, 可以將其換成undertow, 可以顯著減少線程的數量。
<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:io-threads: 16worker-threads: 256buffer-size: 1024buffers-per-region: 1024direct-buffers: true
- server.undertow.io-threads: 設置IO線程數, 主要執行非阻塞的任務。
- server.undertow.worker-threads: 阻塞任務線程池 。
- server.undertow.buffer-size: 類似netty的池化內存管理, buffer的大小, 小的空間被利用更充分。
- server.undertow.buffers-per-region: 每個區分配的buffer數量。
- server.undertow.direct-buffers: 是否分配的直接內存(NIO直接分配的堆外內存)。
2.Feign 配置優化
feign 默認不啟用hystrix, feign.hystrix.enabled=true 開啟熔斷。
feign 啟用壓縮也是一種有效的性能優化方式。
feign:compression:request:enabled: truemime-types: text/xml,application/xml,application/jsonresponse:enabled: true
feign HTTP請求方式選擇。
feign默認使用的是基于JDK提供的URLConnection調用HTTP接口, 無線程池, Apache HttpClient
和okhttp
都支持配置連接池功能, 也可以使用okhttp請求方式。
HttpClient:
feign:httpclient:enabled: truemax-connections:1000max-connections-per-route: 200
okHttp:
feign:okhttp:enabled: truehttpclient:max-connections: 1000max-connections-per-route: 200
- max-connections: 設置整個連接池最大連接數。
- max-connections-per-route: 設置路由的默認最大連接。
3.Zuul配置 優化
Hystrix有隔離策略: THREAD 以及SEMAPHORE, 默認是 SEMAPHORE 。
Zuul默認是使用信號量隔離, 信號量數量為100, 請求的并發線程超過100就會報錯。
zuul:semaphore:max-semaphores: 5000
為了方便ThreadLocal的使用, 可以改變隔離策略, 需要調大hystrix的線程池大小。
zuul:ribbonIsolationStrategy: THREAD
hystrix:threadpool:default:coreSize: 100maximumSize: 400allowMaximumSizeToDivergeFromCoreSize: truemaxQueueSize: -1
- hystrix.threadpool.default.allowMaximumSizeToDivergeFromCoreSize: 是否讓maximumSize生效, false為只有coreSize會生效。
- hystrix.threadpool.default.maxQueueSize: 線程池的隊列大小。
- hystrix.threadpool.default.maximumSize: 最大線程數。
- zuul.ribbon-isolation-strategy: 線程隔離策略。
4.hystrix配置 優化
需要設置參數hystrix.threadpool.default.coreSize 來指定熔斷隔離的線程數, 這個數需要調優。
hystrix:threadpool:default:coreSize: 500command:default:circuitBreaker: requestVolumeThreshold: 1000fallback:enabled: trueexecution:isolation:thread:timeoutInMilliseconds: 100000
- hystrix.command.default: 全局作用域, 作用的所有的hystrix的客戶端, 如果需要對某個微服務, 可以寫serviceId。
- hystrix.command.default.fallback.enabled: 是否開啟回退方法。
- hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 請求處理的超時時間, 缺省為1000,表示默認的超時時間為1S。
- hystrix.threadpool.default.coreSize 核心線程池數量。
- hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests 回退最大線程數。
- hystrix.command.default.circuitBreaker.requestVolumeThreshold: 熔斷器失敗的個數
5.ribbon 優化
Ribbon進行客戶端負載均衡的Client并不是在服務啟動的時候就初始化好的, 而是在調用的時候才會去創建相應的Client, 所有第一次調用的耗時不僅僅包含發送HTTP請求的時間, 還包括了創建RibbonClient的時間。
ribbon:eager-load:enabled:trueclients:service-1,service-2,service-n
- ribbon.eager-load.enabled: 開啟Ribbon的饑餓模式。
- ribbon.eager-load.clients: 指定需要饑餓加載的服務名。