zoukankan      html  css  js  c++  java
  • springboot redis多数据源设置

    遇到这样一个需求:运营人员在发布内容的时候可以选择性的发布到测试库、开发库和线上库。 
    项目使用的是spring boot集成redis,实现如下:

    1. 引入依赖

            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-redis</artifactId>
            </dependency>
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
            </dependency>

    2.多数据源设置

    application.yml设置(application.properties同理):

    spring:
      redis:
        database: 0
        pool:
          max-active: 8
          max-idle: 9
          max-wait: -1
          min-idle: 0
        redis-dev:
          host: 填redis的ip地址
          prot: 填redis的端口号
          password: 填redis的密码
          testOnBorrow: fals
        redis-test:
          host:
          prot:
          password:
          testOnBorrow: false
        redis-online:
          host:
          prot:
          password:
          testOnBorrow: false

    针对每个数据源写一个配置类: 
    这里就只列举其中一个,不同的数据源设置不同的@Bean和@Value注解即可

    @Configuration
    public class RedisDevConfiguration {
    
        @Bean(name = "redisDevTemplate")
        public StringRedisTemplate redisTemplate(@Value("${spring.redis-dev.host}") String hostName,
                @Value("${spring.redis-dev.port}") int port, @Value("${spring.redis-dev.password}") String password,
                @Value("${spring.redis-dev.testOnBorrow}") boolean testOnBorrow,
                @Value("${spring.redis.pool.max-idle}") int maxIdle, @Value("${spring.redis.pool.max-active}") int maxTotal,
                @Value("${spring.redis.database}") int index, @Value("${spring.redis.pool.max-wait}") long maxWaitMillis) {
            StringRedisTemplate temple = new StringRedisTemplate();
            temple.setConnectionFactory(
                    connectionFactory(hostName, port, password, maxIdle, maxTotal, index, maxWaitMillis, testOnBorrow));
    
            return temple;
        }
    
        public RedisConnectionFactory connectionFactory(String hostName, int port, String password, int maxIdle,
                int maxTotal, int index, long maxWaitMillis, boolean testOnBorrow) {
            JedisConnectionFactory jedis = new JedisConnectionFactory();
            jedis.setHostName(hostName);
            jedis.setPort(port);
            if (StringUtils.isNotEmpty(password)) {
                jedis.setPassword(password);
            }
            if (index != 0) {
                jedis.setDatabase(index);
            }
            jedis.setPoolConfig(poolCofig(maxIdle, maxTotal, maxWaitMillis, testOnBorrow));
            // 初始化连接pool
            jedis.afterPropertiesSet();
            RedisConnectionFactory factory = jedis;
    
            return factory;
        }
    
        public JedisPoolConfig poolCofig(int maxIdle, int maxTotal, long maxWaitMillis, boolean testOnBorrow) {
            JedisPoolConfig poolCofig = new JedisPoolConfig();
            poolCofig.setMaxIdle(maxIdle);
            poolCofig.setMaxTotal(maxTotal);
            poolCofig.setMaxWaitMillis(maxWaitMillis);
            poolCofig.setTestOnBorrow(testOnBorrow);
            return poolCofig;
        }
    }

    3.构造redis实例

    写一个redis实例抽象类

    public abstract class AbRedisConfiguration {
        protected StringRedisTemplate temple;
    
        public void setData(String key, String value) {
            getTemple().opsForValue().set(key, value);
        }
    
        public String getData(String key) {
            return getTemple().opsForValue().get(key);
        }
    
        public StringRedisTemplate getTemple() {
            return temple;
        }
    }

    继成抽象类实现操作类,这里只列举一个

    @Component
    public class RedisDev extends AbRedisConfiguration {
        @Resource(name = "redisDevTemplate")
        private StringRedisTemplate temple;
    
        public StringRedisTemplate getTemple() {
            return temple;
        }
    }

    4.使用

    在service中注入redis操作类

        @Autowired
        private RedisDev redisDev;
        @Autowired
        private RedisTest redisTest;
        @Autowired
        private RedisTest redisOnline;

    调用不同的操作类即可使用不同的redis数据源。

  • 相关阅读:
    什么是webview
    juqery.fn.extend和jquery.extend
    LeetCode
    5. Longest Palindromic Substring
    42. Trapping Rain Water
    11. Container With Most Water
    621. Task Scheduler
    49. Group Anagrams
    739. Daily Temperatures
    3. Longest Substring Without Repeating Characters
  • 原文地址:https://www.cnblogs.com/azhqiang/p/9132799.html
Copyright © 2011-2022 走看看