部署
部署6个虚拟机(192.168.1.11-16),其中3个master节点,3个slave节点。
在每一台虚拟机运行如下shell脚本:
#!/usr/bin/env bash
sudo apt-get update
sudo apt-get install -y build-essential tcl ruby
cd /tmp
curl -O http://download.redis.io/redis-stable.tar.gz
tar xzvf redis-stable.tar.gz
cd redis-stable
make
make test
sudo make install
sudo mkdir /etc/redis
sudo cp /tmp/redis-stable/redis.conf /etc/redis
sudo sed -i 's/supervised no/supervised systemd/' /etc/redis/redis.conf
sudo sed -i 's/protected-mode yes/protected-mode no/' /etc/redis/redis.conf
sudo sed -i 's/bind 127.0.0.1/#bind 127.0.0.1/' /etc/redis/redis.conf
sudo sed -i 's/# cluster-enabled yes/cluster-enabled yes/' /etc/redis/redis.conf
sudo sed -i 's#logfile ""#logfile "/var/lib/redis/redis.log"#' /etc/redis/redis.conf
sudo sed -i 's#dir ./#dir /var/lib/redis#' /etc/redis/redis.conf
cat >redis.service <<EOF
[Unit]
Description=Redis In-Memory Data Store
After=network.target
[Service]
User=redis
Group=redis
ExecStart=/usr/local/bin/redis-server /etc/redis/redis.conf
ExecStop=/usr/local/bin/redis-cli shutdown
Restart=always
[Install]
WantedBy=multi-user.target
EOF
sudo cp redis.service /etc/systemd/system/redis.service
sudo adduser --system --group --no-create-home redis
sudo mkdir /var/lib/redis
sudo chown redis:redis /var/lib/redis
sudo chmod 770 /var/lib/redis
sudo systemctl start redis
sudo systemctl status redis
或者直接执行: wget -O - https://gitee.com/power/repo/raw/master/redis-cluster.sh | sh
脚本会下载编译Redis,并安装为systemd服务,端口为6379,监听任意地址。日志文件是/var/lib/redis/redis.log
集群
在任一虚拟机执行命令:
sudo redis-cli --cluster create --cluster-replicas 1 192.168.1.11:6379 192.168.1.12:6379 192.168.1.13:6379 192.168.1.14:6379 192.168.1.15:6379 192.168.1.16:6379
将会创建
3个master节点:192.168.1.11:6379 192.168.1.12:6379 192.168.1.13:6379
3个slave节点:192.168.1.14:6379 192.168.1.15:6379 192.168.1.16:6379
测试
redis-cli --cluster check 127.0.0.1:6379
在节点1进入redis-cli -c
,执行命令:
SET key test
在节点2进入redis-cli -c
,执行命令:
GET key
客户端
Spring Boot配置:
spring.redis.cluster.nodes=192.168.1.11:6379, 192.168.1.12:6379, 192.168.1.13:6379