菜单
登录注册
欢 迎
登录
自动登录
忘记密码?
新朋友
注册
注册
老朋友
登录
笔记内容为空!
TA的笔记树 >>
spring-boot配置redis多数据源
Redis
## 引用包 ```
org.springframework.boot
spring-boot-starter-data-redis
org.apache.commons
commons-pool2
``` ## properties配置 ``` redis-common: lettuce: pool: max-total: 16 max-wait: -1 max-idle: 2 min-idle: 0 redis-user-pool: host-name: bigdata005 port: 60001 database: 14 password: ppwwdd timeout: 60000 ``` ## 多数据源配置 ``` import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.springframework.beans.factory.annotation.Qualifier; 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.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisStandaloneConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.serializer.StringRedisSerializer; /** * redis配置 * * @author vanki */ @Configuration public class RedisConfiguration { /* ================================== 用户池Redis ================================== */ @Bean public StringRedisTemplate userPoolRedisTemplate(@Qualifier("userPoolRedisConnFactory") RedisConnectionFactory userPoolRedisConnFactory) { return getStringRedisTemplate(userPoolRedisConnFactory); } @Primary @Bean public LettuceConnectionFactory userPoolRedisConnFactory( @Qualifier("userPoolRedisConfig") RedisStandaloneConfiguration userPoolRedisConfig, GenericObjectPoolConfig> redisPoolConfig ) { return new LettuceConnectionFactory(userPoolRedisConfig, LettucePoolingClientConfiguration.builder().poolConfig(redisPoolConfig).build()); } @Bean @ConfigurationProperties(prefix = "spring.redis-user-pool") public RedisStandaloneConfiguration userPoolRedisConfig() { return new RedisStandaloneConfiguration(); } /* ================================================================================ */ // /* ================================== 多数据源模版 ================================== */ // // @Bean // public StringRedisTemplate secondRedisTemplate(@Qualifier("secondRedisConnFactory") RedisConnectionFactory secondRedisConnFactory) { // return getStringRedisTemplate(secondRedisConnFactory); // } // // @Bean // public LettuceConnectionFactory secondRedisConnFactory( // @Qualifier("secondRedisConfig") RedisStandaloneConfiguration userPoolRedisConfig, // GenericObjectPoolConfig> redisPoolConfig // ) { // return new LettuceConnectionFactory(userPoolRedisConfig, LettucePoolingClientConfiguration.builder().poolConfig(redisPoolConfig).build()); // } // // @Bean // @ConfigurationProperties(prefix = "spring.redis-second") // public RedisStandaloneConfiguration secondRedisConfig() { // return new RedisStandaloneConfiguration(); // } // // /* ================================================================================ */ @Bean @ConfigurationProperties(prefix = "spring.redis-common.lettuce.pool") public GenericObjectPoolConfig> redisPoolConfig() { return new GenericObjectPoolConfig<>(); } protected StringRedisTemplate getStringRedisTemplate(RedisConnectionFactory factory) { final StringRedisTemplate template = new StringRedisTemplate(); template.setConnectionFactory(factory); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); template.setKeySerializer(stringRedisSerializer); template.setHashKeySerializer(stringRedisSerializer); template.setValueSerializer(stringRedisSerializer); template.setHashValueSerializer(stringRedisSerializer); template.afterPropertiesSet(); return template; } } ``` ## 使用 ``` @Autowired private StringRedisTemplate userPoolRedisTemplate; ```
vanki
我以为看到了你,便拥有了整个世界。却没想到你的梦里,早已有了我。
浏览:
1197
创建:
2022-05-06 17:06:23
更新:
2022-05-06 17:08:47
TA的最新笔记
spring-boot配置redis多数据源
linux源修改(阿里)
python安装postgresql依赖
arthas使用
java基于spring的NamedParameterJdbcTemplate封装的获取sql工具类
Impala添加负载
S3常用使用
redis常用操作
hdfs相关命令
crontab使用
TA的最热笔记
java异步http请求工具类(org.asynchttpclient)
iTerm2主题配置与常用技巧
java基于spring.redisTemplate实现分布式锁工具类
Kotlin + SpringBoot + JPA(Hibernate) + Repository自定义方法
IDEA汉化
Sequel Pro连接mysql8打开数据库报错
centos-Hadoop2.7.3完全分布式搭建(HA)
SpringBoot上传文件报错(The temporary upload location [..] is not valid)
mac常用软件
kotlin对象属性值拷贝工具类