文章目录
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连接服务器存储数据共有俩种方式:
- 使用连接工厂
- 使用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());
}
}
}
完成后运行代码并在服务中查看数据: