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