先用docker-compose搭建redis集群

version: '3.8'
services:
  redis-node-0:
    image: bitnami/redis-cluster
    container_name: redis-node-0
    restart: always
    networks:
      redis:
        ipv4_address: 172.22.0.100
    hostname: redis-node-0
    environment:
      - 'REDIS_PORT_NUMBER=7000'
      - 'REDIS_PASSWORD=${REDIS_PASSWORD}'
      - 'REDIS_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-3 redis-node-4 redis-node-5'
      - 'REDIS_CLUSTER_ANNOUNCE_PORT=7000'
      - 'REDIS_CLUSTER_ANNOUNCE_IP=${REDIS_CLUSTER_ANNOUNCE_IP}'
      - 'REDIS_CLUSTER_BUS_ANNOUNCE_PORT=17000'
      - 'REDIS_CLUSTER_DYNAMIC_IPS=no'
    ports:
      - "7000:7000"
      - "17000:17000"

  redis-node-1:
    image: bitnami/redis-cluster
    container_name: redis-node-1
    restart: always
    networks:
      redis:
        ipv4_address: 172.22.0.101
    hostname: redis-node-1
    environment:
      - 'REDIS_PORT_NUMBER=7001'
      - 'REDIS_PASSWORD=${REDIS_PASSWORD}'
      - 'REDIS_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-3 redis-node-4 redis-node-5'
      - 'REDIS_CLUSTER_ANNOUNCE_PORT=7001'
      - 'REDIS_CLUSTER_ANNOUNCE_IP=${REDIS_CLUSTER_ANNOUNCE_IP}'
      - 'REDIS_CLUSTER_BUS_ANNOUNCE_PORT=17001'
      - 'REDIS_CLUSTER_DYNAMIC_IPS=no'
    ports:
      - "7001:7001"
      - "17001:17001"

  redis-node-2:
    image: bitnami/redis-cluster
    container_name: redis-node-2
    restart: always
    networks:
      redis:
        ipv4_address: 172.22.0.102
    hostname: redis-node-2
    environment:
      - 'REDIS_PORT_NUMBER=7002'
      - 'REDIS_PASSWORD=${REDIS_PASSWORD}'
      - 'REDIS_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-3 redis-node-4 redis-node-5'
      - 'REDIS_CLUSTER_ANNOUNCE_PORT=7002'
      - 'REDIS_CLUSTER_ANNOUNCE_IP=${REDIS_CLUSTER_ANNOUNCE_IP}'
      - 'REDIS_CLUSTER_BUS_ANNOUNCE_PORT=17002'
      - 'REDIS_CLUSTER_DYNAMIC_IPS=no'
    ports:
      - "7002:7002"
      - "17002:17002"

  redis-node-3:
    image: bitnami/redis-cluster
    container_name: redis-node-3
    restart: always
    networks:
      redis:
        ipv4_address: 172.22.0.103
    hostname: redis-node-3
    environment:
      - 'REDIS_PORT_NUMBER=7003'
      - 'REDIS_PASSWORD=${REDIS_PASSWORD}'
      - 'REDIS_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-3 redis-node-4 redis-node-5'
      - 'REDIS_CLUSTER_ANNOUNCE_PORT=7003'
      - 'REDIS_CLUSTER_ANNOUNCE_IP=${REDIS_CLUSTER_ANNOUNCE_IP}'
      - 'REDIS_CLUSTER_BUS_ANNOUNCE_PORT=17003'
      - 'REDIS_CLUSTER_DYNAMIC_IPS=no'
    ports:
      - "7003:7003"
      - "17003:17003"

  redis-node-4:
    image: bitnami/redis-cluster
    container_name: redis-node-4
    restart: always
    networks:
      redis:
        ipv4_address: 172.22.0.104
    hostname: redis-node-4
    environment:
      - 'REDIS_PORT_NUMBER=7004'
      - 'REDIS_PASSWORD=${REDIS_PASSWORD}'
      - 'REDIS_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-3 redis-node-4 redis-node-5'
      - 'REDIS_CLUSTER_ANNOUNCE_PORT=7004'
      - 'REDIS_CLUSTER_ANNOUNCE_IP=${REDIS_CLUSTER_ANNOUNCE_IP}'
      - 'REDIS_CLUSTER_BUS_ANNOUNCE_PORT=17004'
      - 'REDIS_CLUSTER_DYNAMIC_IPS=no'
    ports:
      - "7004:7004"
      - "17004:17004"

  redis-node-5:
    image: bitnami/redis-cluster
    container_name: redis-node-5
    restart: always
    networks:
      redis:
        ipv4_address: 172.22.0.105
    hostname: redis-node-5
    environment:
      - 'REDIS_PORT_NUMBER=7005'
      - 'REDIS_PASSWORD=${REDIS_PASSWORD}'
      - 'REDIS_NODES=redis-node-0 redis-node-1 redis-node-2 redis-node-3 redis-node-4 redis-node-5'
      - 'REDIS_CLUSTER_ANNOUNCE_PORT=7005'
      - 'REDIS_CLUSTER_ANNOUNCE_IP=${REDIS_CLUSTER_ANNOUNCE_IP}'
      - 'REDIS_CLUSTER_BUS_ANNOUNCE_PORT=17005'
      - 'REDIS_CLUSTER_DYNAMIC_IPS=no'
    ports:
      - "7005:7005"
      - "17005:17005"

  redis-cluster-init:
    image: redis:6.2
    container_name: redis-cluster-init
    restart: 'no'
    networks:
      redis:
        ipv4_address: 172.22.0.106
    depends_on:
      - redis-node-0
      - redis-node-1
      - redis-node-2
      - redis-node-3
      - redis-node-4
      - redis-node-5
    entrypoint: []
    command:
      - /bin/bash
      - -c
      - redis-cli -a ${REDIS_PASSWORD} --cluster create 172.22.0.100:7000 172.22.0.101:7001 172.22.0.102:7002 172.22.0.103:7003 172.22.0.104:7004 172.22.0.105:7005 --cluster-replicas 1 --cluster-yes


networks:
  redis:
    driver: bridge
    ipam:
      config:
        - subnet: 172.22.0.0/16
          gateway: 172.22.0.1


${REDIS_PASSWORD} 为密码

${REDIS_CLUSTER_ANNOUNCE_IP} 为宿主机ip

可自行替换或写入env配置。直接docker-compose up -d 启动就好了

该集群为三主三从

整合spring boot

pom

         <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>

config

/**
 * @program: onefint-oms
 * @description: RedisConfig
 * @create: 2022-08-11 19:37
 **/
@Configuration
public class RedisConfig extends CachingConfigurerSupport {

    @Resource
    private RedisClusterConfigProperties clusterProperties;

    @Bean
    public RedisClusterConfiguration getClusterConfig() {
        RedisClusterConfiguration rcc = new RedisClusterConfiguration(clusterProperties.getNodes());
        rcc.setMaxRedirects(clusterProperties.getMaxAttempts());
        rcc.setPassword(RedisPassword.of(clusterProperties.getPassword()));
        return rcc;
    }

    @Bean
    public JedisCluster getJedisCluster() {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        // 截取集群节点
        String[] cluster = clusterProperties.getNodes().toArray(new String[0]);
        // 创建set集合
        Set<HostAndPort> nodes = new HashSet<HostAndPort>();
        // 循环数组把集群节点添加到set集合中
        for (String node : cluster) {
            String[] host = node.split(":");
            //添加集群节点
            if (host.length >= 2) {
                nodes.add(new HostAndPort(host[0], Integer.parseInt(host[1])));
            }
        }
        return new JedisCluster(nodes, clusterProperties.getConnectionTimeout(), clusterProperties.getSoTimeout(), clusterProperties.getMaxAttempts(), clusterProperties.getPassword(), poolConfig);
    }


    @Bean
    public JedisConnectionFactory redisConnectionFactory(RedisClusterConfiguration cluster) {
        return new JedisConnectionFactory(cluster);
    }

    /**
     * RedisTemplate配置
     * key 和 value 都为String类型
     * 都使用Jackson2JsonRedisSerializer进行序列化
     */
    @Bean(name = "redisTemplate")
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
        StringRedisTemplate template = new StringRedisTemplate(factory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }

}

entiy

@Component
@ConfigurationProperties(prefix = "spring.redis.cluster")
@Data //使用了lombok的标签 如果未引用lombok需写getter 和 setter方法
public class RedisClusterConfigProperties {
    private List<String> nodes;
    private Integer maxAttempts;
    private Integer connectionTimeout;
    private Integer soTimeout;
    private String password;
}

配置

  redis:
    cluster:
      nodes:
        #替换为正确的redis集群的IP和端口号
        - 127.0.0.1:7000
        - 127.0.0.1:7001
        - 127.0.0.1:7002
      connectionTimeout: 6000
      soTimeout: 6000
      maxAttempts: 5
      password: password #写正确的密码