我是靠谱客的博主 瘦瘦黑米,最近开发中收集的这篇文章主要介绍SpringBoot+RedisTemplate多数据源,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

采用的Spring-boot-starter-data中的redis,通过lettuce创建工厂类进行创建bean

第一步 引入依赖

PS:采用的是Spring Boot 2.3.4.RELEASE版本


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

第二步 配置yml


redis:
host: r-2.redis.rds.aliyuncs.com
port: 6379
password: 666
timeout: 5000
lettuce:
pool:
max-active: 32
max-wait: 300
max-idle: 16
min-idle: 8
database: 0
redis-one:
host: r-2.redis.rds.aliyuncs.com
port: 6379
password: 666
timeout: 5000
lettuce:
pool:
max-active: 32
max-wait: 300
max-idle: 16
min-idle: 8
database: 1

第三步 自定义配置了连接池对象和Redis的Standalone模式的配置(为了简化代码)

package net.zhongjunkeji.common.config.redis;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
public class UserRedisStandaloneConfiguration extends RedisStandaloneConfiguration {
public UserRedisStandaloneConfiguration(RedisProperties redisProperties) {
super.setHostName(redisProperties.getHost());
super.setPassword(RedisPassword.of(redisProperties.getPassword()));
super.setPort(redisProperties.getPort());
super.setDatabase(redisProperties.getDatabase());
}
}
package net.zhongjunkeji.common.config.redis;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
/**
* <p>
* 描述: 创建对象池()
* </p>
*
* @author
GHQ·阿甘
* @version 1.0.0
* @since
2021/7/6 23:34
**/
public class UserGenericObjectPoolConfig extends GenericObjectPoolConfig{
public UserGenericObjectPoolConfig(RedisProperties redisProperties){
this.setMaxTotal(redisProperties.getLettuce().getPool().getMaxActive());
this.setMaxIdle(redisProperties.getLettuce().getPool().getMaxIdle());
this.setMinIdle(redisProperties.getLettuce().getPool().getMinIdle());
this.setMaxWaitMillis(redisProperties.getLettuce().getPool().getMaxWait().toMillis());
}
}

第四步 创建Lettuce工厂类和RedisTemlateBean

package net.zhongjunkeji.common.config.redis;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import java.time.Duration;
/**
* <p>
* 描述: TODO
* </p>
*
* @author GHQ·阿甘
* @version 1.0.0
* @since 2021-07-06
**/
@Configuration
public class RedisMoreDataSourceConfig {
/**
* <p>描述:
默认(主0)Lettuce工厂类
* <p>开发者: GHQ·阿甘
* <p>时间:
2021/7/6 23:11
*
* @param defaultRedisConfig
* @param defaultPoolConfig
* @return org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory
* @throws
**/
@Bean
@Primary
public LettuceConnectionFactory defaultLettuceConnectionFactory(RedisStandaloneConfiguration defaultRedisConfig,
GenericObjectPoolConfig defaultPoolConfig) {
LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder().commandTimeout(Duration.ofMillis(100))
.poolConfig(defaultPoolConfig).build();
return new LettuceConnectionFactory(defaultRedisConfig, clientConfig);
}
/**
* <p>描述:
one Lettuce工厂类
* <p>开发者: GHQ·阿甘
* <p>时间:
2021/7/7 0:20
*
* @param oneRedisConfig
* @param onePoolConfig
* @return org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory
* @throws
**/
@Bean("oneLettuceConnectionFactory")
@ConditionalOnBean(name = "oneRedisConfig")
public LettuceConnectionFactory oneLettuceConnectionFactory(@Qualifier("oneRedisConfig") RedisStandaloneConfiguration oneRedisConfig,
@Qualifier("onePoolConfig") GenericObjectPoolConfig onePoolConfig) {
LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder().commandTimeout(Duration.ofMillis(100)).poolConfig(onePoolConfig).build();
return new LettuceConnectionFactory(oneRedisConfig, clientConfig);
}
/**
* <p>描述:
主默认(0)RedisTemplate
* <p>开发者: GHQ·阿甘
* <p>时间:
2021/7/7 0:26
*
* @param defaultLettuceConnectionFactory
* @return org.springframework.data.redis.core.RedisTemplate<java.lang.String, java.lang.String>
* @throws
**/
@Bean
@Primary
public RedisTemplate<String, String> defaultRedisTemplate(
LettuceConnectionFactory defaultLettuceConnectionFactory) {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(defaultLettuceConnectionFactory);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
/**
* <p>描述:
(1)RedisTemplate
* <p>开发者: GHQ·阿甘
* <p>时间:
2021/7/7 0:26
*
* @param localLettuceConnectionFactory
* @return org.springframework.data.redis.core.RedisTemplate<java.lang.String, java.lang.String>
* @throws
**/
@Bean(name = "oneRedisTemplate")
@ConditionalOnBean(name = "oneLettuceConnectionFactory")
public RedisTemplate<String, String> oneRedisTemplate(@Qualifier("oneLettuceConnectionFactory") LettuceConnectionFactory localLettuceConnectionFactory) {
RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(localLettuceConnectionFactory);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
@Configuration
@ConfigurationProperties(prefix = "spring.redis-one")
public class OneRedisConfig extends RedisProperties {
@Bean("onePoolConfig")
public GenericObjectPoolConfig onePoolConfig() {
return new UserGenericObjectPoolConfig(this);
}
@Bean("oneRedisConfig")
public RedisStandaloneConfiguration oneRedisConfig() {
return new UserRedisStandaloneConfiguration(this);
}
}
@Configuration
@ConfigurationProperties(prefix = "spring.redis")
public class DefaultRedisConfig extends RedisProperties {
@Bean
@Primary
public GenericObjectPoolConfig defaultPoolConfig() {
return new UserGenericObjectPoolConfig(this);
}
@Bean
@Primary
public RedisStandaloneConfiguration defaultRedisConfig() {
return new UserRedisStandaloneConfiguration(this);
}
}
}

最后

以上就是瘦瘦黑米为你收集整理的SpringBoot+RedisTemplate多数据源的全部内容,希望文章能够帮你解决SpringBoot+RedisTemplate多数据源所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(42)

评论列表共有 0 条评论

立即
投稿
返回
顶部