Docker 搭建redis集群

Posted Wanght6

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker 搭建redis集群相关的知识,希望对你有一定的参考价值。

Docker 搭建redis集群

六个Redis实例的配置文件:
redis.conf

port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
/opt/redis
		/7000/redis.conf
		/7001/redis.conf
		/7002/redis.conf
		/7003/redis.conf
		/7004/redis.conf
		/7005/redis.conf
mkdir /opt/redis
cd /opt/redis
mkdir 7000 7001 7002 7003 7004 7005

cat <<EOF >7000/redis.conf
port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF

cat <<EOF >7001/redis.conf
port 7001
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF

cat <<EOF >7002/redis.conf
port 7002
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF

cat <<EOF >7003/redis.conf
port 7003
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF

cat <<EOF >7004/redis.conf
port 7004
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF

cat <<EOF >7005/redis.conf
port 7005
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF


先启动6个redis实例

docker run -d --name redis7000 \\
-v /opt/redis/7000/redis.conf:/redis.conf \\
--net=host \\
--restart=always \\
redis \\
redis-server /redis.conf

docker run -d --name redis7001 \\
-v /opt/redis/7001/redis.conf:/redis.conf \\
--net=host \\
--restart=always \\
redis \\
redis-server /redis.conf

docker run -d --name redis7002 \\
-v /opt/redis/7002/redis.conf:/redis.conf \\
--net=host \\
--restart=always \\
redis \\
redis-server /redis.conf

docker run -d --name redis7003 \\
-v /opt/redis/7003/redis.conf:/redis.conf \\
--net=host \\
--restart=always \\
redis \\
redis-server /redis.conf

docker run -d --name redis7004 \\
-v /opt/redis/7004/redis.conf:/redis.conf \\
--net=host \\
--restart=always \\
redis \\
redis-server /redis.conf

docker run -d --name redis7005 \\
-v /opt/redis/7005/redis.conf:/redis.conf \\
--net=host \\
--restart=always \\
redis \\
redis-server /redis.conf


执行命令配置集群

# 进入容器执行集群配置命令

docker exec -it redis7000 \\
redis-cli --cluster create \\
192.168.64.150:7000 192.168.64.150:7001 \\
192.168.64.150:7002 192.168.64.150:7003 \\
192.168.64.150:7004 192.168.64.150:7005 \\
--cluster-replicas 1

查看集群信息

docker exec -it redis7000 redis-cli -c -p 7000

cluster info

cluster nodes

存数据

docker exec -it redis7000 redis-cli -c -p 7000
# 7002
set a aaaaaaaaaaaaa
# 7000
set b bbbbbbbbbbb
# 7001
set c cccccccccccccccc

spring redis api


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        
        
spring:
  redis:
    cluster:
      nodes:
        - 192.168.64.150:7000
        - 192.168.64.150:7001
        - 192.168.64.150:7002
        - 192.168.64.150:7003
        - 192.168.64.150:7004
        - 192.168.64.150:7005
        
        
    @Autowired
    private RedisConnectionFactory connectionFactory;

    public void test() {
        RedisClusterConnection c = connectionFactory.getClusterConnection();
        for (int i = 0; i < 100; i++) {
            c.set(("k"+i).getBytes(StandardCharsets.UTF_8),
                    ("v"+i).getBytes(StandardCharsets.UTF_8));
        }
    }
    
    
    @Autowired
    @Qualifier("stringRedisTemplate")
    private RedisTemplate t;

    public void test() {
        ValueOperations<String, Object> o = t.opsForValue();
        o.set("hhhh", "vvvv");
        o.set("nnnn", "gggg");
    }

以上是关于Docker 搭建redis集群的主要内容,如果未能解决你的问题,请参考以下文章

基于docker环境搭建redis-cluster集群(多台机器)

Docker 搭建redis集群

Docker搭建Redis Cluster集群及扩容和收容

docker-compose搭建redis-cluster集群

Docker redis集群搭建

docker 环境搭建redis集群相关问题