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

SpringBoot Redis配置多數(shù)據(jù)源的項目實踐

 更新時間:2023年07月25日 15:16:55   作者:算.子  
springboot中默認(rèn)的redis配置是只能對單個redis庫進行操作的, 那么我們需要多個庫操作的時候這個時候就可以采用redis多數(shù)據(jù)源 ,本文就介紹了SpringBoot Redis配置多數(shù)據(jù)源,感興趣的可以了解一下

1.教程

0. 添加依賴

在項目中使用 RedisTemplate 支持多個 Redis 數(shù)據(jù)庫之前,需要先添加 Spring Data Redis 的依賴。在 Maven 項目中,可以通過在 pom.xml 文件中添加以下依賴來引入 Spring Data Redis:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

1. 配置多個 Redis 連接信息

在 Spring Boot 中,可以通過在 application.properties 或 application.yml 文件中指定不同的 Redis 連接信息來配置多個 RedisConnectionFactory 實例,并通過 @Bean 注解將它們注入到 RedisTemplate 中,例如:

Redis的常用配置大概是這些

# Redis 服務(wù)器的主機名或 IP 地址
spring.redis.host=127.0.0.1
# Redis 服務(wù)器的端口號
spring.redis.port=6379
# Redis 服務(wù)器的密碼,如果沒有設(shè)置密碼,則為空字符串
spring.redis.password=
# Redis 數(shù)據(jù)庫的編號,默認(rèn)為 0
spring.redis.database=0
# Redis 服務(wù)器連接超時時間(毫秒),默認(rèn)為 5000 毫秒
spring.redis.timeout=5000
# 連接池最大連接數(shù),即最多允許多少個客戶端同時連接到 Redis 服務(wù)器
spring.redis.pool.max-active=8
# 連接池中最大空閑連接數(shù),即在連接池中最多允許多少個連接處于空閑狀態(tài)
spring.redis.pool.max-idle=8
# 連接池中最小空閑連接數(shù),即在連接池中最少保持多少個連接處于空閑狀態(tài)
spring.redis.pool.min-idle=0
# 連接池最大等待時間(毫秒),即當(dāng)連接池中的連接全部被占用時,新的連接請求最多等待多長時間
# 如果設(shè)置為-1,則表示無限等待
spring.redis.pool.max-wait=-1
# 是否啟用 SSL 加密連接,默認(rèn)為 false
spring.redis.ssl=false

我們將上面的配置改造一下,支持Redis多數(shù)據(jù)源

# 配置 Redis 數(shù)據(jù)庫 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ù)庫 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") 注解來將不同的 Redis 配置注入到 Java 類中,例如:

@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 實例中,并通過 @Bean 注解將不同的 RedisTemplate 實例注入到 Spring 容器中。這樣,在代碼中就可以通過 @Qualifier 注解來注入不同的 RedisTemplate 實例,從而訪問不同的 Redis 數(shù)據(jù)庫。

3. 創(chuàng)建 RedisTemplate 實例

在 Spring Boot 中,可以通過 @Qualifier 和 @Autowired 注解將不同的 RedisTemplate 實例注入到 Java 類中,例如:

@Autowired
@Qualifier("redisTemplate0")
private RedisTemplate<String, Object> redisTemplate0;
@Autowired
@Qualifier("redisTemplate1")
private RedisTemplate<String, Object> redisTemplate1;

4. 使用 RedisTemplate 操作 Redis

在 RedisTemplate 中,提供了一系列方法來操作 Redis,例如:

// 存儲數(shù)據(jù)到 Redis 數(shù)據(jù)庫 0
redisTemplate0.opsForValue().set("key0", "value0");
// 獲取數(shù)據(jù)從 Redis 數(shù)據(jù)庫 0
Object value0 = redisTemplate0.opsForValue().get("key0");
// 刪除數(shù)據(jù)從 Redis 數(shù)據(jù)庫 0
redisTemplate0.delete("key0");
// 存儲數(shù)據(jù)到 Redis 數(shù)據(jù)庫 1
redisTemplate1.opsForValue().set("key1", "value1");
// 獲取數(shù)據(jù)從 Redis 數(shù)據(jù)庫 1
Object value1 = redisTemplate1.opsForValue().get("key1");
// 刪除數(shù)據(jù)從 Redis 數(shù)據(jù)庫 1
redisTemplate1.delete("key1");

2. 常見問題

在使用 Spring Boot 中的 Redis 進行多數(shù)據(jù)源配置時,可能會遇到以下幾個常見問題:

2.1. RedisTemplate 實例重名問題

在配置多個 Redis 數(shù)據(jù)庫時,需要為每個 Redis 數(shù)據(jù)庫創(chuàng)建一個 RedisTemplate 實例。如果不同的 RedisTemplate 實例的名稱相同,可能會導(dǎo)致實例重名的問題,進而導(dǎo)致應(yīng)用程序無法啟動。為每個 RedisTemplate 實例指定不同的名稱。例如,可以在配置類中通過 @Bean 注解為每個 RedisTemplate 實例指定名稱

@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;
}

在上面的代碼中,我們分別為兩個 RedisTemplate 實例指定了不同的名稱,分別為 “redisTemplate0” 和 “redisTemplate1”。

2.2. RedisConnectionFactory 實例重用問題

在配置多個 Redis 數(shù)據(jù)庫時,需要為每個 Redis 數(shù)據(jù)庫創(chuàng)建一個 RedisConnectionFactory 實例。如果多個 RedisConnectionFactory 實例使用了同一個 Redis 連接池,可能會導(dǎo)致實例重用的問題,進而導(dǎo)致應(yīng)用程序無法啟動??梢詾槊總€ RedisConnectionFactory 實例配置不同的 Redis 連接池。例如,可以在配置類中創(chuàng)建不同的 RedisConnectionFactory 實例,并分別為它們配置不同的 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ù)庫編號配置問題

在配置多個 Redis 數(shù)據(jù)庫時,需要為每個 Redis 數(shù)據(jù)庫指定不同的數(shù)據(jù)庫編號。如果多個 Redis 數(shù)據(jù)庫使用了同一個數(shù)據(jù)庫編號,可能會導(dǎo)致數(shù)據(jù)被覆蓋或丟失。為了解決這個問題,可以為每個 RedisConnectionFactory 實例配置不同的數(shù)據(jù)庫編號。例如,可以在 RedisStandaloneConfiguration 中指定不同的數(shù)據(jù)庫編號

@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 序列化問題

在使用 RedisTemplate 時,需要對數(shù)據(jù)進行序列化和反序列化。如果不同的 Redis 數(shù)據(jù)庫使用了不同的序列化方式,可能會導(dǎo)致數(shù)據(jù)無法正常讀寫。每個 RedisTemplate 實例指定不同的序列化器。例如,可以為每個 RedisTemplate 實例分別設(shè)置不同的 keySerializer 和 valueSerializer 。但是通常情況下我們的的項目中的序列化方式都是一致的,除非是在連別的項目的Redis時候人家已經(jīng)按自己的序列化方式將值已經(jī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 GenericJackson2JsonRedisSerializer());
? ? redisTemplate.afterPropertiesSet();
? ? return redisTemplate;
}

到此這篇關(guān)于SpringBoot Redis配置多數(shù)據(jù)源的項目實踐的文章就介紹到這了,更多相關(guān)SpringBoot Redis多數(shù)據(jù)源內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring Boot配置Swagger的實現(xiàn)代碼

    Spring Boot配置Swagger的實現(xiàn)代碼

    這篇文章主要介紹了Spring Boot配置Swagger的實現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • mybatisPlus批量插入優(yōu)化加快性能

    mybatisPlus批量插入優(yōu)化加快性能

    這篇文章主要介紹了mybatisPlus批量插入優(yōu)化加快性能,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-12-12
  • Java動態(tài)代理模式的深入揭秘

    Java動態(tài)代理模式的深入揭秘

    這篇文章主要給大家介紹了關(guān)于Java動態(tài)代理模式的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用Java具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • Spring?Boot如何處理@Resource示例分析

    Spring?Boot如何處理@Resource示例分析

    這篇文章主要為大家介紹了Spring?Boot如何處理@Resource示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-07-07
  • 簡單說明Java的Struts框架中merge標(biāo)簽的使用方法

    簡單說明Java的Struts框架中merge標(biāo)簽的使用方法

    這篇文章主要簡單介紹了Java的Struts框架中merge標(biāo)簽的使用方法,Struts是Java的SSH三大web開發(fā)框架之一,需要的朋友可以參考下
    2015-12-12
  • SpringBoot+Logback實現(xiàn)一個簡單的鏈路追蹤功能

    SpringBoot+Logback實現(xiàn)一個簡單的鏈路追蹤功能

    Spring Boot默認(rèn)使用LogBack日志系統(tǒng),并且已經(jīng)引入了相關(guān)的jar包,所以我們無需任何配置便可以使用LogBack打印日志。這篇文章主要介紹了SpringBoot+Logback實現(xiàn)一個簡單的鏈路追蹤功能,需要的朋友可以參考下
    2019-10-10
  • SpringBoot和Swagger結(jié)合提高API開發(fā)效率

    SpringBoot和Swagger結(jié)合提高API開發(fā)效率

    這篇文章主要介紹了SpringBoot和Swagger結(jié)合提高API開發(fā)效率的相關(guān)資料,需要的朋友可以參考下
    2017-09-09
  • Java中的static關(guān)鍵字你了解多少

    Java中的static關(guān)鍵字你了解多少

    這篇文章主要為大家詳細介紹了Java中的static關(guān)鍵字,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • Java位集合之BitMap實現(xiàn)和應(yīng)用詳解

    Java位集合之BitMap實現(xiàn)和應(yīng)用詳解

    這篇文章主要介紹了Java位集合之BitMap實現(xiàn)和應(yīng)用的相關(guān)資料,BitMap是一種高效的數(shù)據(jù)結(jié)構(gòu),適用于快速排序、去重和查找等操作,通過簡單的數(shù)組和位運算,可以在Java中實現(xiàn)BitMap,從而節(jié)省存儲空間并提高性能,需要的朋友可以參考下
    2024-12-12
  • idea切換分支的時候,忽略一些無用的修改設(shè)置

    idea切換分支的時候,忽略一些無用的修改設(shè)置

    這篇文章主要介紹了idea切換分支的時候,忽略一些無用的修改操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02

最新評論