5.6 Docker 搭建redis集群



1. Docker 搭建redis集群

我们需要搭建6个服务的集群,通过命令配置集群;
在这里插入图片描述

2. 创建Redis实例的配置文件:

首先新建redis文件夹:

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

3. 启动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

此时集群处于宕机状态,并没有覆盖hash槽算法,此时还无法存取数据:
在这里插入图片描述

4. 执行命令配置集群

通过--cluster create参数创建集群,后面将所有服务器的地址都写出来,并通过--cluster-replicas参数设置一个主服务器携带几个从服务器,我们这里设置为1个主服务器配置1个从服务器:

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

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

执行后需要输入yes确认执行方案:
在这里插入图片描述

在这里插入图片描述

5. 查看集群信息

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

cluster info

cluster nodes

在这里插入图片描述

6. 存取数据

参数-c是指使用集群模式连接服务器:

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

如图所示,我们进入7000服务器存储数据时,会计划hash槽位置,根据其位置自动判断存放到哪个服务器,并自动切换到对应的服务器:
在这里插入图片描述

7. spring redis api

jedis原生的API比较繁琐,所以我们使用spring的API,spring的API连接服务器存储数据共有俩种方式:

  1. 使用连接工厂
  2. 使用redistemplate连接对象访问
    使用连接工厂代码会更直接一点,我们这里选用连接工厂进行访问;

7.1 添加依赖

新建spring模块并添加依赖:
在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cn.study</groupId>
    <artifactId>redis-spring</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>redis-spring</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

7.2 配置文件

创建application.yml配置文件,并添加下列服务器地址配置:

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

7.3 代码实现

在默认创建的测试类中完成下列代码:

package cn.study.redisspring;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.connection.RedisClusterConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;

@SpringBootTest
class RedisSpringApplicationTests {
    //注入连接工厂
    @Autowired
    private RedisConnectionFactory redisConnectionFactory;

    @Test
    void contextLoads() {
        //使用连接工厂获取连接对象RedisClusterConnection 
        RedisClusterConnection clusterConnection = redisConnectionFactory.getClusterConnection();
        for (int i = 0; i < 100; i++) {
            String k = "k" + i;
            String v = "v" + i;
            //将字符串转换为数组后存储
            clusterConnection.set(k.getBytes(), v.getBytes());
        }
    }

}

完成后运行代码并在服务中查看数据:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值