Redis数据源配置

Redis数据源配置

mop文件
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
单数据源配置
  1. yml文件

    spring:
      redis:
          db1:
             database: 162         #redis缓存数据库下标
             hostName: 127.0.0.1      #redis缓存地址
             port: 6379          #redis缓存端口
             password: root         #reids登录密码
          lettuce:
             pool:
                max-active: 8      #连接池最大连接数(使用负值表示没有限制) 默认 8
                max-idle: 8        #连接池中的最大空闲连接 默认 8
                max-wait: -1       #连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1
                min-idle: 0        #连接池中的最小空闲连接 默认 0  
    
    1. javaBean配置
    /**
     * @author : fzz
     */
    @Configuration
    public class RedisConfig {
    
        private StringRedisTemplate getStringRedisTemplate(RedisConnectionFactory factory) {
            StringRedisTemplate template = new StringRedisTemplate(factory);
            template.afterPropertiesSet();
            return template;
        }
    
        private RedisTemplate<Serializable, Object> getObjectRedisTemplate(RedisConnectionFactory factory) {
            RedisTemplate<Serializable, Object> redisTemplate = new RedisTemplate<>();
            redisTemplate.setConnectionFactory(factory);
            redisTemplate.afterPropertiesSet();
            return redisTemplate;
        }
        @Bean
        @ConfigurationProperties(prefix = "spring.redis.lettuce.pool")
        public GenericObjectPoolConfig redisPool() {
            return new GenericObjectPoolConfig();
        }
    
        @Bean
        @ConfigurationProperties(prefix = "spring.redis.db1")
        public RedisStandaloneConfiguration commentRedisConfig() {
            return new RedisStandaloneConfiguration();
        }
    
        @Bean("db1Factory")
        public LettuceConnectionFactory commentFactory(GenericObjectPoolConfig config, RedisStandaloneConfiguration commentRedisConfig) {
            LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(config).build();
            return new LettuceConnectionFactory(commentRedisConfig, clientConfiguration);
        }
    
        @Bean("db1StringRedisTemplate")
        public StringRedisTemplate countryStringRedisTemplate(@Qualifier("db1Factory") RedisConnectionFactory factory) {
            return getStringRedisTemplate(factory);
        }
    
        @Bean("db1ObjectRedisTemplate")
        public RedisTemplate<Serializable, Object> countryObjectRedisTemplate(@Qualifier("db1Factory") RedisConnectionFactory factory) {
            return getObjectRedisTemplate(factory);
        }
    }
    
多数据源
  1. yml文件

    spring:
      redis:
          db1:
             database: 162         #redis缓存数据库下标
             hostName: 127.0.0.1      #redis缓存地址
             port: 6379          #redis缓存端口
             password: root         #reids登录密码
          db2:
             database: 162         #redis缓存数据库下标
             hostName: 127.0.0.1      #redis缓存地址
             port: 6379          #redis缓存端口
             password: root         #reids登录密码
          lettuce:
             pool:
                max-active: 8      #连接池最大连接数(使用负值表示没有限制) 默认 8
                max-idle: 8        #连接池中的最大空闲连接 默认 8
                max-wait: -1       #连接池最大阻塞等待时间(使用负值表示没有限制) 默认 -1
                min-idle: 0        #连接池中的最小空闲连接 默认 0  
    
    1. javaBean配置
    /**
     * @author : fzz
     */
    @Configuration
    public class RedisConfig {
    
        private StringRedisTemplate getStringRedisTemplate(RedisConnectionFactory factory) {
            StringRedisTemplate template = new StringRedisTemplate(factory);
            template.afterPropertiesSet();
            return template;
        }
    
        private RedisTemplate<Serializable, Object> getObjectRedisTemplate(RedisConnectionFactory factory) {
            RedisTemplate<Serializable, Object> redisTemplate = new RedisTemplate<>();
            redisTemplate.setConnectionFactory(factory);
            redisTemplate.afterPropertiesSet();
            return redisTemplate;
        }
        @Bean
        @ConfigurationProperties(prefix = "spring.redis.lettuce.pool")
        public GenericObjectPoolConfig redisPool() {
            return new GenericObjectPoolConfig();
        }
    
        @Bean
        @ConfigurationProperties(prefix = "spring.redis.db1")
        public RedisStandaloneConfiguration commentRedisConfig() {
            return new RedisStandaloneConfiguration();
        }
    
        @Bean("db1Factory")
        @Primary	//多个相同的bean时,优先选择带有该注解的bean
        public LettuceConnectionFactory commentFactory(GenericObjectPoolConfig config, RedisStandaloneConfiguration commentRedisConfig) {
            LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(config).build();
            return new LettuceConnectionFactory(commentRedisConfig, clientConfiguration);
        }
    
        @Bean("db1StringRedisTemplate")
        @Primary
        public StringRedisTemplate countryStringRedisTemplate(@Qualifier("db1Factory") RedisConnectionFactory factory) {
            return getStringRedisTemplate(factory);
        }
    
        @Bean("db1ObjectRedisTemplate")
        @Primary
        public RedisTemplate<Serializable, Object> countryObjectRedisTemplate(@Qualifier("db1Factory") RedisConnectionFactory factory) {
            return getObjectRedisTemplate(factory);
        }
    
        @Bean
        @ConfigurationProperties(prefix = "spring.redis.db2")
        public RedisStandaloneConfiguration commentRedisConfig2() {
            return new RedisStandaloneConfiguration();
        }
    
        @Bean("db2Factory")
        public LettuceConnectionFactory commentFactory2(GenericObjectPoolConfig config, RedisStandaloneConfiguration commentRedisConfig2) {
            LettuceClientConfiguration clientConfiguration = LettucePoolingClientConfiguration.builder().poolConfig(config).build();
            return new LettuceConnectionFactory(commentRedisConfig2, clientConfiguration);
        }
    
        @Bean("db2StringRedisTemplate")
        public StringRedisTemplate countryStringRedisTemplate2(@Qualifier("db2Factory") RedisConnectionFactory factory) {
            return getStringRedisTemplate(factory);
        }
    
        @Bean("db2ObjectRedisTemplate")
        public RedisTemplate<Serializable, Object> countryObjectRedisTemplate2(@Qualifier("db2Factory") RedisConnectionFactory factory) {
            return getObjectRedisTemplate(factory);
        }
    }
    
    
使用
@Autowired //使用带有@Primary模板
protected StringRedisTemplate stringRedisTemplate;

@Autowired
protected RedisTemplate<Serializable, Object> objectRedisTemplate;

@Autowired
@Qualifier("db2StringRedisTemplate")
protected StringRedisTemplate stringRedisTemplate2;

@Autowired
@Qualifier("db2ObjectRedisTemplate") //指定使用哪个
protected RedisTemplate<Serializable, Object> objectRedisTemplate2;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值