SpringBoot Redis配置多數(shù)據(jù)源的項(xiàng)目實(shí)踐
1.教程
0. 添加依賴(lài)
在項(xiàng)目中使用 RedisTemplate 支持多個(gè) Redis 數(shù)據(jù)庫(kù)之前,需要先添加 Spring Data Redis 的依賴(lài)。在 Maven 項(xiàng)目中,可以通過(guò)在 pom.xml 文件中添加以下依賴(lài)來(lái)引入 Spring Data Redis:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
1. 配置多個(gè) Redis 連接信息
在 Spring Boot 中,可以通過(guò)在 application.properties 或 application.yml 文件中指定不同的 Redis 連接信息來(lái)配置多個(gè) RedisConnectionFactory 實(shí)例,并通過(guò) @Bean 注解將它們注入到 RedisTemplate 中,例如:
Redis的常用配置大概是這些
# Redis 服務(wù)器的主機(jī)名或 IP 地址 spring.redis.host=127.0.0.1 # Redis 服務(wù)器的端口號(hào) spring.redis.port=6379 # Redis 服務(wù)器的密碼,如果沒(méi)有設(shè)置密碼,則為空字符串 spring.redis.password= # Redis 數(shù)據(jù)庫(kù)的編號(hào),默認(rèn)為 0 spring.redis.database=0 # Redis 服務(wù)器連接超時(shí)時(shí)間(毫秒),默認(rèn)為 5000 毫秒 spring.redis.timeout=5000 # 連接池最大連接數(shù),即最多允許多少個(gè)客戶(hù)端同時(shí)連接到 Redis 服務(wù)器 spring.redis.pool.max-active=8 # 連接池中最大空閑連接數(shù),即在連接池中最多允許多少個(gè)連接處于空閑狀態(tài) spring.redis.pool.max-idle=8 # 連接池中最小空閑連接數(shù),即在連接池中最少保持多少個(gè)連接處于空閑狀態(tài) spring.redis.pool.min-idle=0 # 連接池最大等待時(shí)間(毫秒),即當(dāng)連接池中的連接全部被占用時(shí),新的連接請(qǐng)求最多等待多長(zhǎng)時(shí)間 # 如果設(shè)置為-1,則表示無(wú)限等待 spring.redis.pool.max-wait=-1 # 是否啟用 SSL 加密連接,默認(rèn)為 false spring.redis.ssl=false
我們將上面的配置改造一下,支持Redis多數(shù)據(jù)源
# 配置 Redis 數(shù)據(jù)庫(kù) 0 spring.redis.database0.host=127.0.0.1 spring.redis.database0.port=6379 spring.redis.database0.password= spring.redis.database0.database=0 spring.redis.database0.timeout=5000 spring.redis.database0.pool.max-active=8 spring.redis.database0.pool.max-idle=8 spring.redis.database0.pool.min-idle=0 spring.redis.database0.pool.max-wait=-1 spring.redis.database0.ssl=false # 配置 Redis 數(shù)據(jù)庫(kù) 1 spring.redis.database1.host=127.0.0.1 spring.redis.database1.port=6380 spring.redis.database1.password= spring.redis.database1.database=1 spring.redis.database1.timeout=5000 spring.redis.database1.pool.max-active=8 spring.redis.database1.pool.max-idle=8 spring.redis.database1.pool.min-idle=0 spring.redis.database1.pool.max-wait=-1 spring.redis.database1.ssl=false
2. 配置
@ConfigurationProperties(prefix = "spring.redis.database0") 和 @ConfigurationProperties(prefix = "spring.redis.database1") 注解來(lái)將不同的 Redis 配置注入到 Java 類(lèi)中,例如:
@Configuration public class RedisConfig { ? ?@Bean(name = "redisTemplate0") ? ?public RedisTemplate<String, Object> redisTemplate0(RedisConnectionFactory redisConnectionFactory0) { ? ? ? ?RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); ? ? ? ?redisTemplate.setConnectionFactory(redisConnectionFactory0); ? ? ? ?redisTemplate.afterPropertiesSet(); ? ? ? ?return redisTemplate; ? ?} ? ?@Bean(name = "redisTemplate1") ? ?public RedisTemplate<String, Object> redisTemplate1(RedisConnectionFactory redisConnectionFactory1) { ? ? ? ?RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); ? ? ? ?redisTemplate.setConnectionFactory(redisConnectionFactory1); ? ? ? ?redisTemplate.afterPropertiesSet(); ? ? ? ?return redisTemplate; ? ?} ? ?@Bean(name = "redisConnectionFactory0") ? ?@ConfigurationProperties(prefix = "spring.redis.database0") ? ?public RedisConnectionFactory redisConnectionFactory0() { ? ? ? ?return new JedisConnectionFactory(); ? ?} ? ?@Bean(name = "redisConnectionFactory1") ? ?@ConfigurationProperties(prefix = "spring.redis.database1") ? ?public RedisConnectionFactory redisConnectionFactory1() { ? ? ? ?return new JedisConnectionFactory(); ? ?} }
使用 @ConfigurationProperties(prefix = "spring.redis.database0") 和 @ConfigurationProperties(prefix = "spring.redis.database1") 注解將不同的Redis 配置注入到 RedisConnectionFactory 實(shí)例中,并通過(guò) @Bean 注解將不同的 RedisTemplate 實(shí)例注入到 Spring 容器中。這樣,在代碼中就可以通過(guò) @Qualifier 注解來(lái)注入不同的 RedisTemplate 實(shí)例,從而訪問(wèn)不同的 Redis 數(shù)據(jù)庫(kù)。
3. 創(chuàng)建 RedisTemplate 實(shí)例
在 Spring Boot 中,可以通過(guò) @Qualifier 和 @Autowired 注解將不同的 RedisTemplate 實(shí)例注入到 Java 類(lèi)中,例如:
@Autowired @Qualifier("redisTemplate0") private RedisTemplate<String, Object> redisTemplate0; @Autowired @Qualifier("redisTemplate1") private RedisTemplate<String, Object> redisTemplate1;
4. 使用 RedisTemplate 操作 Redis
在 RedisTemplate 中,提供了一系列方法來(lái)操作 Redis,例如:
// 存儲(chǔ)數(shù)據(jù)到 Redis 數(shù)據(jù)庫(kù) 0 redisTemplate0.opsForValue().set("key0", "value0"); // 獲取數(shù)據(jù)從 Redis 數(shù)據(jù)庫(kù) 0 Object value0 = redisTemplate0.opsForValue().get("key0"); // 刪除數(shù)據(jù)從 Redis 數(shù)據(jù)庫(kù) 0 redisTemplate0.delete("key0"); // 存儲(chǔ)數(shù)據(jù)到 Redis 數(shù)據(jù)庫(kù) 1 redisTemplate1.opsForValue().set("key1", "value1"); // 獲取數(shù)據(jù)從 Redis 數(shù)據(jù)庫(kù) 1 Object value1 = redisTemplate1.opsForValue().get("key1"); // 刪除數(shù)據(jù)從 Redis 數(shù)據(jù)庫(kù) 1 redisTemplate1.delete("key1");
2. 常見(jiàn)問(wèn)題
在使用 Spring Boot 中的 Redis 進(jìn)行多數(shù)據(jù)源配置時(shí),可能會(huì)遇到以下幾個(gè)常見(jiàn)問(wèn)題:
2.1. RedisTemplate 實(shí)例重名問(wèn)題
在配置多個(gè) Redis 數(shù)據(jù)庫(kù)時(shí),需要為每個(gè) Redis 數(shù)據(jù)庫(kù)創(chuàng)建一個(gè) RedisTemplate 實(shí)例。如果不同的 RedisTemplate 實(shí)例的名稱(chēng)相同,可能會(huì)導(dǎo)致實(shí)例重名的問(wèn)題,進(jìn)而導(dǎo)致應(yīng)用程序無(wú)法啟動(dòng)。為每個(gè) RedisTemplate 實(shí)例指定不同的名稱(chēng)。例如,可以在配置類(lèi)中通過(guò) @Bean 注解為每個(gè) RedisTemplate 實(shí)例指定名稱(chēng)
@Bean(name = "redisTemplate0") public RedisTemplate<String, Object> redisTemplate0(RedisConnectionFactory redisConnectionFactory0) { ? ? RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); ? ? redisTemplate.setConnectionFactory(redisConnectionFactory0); ? ? redisTemplate.setKeySerializer(new StringRedisSerializer()); ? ? redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class)); ? ? redisTemplate.afterPropertiesSet(); ? ? return redisTemplate; } @Bean(name = "redisTemplate1") public RedisTemplate<String, Object> redisTemplate1(RedisConnectionFactory redisConnectionFactory1) { ? ? RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); ? ? redisTemplate.setConnectionFactory(redisConnectionFactory1); ? ? redisTemplate.setKeySerializer(new StringRedisSerializer()); ? ? redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class)); ? ? redisTemplate.afterPropertiesSet(); ? ? return redisTemplate; }
在上面的代碼中,我們分別為兩個(gè) RedisTemplate 實(shí)例指定了不同的名稱(chēng),分別為 “redisTemplate0” 和 “redisTemplate1”。
2.2. RedisConnectionFactory 實(shí)例重用問(wèn)題
在配置多個(gè) Redis 數(shù)據(jù)庫(kù)時(shí),需要為每個(gè) Redis 數(shù)據(jù)庫(kù)創(chuàng)建一個(gè) RedisConnectionFactory 實(shí)例。如果多個(gè) RedisConnectionFactory 實(shí)例使用了同一個(gè) Redis 連接池,可能會(huì)導(dǎo)致實(shí)例重用的問(wèn)題,進(jìn)而導(dǎo)致應(yīng)用程序無(wú)法啟動(dòng)。可以為每個(gè) RedisConnectionFactory 實(shí)例配置不同的 Redis 連接池。例如,可以在配置類(lèi)中創(chuàng)建不同的 RedisConnectionFactory 實(shí)例,并分別為它們配置不同的 Redis 連接池
@Bean(name = "redisConnectionFactory0") public RedisConnectionFactory redisConnectionFactory0() { ? ? RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(); ? ? config.setHostName("localhost"); ? ? config.setPort(6379); ? ? config.setPassword(RedisPassword.of("password")); ? ? LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(config); ? ? connectionFactory.setDatabase(0); ? ? connectionFactory.afterPropertiesSet(); ? ? return connectionFactory; } @Bean(name = "redisConnectionFactory1") public RedisConnectionFactory redisConnectionFactory1() { ? ? RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(); ? ? config.setHostName("localhost"); ? ? config.setPort(6379); ? ? config.setPassword(RedisPassword.of("password")); ? ? LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(config); ? ? connectionFactory.setDatabase(1); ? ? connectionFactory.afterPropertiesSet(); ? ? return connectionFactory; }
2.3. 數(shù)據(jù)庫(kù)編號(hào)配置問(wèn)題
在配置多個(gè) Redis 數(shù)據(jù)庫(kù)時(shí),需要為每個(gè) Redis 數(shù)據(jù)庫(kù)指定不同的數(shù)據(jù)庫(kù)編號(hào)。如果多個(gè) Redis 數(shù)據(jù)庫(kù)使用了同一個(gè)數(shù)據(jù)庫(kù)編號(hào),可能會(huì)導(dǎo)致數(shù)據(jù)被覆蓋或丟失。為了解決這個(gè)問(wèn)題,可以為每個(gè) RedisConnectionFactory 實(shí)例配置不同的數(shù)據(jù)庫(kù)編號(hào)。例如,可以在 RedisStandaloneConfiguration 中指定不同的數(shù)據(jù)庫(kù)編號(hào)
@Bean(name = "redisConnectionFactory0") public RedisConnectionFactory redisConnectionFactory0() { ? ? RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(); ? ? config.setHostName("localhost"); ? ? config.setPort(6379); ? ? config.setPassword(RedisPassword.of("password")); ? ? config.setDatabase(0); ? ? LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(config); ? ? connectionFactory.afterPropertiesSet(); ? ? return connectionFactory; } @Bean(name = "redisConnectionFactory1") public RedisConnectionFactory redisConnectionFactory1() { ? ? RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(); ? ? config.setHostName("localhost"); ? ? config.setPort(6379); ? ? config.setPassword(RedisPassword.of("password")); ? ? config.setDatabase(1); ? ? LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(config); ? ? connectionFactory.afterPropertiesSet(); ? ? return connectionFactory; }
2.4. RedisTemplate 序列化問(wèn)題
在使用 RedisTemplate 時(shí),需要對(duì)數(shù)據(jù)進(jìn)行序列化和反序列化。如果不同的 Redis 數(shù)據(jù)庫(kù)使用了不同的序列化方式,可能會(huì)導(dǎo)致數(shù)據(jù)無(wú)法正常讀寫(xiě)。每個(gè) RedisTemplate 實(shí)例指定不同的序列化器。例如,可以為每個(gè) RedisTemplate 實(shí)例分別設(shè)置不同的 keySerializer 和 valueSerializer 。但是通常情況下我們的的項(xiàng)目中的序列化方式都是一致的,除非是在連別的項(xiàng)目的Redis時(shí)候人家已經(jīng)按自己的序列化方式將值已經(jīng)寫(xiě)入,我們只能按照對(duì)接方的方式配置序列化。
@Bean(name = "redisTemplate0") public RedisTemplate<String, Object> redisTemplate0(RedisConnectionFactory redisConnectionFactory0) { ? ? RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); ? ? redisTemplate.setConnectionFactory(redisConnectionFactory0); ? ? redisTemplate.setKeySerializer(new StringRedisSerializer()); ? ? redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class)); ? ? redisTemplate.afterPropertiesSet(); ? ? return redisTemplate; } @Bean(name = "redisTemplate1") public RedisTemplate<String, Object> redisTemplate1(RedisConnectionFactory redisConnectionFactory1) { ? ? RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); ? ? redisTemplate.setConnectionFactory(redisConnectionFactory1); ? ? redisTemplate.setKeySerializer(new StringRedisSerializer()); ? ? redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); ? ? redisTemplate.afterPropertiesSet(); ? ? return redisTemplate; }
到此這篇關(guān)于SpringBoot Redis配置多數(shù)據(jù)源的項(xiàng)目實(shí)踐的文章就介紹到這了,更多相關(guān)SpringBoot Redis多數(shù)據(jù)源內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot多數(shù)據(jù)源配置方式以及報(bào)錯(cuò)問(wèn)題的解決
- SpringBoot中配置雙數(shù)據(jù)源的實(shí)現(xiàn)示例
- SpringBoot中動(dòng)態(tài)數(shù)據(jù)源是實(shí)現(xiàn)與用途
- SpringBoot開(kāi)發(fā)中的數(shù)據(jù)源詳解
- springboot添加多數(shù)據(jù)源的方法實(shí)例教程
- 詳解SpringBoot Mybatis如何對(duì)接多數(shù)據(jù)源
- springboot配置多數(shù)據(jù)源(靜態(tài)和動(dòng)態(tài)數(shù)據(jù)源)
- SpringBoot配置默認(rèn)HikariCP數(shù)據(jù)源
- SpringBoot集成Mybatis實(shí)現(xiàn)對(duì)多數(shù)據(jù)源訪問(wèn)原理
- SpringBoot?整合數(shù)據(jù)源的具體實(shí)踐
相關(guān)文章
Spring Boot配置Swagger的實(shí)現(xiàn)代碼
這篇文章主要介紹了Spring Boot配置Swagger的實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12簡(jiǎn)單說(shuō)明Java的Struts框架中merge標(biāo)簽的使用方法
這篇文章主要簡(jiǎn)單介紹了Java的Struts框架中merge標(biāo)簽的使用方法,Struts是Java的SSH三大web開(kāi)發(fā)框架之一,需要的朋友可以參考下2015-12-12SpringBoot+Logback實(shí)現(xiàn)一個(gè)簡(jiǎn)單的鏈路追蹤功能
Spring Boot默認(rèn)使用LogBack日志系統(tǒng),并且已經(jīng)引入了相關(guān)的jar包,所以我們無(wú)需任何配置便可以使用LogBack打印日志。這篇文章主要介紹了SpringBoot+Logback實(shí)現(xiàn)一個(gè)簡(jiǎn)單的鏈路追蹤功能,需要的朋友可以參考下2019-10-10SpringBoot和Swagger結(jié)合提高API開(kāi)發(fā)效率
這篇文章主要介紹了SpringBoot和Swagger結(jié)合提高API開(kāi)發(fā)效率的相關(guān)資料,需要的朋友可以參考下2017-09-09Java位集合之BitMap實(shí)現(xiàn)和應(yīng)用詳解
這篇文章主要介紹了Java位集合之BitMap實(shí)現(xiàn)和應(yīng)用的相關(guān)資料,BitMap是一種高效的數(shù)據(jù)結(jié)構(gòu),適用于快速排序、去重和查找等操作,通過(guò)簡(jiǎn)單的數(shù)組和位運(yùn)算,可以在Java中實(shí)現(xiàn)BitMap,從而節(jié)省存儲(chǔ)空間并提高性能,需要的朋友可以參考下2024-12-12idea切換分支的時(shí)候,忽略一些無(wú)用的修改設(shè)置
這篇文章主要介紹了idea切換分支的時(shí)候,忽略一些無(wú)用的修改操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02