1. 搭建
下载并编译Redis
cd /usr/local
wget http://download.redis.io/releases/redis-5.0.4.tar.gz
tar xzf redis-5.0.4.tar.gz
cd redis-5.0.4
make
创建配置文件
mkdir cluster-conf
cd cluster-conf
mkdir 7000 7001 7002 7003 7004 7005
cd 7000
cp ../../src/redis-server .
vi redis.conf
port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
protected-mode no
pidfile /var/run/redis_7000.pid
在7000端口启动实例
./redis-server redis.conf
依次进入7001-7005目录, 重复上面的步骤, redis.conf文件中的port和pidfile不能重复, 依次加1即可
cd /usr/local/redis-5.0.4/src
./redis-cli --cluster create 192.168.1.11:7000 192.168.1.11:7001 192.168.1.11:7002 192.168.1.11:7003 192.168.1.11:7004 192.168.1.11:7005 --cluster-replicas 1
2. 在SpringBoot中集成
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
添加配置
redis:
cluster:
nodes: 192.168.1.11:7000,192.168.1.11:7001,192.168.1.11:7002,192.168.1.11:7003,192.168.1.11:7004,192.168.1.11:7005
3. 集群管理
cd /usr/local/redis-5.0.4/utils/create-cluster
vi create-cluster
把其中的PORT修改为6999, 即上面设置的最小的端口7000减1
关闭集群
./create-cluster stop
启动集群
./create-cluster start
关注我的微信