docker 搭建redis集群.并使用Spring Boot操作集群

206 阅读2分钟

一、哨兵集群

1、拉取redis镜像

docker pull redis

2、 编写reids主从docker-compose.yml和redis.conf

version: '3.7'
services:
  master:
    image: redis
    container_name: redis-master
    restart: always
    command: redis-server /usr/local/etc/redis/redis.conf --requirepass redispwd  --appendonly yes 
    ports:
      - 6379:6379
    volumes:
      - ./data1:/data
      - ./config/redis.conf:/usr/local/etc/redis/redis.conf
  slave1:
    image: redis
    container_name: redis-slave-1
    restart: always
    command: redis-server /usr/local/etc/redis/redis.conf --slaveof redis-master 6379  --requirepass redispwd --masterauth redispwd  --appendonly yes 
    ports:
      - 6380:6379
    volumes:
      - ./data2:/data
      - ./config/redis1.conf:/usr/local/etc/redis/redis.conf
  slave2:
    image: redis
    container_name: redis-slave-2
    restart: always
    command: redis-server /usr/local/etc/redis/redis.conf --slaveof redis-master 6379  --requirepass redispwd --masterauth redispwd  --appendonly yes 
    ports:
      - 6381:6379
    volumes:
      - ./data3:/data
      - ./config/redis2.conf:/usr/local/etc/redis/redis.conf

image.png 3份除端口不一致外其他内容一致。

daemonize no
protected-mode no
requirepass redisowd
masterauth redispwd
#指定 slave的ip  这边设置是为了防止主挂掉后,重启后ip会变成自己容器中的IP
slave-announce-ip 192.168.0.155
#指定 salve的端口
slave-announce-port 6379
#0.0.0.0在服务器的环境中,指的就是服务器上所有的ipv4地址
bind 0.0.0.0

3、启动主从redis

进入redis对应的docker-compose.yml的目录,执行命令:

docker-compose up -d

未找到命令:

curl -L https://get.daocloud.io/docker/compose/releases/download/1.29.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
#最后,测试安装是否成功
docker-compose version

4.编写哨兵docker-compose.yml

version: '3.7'
services:
  sentinel1:
    image: redis
    container_name: redis-sentinel-1
    restart: always
    ports:
      - 26379:26379
    command: redis-sentinel /usr/local/sentinel/sentinel.conf
    volumes:
      - ./sentinel1.conf:/usr/local/sentinel/sentinel.conf
  sentinel2:
    image: redis
    container_name: redis-sentinel-2
    restart: always
    ports:
    - 26380:26379
    command: redis-sentinel /usr/local/sentinel/sentinel.conf
    volumes:
      - ./sentinel2.conf:/usr/local/sentinel/sentinel.conf
  sentinel3:
    image: redis
    container_name: redis-sentinel-3
    ports:
      - 26381:26379
    command: redis-sentinel /usr/local/sentinel/sentinel.conf
    volumes:
      - ./sentinel3.conf:/usr/local/sentinel/sentinel.conf

5.编写哨兵sentinel.conf

3份除端口不一致外其他内容一致。

protected-mode no
port 26379
dir /tmp
sentinel monitor mymaster 192.168.0.155 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel parallel-syncs mymaster 1
sentinel auth-pass mymaster redispwd
sentinel failover-timeout mymaster 10000
sentinel deny-scripts-reconfig yes

sentinel announce-ip 192.168.0.155
sentinel announce-port 26379

6.进入哨兵docker-compose.yml所在目录,执行命令:

docker-compose up -d
docker ps #查看启动的镜像
docker stop $(docker ps -q) #关闭所有镜像

目录结构:

image.png image.png image.png

7、SpringBoot连接

导入依赖

implementation 'org.springframework.boot:spring-boot-starter-data-redis'

配置文件

spring:
  redis:
    password: redispwd
    timeout: 5000
    sentinel:
      master: mymaster
      nodes: 192.168.0.155:26379,192.168.0.155:26380,192.168.0.155:26381 # 哨兵的IP:Port列表
    jedis: # 或lettuce
      pool:
        max-active: 8
        max-wait: -1
        max-idle: 8
        min-idle: 0

redis配置类

@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        // 使用Jackson2JsonRedisSerialize 替换默认的jdkSerializeable序列化
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        jackson2JsonRedisSerializer.setObjectMapper(om);

        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();

        // key采用String的序列化方式
        template.setKeySerializer(stringRedisSerializer);
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(stringRedisSerializer);
        // value序列化方式采用jackson
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
}

二、cluster集群

暂未操作