欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot程序預(yù)裝載數(shù)據(jù)的實(shí)現(xiàn)方法及實(shí)踐

 更新時(shí)間:2022年04月28日 16:50:46   作者:Naylor  
在項(xiàng)目實(shí)際的開(kāi)發(fā)過(guò)程中,有時(shí)候會(huì)遇到需要在應(yīng)用程序啟動(dòng)完畢對(duì)外提供服務(wù)之前預(yù)先將部分?jǐn)?shù)據(jù)裝載到緩存的需求。本文就總結(jié)了常見(jiàn)的數(shù)據(jù)預(yù)裝載方式及其實(shí)踐,感興趣的朋友一起看看吧

簡(jiǎn)介

在項(xiàng)目實(shí)際的開(kāi)發(fā)過(guò)程中,有時(shí)候會(huì)遇到需要在應(yīng)用程序啟動(dòng)完畢對(duì)外提供服務(wù)之前預(yù)先將部分?jǐn)?shù)據(jù)裝載到緩存的需求。本文就總結(jié)了常見(jiàn)的數(shù)據(jù)預(yù)裝載方式及其實(shí)踐。

適用場(chǎng)景

  • 預(yù)裝載應(yīng)用級(jí)別數(shù)據(jù)到緩存:如字典數(shù)據(jù)、公共的業(yè)務(wù)數(shù)據(jù)
  • 系統(tǒng)預(yù)熱
  • 心跳檢測(cè):如在系統(tǒng)啟動(dòng)完畢訪問(wèn)一個(gè)外服務(wù)接口等場(chǎng)景

常用方法

  • ApplicationEvent
  • CommandLineRunner
  • ApplicationRunner

ApplicationEvent

應(yīng)用程序事件,就是發(fā)布訂閱模式。在系統(tǒng)啟動(dòng)完畢,向應(yīng)用程序注冊(cè)一個(gè)事件,監(jiān)聽(tīng)者一旦監(jiān)聽(tīng)到了事件的發(fā)布,就可以做一些業(yè)務(wù)邏輯的處理了。

既然是發(fā)布-訂閱模式,那么訂閱者既可以是一個(gè),也可以是多個(gè)。

定義event

import org.springframework.context.ApplicationEvent;
public class CacheEvent   extends ApplicationEvent {
    public CacheEvent(Object source) {
        super(source);
    }
}

定義listener

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Slf4j
@Component
public class CacheEventListener implements ApplicationListener<CacheEvent> {
    @Autowired
    private MaskingService maskingService;
    @Autowired
    private RedisCache redisCache;
    @Override
    public void onApplicationEvent(CacheEvent cacheEvent) {
        log.debug("CacheEventListener-start");
        List<SysMasking> maskings = maskingService.selectAllSysMaskings();
        if (!CollectionUtils.isEmpty(maskings)) {
            log.debug("CacheEventListener-data-not-empty");
            Map<String, List<SysMasking>> cacheMap = maskings.stream().collect(Collectors.groupingBy(SysMasking::getFieldKey));
            cacheMap.keySet().forEach(x -> {
                if (StringUtils.isNotEmpty(x)) {
                    log.debug("CacheEventListener-x={}", x);
                    List<SysMasking> list = cacheMap.get(x);
                    long count = redisCache.setCacheList(RedisKeyPrefix.MASKING.getPrefix() + x, list);
                    log.debug("CacheEventListener-count={}", count);
                } else {
                    log.debug("CacheEventListener-x-is-empty");
                }
            });
        } else {
            log.debug("CacheEventListener-data-is-empty");
        }
        log.debug("CacheEventListener-end");
    }
}

注冊(cè)event

@Slf4j
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class BAMSApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(BAMSApplication.class, args);
        log.debug("app-started");
        context.publishEvent(new CacheEvent("處理緩存事件"));
    }
}

CommandLineRunner

通過(guò)實(shí)現(xiàn) CommandLineRunner 接口,可以在應(yīng)用程序啟動(dòng)完畢,回調(diào)到指定的方法中。

package com.ramble.warmupservice.runner;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class CacheCommandLineRunner implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        log.debug("CacheCommandLineRunner-start");
        log.debug("CacheCommandLineRunner-參數(shù)={}", args);
        // 注入業(yè)務(wù) service ,獲取需要緩存的數(shù)據(jù)
        // 注入 redisTemplate ,將需要緩存的數(shù)據(jù)存放到 redis 中
        log.debug("CacheCommandLineRunner-end");
    }
}

ApplicationRunner

同CommandLineRunner 類(lèi)似,區(qū)別在于,對(duì)參數(shù)做了封裝。

package com.ramble.warmupservice.runner;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class CacheApplicationRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.debug("CacheApplicationRunner-start");
        log.debug("CacheApplicationRunner-參數(shù)={}", JSON.toJSONString(args));
        // 注入業(yè)務(wù) service ,獲取需要緩存的數(shù)據(jù)
        // 注入 redisTemplate ,將需要緩存的數(shù)據(jù)存放到 redis 中
        log.debug("CacheApplicationRunner-end");
    }
}

測(cè)試

上述代碼在idea中啟動(dòng),若不帶參數(shù),輸出如下:

2022-04-28 15:44:00.981  INFO 1160 --- [           main] c.r.w.WarmupServiceApplication           : Started WarmupServiceApplication in 1.335 seconds (JVM running for 2.231)
2022-04-28 15:44:00.982 DEBUG 1160 --- [           main] c.r.w.runner.CacheApplicationRunner      : CacheApplicationRunner-start
2022-04-28 15:44:01.025 DEBUG 1160 --- [           main] c.r.w.runner.CacheApplicationRunner      : CacheApplicationRunner-參數(shù)={"nonOptionArgs":[],"optionNames":[],"sourceArgs":[]}
2022-04-28 15:44:01.025 DEBUG 1160 --- [           main] c.r.w.runner.CacheApplicationRunner      : CacheApplicationRunner-end
2022-04-28 15:44:01.025 DEBUG 1160 --- [           main] c.r.w.runner.CacheCommandLineRunner      : CacheCommandLineRunner-start
2022-04-28 15:44:01.026 DEBUG 1160 --- [           main] c.r.w.runner.CacheCommandLineRunner      : CacheCommandLineRunner-參數(shù)={}
2022-04-28 15:44:01.026 DEBUG 1160 --- [           main] c.r.w.runner.CacheCommandLineRunner      : CacheCommandLineRunner-end
2022-04-28 15:44:01.026 DEBUG 1160 --- [           main] c.r.w.listener.CacheEventListener        : CacheEventListener-start
2022-04-28 15:44:01.026 DEBUG 1160 --- [           main] c.r.w.listener.CacheEventListener        : CacheEventListener-參數(shù)=ApplicationEvent-->緩存系統(tǒng)數(shù)據(jù)
2022-04-28 15:44:01.029 DEBUG 1160 --- [           main] c.r.w.listener.CacheEventListener        : CacheEventListener-end
Disconnected from the target VM, address: '127.0.0.1:61320', transport: 'socket'
Process finished with exit code 130

若使用 java -jar xxx.jar --server.port=9009 啟動(dòng),則輸入如下:

2022-04-28 16:02:05.327  INFO 9916 --- [           main] c.r.w.WarmupServiceApplication           : Started WarmupServiceApplication in 1.78 seconds (JVM running for 2.116)
2022-04-28 16:02:05.329 DEBUG 9916 --- [           main] c.r.w.runner.CacheApplicationRunner      : CacheApplicationRunner-start
2022-04-28 16:02:05.393 DEBUG 9916 --- [           main] c.r.w.runner.CacheApplicationRunner      : CacheApplicationRunner-參數(shù)={"nonOptionArgs":[],"optionNames":["server.port"],"sourceArgs":["--server.port=9009"]}
2022-04-28 16:02:05.395 DEBUG 9916 --- [           main] c.r.w.runner.CacheApplicationRunner      : CacheApplicationRunner-end
2022-04-28 16:02:05.395 DEBUG 9916 --- [           main] c.r.w.runner.CacheCommandLineRunner      : CacheCommandLineRunner-start
2022-04-28 16:02:05.395 DEBUG 9916 --- [           main] c.r.w.runner.CacheCommandLineRunner      : CacheCommandLineRunner-參數(shù)=--server.port=9009
2022-04-28 16:02:05.395 DEBUG 9916 --- [           main] c.r.w.runner.CacheCommandLineRunner      : CacheCommandLineRunner-end
2022-04-28 16:02:05.395 DEBUG 9916 --- [           main] c.r.w.listener.CacheEventListener        : CacheEventListener-start
2022-04-28 16:02:05.396 DEBUG 9916 --- [           main] c.r.w.listener.CacheEventListener        : CacheEventListener- 參數(shù)=ApplicationEvent-->緩存系統(tǒng)數(shù)據(jù)
2022-04-28 16:02:05.396 DEBUG 9916 --- [           main] c.r.w.listener.CacheEventListener        : CacheEventListener-end

執(zhí)行順序

從上面測(cè)試的輸出,可以看到三種方式執(zhí)行的順序?yàn)椋?br />ApplicationRunner--->CommandLineRunner--->ApplicationEvent

另外,若同時(shí)定義多個(gè)runner,可以通過(guò)order來(lái)指定他們的優(yōu)先級(jí)。

代碼

https://gitee.com/naylor_personal/ramble-spring-cloud/tree/master/warmup-service

到此這篇關(guān)于SpringBoot程序預(yù)裝載數(shù)據(jù)的文章就介紹到這了,更多相關(guān)SpringBoot預(yù)裝載數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • JDK17、JDK19、JDK1.8輕松切換(無(wú)坑版,小白也可以看懂!)

    JDK17、JDK19、JDK1.8輕松切換(無(wú)坑版,小白也可以看懂!)

    在做不同的java項(xiàng)目時(shí)候,因項(xiàng)目需要很可能來(lái)回切換jdk版本,下面這篇文章主要介紹了JDK17、JDK19、JDK1.8輕松切換的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2023-02-02
  • java編程實(shí)現(xiàn)求解八枚銀幣代碼分享

    java編程實(shí)現(xiàn)求解八枚銀幣代碼分享

    這篇文章主要介紹了java編程實(shí)現(xiàn)求解八枚銀幣代碼分享,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11
  • Java?設(shè)計(jì)模式中的策略模式詳情

    Java?設(shè)計(jì)模式中的策略模式詳情

    這篇文章主要介紹了Java?設(shè)計(jì)模式中的策略模式詳情,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09
  • Redis分布式鎖介紹與使用

    Redis分布式鎖介紹與使用

    服務(wù)器集群項(xiàng)目中的鎖是無(wú)法精準(zhǔn)的鎖住線程資源的,于是我們就是需要使用分布式鎖,分布式鎖該如何使用又有什么注意點(diǎn)呢?就讓我們進(jìn)入接下來(lái)的學(xué)習(xí)
    2022-09-09
  • idea在工具欄中顯示快速創(chuàng)建包和類(lèi)的圖標(biāo)的詳細(xì)步驟

    idea在工具欄中顯示快速創(chuàng)建包和類(lèi)的圖標(biāo)的詳細(xì)步驟

    點(diǎn)擊需要?jiǎng)?chuàng)建包或者類(lèi)的位置,在點(diǎn)擊對(duì)用的圖標(biāo)就可以快速創(chuàng)建類(lèi)或者包了,下面小編給大家介紹idea在工具欄中顯示快速創(chuàng)建包和類(lèi)的圖標(biāo)的詳細(xì)步驟,感興趣的朋友一起看看吧
    2024-02-02
  • java實(shí)現(xiàn)簡(jiǎn)易五子棋游戲

    java實(shí)現(xiàn)簡(jiǎn)易五子棋游戲

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)易五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • ArrayList和JSONArray邊遍歷邊刪除到底該如何做

    ArrayList和JSONArray邊遍歷邊刪除到底該如何做

    這篇文章主要介紹了ArrayList和JSONArray邊遍歷邊刪除到底該如何做,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Fluent Mybatis實(shí)現(xiàn)環(huán)境隔離和租戶隔離

    Fluent Mybatis實(shí)現(xiàn)環(huán)境隔離和租戶隔離

    我們?cè)趯?shí)際的業(yè)務(wù)開(kāi)發(fā)中,經(jīng)常會(huì)碰到環(huán)境邏輯隔離和租戶數(shù)據(jù)邏輯隔離的問(wèn)題。本文就詳細(xì)的來(lái)介紹一下,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Java異常處理Guava?Throwables類(lèi)使用實(shí)例解析

    Java異常處理Guava?Throwables類(lèi)使用實(shí)例解析

    這篇文章主要為大家介紹了Java異常處理神器Guava?Throwables類(lèi)使用深入詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • 解決mybatis使用foreach批量insert異常的問(wèn)題

    解決mybatis使用foreach批量insert異常的問(wèn)題

    這篇文章主要介紹了解決mybatis使用foreach批量insert異常的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-01-01

最新評(píng)論