日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

为Spring Cloud Ribbon配置请求重试(Camden.SR2+)

發(fā)布時間:2024/7/5 javascript 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 为Spring Cloud Ribbon配置请求重试(Camden.SR2+) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

當(dāng)我們使用Spring Cloud Ribbon實(shí)現(xiàn)客戶端負(fù)載均衡的時候,通常都會利用@LoadBalanced來讓RestTemplate具備客戶端負(fù)載功能,從而實(shí)現(xiàn)面向服務(wù)名的接口訪問(原理可見《Spring Cloud源碼分析(二)Ribbon》一文,如果對Spring Cloud中使用Ribbon進(jìn)行服務(wù)消費(fèi)還沒有概念的話,建議先閱讀《Spring Cloud構(gòu)建微服務(wù)架構(gòu)(二)服務(wù)消費(fèi)者》一文。)。

下面的例子,實(shí)現(xiàn)了對服務(wù)名為hello-service的/hello接口的調(diào)用。由于RestTemplate被@LoadBalanced修飾,所以它具備客戶端負(fù)載均衡的能力,當(dāng)請求真正發(fā)起的時候,url中的服務(wù)名會根據(jù)負(fù)載均衡策略從服務(wù)清單中挑選出一個實(shí)例來進(jìn)行訪問。

@SpringCloudApplication
public class Application {

@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}

@RestController
class ConsumerController {

@Autowired
RestTemplate restTemplate;

@RequestMapping(value = "/ribbon-consumer", method = RequestMethod.GET)
public String helloConsumer() {
return restTemplate.getForObject("http://hello-service/hello", String.class);
}

}

public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}

}

大多數(shù)情況下,上面的實(shí)現(xiàn)沒有任何問題,但是總有一些意外發(fā)生,比如:有一個實(shí)例發(fā)生了故障而該情況還沒有被服務(wù)治理機(jī)制及時的發(fā)現(xiàn)和摘除,這時候客戶端訪問該節(jié)點(diǎn)的時候自然會失敗。所以,為了構(gòu)建更為健壯的應(yīng)用系統(tǒng),我們希望當(dāng)請求失敗的時候能夠有一定策略的重試機(jī)制,而不是直接返回失敗。這個時候就需要開發(fā)人員人工的來為上面的RestTemplate調(diào)用實(shí)現(xiàn)重試機(jī)制。

不過,從Spring Cloud Camden SR2版本開始,我們就不用那么麻煩了。從該版本開始,Spring Cloud整合了Spring Retry來實(shí)現(xiàn)重試邏輯,而對于開發(fā)者只需要做一些配置即可。

以上面對hello-service服務(wù)的調(diào)用為例,我們可以在配置文件中增加如下內(nèi)容:

spring.cloud.loadbalancer.retry.enabled=true

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=10000

hello-service.ribbon.ConnectTimeout=250
hello-service.ribbon.ReadTimeout=1000
hello-service.ribbon.OkToRetryOnAllOperations=true
hello-service.ribbon.MaxAutoRetriesNextServer=2
hello-service.ribbon.MaxAutoRetries=1

各參數(shù)的配置說明:

  • spring.cloud.loadbalancer.retry.enabled:該參數(shù)用來開啟重試機(jī)制,它默認(rèn)是關(guān)閉的。這里需要注意,官方文檔中的配置參數(shù)少了enabled。

    源碼定義如下:

    @ConfigurationProperties("spring.cloud.loadbalancer.retry")
    public class LoadBalancerRetryProperties {
    private boolean enabled = false;
    ...
    }
  • hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds:斷路器的超時時間需要大于ribbon的超時時間,不然不會觸發(fā)重試。

  • hello-service.ribbon.ConnectTimeout:請求連接的超時時間

  • hello-service.ribbon.ReadTimeout:請求處理的超時時間

  • hello-service.ribbon.OkToRetryOnAllOperations:對所有操作請求都進(jìn)行重試

  • hello-service.ribbon.MaxAutoRetriesNextServer:切換實(shí)例的重試次數(shù)

  • hello-service.ribbon.MaxAutoRetries:對當(dāng)前實(shí)例的重試次數(shù)

根據(jù)如上配置,當(dāng)訪問到故障請求的時候,它會再嘗試訪問一次當(dāng)前實(shí)例(次數(shù)由MaxAutoRetries配置),如果不行,就換一個實(shí)例進(jìn)行訪問,如果還是不行,再換一次實(shí)例訪問(更換次數(shù)由MaxAutoRetriesNextServer配置),如果依然不行,返回失敗信息。


總結(jié)

以上是生活随笔為你收集整理的为Spring Cloud Ribbon配置请求重试(Camden.SR2+)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。