一、web服務器配置
選用undertow服務器,添加undertow依賴
<dependency>
? <groupId>org.springframework.boot</groupId>
? <artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
a、相關配置
server:
? undertow:
? ? io-threads: 16
? ? worker-threads: 256
? ? buffer-size: 1024
? ? buffers-per-region: 1024
? ? direct-buffers: true
b、參數(shù)解釋
server.undertow.io-threads: 設置IO線程數(shù), 它主要執(zhí)行非阻塞的任務,它們會負責多個連接, 默認設置每個CPU核心一個線程,不要設置過大,如果過大,啟動項目會報錯:打開文件數(shù)過多
server.undertow.worker-threads: 阻塞任務線程池, 當執(zhí)行類似servlet請求阻塞IO操作, undertow會從這個線程池中取得線程,它的值設置取決于系統(tǒng)線程執(zhí)行任務的阻塞系數(shù),默認值是IO線程數(shù)*8
server.undertow.buffer-size: 以下的配置會影響buffer,這些buffer會用于服務器連接的IO操作,有點類似netty的池化內(nèi)存管理,每塊buffer的空間大小,越小的空間被利用越充分,不要設置太大,以免影響其他應用,合適即可
server.undertow.buffers-per-region: 每個區(qū)分配的buffer數(shù)量 , 所以pool的大小是buffer-size * buffers-per-region
server.undertow.direct-buffers: 是否分配的直接內(nèi)存(NIO直接分配的堆外內(nèi)存)
二、Fegin配置
服務之間的調(diào)用,目前比較常用的都是通過openfeign來進行調(diào)用,而openfeign是集成有負載均衡ribbon、熔斷器hystrix的
1、hystrix熔斷
feign默認不啟用hystrix,需要手動指定開啟熔斷
feign.hytrix.enable=true
2、壓縮
#啟用feign請求壓縮
feign.compression.request.enable=true
#壓縮的mimetype
feign.compression.request.mime-types=text/xml,application/xml,application/json
#啟用feign響應壓縮
feign.compression.request.enable=true
3、feign http請求方式選擇
feign默認使用的是基于JDK提供的URLConnection調(diào)用HTTP接口,不具備連接池,所以資源開銷上有點影響,經(jīng)測試JDK的URLConnection比Apache HttpClient快很多倍。Apache HttpClient和okhttp都支持配置連接池功能,也可以使用okhttp請求方式。
a、當使用HttpClient時,可如下設置:
feign:
? httpclient:
? ? enabled: true
? ? max-connections:1000
? ? max-connections-per-route: 200
b、當使用OKHttp時,可如下設置
feign:
? okhttp:
? ? enabled: true
? httpclient:
? ? max-connections: 1000
? ? max-connections-per-route: 200
參數(shù)解釋
max-connections 設置整個連接池最大連接數(shù)(該值默認為200), 根據(jù)自己的場景決定
max-connections-per-route 設置路由的默認最大連接(該值默認為50),限制數(shù)量實際使用
三、hystrix配置
a、hystrix隔離配置
##hystrix隔離機制,線程
hystrix.command.default.execution.isolation.strategy=THREAD
#hystrixsamephore隔離極致并發(fā)數(shù)設置
hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests=1000
#開啟hystrix超時
hystrix.command.default.execution.timeout.enabled=true
#hystix超時設置? (1 + MaxAutoRetries + MaxAutoRetriesNextServer) * ReadTimeout
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=3000
b、hystrix線程池配置
#hystrix線程隔離線程池核心線程數(shù)
hystrix.threadpool.default.coreSize=1000
#hystrix線程隔離線程池最大線程數(shù)
hystrix.threadpool.default.maximumSize=1000
#hystrix線程隔離線程池隊列大?。?1:SynchronousQueue)
hystrix.threadpool.default.maxQueueSize=10
#允許達到最大線程數(shù)
hystrix.threadpool.default.allowMaximumSizeToDivergeFromCoreSize=true
# ##即使maxQueueSize沒有達到,達到queueSizeRejectionThreshold該值后,請求也會被拒絕
hystrix.threadpool.default.queueSizeRejectionThreshold=10
c、Hystrix針對單個接口設置超時時間語法
類名#方法名(參數(shù)類型,參數(shù)類型……)
類名:要設置的某個FeignClient的類名
方法名:設置FeignClient里面的方法
入?yún)㈩愋停悍椒ɡ锩嫘枰獋魅氲膮?shù)。如果是基礎類型,就直接填基礎類型:String/int等;如果是某個對象,就直接填對象的類名
舉例如下:
hystrix.command.UserInfoOpenClient#getUserById(Long).execution.isolation.strategy=THREAD
hystrix.command.UserInfoOpenClient#getUserById(Long).isolation.semaphore.maxConcurrentRequests=1000
hystrix.command.UserInfoOpenClient#getUserById(Long).execution.timeout.enabled=true
hystrix.command.UserInfoOpenClient#getUserById(Long).execution.isolation.thread.timeoutInMilliseconds=1000
d、Hystrix針對單個服務設置超時時間語法
hystrix.command.服務名.execution,舉例如下:
hystrix.command.gf-oss-service.execution.isolation.strategy=THREAD
hystrix.command.gf-oss-ser#Zvice.execution.isolation.semaphore.maxConcurrentRequests=1000
hystrix.command.gf-oss-service.execution.timeout.enabled=true
hystrix.command.gf-oss-service.execution.isolation.thread.timeoutInMilliseconds=300000
四、ribbon配置
a、超時時間
# 請求連接的超時時間
ribbon.ConnectTimeout=2000
# 請求處理的超時時間
ribbon.ReadTimeout=5000
#每個Ribbon客戶端設置不同的超時時間, 通過服務名稱進行指定:
serviceId.ribbon.ConnectTimeout=2000
serviceId.ribbon.ReadTimeout=5000
b、并發(fā)參數(shù)
# 最大連接數(shù)
ribbon.MaxTotalConnections=500
# 每個host最大連接數(shù)
ribbon.MaxConnectionsPerHost=500
c、重試
# 對當前實例的重試次數(shù)
ribbon.maxAutoRetries=1
# 切換實例的重試次數(shù)
ribbon.maxAutoRetriesNextServer=3
# 對所有操作請求都進行重試
ribbon.okToRetryOnAllOperations=true
# 對Http響應碼進行重試
ribbon.retryableStatusCodes=500,404,502
d、ribbon與hystrix之間的超時時間關系:
如果hystrix.command.default.execution.timeout.enabled為true,則會有兩個執(zhí)行方法超時的配置,一個就是ribbon的ReadTimeout,一個就是熔斷器hystrix的timeoutInMilliseconds, 此時誰的值小誰生效
如果hystrix.command.default.execution.timeout.enabled為false,則熔斷器不進行超時熔斷,而是根據(jù)ribbon的ReadTimeout拋出的異常而熔斷,也就是取決于ribbon
ribbon的ConnectTimeout,配置的是請求服務的超時時間,除非服務找不到,或者網(wǎng)絡原因,這個時間才會生效
由于ribbon的重試機制,通常熔斷的超時時間需要配置的比ReadTimeout長,ReadTimeout比ConnectTimeout長,否則還未重試,就熔斷了
為了確保重試機制的正常運作,理論上(以實際情況為準)建議hystrix的超時時間為:(1 + MaxAutoRetries + MaxAutoRetriesNextServer) * ReadTimeout
五、gateway配置
System.setProperty("reactor.netty.pool.leasingStrategy", "lifo");
spring.cloud.gateway.httpclient.pool.max-idle-time=1S
#TCP連接超時時間 單位:ms
spring.cloud.gateway.httpclient.connect-timeout=2000
#響應超時時間
spring.cloud.gateway.httpclient.response-timeout=PT30S
#接口級別
spring.cloud.gateway.default-filters[0].args.timeouts[0].apiPattern=/gf-oss-service//oss/part/upload(接口apiPattern)
#毫秒
spring.cloud.gateway.default-filters[0].args.timeouts[0].timeout=100000