当前位置:   article > 正文

Redisson兼容redis多模式部署的配置方式

Redisson兼容redis多模式部署的配置方式

RedissonConfig.java

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.ClusterServersConfig;
import org.redisson.config.Config;
import org.redisson.config.SentinelServersConfig;
import org.redisson.config.SingleServerConfig;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Slf4j
@Configuration
public class RedissonConfig {

    @Resource
    private RedisProperties redisProperties;

    private int connectionPoolSize = 64;
    private int connectionMinimumIdleSize = 10;
    private int pingConnectionInterval = 60000;
    private static String ADDRESS_PREFIX = "redis://";

    @Bean(destroyMethod = "shutdown")
    public RedissonClient redissonClient() {
        // 哨兵模式
        RedisProperties.Sentinel sentinel = redisProperties.getSentinel();
        if (Objects.nonNull(sentinel)) {
            log.info("redis is sentinel mode");
            return redissonSentinel();
        }
        // 集群模式
        RedisProperties.Cluster cluster = redisProperties.getCluster();
        if (Objects.nonNull(cluster)) {
            log.info("redis is cluster mode");
            return redissonCluster();
        }
        // 单机模式
        String host = redisProperties.getHost();
        if (StringUtils.isNotBlank(host)) {
            log.info("redis is single mode");
            return redissonSingle();
        }

        log.error("redisson config can not support this redis mode");
        return null;
    }

    /**
     * 单机模式
     */
    private RedissonClient redissonSingle() {
        String host = redisProperties.getHost();
        String password = redisProperties.getPassword();
        int port = redisProperties.getPort();
        // 声明一个配置类
        Config config = new Config();
        SingleServerConfig serverConfig = config.useSingleServer()
                .setAddress(ADDRESS_PREFIX + host + ":" + port)
                .setPingConnectionInterval(pingConnectionInterval)
                .setConnectionPoolSize(this.connectionPoolSize)
                .setConnectionMinimumIdleSize(this.connectionMinimumIdleSize);
        // 判断密码
        if (!StringUtils.isEmpty(password)) {
            serverConfig.setPassword(password);
        }
        return Redisson.create(config);
    }


    /**
     * 哨兵模式
     */
    private RedissonClient redissonSentinel() {
        // mymaster
        String masterName = redisProperties.getSentinel().getMaster();
        // 127.0.0.1:26389,127.0.0.1:26379
        List<String> nodes = redisProperties.getSentinel().getNodes();
        String password = redisProperties.getPassword();
        // 声明一个配置类
        Config config = new Config();
        SentinelServersConfig sentinelServersConfig = config.useSentinelServers();
        // 扫描间隔
        sentinelServersConfig.setScanInterval(2000);
        // 判断密码
        if (!StringUtils.isEmpty(password)) {
            sentinelServersConfig.setPassword(password);
        }
        sentinelServersConfig.setMasterName(masterName);
        String[] nodeArr = nodes.toArray(new String[nodes.size()]);
        for (int i = 0; i < nodeArr.length; i++) {
            nodeArr[i] = ADDRESS_PREFIX + nodeArr[i];
        }
        // 添加redis节点
        sentinelServersConfig.addSentinelAddress(nodeArr);
        return Redisson.create(config);
    }

    /**
     * 集群模式
     */
    private RedissonClient redissonCluster() {
        // 192.168.116.156:1901,192.168.116.156:1902
        List<String> nodes = redisProperties.getCluster().getNodes();
        String password = redisProperties.getPassword();
        // 声明一个配置类
        Config config = new Config();
        ClusterServersConfig clusterServersConfig = config.useClusterServers();
        // 扫描间隔
        clusterServersConfig.setScanInterval(2000);
        // 判断密码
        if (!StringUtils.isEmpty(password)) {
            clusterServersConfig.setPassword(password);
        }
        // 添加redis节点
        for (String node : nodes) {
            clusterServersConfig.addNodeAddress(ADDRESS_PREFIX + node);
        }
        return Redisson.create(config);
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127

YAML配置

1、单机部署/主从部署

spring:
  redis:
    database: 1
    host: 192.168.0.111
    password: 123456
    port: 6379
    timeout: 3000
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2、哨兵部署

spring:
  redis:
    database: 1
    password: 123456
    port: 6379
    sentinel:
      master: mymaster
      nodes:
        - 192.168.0.111:26379
        - 192.168.0.112:26379
        - 192.168.0.113:26379
      password: 123456
    timeout: 10000ms
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

3、集群部署

spring:
  redis:
    password: 123456
    database: 1
    cluster:
      nodes:
        - 192.168.0.111:6379
        - 192.168.0.112:6379
        - 192.168.0.113:6379
    timeout: 3000ms
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/351568
推荐阅读
相关标签
  

闽ICP备14008679号