Redis Cluster集群远程访问(SpringBoot 2.0)

1.RedisDesktopManager 不支持集群访问方式
2.FastoRedis支持集群访问方式
3.spirngboot2.0 连接redis集群
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-redisartifactId>
        dependency>

        <dependency>
            <groupId>redis.clientsgroupId>
            <artifactId>jedisartifactId>
        dependency>
# yml文件配置
spring:
  redis:
    #通过jedis进行连接池管理
    jedis:
      pool:
        #最大连接数
        max-active: 8
        #最大空闲连接数
        max-idle: 8
        #最小空闲连接数
        min-idle: 0
        #最大阻塞等待时间,负值为无限制
        max-wait: -1
    password:
    #超时时间,毫秒
    timeout: 50000
    #出现异常最大重试次数
    maxAttempts: 5
    cluster:
      nodes: 192.168.3.8:6380,192.168.3.8:6381,192.168.3.8:6382,192.168.3.8:6383,192.168.3.8:6384,192.168.3.8:6385
@Configuration
@ConditionalOnClass({JedisCluster.class})
public class RedisConfig {
    @Value("${spring.redis.cluster.nodes}")
    private String clusterNodes;
    @Value("${spring.redis.timeout}")
    private int timeout;
    @Value("${spring.redis.jedis.pool.max-idle}")
    private int maxIdle;
    @Value("${spring.redis.jedis.pool.max-wait}")
    private long maxWaitMillis;
    @Value("${spring.redis.maxAttempts}")
    private int maxAttempts;
    @Value("${spring.redis.password}")
    private String password;

    @Bean
    public JedisCluster getJedisCluster() {
        String[] cNodes = clusterNodes.split(",");
        Set nodes = new HashSet();
        // 分割出集群节点
        for (String node : cNodes) {
            String[] hp = node.split(":");
            nodes.add(new HostAndPort(hp[0], Integer.parseInt(hp[1])));
        }
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
        // 创建集群对象
        JedisCluster jedisCluster = new JedisCluster(nodes, timeout, timeout, maxAttempts, jedisPoolConfig);
        return jedisCluster;
    }
}

测试过程中出现Could not get a resource from the pool,是因为之前做集群时使用127.0.0.1地址,需要改为真实可访问ip地址。
删除各个目录下nodes.conf文件,重新制作集群,使用ip地址

./redis-trib.rb create --replicas 1 192.168.3.8:6380 192.168.3.8:6381 192.168.3.8:6382 192.168.3.8:6383 192.168.3.8:6384 192.168.3.8:6385

测试OK~

也可为集群设置统一密码,带密码访问。
https://www.cnblogs.com/linjiqin/p/7462822.html

你可能感兴趣的:(Spring)