学习笔记:案例
1 分片访问
docker run -d --name redis7000 -p 7000:6379 redis
docker run -d --name redis7001 -p 7001:6379 redis
docker run -d --name redis7002 -p 7002:6379 redis
# 进入redis容器
docker exec -it redis7000 bash redis-cli
package test;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
import java.util.ArrayList;
import java.util.List;
public class Test1 {
public static void main(String[] args) {
// 服务器的地址列表
List<JedisShardInfo> list= new ArrayList<>();
list.add(new JedisShardInfo("192.168.64.150",7000));
list.add(new JedisShardInfo("192.168.64.150",7001));
list.add(new JedisShardInfo("192.168.64.150",7002));
// 连接池配置对象
GenericObjectPoolConfig cfg = new JedisPoolConfig();
// 连接池配置对象
ShardedJedisPool pool = new ShardedJedisPool(cfg, list);
// 从连接池获取数据操作的工具对象
ShardedJedis j = pool.getResource();
// 从连接池获取数据操作的工具对象
for (int i = 0; i < 100; i++) {
j.set("k"+i, "v"+i);
}
pool.close();
}
}
进入redis 查看呢数据 keys *
2 主从哨兵
学习链接:Docker搭建主从哨兵
启动主服务器
# --net=host 容器直接使用宿主机的端口,不需要做端口映射
docker run -d --name redis6379 --net=host --restart=always redis
# 进入容器,运行redis客户端
docker exec -it redis6379 redis-cli
# 查看集群信息,默认是主服务器
info replication
设置从服务器
6380
docker run -d --name redis6380 --net host redis \
redis-server --port 6380 \
--slaveof 192.168.64.150 6379 # 作为6379的从服务器
6381
docker run -d --name redis6381 --net host redis \
redis-server --port 6381 \
--slaveof 192.168.64.150 6379 # 作为6379的从服务器
查看集群信息
docker exec -it redis6381 redis-cli -p 6381
info replication
在6379添加数据,在6380 get
启动3个哨兵
官方文档地址: https://redis.io/topics/sentinel
配置文件:
mkdir /opt/sentinel/
cd /opt/sentinel/
# 配置文件中的 "sentinel monitor mymaster 192.168.64.150 6379 2"
# 末尾的 2 表示两个哨兵投票确认主服务器宕机,哨兵才会认为主服务器宕机
cat <<EOF >5000.conf
port 5000
sentinel monitor mymaster 192.168.64.150 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
sentinel parallel-syncs mymaster 1
EOF
cat <<EOF >5001.conf
port 5001
sentinel monitor mymaster 192.168.64.150 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
sentinel parallel-syncs mymaster 1
EOF
cat <<EOF >5002.conf
port 5002
sentinel monitor mymaster 192.168.64.150 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
sentinel parallel-syncs mymaster 1
EOF
启动
docker run -d --name sentinel5000 \
-v /opt/sentinel/5000.conf:/sentinel.conf \
--net=host \
redis redis-sentinel /sentinel.conf
docker run -d --name sentinel5001 \
-v /opt/sentinel/5001.conf:/sentinel.conf \
--net=host \
redis redis-sentinel /sentinel.conf
docker run -d --name sentinel5002 \
-v /opt/sentinel/5002.conf:/sentinel.conf \
--net=host \
redis redis-sentinel /sentinel.conf
# 进入一个哨兵容器,查看它监控的主从服务器和其他哨兵
docker exec -it sentinel5000 redis-cli -p 5000
> sentinel master mymaster
> sentinel slaves mymaster
> sentinel sentinels mymaster
模拟主服务器宕机
# 停止主服务器
docker stop redis6379
# 在哨兵日志中查看服务器切换日志: +switch-master mymaster 192.168.64.150 6379 192.168.64.150 6381
docker logs sentinel5000
# 查看 6380 和 6381 服务器的角色变化
docker exec -it redis6380 redis-cli -p 6380
> info replication
docker exec -it redis6381 redis-cli -p 6381
> info replication
重启6379 查看
3 Docker 搭建集群
学习链接: Docker 搭建集群
了解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
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 --cluster create \
192.168.126.125:7000 192.168.126.125:7001 \
192.168.126.125:7002 192.168.126.125:7003 \
192.168.126.125:7004 192.168.126.125:7005 \
--cluster-replicas 1
查看集群信息
docker exec -it redis7000 redis-cli -c -p 7000
cluster info
cluster nodes
存数据 配置集群后可以进入容器存取数据
docker exec -it redis7000 redis-cli -p 7000 -c
api 测试
application.yml
spring:
redis:
cluster:
nodes:
- 192.168.126.125:7000
- 192.168.126.125:7001
- 192.168.126.125:7002
- 192.168.126.125:7003
- 192.168.126.125:7004
- 192.168.126.125:7005
package cn.tedu.rediscluster;
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
public class Test1 {
@Autowired
private RedisConnectionFactory f;
@Test
public void test1() {
RedisClusterConnection c = f.getClusterConnection();
for (int i = 0; i <100; i++) {
String k = "k"+i;
String v = "v"+i;
c.set(k.getBytes(), v.getBytes());
}
}
}
查看存入的数据
报错
Cannot retrieve initial cluster partitions from initial URIs
直接关闭防火墙即可
# 关闭防火墙
systemctl stop firewalld.service
# 禁止防火墙开机启动
systemctl disable firewalld.service