栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot中Redis双DB

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot中Redis双DB

SpringBoot中Redis双DB
  • 第一种实现方式
    • yml
    • configuation
  • 第二种实现方式

第一种实现方式
  • redis版本为1.x
yml
spring:
  redis:
    sentinel:
      master: mymaster
      nodes: 192.168.100.111:26379,192.168.100.112:26379,192.168.100.113:36379
    password: 123456
    timeout: 5000
    database: 2
    lettuce:
      pool:
        max-active: 30
        max-wait: 20000
        max-idle: 30
        min-idle: 10
    jedis:
      pool:
        max-active: 30
        max-wait: 20000
        max-idle: 30
        min-idle: 10
configuation
    
    //redis配置映射
    @Autowired
    RedisProperties redisProperties;

    //多例连接工厂
    @Bean
    @Scope(scopeName = "prototype")
    public JedisConnectionFactory jedisConnectionFactory() {

        RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration();
        List nodes = new ArrayList<>();
        for (String node : redisProperties.getSentinel().getNodes()) {
            try {
                String[] parts = StringUtils.split(node, ":");
                Assert.state(parts.length == 2, "Must be defined as 'host:port'");
                nodes.add(new RedisNode(parts[0], Integer.parseInt(parts[1])));
            }
            catch (RuntimeException ex) {
                throw new IllegalStateException("Invalid redis sentinel property '" + node + "'", ex);
            }
        }
        redisSentinelConfiguration.setSentinels(nodes);
        redisSentinelConfiguration.setMaster(redisProperties.getSentinel().getMaster());
        redisSentinelConfiguration.setDatabase(redisProperties.getDatabase());
        redisSentinelConfiguration.setPassword(redisProperties.getPassword());
        redisSentinelConfiguration.setSentinelPassword(redisProperties.getPassword());

        return new JedisConnectionFactory(redisSentinelConfiguration);
    }

    
    @Bean
    public RedisTemplate redisTemplate() {
        RedisTemplate redisTemplate = new RedisTemplate<>();
        JedisConnectionFactory jedisConnectionFactory = jedisConnectionFactory();
        redisTemplate.setConnectionFactory(jedisConnectionFactory);

        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(stringRedisSerializer);
        redisTemplate.setValueSerializer(stringRedisSerializer);
        redisTemplate.setHashKeySerializer(stringRedisSerializer);
        redisTemplate.setHashValueSerializer(stringRedisSerializer);
        redisTemplate.afterPropertiesSet();

        return redisTemplate;
    }

    
    @Bean("redisTemplateOfDb0")
    public RedisTemplate redisTemplateOfDb0() {
        RedisTemplate redisTemplate = new RedisTemplate<>();
        JedisConnectionFactory jedisConnectionFactory = jedisConnectionFactory();
        //此方法已过时
        jedisConnectionFactory.setDatabase(0);
        redisTemplate.setConnectionFactory(jedisConnectionFactory);

        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(stringRedisSerializer);
        redisTemplate.setValueSerializer(stringRedisSerializer);
        redisTemplate.setHashKeySerializer(stringRedisSerializer);
        redisTemplate.setHashValueSerializer(stringRedisSerializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
  • 通过jedisConnectionFactory指定数据库的方法在1.x版本已过时,有无替代?
  • 这样写在分布式环境中是否有性能问题?
第二种实现方式
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/354145.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号