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

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

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

簡介

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

適用場景

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

常用方法

  • ApplicationEvent
  • CommandLineRunner
  • ApplicationRunner

ApplicationEvent

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

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

定義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");
    }
}

注冊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

通過實(shí)現(xiàn) CommandLineRunner 接口,可以在應(yī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 類似,區(qū)別在于,對參數(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");
    }
}

測試

上述代碼在idea中啟動,若不帶參數(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 啟動,則輸入如下:

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í)行順序

從上面測試的輸出,可以看到三種方式執(zhí)行的順序為:
ApplicationRunner--->CommandLineRunner--->ApplicationEvent

另外,若同時定義多個runner,可以通過order來指定他們的優(yōu)先級。

代碼

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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 關(guān)于MyBatis結(jié)果映射的實(shí)例總結(jié)

    關(guān)于MyBatis結(jié)果映射的實(shí)例總結(jié)

    結(jié)果集映射主要是為了解決屬性名和類型名不一致的問題,下面這篇文章主要給大家介紹了關(guān)于MyBatis結(jié)果映射的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-05-05
  • Java實(shí)現(xiàn)在線聊天功能

    Java實(shí)現(xiàn)在線聊天功能

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)在線聊天功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • Java分層概念詳解

    Java分層概念詳解

    這篇文章主要介紹了Java分層概念詳解,內(nèi)容十分詳細(xì),在這里給大家分享下,需要的朋友可以參考。
    2017-09-09
  • java如何讀取超大文件

    java如何讀取超大文件

    這篇文章主要為大家詳細(xì)介紹了java如何讀取超大文件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • Java創(chuàng)建多線程異步執(zhí)行實(shí)現(xiàn)代碼解析

    Java創(chuàng)建多線程異步執(zhí)行實(shí)現(xiàn)代碼解析

    這篇文章主要介紹了Java創(chuàng)建多線程異步執(zhí)行實(shí)現(xiàn)代碼解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-07-07
  • Spring中的NamespaceHandler加載過程源碼詳解

    Spring中的NamespaceHandler加載過程源碼詳解

    這篇文章主要介紹了Spring中的NamespaceHandler加載過程源碼詳解,Spring提供的NamespaceHandler的處理機(jī)制,簡單來說就是命名空間處理器,Spring為了開放性提供了NamespaceHandler機(jī)制,這樣我們就可以根據(jù)需求自己來處理我們設(shè)置的標(biāo)簽元素,需要的朋友可以參考下
    2024-02-02
  • IntelliJ IDEA 好用插件之a(chǎn)nalyze inspect code詳解

    IntelliJ IDEA 好用插件之a(chǎn)nalyze inspect code詳解

    這篇文章主要介紹了IntelliJ IDEA 好用插件之a(chǎn)nalyze inspect code的相關(guān)知識,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2020-12-12
  • 源碼解析Spring 數(shù)據(jù)庫異常抽理知識點(diǎn)總結(jié)

    源碼解析Spring 數(shù)據(jù)庫異常抽理知識點(diǎn)總結(jié)

    在本篇文章里小編給大家分享了關(guān)于源碼解析Spring 數(shù)據(jù)庫異常抽理知識點(diǎn)內(nèi)容,對此有需要的朋友們學(xué)習(xí)參考下。
    2019-05-05
  • 如何在IDEA運(yùn)行spark程序(搭建Spark開發(fā)環(huán)境)

    如何在IDEA運(yùn)行spark程序(搭建Spark開發(fā)環(huán)境)

    spark程序可以通過pom.xml的文件配置,添加spark-core依賴,可以直接在IDEA中編寫spark程序并運(yùn)行結(jié)果,這篇文章主要介紹了如何在IDEA運(yùn)行spark程序(搭建Spark開發(fā)環(huán)境),需要的朋友可以參考下
    2024-02-02
  • Spring AspectJ 實(shí)現(xiàn)AOP的方法你了解嗎

    Spring AspectJ 實(shí)現(xiàn)AOP的方法你了解嗎

    這篇文章主要為大家介紹了Spring AspectJ 實(shí)現(xiàn)AOP的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01

最新評論

工布江达县| 黄平县| 平利县| 大田县| 盐池县| 肥西县| 西充县| 巴南区| 怀远县| 承德县| 正宁县| 尼玛县| 鄱阳县| 朔州市| 麻江县| 岱山县| 抚宁县| 利川市| 杭锦后旗| 保靖县| 萝北县| 东兰县| 沈阳市| 永福县| 交城县| 蓝山县| 山丹县| 汪清县| 翁源县| 古交市| 江源县| 枞阳县| 凤凰县| 永善县| 闻喜县| 洪洞县| 鄂托克前旗| 丹江口市| 措美县| 哈密市| 太仆寺旗|