前言
我們的服務(wù)需要調(diào)用別人的接口,由于對(duì)方的接口服務(wù)不是很穩(wěn)定,經(jīng)常超時(shí),于是需要增加一套重試邏輯。這里使用 Spring Retry
的方式來(lái)實(shí)現(xiàn)。
一、引入POM
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<scope>test</scope>
</dependency>
二、 修改啟動(dòng)類
在Spring Boot 應(yīng)用入口啟動(dòng)類,也就是配置類的上面加上@EnableRetry
注解,表示讓重試機(jī)制生效。
@EnableRetry
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class RuoYiApplication
{
@Bean
RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args)
{
SpringApplication.run(RuoYiApplication.class, args);
}
}
注意:
我這里還注入了Bean,因?yàn)樵趩?dòng)項(xiàng)目是會(huì)報(bào)錯(cuò)。
@Bean
RestTemplate restTemplate(){
return new RestTemplate();
}
報(bào)錯(cuò)信息:
Field restTemplate in com.cloud.ribbon_consumer.project.service.HelloService required a bean of type 'org.springframework.web.client.RestTemplate' that could not be found.
問(wèn)題原因:
在 Spring Boot 1.3
版本中,會(huì)默認(rèn)提供一個(gè)RestTemplate
的實(shí)例Bean,而在 Spring Boot 1.4
以及以后的版本中,這個(gè)默認(rèn)的bean
不再提供了,我們需要在Application
啟動(dòng)時(shí),手動(dòng)創(chuàng)建一個(gè)RestTemplate
的配置。
這樣,我們?cè)陬愔型ㄟ^(guò)注解 @Autowired 使用 TestTemplate 的時(shí)候,程序就可以找到被實(shí)例化的 TestTemplate,就不會(huì)出現(xiàn)上述的報(bào)錯(cuò)了。
三、具體使用
寫一個(gè)模擬的業(yè)務(wù)類RetryService ,在其里面注入RestTemplate 。
@Service
public class RetryService {
private static Logger log = LoggerFactory.getLogger(RetryService.class);
@Resource
private RestTemplate restTemplate;
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
@Retryable(value = RestClientException.class, maxAttempts = 3,
backoff = @Backoff(delay = 5000L,multiplier = 2))
public String getRequest(String address) {
log.info("發(fā)起遠(yuǎn)程API 地址: {} ,請(qǐng)求事件: {}", address, DATE_TIME_FORMATTER.format(LocalDateTime.now()));
ResponseEntity<String> responseEntity
= restTemplate.getForEntity(address, String.class);
// 獲取響應(yīng)結(jié)果
return responseEntity.getBody();
}
}
-
@Retryable
注解的方法在發(fā)生異常時(shí)會(huì)重試,參數(shù)說(shuō)明:
-
value
:當(dāng)指定異常發(fā)生時(shí)會(huì)進(jìn)行重試 ,HttpClientErrorException是RestClientException的子類。 -
include
:和value一樣,默認(rèn)空。如果 exclude也為空時(shí),所有異常都重試 -
exclude
:指定異常不重試,默認(rèn)空。如果 include也為空時(shí),所有異常都重試 -
maxAttemps
:最大重試次數(shù),默認(rèn)3 -
backoff
:重試等待策略,默認(rèn)空
-
@Backoff
注解為重試等待的策略,參數(shù)說(shuō)明:
-
delay
:指定重試的延時(shí)時(shí)間,默認(rèn)為1000毫秒 -
multiplier
:指定延遲的倍數(shù),比如設(shè)置delay=5000,multiplier=2時(shí),第一次重試為5秒后,第二次為10(5x2)秒,第三次為20(10x2)秒。
四、測(cè)試
我這里寫了一個(gè)不存在的服務(wù),用于測(cè)試重試機(jī)制
@GetMapping(value = "/update1")
public String updatePingTai1() {
String request = retryService.getRequest("http://127.0.0.1:3214");
return request);
}
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-475118.html
這里可以看到我們是重試了三次,才拋出的異常。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-475118.html
參考:
- https://www.cnblogs.com/zimug/p/13507850.html
- https://www.cnblogs.com/liuyupen/p/13957171.html#%E6%96%B9%E5%BC%8F%E5%9B%9Bspring-%E8%87%AA%E5%B8%A6%E9%87%8D%E8%AF%95%E5%B7%A5%E5%85%B7
- https://www.cnblogs.com/liuyupen/p/13957171.html#%E6%96%B9%E5%BC%8F%E5%9B%9Bspring-%E8%87%AA%E5%B8%A6%E9%87%8D%E8%AF%95%E5%B7%A5%E5%85%B7
到了這里,關(guān)于【SpringBoot】springboot使用RestTemplate 進(jìn)行http請(qǐng)求失敗自動(dòng)重試的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!