最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

spring?retry實(shí)現(xiàn)方法請(qǐng)求重試的使用步驟

 更新時(shí)間:2022年07月08日 10:09:03   作者:趙廣陸  
這篇文章主要介紹了spring?retry實(shí)現(xiàn)方法請(qǐng)求重試及使用步驟,本文分步驟通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

1 spring-retry是什么?

以往我們?cè)谶M(jìn)行網(wǎng)絡(luò)請(qǐng)求的時(shí)候,需要考慮網(wǎng)絡(luò)異常的情況,本文就介紹了利用spring-retry,是spring提供的一個(gè)重試框架,原本自己實(shí)現(xiàn)的重試機(jī)制,現(xiàn)在spring幫封裝好提供更加好的編碼體驗(yàn)。

2 使用步驟

2.1 引入maven庫(kù)

代碼如下(示例):

	<dependency>
	    <groupId>org.springframework.retry</groupId>
	    <artifactId>spring-retry</artifactId>
	</dependency>
	<dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-aspects</artifactId>
	    <version>4.3.9.RELEASE</version>
	</dependency>

2.2 在spring啟動(dòng)類上開啟重試功能

提示:添加@EnableRetry注解開啟
@SpringBootApplication
@EnableRetry
public class SpringRetryDemoApplication {
    public static SpringRetryAnnotationService springRetryAnnotationService;
    public static SpringRetryImperativeService springRetryImperativeService;
    public static TranditionalRetryService tranditionalRetryService;
    @Autowired
    public void setSpringRetryAnnotationService(SpringRetryAnnotationService springRetryAnnotationService) {
        SpringRetryDemoApplication.springRetryAnnotationService = springRetryAnnotationService;
    }
    @Autowired
    public void setSpringRetryImperativeService(SpringRetryImperativeService springRetryImperativeService) {
        SpringRetryDemoApplication.springRetryImperativeService = springRetryImperativeService;
    }
    @Autowired
    public void setTranditionalRetryService(TranditionalRetryService tranditionalRetryService) {
        SpringRetryDemoApplication.tranditionalRetryService = tranditionalRetryService;
    }
    public static void main(String[] args) {
        SpringApplication.run(SpringRetryDemoApplication.class, args);
        springRetryAnnotationService.test();
        springRetryImperativeService.test();
        tranditionalRetryService.test();
    }
}

2.3 公共業(yè)務(wù)代碼

@Service
@Slf4j
public class CommonService {
    public void test(String before) {
        for (int i = 0; i < 10; i++) {
            if (i == 2) {
                log.error("{}:有異常哦,我再試多幾次看下還有沒(méi)異常", before);
                throw new RuntimeException();
            }
            log.info("{}:打印次數(shù): {}", before, i + 1);
        }
    }

    public void recover(String before) {
        log.error("{}:還是有異常,程序有bug哦", before);
    }
}

2.4 傳統(tǒng)的重試做法

@Service
@Slf4j
public class TranditionalRetryService {
    @Autowired
    CommonService commonService;

    public void test() {
        // 定義重試次數(shù)以及重試時(shí)間間隔
        int retryCount = 3;
        int retryTimeInterval = 3;
        for (int r = 0; r < retryCount; r++) {
            try {
                commonService.test("以前的做法");
            } catch (RuntimeException e) {
                if (r == retryCount - 1) {
                    commonService.recover("以前的做法");
                    return;
                }
                try {
                    Thread.sleep(retryTimeInterval * 1000);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }

        }

    }
}

2.5 使用spring-retry的命令式編碼

2.5.1 定義重試監(jiān)聽器

提示:監(jiān)聽重試過(guò)程的生命周期
@Slf4j
public class MyRetryListener extends RetryListenerSupport {
    @Override
    public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) {
        log.info("監(jiān)聽到重試過(guò)程關(guān)閉了");
        log.info("=======================================================================");
    }

    @Override
    public <T, E extends Throwable> void onError(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) {
        log.info("監(jiān)聽到重試過(guò)程錯(cuò)誤了");
    }

    @Override
    public <T, E extends Throwable> boolean open(RetryContext context, RetryCallback<T, E> callback) {
        log.info("=======================================================================");
        log.info("監(jiān)聽到重試過(guò)程開啟了");
        return true;
    }
}

2.5.2 定義重試配置

提示:配置RetryTemplate重試模板類
@Configuration
public class RetryConfig {
    @Bean
    public RetryTemplate retryTemplate() {
        // 定義簡(jiǎn)易重試策略,最大重試次數(shù)為3次,重試間隔為3s
        RetryTemplate retryTemplate = RetryTemplate.builder()
                .maxAttempts(3)
                .fixedBackoff(3000)
                .retryOn(RuntimeException.class)
                .build();
        retryTemplate.registerListener(new MyRetryListener());
        return retryTemplate;
    }
}

2.5.3 命令式編碼

@Service
@Slf4j
public class SpringRetryImperativeService {
    @Autowired
    RetryTemplate retryTemplate;
    @Autowired
    CommonService commonService;

    public void test() {
        retryTemplate.execute(
                retry -> {
                    commonService.test("命令式");
                    return null;
                },
                recovery -> {
                    commonService.recover("命令式");
                    return null;
                }
        );
    }
}

2.6使用spring-retry的注解式編碼

@Service
@Slf4j
public class SpringRetryAnnotationService {
    @Autowired
    CommonService commonService;

    /**
     * 如果失敗,定義重試3次,重試間隔為3s,指定恢復(fù)名稱,指定監(jiān)聽器
     */
    @Retryable(value = RuntimeException.class, maxAttempts = 3, backoff = @Backoff(value = 3000L), recover = "testRecover", listeners = {"myRetryListener"})
    public void test() {
        commonService.test("注解式");
    }

    @Recover
    public void testRecover(RuntimeException runtimeException) {
        commonService.recover("注解式");
    }
}

3 SpringBoot整合spring-retry

我們使用SpringBoot來(lái)整合spring-retry組件實(shí)現(xiàn)重試機(jī)制。

3.1 添加@EnableRetry注解

在主啟動(dòng)類Application上添加@EnableRetry注解,實(shí)現(xiàn)對(duì)重試機(jī)制的支持

@SpringBootApplication
@EnableRetry
public class RetryApplication {
 
    public static void main(String[] args) {
 
        SpringApplication.run(RetryApplication.class, args);
    }
 
}

注意:@EnableRetry也可以使用在配置類、ServiceImpl類、方法上

3.2 接口實(shí)現(xiàn)

注意:接口類一定不能少,在接口類中定義你需要實(shí)現(xiàn)重試的方法,否則可能會(huì)無(wú)法實(shí)現(xiàn)重試功能

我的測(cè)試接口類如下:

public interface RetryService {    public String testRetry()  throws Exception;}

3.3 添加@Retryable注解

我們針對(duì)需要實(shí)現(xiàn)重試的方法上添加@Retryable注解,使該方法可以實(shí)現(xiàn)重試,這里我列出ServiceImpl中的一個(gè)方法:

@Service
public class RetryServiceImpl implements RetryService {
    @Override
    @Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 2000,multiplier = 1.5))
    public String testRetry() throws Exception {
 
        System.out.println("開始執(zhí)行代碼:"+ LocalTime.now());
        int code = 0;
        // 模擬一直失敗
        if(code == 0){
           // 這里可以使自定義異常,@Retryable中value需與其一致
            throw new Exception("代碼執(zhí)行異常");
        }
        System.out.println("代碼執(zhí)行成功");
        return "success";
    }
}

說(shuō)明:@Retryable配置元數(shù)據(jù)情況:
value :針對(duì)指定拋出的異常類型,進(jìn)行重試,這里指定的是Exception
maxAttempts :配置最大重試次數(shù),這里配置為3次(包含第一次和最后一次)
delay: 第一次重試延遲間隔,這里配置的是2s
multiplier :每次重試時(shí)間間隔是前一次幾倍,這里是1.5倍

3.4 Controller測(cè)試代碼

@RestController
@RequestMapping("/test")
public class TestController {
    //  一定要注入接口,通過(guò)接口去調(diào)用方法
    @Autowired
    private RetryService retryService;
 
    @GetMapping("/retry")
    public String testRetry() throws Exception {
        return retryService.testRetry();
    }
}

3.5 發(fā)送請(qǐng)求

發(fā)送請(qǐng)求后,我們發(fā)現(xiàn)后臺(tái)打印情況,確實(shí)重試了3次,并且在最后一次重試失敗的情況下,才拋出異常,具體如下(可以注意下時(shí)間間隔)

3.6 補(bǔ)充:@Recover

一般情況下,我們重試最大設(shè)置的次數(shù)后,仍然失敗拋出異常,我們會(huì)通過(guò)全局異常處理類進(jìn)行統(tǒng)一處理,但是我們其實(shí)也可以自行處理,可以通過(guò)@Recover注解來(lái)實(shí)現(xiàn),具體如下:

@Service
public class RetryServiceImpl implements RetryService {
 
    @Retryable(value = Exception.class, maxAttempts = 3, backoff = @Backoff(delay = 2000,multiplier = 1.5))
    public String testRetry() throws Exception {
 
        System.out.println("開始執(zhí)行代碼:"+ LocalTime.now());
        int code = 0;
        if(code == 0){
            // 這里可以使自定義異常,@Retryable中value需與其一致
            throw new Exception("代碼執(zhí)行異常");
        }
        System.out.println("代碼執(zhí)行成功");
        return "success";
    }
 
    /**
     * 最終重試失敗處理
     * @param e
     * @return
     */
    @Recover
    public String recover(Exception e){
 
        System.out.println("代碼執(zhí)行重試后依舊失敗");
        return "fail";
    }
}

注意:
1)@Recover的方法中的參數(shù)異常類型需要與重試方法中一致
2)該方法的返回值類型與重試方法保持一致

再次測(cè)試如下(發(fā)現(xiàn)不會(huì)再拋出異常)

到此這篇關(guān)于spring retry實(shí)現(xiàn)方法請(qǐng)求重試的文章就介紹到這了,更多相關(guān)spring retry方法請(qǐng)求重試內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • springboot中JSONObject遍歷并替換部分json值

    springboot中JSONObject遍歷并替換部分json值

    這篇文章主要介紹了springboot中JSONObject遍歷并替換部分json值,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Java Convert Kotlin空指針異常的解決方法

    Java Convert Kotlin空指針異常的解決方法

    本文主要介紹了Java?Convert?Kotlin空指針異常的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • java如何使用redis加鎖

    java如何使用redis加鎖

    這篇文章主要介紹了java如何使用redis加鎖問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • SpringBoot整合Swagger2的完整過(guò)程記錄

    SpringBoot整合Swagger2的完整過(guò)程記錄

    Swagger是一款RESTful接口的文檔在線自動(dòng)生成、功能測(cè)試功能框架,這篇文章主要給大家介紹了關(guān)于SpringBoot整合Swagger2的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2021-09-09
  • 利用 filter 機(jī)制給靜態(tài)資源 url 加上時(shí)間戳,來(lái)防止js和css文件的緩存問(wèn)題

    利用 filter 機(jī)制給靜態(tài)資源 url 加上時(shí)間戳,來(lái)防止js和css文件的緩存問(wèn)題

    這篇文章主要介紹了利用 filter 機(jī)制給靜態(tài)資源 url 加上時(shí)間戳,來(lái)防止js和css文件的緩存問(wèn)題的相關(guān)資料,需要的朋友可以參考下
    2016-05-05
  • Java使用OpenCV進(jìn)行圖像處理的示例代碼

    Java使用OpenCV進(jìn)行圖像處理的示例代碼

    OpenCV是一個(gè)開源的計(jì)算機(jī)視覺(jué)庫(kù),廣泛應(yīng)用于圖像處理、機(jī)器學(xué)習(xí)和計(jì)算機(jī)視覺(jué)等領(lǐng)域,盡管OpenCV主要使用C/C++進(jìn)行開發(fā),但它也為Java提供了綁定,使得Java開發(fā)者能夠利用其強(qiáng)大的圖像處理功能,在本篇文章中,我們將詳細(xì)介紹如何在Java中使用OpenCV,需要的朋友可以參考下
    2025-03-03
  • 吊打Java面試官之Lambda表達(dá)式 Stream API

    吊打Java面試官之Lambda表達(dá)式 Stream API

    這篇文章主要介紹了吊打Java之jdk8的新特性包括Lambda表達(dá)式、函數(shù)式接口、Stream API全面刨析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09
  • SpringBoot的WebSocket實(shí)現(xiàn)單聊群聊

    SpringBoot的WebSocket實(shí)現(xiàn)單聊群聊

    這篇文章主要為大家詳細(xì)介紹了SpringBoot的WebSocket實(shí)現(xiàn)單聊群聊,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-02-02
  • maven如何打包動(dòng)態(tài)環(huán)境變量(包括啟動(dòng)腳本)

    maven如何打包動(dòng)態(tài)環(huán)境變量(包括啟動(dòng)腳本)

    這篇文章主要介紹了maven如何打包動(dòng)態(tài)環(huán)境變量(包括啟動(dòng)腳本)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • SpringBoot集成Redis之配置、序列化與持久化方式

    SpringBoot集成Redis之配置、序列化與持久化方式

    本文介紹了Redis的基本概念、常用數(shù)據(jù)類型及操作、SpringBoot整合Redis的方法、高級(jí)特性與安全性、性能優(yōu)化、測(cè)試與部署、數(shù)據(jù)一致性及版本更新等內(nèi)容,通過(guò)本文的學(xué)習(xí),讀者可以掌握Redis的使用方法,并在實(shí)際項(xiàng)目中發(fā)揮其優(yōu)勢(shì)
    2024-11-11

最新評(píng)論

房产| 南和县| 苏尼特右旗| 沅江市| 镇安县| 章丘市| 株洲县| 芮城县| 鹤岗市| 余江县| 富蕴县| 翁牛特旗| 灵武市| 富锦市| 邹城市| 晋宁县| 五台县| 商洛市| 宜昌市| 称多县| 天水市| 格尔木市| 确山县| 施秉县| 乡城县| 瓮安县| 毕节市| 海南省| 永州市| 云和县| 根河市| 固安县| 莫力| 石阡县| 黑龙江省| 巴彦淖尔市| 普定县| 准格尔旗| 乐陵市| 绍兴县| 寻甸|