六个Redis实例的配置文件:
redis.conf
port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
/opt/redis
/7000/redis.conf
/7001/redis.conf
/7002/redis.conf
/7003/redis.conf
/7004/redis.conf
/7005/redis.conf
mkdir /opt/redis
cd /opt/redis
mkdir 7000 7001 7002 7003 7004 7005
cat <7000/redis.conf
port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF
cat <7001/redis.conf
port 7001
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF
cat <7002/redis.conf
port 7002
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF
cat <7003/redis.conf
port 7003
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF
cat <7004/redis.conf
port 7004
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF
cat <7005/redis.conf
port 7005
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
EOF
先启动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
执行命令配置集群
# 进入容器执行集群配置命令
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 -c -p 7000
cluster info
cluster nodes
存数据
docker exec -it redis7000 redis-cli -c -p 7000
# 7002
set a aaaaaaaaaaaaa
# 7000
set b bbbbbbbbbbb
# 7001
set c cccccccccccccccc
spring redis api
org.springframework.boot
spring-boot-starter-data-redis
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
@Autowired
private RedisConnectionFactory connectionFactory;
public void test() {
RedisClusterConnection c = connectionFactory.getClusterConnection();
for (int i = 0; i < 100; i++) {
c.set(("k"+i).getBytes(StandardCharsets.UTF_8),
("v"+i).getBytes(StandardCharsets.UTF_8));
}
}
@Autowired
@Qualifier("stringRedisTemplate")
private RedisTemplate t;
public void test() {
ValueOperations o = t.opsForValue();
o.set("hhhh", "vvvv");
o.set("nnnn", "gggg");
}
redis主从复制结构中,当主服务器宕机,哨兵可以监控到服务宕机,在从服务器中选举产生一个新的主服务器。
清理容器(可选)
docker rm -f $(docker ps -aq)
启动主服务器
# --net=host 容器直接使用宿主机的端口,不需要做端口映射
docker run -d --name redis6379 --net=host --restart=always redis
# 进入容器,运行redis客户端
docker exec -it redis6379 redis-cli
# 查看集群信息,默认是主服务器
> info replication
启动两个从服务器
# 启动redis6380容器,作为 redis6379 的从服务器启动
# --port 和 --slaveof 是 redis-server 命令的参数
docker run -d --name redis6380 --net=host --restart=always redis \
redis-server --port 6380 --slaveof 192.168.64.150 6379
# 启动redis6381容器,作为 redis6379 的从服务器启动
docker run -d --name redis6381 --net=host --restart=always redis \
redis-server --port 6381 --slaveof 192.168.64.150 6379
# 查看三个 redis 服务的角色
docker exec -it redis6379 redis-cli
> info replication
docker exec -it redis6380 redis-cli -p 6380
> info replication
docker exec -it redis6381 redis-cli -p 6381
> info replication
哨兵的配置文件
mkdir /opt/sentinel/
cd /opt/sentinel/
# 配置文件中的 "sentinel monitor mymaster 192.168.64.150 6379 2"
# 末尾的 2 表示两个哨兵投票确认主服务器宕机,哨兵才会认为主服务器宕机
cat <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 <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 <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,不会把6379切换成主服务器,而是作为从服务器
docker start redis6379
docker exec -it redis6379 redis-cli
> info replication
创建maven项目 添加jedis相关依赖
redis.clients
jedis
2.9.0
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 sun.awt.util.IdentityLinkedList;
import java.util.List;
public class Test1 {
public static void main(String[] args) {
//参数 1服务器地址列表 2连接池配置对象
List list = new IdentityLinkedList();
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 config = new JedisPoolConfig();
//新建分片连接池
ShardedJedisPool pool = new ShardedJedisPool(config, list);
//从连接池获取数据操作的工具对象
ShardedJedis j = pool.getResource();
//添加数据
for (int i=0 ;i<100 ;i++){
j.set("k"+i,"v"+i);
}
for (int i=0 ;i<100 ;i++){
String s = j.get("k" + i);
System.out.println("获取的数据:"+s);
}
pool.close();
}
}