spring boot redis 多数据源配置 spring boot data redis
此文参考自 微笑点燃希望 spring boot redis多数据源
我的代码地址 github代码示例 觉得有用的可以点个star
第一步添加依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- redis序列化使用 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.68</version> </dependency> <!-- letture连接池 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.8.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> </dependencies>
spring boot web必须跟redis依赖 再加上 redis驱动 lettuce 连接池 `commons-pool2`
第二步 配置文件 配置多个数据源
spring: redis: hostName: reids地址 port: 6379 database: 0 password: redis密码 lettuce: pool: max-active: 200 #连接池最大连接数(使用负值表示没有限制) max-idle: 20 # 连接池中的最大空闲连接 min-idle: 5 #连接池中的最小空闲连接 # 这里在IDEA中显示红色错误,不用管,因为org.springframework.boot.autoconfigure.data.redis.RedisProperties.Pool.setMaxWait(Duration maxWait) # 方法参数是Duration,IDEA无法识别,但是会自动注入成功 max-wait: 2000 # 当连接池耗尽时, 抛出异常之前,连接分配被阻塞的时间,也就是连接池满后最长等待时间,负值表示无限期等待 redis2: hostName: reids地址 port: 6379 database: 1 password: redis密码
配置多个数据源原文微笑点燃希望中是host我没成功 改成了hostName 源码是这个
第三步 java读取配置文件
package com.example.redis.config; import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer; 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.LettuceClientConfiguration; 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.RedisSerializer; /** * @author ccubee * @since 20-4-4 7:15 */ @Configuration public class SpringBootRedisConfig { @Bean @ConfigurationProperties(prefix = "spring.redis.lettuce.pool") public GenericObjectPoolConfig config(){ return new GenericObjectPoolConfig<>(); } /** * 配置第一个数据源的 * * @return */ @Bean @ConfigurationProperties(prefix = "spring.redis") public RedisStandaloneConfiguration redisConfig() { return new RedisStandaloneConfiguration(); } /** * 配置第二个数据源 * * @return */ @Bean @ConfigurationProperties(prefix = "spring.redis2") public RedisStandaloneConfiguration redisConfig2() { return new RedisStandaloneConfiguration(); } /** * 配置第一个数据源的连接工厂 * 这里注意:需要添加@Primary 指定bean的名称,目的是为了创建两个不同名称的LettuceConnectionFactory * * @param config * @param redisConfig * @return */ @Bean("factory") @Primary public LettuceConnectionFactory factory(GenericObjectPoolConfig config, RedisStandaloneConfiguration redisConfig) { LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(config).build(); return new LettuceConnectionFactory(redisConfig, clientConfiguration); } @Bean("factory2") public LettuceConnectionFactory factory2(GenericObjectPoolConfig config, RedisStandaloneConfiguration redisConfig2) { LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(config).build(); return new LettuceConnectionFactory(redisConfig2, clientConfiguration); } /** * 配置第一个数据源的RedisTemplate * 注意:这里指定使用名称=factory 的 RedisConnectionFactory * 并且标识第一个数据源是默认数据源 @Primary * * @param factory * @return */ @Bean("stringRedisTemplate") @Primary public StringRedisTemplate redisTemplate(@Qualifier("factory") RedisConnectionFactory factory) { return getStringStringRedisTemplate(factory); } /** * 配置第一个数据源的RedisTemplate * 注意:这里指定使用名称=factory2 的 RedisConnectionFactory * * @param factory2 * @return */ @Bean("stringRedisTemplate2") public StringRedisTemplate redisTemplate2(@Qualifier("factory2") RedisConnectionFactory factory2) { return getStringStringRedisTemplate(factory2); } /** * 设置序列化方式 (这一步不是必须的) * * @param factory2 * @return */ private StringRedisTemplate getStringStringRedisTemplate(RedisConnectionFactory factory2) { StringRedisTemplate template = new StringRedisTemplate(factory2); template.setKeySerializer(RedisSerializer.string()); template.setValueSerializer(new FastJsonRedisSerializer<>(Object.class)); template.setHashKeySerializer(RedisSerializer.string()); template.setHashValueSerializer(new FastJsonRedisSerializer<>(Object.class)); return template; } }
最后测试一下
package com.example.redis.rest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * @author ccubee * @since 20-4-2 22:15 */ @RestController public class RedisRest { @Autowired @Qualifier("stringRedisTemplate2") StringRedisTemplate stringRedisTemplate; @GetMapping("redis") public String redis(){ stringRedisTemplate.opsForValue().set("key","123"); return stringRedisTemplate.opsForValue().get("key"); } }
启动后 调用输入 可以安装idea插件 restfultoolkit 进行测试