zuul 參數(shù)調優(yōu)

zuul 參數(shù)調優(yōu)

zuul 參數(shù)調優(yōu)

適用版本:
spring-boot: 1.4.x.RELEASE
spring-cloud:Camden.SR3
Hystrix: 1.5.6

spring-boot-tomcat 優(yōu)化參數(shù):

主要只有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 優(yōu)化參數(shù):

ioThreads

設置IO線程數(shù), 它主要執(zhí)行非阻塞的任務,它們會負責多個連接,默認取CPU核心數(shù)量,最小值為2。
Math.max(Runtime.getRuntime().availableProcessors(), 2);
spring-boot 參數(shù):server.undertow.io-threads=

worker-threads

阻塞任務線程池, 當執(zhí)行類似servlet請求阻塞操作, undertow會從這個線程池中取得線程,它的值設置取決于系統(tǒng)的負載,默認值為io-threads*8。

spring-boot 參數(shù):server.undertow.worker-threads=

buffer

buffer-size:

每塊buffer的空間大小,越小的空間被利用越充分。

**buffers-per-region: **

每個區(qū)分配的buffer數(shù)量 , 所以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 參數(shù):

# 最大可用內存<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數(shù)量,最小為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 內置參數(shù)

zuul.host.maxTotalConnections

適用于ApacheHttpClient,如果是okhttp無效。每個服務的http客戶端連接池最大連接,默認是200.

zuul.host.maxPerRouteConnections

適用于ApacheHttpClient,如果是okhttp無效。每個route可用的最大連接數(shù),默認值是20。

zuul.semaphore.max-semaphores

Hystrix最大的并發(fā)請求execution.isolation.semaphore.maxConcurrentRequests,這個值并非TPS、QPS、RPS等都是相對值,指的是1秒時間窗口內的事務/查詢/請求,semaphore.maxConcurrentRequests是一個絕對值,無時間窗口,相當于亞毫秒級的。當請求達到或超過該設置值后,其其余就會被拒絕。默認值是100。參考: Hystrix semaphore和thread隔離策略的區(qū)別及配置參考

這個參數(shù)本來直接可以通過Hystrix的命名規(guī)則來設置,但被zuul重新設計了,使得在zuul中semaphores的最大并發(fā)請求有4個方法的參數(shù)可以設置,如果4個參數(shù)都存在優(yōu)先級(1~4)由高到低:

  • [優(yōu)先級1]zuul.eureka.api.semaphore.maxSemaphores
  • [優(yōu)先級2]zuul.semaphore.max-semaphores
  • [優(yōu)先級3]hystrix.command.api.execution.isolation.semaphore.maxConcurrentRequests
  • [優(yōu)先級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的優(yōu)先級高于hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests。

zuul.eureka.[commandKey].semaphore.maxSemaphores:
其中commandKey為

參考設置參數(shù):

#
zuul.host.maxTotalConnections: 200
zuul.host.maxPerRouteConnections: 10
#zuul.semaphore.max-semaphores: 128
# 建議使用這種方式來設置,可以給每個不同的后端微服務設置不同的信號量
zuul.eureka.[service id].semaphore.maxSemaphores: 128

其他Hystrix參數(shù):

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds用來設置thread和semaphore兩種隔離策略的超時時間,默認值是1000。

  • 建議設置這個參數(shù),在Hystrix 1.4.0之前,semaphore-isolated隔離策略是不能超時的,從1.4.0開始semaphore-isolated也支持超時時間了。
  • 建議通過CommandKey設置不同微服務的超時時間,對于zuul而言,CommandKey就是service id:hystrix.command.[CommandKey].execution.isolation.thread.timeoutInMilliseconds

ribbon參數(shù)

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

主要是ConnectTimeoutReadTimeout2個參數(shù),最終會設置到http Client中。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容