
zuul 參數(shù)調(diào)優(yōu)
適用版本:
spring-boot: 1.4.x.RELEASE
spring-cloud:Camden.SR3
Hystrix: 1.5.6
spring-boot-tomcat 優(yōu)化參數(shù):
主要只有2個(gè),最大和最小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
設(shè)置IO線程數(shù), 它主要執(zhí)行非阻塞的任務(wù),它們會(huì)負(fù)責(zé)多個(gè)連接,默認(rèn)取CPU核心數(shù)量,最小值為2。
Math.max(Runtime.getRuntime().availableProcessors(), 2);
spring-boot 參數(shù):server.undertow.io-threads=
worker-threads
阻塞任務(wù)線程池, 當(dāng)執(zhí)行類(lèi)似servlet請(qǐng)求阻塞操作, undertow會(huì)從這個(gè)線程池中取得線程,它的值設(shè)置取決于系統(tǒng)的負(fù)載,默認(rèn)值為io-threads*8。
spring-boot 參數(shù):server.undertow.worker-threads=
buffer
buffer-size:
每塊buffer的空間大小,越小的空間被利用越充分。
**buffers-per-region: **
每個(gè)區(qū)分配的buffer數(shù)量 , 所以pool的大小是buffer-size * buffers-per-region。
directBuffers
是否分配的直接內(nèi)存。
獲取JVM最大可用內(nèi)存maxMemory=Runtime.getRuntime().maxMemory();
maxMemory<64M,不開(kāi)啟directBuffers, bufferSize = 512,buffersPerRegion = 10;
64<=maxMemory<128M,開(kāi)啟directBuffers, bufferSize = 1024 bytes,buffersPerRegion = 10;
maxMemory>128M,開(kāi)啟directBuffers, bufferSize = 16*1024 bytes,buffersPerRegion = 20;
spring-boot 參數(shù):
# 最大可用內(nèi)存<64M,不開(kāi)啟
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.
//默認(rèn)值:cpu數(shù)量,最小為2
server.undertow.io-threads= # Number of I/O threads to create for the worker.
//默認(rèn)值:io-threads*8
server.undertow.worker-threads= # Number of worker threads.
zuul 內(nèi)置參數(shù)
zuul.host.maxTotalConnections
適用于ApacheHttpClient,如果是okhttp無(wú)效。每個(gè)服務(wù)的http客戶端連接池最大連接,默認(rèn)是200.
zuul.host.maxPerRouteConnections
適用于ApacheHttpClient,如果是okhttp無(wú)效。每個(gè)route可用的最大連接數(shù),默認(rèn)值是20。
zuul.semaphore.max-semaphores
Hystrix最大的并發(fā)請(qǐng)求execution.isolation.semaphore.maxConcurrentRequests,這個(gè)值并非TPS、QPS、RPS等都是相對(duì)值,指的是1秒時(shí)間窗口內(nèi)的事務(wù)/查詢(xún)/請(qǐng)求,semaphore.maxConcurrentRequests是一個(gè)絕對(duì)值,無(wú)時(shí)間窗口,相當(dāng)于亞毫秒級(jí)的。當(dāng)請(qǐng)求達(dá)到或超過(guò)該設(shè)置值后,其其余就會(huì)被拒絕。默認(rèn)值是100。參考: Hystrix semaphore和thread隔離策略的區(qū)別及配置參考
這個(gè)參數(shù)本來(lái)直接可以通過(guò)Hystrix的命名規(guī)則來(lái)設(shè)置,但被zuul重新設(shè)計(jì)了,使得在zuul中semaphores的最大并發(fā)請(qǐng)求有4個(gè)方法的參數(shù)可以設(shè)置,如果4個(gè)參數(shù)都存在優(yōu)先級(jí)(1~4)由高到低:
- [優(yōu)先級(jí)1]zuul.eureka.api.semaphore.maxSemaphores
- [優(yōu)先級(jí)2]zuul.semaphore.max-semaphores
- [優(yōu)先級(jí)3]hystrix.command.api.execution.isolation.semaphore.maxConcurrentRequests
- [優(yōu)先級(jí)4]hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests
需要注意的是:在Camden.SR3版本的zuul中hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests設(shè)置不會(huì)起作用,這是因?yàn)樵?code>org.springframework.cloud.netflix.zuul.filters.ZuulProperties.HystrixSemaphore.maxSemaphores=100設(shè)置了默認(rèn)值100,因此zuul.semaphore.max-semaphores的優(yōu)先級(jí)高于hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests。
zuul.eureka.[commandKey].semaphore.maxSemaphores:
其中commandKey為
參考設(shè)置參數(shù):
#
zuul.host.maxTotalConnections: 200
zuul.host.maxPerRouteConnections: 10
#zuul.semaphore.max-semaphores: 128
# 建議使用這種方式來(lái)設(shè)置,可以給每個(gè)不同的后端微服務(wù)設(shè)置不同的信號(hào)量
zuul.eureka.[service id].semaphore.maxSemaphores: 128
其他Hystrix參數(shù):
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds用來(lái)設(shè)置thread和semaphore兩種隔離策略的超時(shí)時(shí)間,默認(rèn)值是1000。
- 建議設(shè)置這個(gè)參數(shù),在Hystrix 1.4.0之前,semaphore-isolated隔離策略是不能超時(shí)的,從1.4.0開(kāi)始semaphore-isolated也支持超時(shí)時(shí)間了。
- 建議通過(guò)CommandKey設(shè)置不同微服務(wù)的超時(shí)時(shí)間,對(duì)于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
主要是ConnectTimeout和ReadTimeout2個(gè)參數(shù),最終會(huì)設(shè)置到http Client中。