ribbon: ReadTimeout:60000 ConnectTimeout:60000 1 2 3 局部設(shè)置:
service-id: ribbon: ReadTimeout:1000 ConnectTimeout:1000 1 2 3 4
其中,service-id是Ribbon所使用的虛擬主機(jī)名,一般和EurekaServer上注冊(cè)的服務(wù)名稱一致,即:與spring.application.name一致。
Feign的超時(shí)從SpringCloudEdgware開始,F(xiàn)eign支持使用屬性配置超時(shí):
feign: client:config: feignName: connectTimeout:5000 readTimeout:5000 1 2 3 4 5
對(duì)于老版本,可以寫個(gè)feign.Request.Options,參考:org.springframework.cloud.netflix.feign.ribbon.FeignRibbonClientAutoConfiguration#feignRequestOptions的寫法即可。
RestTemplate的超時(shí)一些時(shí)侯,我們可能使用了RestTemplate,例如
@Bean @LoadBalanced publicRestTemplaterestTemplate() { returnnewRestTemplate(); } 1 2 3 4 5 6
此時(shí),超時(shí)可使用如下方式設(shè)置:
@Bean @LoadBalanced publicRestTemplaterestTemplate() { SimpleClientHttpRequestFactorysimpleClientHttpRequestFactory=newSimpleClientHttpRequestFactory(); simpleClientHttpRequestFactory.setConnectTimeout(1000); simpleClientHttpRequestFactory.setReadTimeout(1000); returnnewRestTemplate(simpleClientHttpRequestFactory); } 1 2 3 4 5 6 7 8 9 Zuul的超時(shí)
Zuul的超時(shí)比較復(fù)雜,因?yàn)閆uul整合了Ribbon、Hystrix。下面分兩種情況討論:
如果Zuul的路由使用了Ribbonhystrix: command: default: execution: isolation: thread: timeoutInMilliseconds:1000 ribbon: ReadTimeout:1000 ConnectTimeout:1000 1 2 3 4 5 6 7 8 9 10
代碼解析:此種情況下,Zuul轉(zhuǎn)發(fā)所使用的過濾器是org.springframework.cloud.netflix.zuul.filters.route.RibbonRoutingFilter,在這個(gè)過濾器中,整合了Hystrix以及Ribbon。
如果Zuul的路由未使用Ribbonzuul: routes: user-route:#該配置方式中,user-route只是給路由一個(gè)名稱,可以任意起名。 url:http://localhost:8000/#指定的url path:/user/**#url對(duì)應(yīng)的路徑。 1 2 3 4 5
那么,此時(shí)Zuul的超時(shí)只與如下兩個(gè)配置有關(guān):
zuul: host: socket-timeout-millis:10000 connect-timeout-millis:2000 1 2 3 4 5
代碼解析:直接配置URL路由的方式,用不上Ribbon,也用不上Hystrix,Zuul轉(zhuǎn)發(fā)所使用的過濾器是org.springframework.cloud.netflix.zuul.filters.route.SimpleHostRoutingFilter,在這個(gè)過濾器中,Zuul使用ApacheHttpClient進(jìn)行轉(zhuǎn)發(fā)。
在現(xiàn)實(shí)場景中,有時(shí)候可能兩種路由方式配合使用,因此,建議大家配置以上所有屬性。
hystrix: command: default: execution: timeout: enabled:true isolation: thread: timeoutInMilliseconds:1000 1 2 3 4 5 6 7 8 9 10
如上,Hystrix的默認(rèn)超時(shí)時(shí)間是1秒。默認(rèn)開啟超時(shí)機(jī)制。如需關(guān)閉Hystrix的超時(shí),可將xxx.enabled設(shè)置為false。
Tips如有組件跟Hystrix配合使用,一般來講,建議Hystrix的超時(shí)>其他組件的超時(shí),否則將可能導(dǎo)致重試特性失效。
分享標(biāo)題:SpringCloud各組件超時(shí)總結(jié)
鏈接分享:http://chinadenli.net/article46/cpoehg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、虛擬主機(jī)、面包屑導(dǎo)航、網(wǎng)站建設(shè)、App開發(fā)、網(wǎng)站維護(hù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)