您好,登錄后才能下訂單哦!
這篇文章主要介紹“SpringBoot程序預裝載數據怎么實現”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“SpringBoot程序預裝載數據怎么實現”文章能幫助大家解決問題。
在項目實際的開發過程中,有時候會遇到需要在應用程序啟動完畢對外提供服務之前預先將部分數據裝載到緩存的需求。
預裝載應用級別數據到緩存:如字典數據、公共的業務數據
系統預熱
心跳檢測:如在系統啟動完畢訪問一個外服務接口等場景
常用方法
ApplicationEvent
CommandLineRunner
ApplicationRunner
應用程序事件,就是發布訂閱模式。在系統啟動完畢,向應用程序注冊一個事件,監聽者一旦監聽到了事件的發布,就可以做一些業務邏輯的處理了。
既然是發布-訂閱模式,那么訂閱者既可以是一個,也可以是多個。
import org.springframework.context.ApplicationEvent; public class CacheEvent extends ApplicationEvent { public CacheEvent(Object source) { super(source); } }
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"); } }
@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 接口,可以在應用程序啟動完畢,回調到指定的方法中。
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-參數={}", args); // 注入業務 service ,獲取需要緩存的數據 // 注入 redisTemplate ,將需要緩存的數據存放到 redis 中 log.debug("CacheCommandLineRunner-end"); } }
同CommandLineRunner 類似,區別在于,對參數做了封裝。
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-參數={}", JSON.toJSONString(args)); // 注入業務 service ,獲取需要緩存的數據 // 注入 redisTemplate ,將需要緩存的數據存放到 redis 中 log.debug("CacheApplicationRunner-end"); } }
上述代碼在idea中啟動,若不帶參數,輸出如下:
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-參數={"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-參數={}
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-參數=ApplicationEvent-->緩存系統數據
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-參數={"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-參數=--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- 參數=ApplicationEvent-->緩存系統數據
2022-04-28 16:02:05.396 DEBUG 9916 --- [ main] c.r.w.listener.CacheEventListener : CacheEventListener-end
從上面測試的輸出,可以看到三種方式執行的順序為:
ApplicationRunner--->CommandLineRunner--->ApplicationEvent
另外,若同時定義多個runner,可以通過order來指定他們的優先級。
關于“SpringBoot程序預裝載數據怎么實現”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識,可以關注億速云行業資訊頻道,小編每天都會為大家更新不同的知識點。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。