SpringCloud重試機制配置詳解
更新時間:2018年04月06日 11:05:41 作者:張建斌
本篇文章主要介紹了SpringCloud重試機制配置詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
首先聲明一點,這里的重試并不是報錯以后的重試,而是負(fù)載均衡客戶端發(fā)現(xiàn)遠(yuǎn)程請求實例不可到達(dá)后,去重試其他實例。

@Bean
@LoadBalanced
RestTemplate restTemplate() {
HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
httpRequestFactory.setReadTimeout(5000);
httpRequestFactory.setConnectTimeout(5000);
return new RestTemplate(httpRequestFactory);
}

feign重試機制
feign默認(rèn)是通過自己包下的Retryer進行重試配置,默認(rèn)是5次
package feign;
import static java.util.concurrent.TimeUnit.SECONDS;
/**
* Cloned for each invocation to {@link Client#execute(Request, feign.Request.Options)}.
* Implementations may keep state to determine if retry operations should continue or not.
*/
public interface Retryer extends Cloneable {
/**
* if retry is permitted, return (possibly after sleeping). Otherwise propagate the exception.
*/
void continueOrPropagate(RetryableException e);
Retryer clone();
public static class Default implements Retryer {
private final int maxAttempts;
private final long period;
private final long maxPeriod;
int attempt;
long sleptForMillis;
public Default() {
this(100, SECONDS.toMillis(1), 5);
}
public Default(long period, long maxPeriod, int maxAttempts) {
this.period = period;
this.maxPeriod = maxPeriod;
this.maxAttempts = maxAttempts;
this.attempt = 1;
}
feign取消重試
@Bean
Retryer feignRetryer() {
return Retryer.NEVER_RETRY;
}
feign請求超時設(shè)置
@Bean
Request.Options requestOptions(ConfigurableEnvironment env){
int ribbonReadTimeout = env.getProperty("ribbon.ReadTimeout", int.class, 6000);
int ribbonConnectionTimeout = env.getProperty("ribbon.ConnectTimeout", int.class, 3000);
return new Request.Options(ribbonConnectionTimeout, ribbonReadTimeout);
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺談SpringBoot @Autowired的兩種注入方式
本文主要介紹了兩種SpringBoot @Autowired注入方式,具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06
基于Java?SpringBoot的前后端分離信息管理系統(tǒng)的設(shè)計和實現(xiàn)
當(dāng)今社會,人才的流動速度大大增加,因此也對黨建工作的管理層面工作帶來了空前且復(fù)雜的挑戰(zhàn),從而使得如何高效的開展管理黨建工作成為了亟待解決的問題。本文將介紹通過Java?SpringBoot實現(xiàn)前后端分離信息管理系統(tǒng),感興趣的同學(xué)可以了解一下2021-11-11
Java中用Socket實現(xiàn)HTTP文件上傳實例
本篇文章主要介紹了Java中用Socket實現(xiàn)HTTP文件上傳實例,詳細(xì)的介紹了通過讀取Socket的輸入流來實現(xiàn)一個文件上傳的功能,有興趣的同學(xué)可以一起了解一下2017-04-04

