轉自 https://www.jianshu.com/p/d401452fe76e
zuul 參數調優
適用版本:
spring-boot: 1.4.x.RELEASE
spring-cloud:Camden.SR3
Hystrix: 1.5.6
spring-boot-tomcat 優化參數:
主要只有2個,最大和最小worker線程:
server.tomcat.max-threads=128 # Maximum amount of worker threads.
server.tomcat.min-spare-threads=64 # Minimum amount of worker threads.
spring-boot-undertow 優化參數:
ioThreads
設置IO線程數, 它主要執行非阻塞的任務,它們會負責多個連接,默認取CPU核心數量,最小值爲2。Math.max(Runtime.getRuntime().availableProcessors(), 2);
spring-boot 參數:server.undertow.io-threads=
worker-threads
阻塞任務線程池, 當執行類似servlet請求阻塞操作, undertow會從這個線程池中取得線程,它的值設置取決於系統的負載,默認值爲io-threads*8。
spring-boot 參數:server.undertow.worker-threads=
buffer
buffer-size:
每塊buffer的空間大小,越小的空間被利用越充分。
**buffers-per-region: **
每個區分配的buffer數量 , 所以pool的大小是buffer-size * buffers-per-region。
directBuffers
是否分配的直接內存。
獲取JVM最大可用內存maxMemory=Runtime.getRuntime().maxMemory();
maxMemory<64M,不開啓directBuffers, bufferSize = 512,buffersPerRegion = 10;
64<=maxMemory<128M,開啓directBuffers, bufferSize = 1024 bytes,buffersPerRegion = 10;
maxMemory>128M,開啓directBuffers, bufferSize = 16*1024 bytes,buffersPerRegion = 20;
spring-boot 參數:
# 最大可用內存<64M,不開啓
server.undertow.buffer-size= # Size of each buffer in bytes.
server.undertow.buffers-per-region= # Number of buffer per region.
server.undertow.direct-buffers= # Allocate buffers outside the Java heap.
//默認值:cpu數量,最小爲2
server.undertow.io-threads= # Number of I/O threads to create for the worker.
//默認值:io-threads*8
server.undertow.worker-threads= # Number of worker threads.
zuul 內置參數
zuul.host.maxTotalConnections
適用於ApacheHttpClient,如果是okhttp無效。每個服務的http客戶端連接池最大連接,默認是200.
zuul.host.maxPerRouteConnections
適用於ApacheHttpClient,如果是okhttp無效。每個route可用的最大連接數,默認值是20。
zuul.semaphore.max-semaphores
Hystrix最大的併發請求execution.isolation.semaphore.maxConcurrentRequests
,這個值並非TPS
、QPS
、RPS
等都是相對值,指的是1秒時間窗口內的事務/查詢/請求,semaphore.maxConcurrentRequests
是一個絕對值,無時間窗口,相當於亞毫秒級的。當請求達到或超過該設置值後,其其餘就會被拒絕。默認值是100。參考: Hystrix semaphore和thread隔離策略的區別及配置參考
這個參數本來直接可以通過Hystrix的命名規則來設置,但被zuul重新設計了,使得在zuul中semaphores的最大併發請求有4個方法的參數可以設置,如果4個參數都存在優先級(1~4)由高到低:
- [優先級1]zuul.eureka.api.semaphore.maxSemaphores
- [優先級2]zuul.semaphore.max-semaphores
- [優先級3]hystrix.command.api.execution.isolation.semaphore.maxConcurrentRequests
- [優先級4]hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests
需要注意的是:在Camden.SR3版本的zuul中hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests
設置不會起作用,這是因爲在org.springframework.cloud.netflix.zuul.filters.ZuulProperties.HystrixSemaphore.maxSemaphores=100
設置了默認值100,因此zuul.semaphore.max-semaphores
的優先級高於hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests
。
zuul.eureka.[commandKey].semaphore.maxSemaphores:
其中commandKey爲
參考設置參數:
#
zuul.host.maxTotalConnections: 200
zuul.host.maxPerRouteConnections: 10
#zuul.semaphore.max-semaphores: 128
# 建議使用這種方式來設置,可以給每個不同的後端微服務設置不同的信號量
zuul.eureka.[service id].semaphore.maxSemaphores: 128
其他Hystrix參數:
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds
用來設置thread和semaphore兩種隔離策略的超時時間,默認值是1000。
- 建議設置這個參數,在Hystrix 1.4.0之前,semaphore-isolated隔離策略是不能超時的,從1.4.0開始semaphore-isolated也支持超時時間了。
- 建議通過CommandKey設置不同微服務的超時時間,對於zuul而言,CommandKey就是service id:
hystrix.command.[CommandKey].execution.isolation.thread.timeoutInMilliseconds
ribbon參數
ribbon:
# # Max number of next servers to retry (excluding the first server)
# MaxAutoRetries: 1
# # Whether all operations can be retried for this client
# MaxAutoRetriesNextServer: 1
# # Interval to refresh the server list from the source
# OkToRetryOnAllOperations: true
# # Interval to refresh the server list from the source
# ServerListRefreshInterval: 2000
# # Connect timeout used by Apache HttpClient
ConnectTimeout: 3000
# # Read timeout used by Apache HttpClient
ReadTimeout: 3000
主要是ConnectTimeout
和ReadTimeout
2個參數,最終會設置到http Client中。
作者:鐵湯
鏈接:https://www.jianshu.com/p/d401452fe76e
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯繫作者獲得授權並註明出處。