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

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

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

1.教程

0. 添加依賴

在項(xiàng)目中使用 RedisTemplate 支持多個 Redis 數(shù)據(jù)庫之前,需要先添加 Spring Data Redis 的依賴。在 Maven 項(xiàng)目中,可以通過在 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 實(shí)例,并通過 @Bean 注解將它們注入到 RedisTemplate 中,例如:

Redis的常用配置大概是這些

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

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

在 Spring Boot 中,可以通過 @Qualifier 和 @Autowired 注解將不同的 RedisTemplate 實(shí)例注入到 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 進(jìn)行多數(shù)據(jù)源配置時,可能會遇到以下幾個常見問題:

2.1. RedisTemplate 實(shí)例重名問題

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

@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 實(shí)例指定了不同的名稱,分別為 “redisTemplate0” 和 “redisTemplate1”。

2.2. RedisConnectionFactory 實(shí)例重用問題

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

在配置多個 Redis 數(shù)據(jù)庫時,需要為每個 Redis 數(shù)據(jù)庫指定不同的數(shù)據(jù)庫編號。如果多個 Redis 數(shù)據(jù)庫使用了同一個數(shù)據(jù)庫編號,可能會導(dǎo)致數(shù)據(jù)被覆蓋或丟失。為了解決這個問題,可以為每個 RedisConnectionFactory 實(shí)例配置不同的數(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ù)進(jìn)行序列化和反序列化。如果不同的 Redis 數(shù)據(jù)庫使用了不同的序列化方式,可能會導(dǎo)致數(shù)據(jù)無法正常讀寫。每個 RedisTemplate 實(shí)例指定不同的序列化器。例如,可以為每個 RedisTemplate 實(shí)例分別設(shè)置不同的 keySerializer 和 valueSerializer 。但是通常情況下我們的的項(xiàng)目中的序列化方式都是一致的,除非是在連別的項(xiàng)目的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ù)源的項(xiàng)目實(shí)踐的文章就介紹到這了,更多相關(guān)SpringBoot Redis多數(shù)據(jù)源內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • IDEA中的maven沒有dependencies解決方案

    IDEA中的maven沒有dependencies解決方案

    這篇文章主要介紹了IDEA中的maven沒有dependencies解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • 如何使用bootstrap.yml讀取配置中心的配置文件

    如何使用bootstrap.yml讀取配置中心的配置文件

    這篇文章主要介紹了如何使用bootstrap.yml讀取配置中心的配置文件問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • RocketMQ設(shè)計之主從復(fù)制和讀寫分離

    RocketMQ設(shè)計之主從復(fù)制和讀寫分離

    這篇文章主要介紹了RocketMQ設(shè)計之主從復(fù)制和讀寫分離,RocketMQ提高消費(fèi)避免Broker發(fā)生單點(diǎn)故障引起B(yǎng)roker上的消息無法及時消費(fèi),下文關(guān)于了RocketMQ的相關(guān)內(nèi)容,需要的小伙伴可以參考一下
    2022-03-03
  • Java多線程之并發(fā)編程的核心AQS詳解

    Java多線程之并發(fā)編程的核心AQS詳解

    AQS ( AbstractQueuedSynchronizer)是一個用來構(gòu)建鎖和同步器的框架,使用AQS能簡單且高效地構(gòu)造出應(yīng)用廣泛的大量的同步器,下面小編和大家來一起學(xué)習(xí)一下吧
    2021-09-09
  • 分享一個你不知道的Java異常實(shí)現(xiàn)的缺陷

    分享一個你不知道的Java異常實(shí)現(xiàn)的缺陷

    Java中一個大家熟知的知識點(diǎn)就是異常捕獲,try...catch...finally組合,但是很多人不知道這里面有一個關(guān)于Java的缺陷,或者說是異常實(shí)現(xiàn)的一點(diǎn)不足之處。本文就通過一個很簡單的實(shí)驗(yàn)給大家演示下效果玩玩兒,希望大家能覺得有趣
    2022-12-12
  • 淺談s:select 標(biāo)簽中l(wèi)ist存放map對象的使用

    淺談s:select 標(biāo)簽中l(wèi)ist存放map對象的使用

    下面小編就為大家?guī)硪黄獪\談s:select 標(biāo)簽中l(wèi)ist存放map對象的使用。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-11-11
  • java定義受限制的類型參數(shù)操作

    java定義受限制的類型參數(shù)操作

    這篇文章主要介紹了java定義受限制的類型參數(shù)操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • Java雙向鏈表的操作

    Java雙向鏈表的操作

    這篇文章主要介紹了Java雙向鏈表的操作,雙向鏈表,對于該鏈表中的任意節(jié)點(diǎn),既可以通過該節(jié)點(diǎn)向前遍歷,也可以通過該節(jié)點(diǎn)向后遍歷,雙向鏈表在實(shí)際工程中應(yīng)用非常廣泛,是使用鏈表這個結(jié)構(gòu)的首選
    2022-06-06
  • Java運(yùn)算符>、>>、>>>三者的區(qū)別

    Java運(yùn)算符>、>>、>>>三者的區(qū)別

    這篇文章主要介紹了Java運(yùn)算符>、>>、>>>三者的區(qū)別,做了一個簡單的對比,并用實(shí)例說明,需要的朋友可以參考下
    2014-06-06
  • 深入聊聊Java內(nèi)存泄露問題

    深入聊聊Java內(nèi)存泄露問題

    所謂內(nèi)存泄露就是指一個不再被程序使用的對象或變量一直被占據(jù)在內(nèi)存中,下面這篇文章主要給大家介紹了關(guān)于Java內(nèi)存泄露問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-04-04

最新評論

兴义市| 子洲县| 墨江| 五寨县| 大安市| 建昌县| 崇左市| 武威市| 离岛区| 台南县| 建湖县| 临海市| 柯坪县| 和政县| 丰顺县| 三亚市| 堆龙德庆县| 湖口县| 屯留县| 炎陵县| 开平市| 昌图县| 佛冈县| 苗栗县| 岱山县| 绥滨县| 肥城市| 石柱| 福建省| 罗甸县| 兴安县| 新和县| 静宁县| 修武县| 漾濞| 新竹县| 巴南区| 隆昌县| 弥勒县| 和政县| 肇州县|