5. 集群架构特点
特点
(1)所有的redis节点彼此互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽.
(2)节点的fail是通过集群中超过半数的节点检测失效时才生效.
(3)客户端与redis节点直连,不需要中间proxy层.客户端不需要连接集群所有节点,连接集群中任何一个可用节点即可
(4)redis-cluster把所有的物理节点映射到[0-16383]slot上,cluster 负责维护node<->slot<->value
(5)Redis 集群中内置了 16384 个哈希槽,当需要在 Redis 集群中放置一个 key-value 时,redis 先对 key 使用 crc16 算法算出一个结果,然后把结果对 16384 求余数,这样每个 key 都会对应一个编号在 0-16383 之间的哈希槽,redis 会根据节点数量大致均等的将哈希槽映射到不同的节点
(6)一个集群最多可以有16384个master,不过官方建议master的最大数量是1000个;
(7)hash tag的计算规则是:
取一对大括号{}之间的字符进行计算,如果key存在多对大括号,那么就取第一个左括号和第一个右括号之间的字符。如果大括号之前没有字符,
则会对整个字符串进行计算。
{Jackeyzhe}.following和{Jackeyzhe}.follower这两个key都是计算Jackey的hash值
foo{{bar}}这个key就会对{bar进行hash计算
follow{}{Jackey}会对整个字符串进行计算
6. 伪分布式安装及代码测试
6.1 实现
可以再一台服务器上面同时运行6个redis实例,模拟完全分布式集群。需要修改每个redis实例中的端口号,同时设置cluster-enabled的值为yes
6.2 安装
1. 下载redis5.0.2二进制安装包
wget http://download.redis.io/releases/redis-5.0.2.tar.gz
2. 解压到/opt目录下
tar -xzvf redis-5.0.2.tar.gz -C /opt
3. 编译
cd /opt/redis-5.0.2 && make
4. 指定安装位置
make install PREFIX=/usr/local/redis_cluster/redis7001
5. 拷贝安装目录下配置文件到 /usr/local/redis_cluster/redis7001/etc
mkdir /usr/local/redis_cluster/redis7001/etc
cp /opt/redis-5.0.2/redis.conf /usr/local/redis_cluster/redis7001/etc
6. 修改配置文件 /usr/local/redis_cluster/redis7001/etc/redis.conf
vi /usr/local/redis_cluster/redis7001/etc/redis.conf
# 关闭保护模式
protected-mode no
# 以守护进程后台模式运行
daemonize yes
# 绑定本机ip
bind 172.18.203.30
# 修改端口
port 7001
# redis进程文件
pidfile /usr/local/redis_cluster/redis7001/redis_7001.pid
# 日志文件
logfile /usr/local/redis_cluster/redis7001/log/redis_7001.log
# 快照数据存放目录,一定是目录
dir /usr/local/redis_cluster/redis7001/data/
# 启用集群
cluster-enabled yes
7. 新建data和log目录
mkdir -p data log
8. 安装redis7002
8.1 拷贝安装目录
cp -r /usr/local/redis_cluster/redis7001 /usr/local/redis_cluster/redis7002
8.2 编辑配置文件
vi /usr/local/redis_cluster/redis7002/etc/redis.conf
# 修改端口
port 7002
# redis进程文件
pidfile /usr/local/redis_cluster/redis7002/redis_7002.pid
# 日志文件
logfile /usr/local/redis_cluster/redis7002/log/redis_7002.log
# 快照数据存放目录,一定是目录
dir /usr/local/redis_cluster/redis7002/data/
9. 仿照步骤8配置安装redis7003,redis7004,redis7005,redis7006
10. 编写启动脚本并赋予执行权限
vi start_redis_cluster.sh
/usr/local/redis_cluster/redis7001/bin/redis-server /usr/local/redis_cluster/redis7001/etc/redis.conf
/usr/local/redis_cluster/redis7002/bin/redis-server /usr/local/redis_cluster/redis7002/etc/redis.conf
/usr/local/redis_cluster/redis7003/bin/redis-server /usr/local/redis_cluster/redis7003/etc/redis.conf
/usr/local/redis_cluster/redis7004/bin/redis-server /usr/local/redis_cluster/redis7004/etc/redis.conf
/usr/local/redis_cluster/redis7005/bin/redis-server /usr/local/redis_cluster/redis7005/etc/redis.conf
/usr/local/redis_cluster/redis7006/bin/redis-server /usr/local/redis_cluster/redis7006/etc/redis.conf
chmod u+x start_redis_cluster.sh
11. 编写停止脚本并赋予执行权限
vi stop_redis_cluster.sh
/usr/local/redis_cluster/redis7001/bin/redis-cli -p 7001 shutdown
/usr/local/redis_cluster/redis7002/bin/redis-cli -p 7002 shutdown
/usr/local/redis_cluster/redis7003/bin/redis-cli -p 7003 shutdown
/usr/local/redis_cluster/redis7004/bin/redis-cli -p 7004 shutdown
/usr/local/redis_cluster/redis7005/bin/redis-cli -p 7005 shutdown
/usr/local/redis_cluster/redis7006/bin/redis-cli -p 7006 shutdown
chmod u+x stop_redis_cluster.sh
12. 启动所有redis节点
sh ./start_redis_cluster.sh
13. 查看是否启动成功
ps aux | grep redis
14. 使用5.0新特性redis-cli命令创建集群
/usr/local/redis_cluster/redis7001/bin/redis-cli --cluster create 172.18.203.30:7001 172.18.203.30:7002 172.18.203.30:7003 172.18.203.30:7004 172.18.203.30:7005 172.18.203.30:7006 --cluster-replicas 1
ps:如愿意,可以对Redis 4集群使用Redis 5 Redis -cli而不会出现问题
15. 恭喜配置成功,提示如下
[OK] All 16384 slots covered.
6.3 代码
6.3.1 Jedis cluster
pom依赖
<!--jedis 客户端-->
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.0.0</version>
</dependency>
测试代码
public static void main(String[] args) {
int port = 6379;
// 创建set集合
Set<HostAndPort> nodes = new HashSet<>();
// 将每一个节点的ip地址和端口号添加到集合中
nodes.add(new HostAndPort("172.18.203.30", 7001));
nodes.add(new HostAndPort("172.18.203.30", 7002));
nodes.add(new HostAndPort("172.18.203.30", 7003));
nodes.add(new HostAndPort("172.18.203.30", 7004));
nodes.add(new HostAndPort("172.18.203.30", 7005));
nodes.add(new HostAndPort("172.18.203.30", 7006));
// 创建jedisCluster对象,需要将创建的redis集合列表作为参数传递
JedisCluster jedisCluster = new JedisCluster(nodes);
// 通过集群对象操作redis
jedisCluster.set("address", "北京市长安街");
String value = jedisCluster.get("address");
// 打印结果
System.out.println(value);
// 系统关闭前,关闭jedisCluster对象
jedisCluster.close();
}
6.3.2 Lettuce cluster
pom依赖
<!-- https://mvnrepository.com/artifact/io.lettuce/lettuce-core -->
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
测试代码
public static void main(String[] args) {
List<RedisURI> list = new ArrayList<>();
list.add(RedisURI.create("redis://172.18.203.30:7001"));
list.add(RedisURI.create("redis://172.18.203.30:7002"));
list.add(RedisURI.create("redis://172.18.203.30:7003"));
list.add(RedisURI.create("redis://172.18.203.30:7004"));
list.add(RedisURI.create("redis://172.18.203.30:7005"));
list.add(RedisURI.create("redis://172.18.203.30:7006"));
RedisClusterClient client = RedisClusterClient.create(list);
StatefulRedisClusterConnection<String, String> connect = client.connect();
/**
* 同步执行命令
*/
RedisAdvancedClusterCommands<String, String> commands = connect.sync();
commands.set("hello","hello world2");
String str = commands.get("hello");
System.out.println(str);
/**
* 异步执行命令
*/
RedisAdvancedClusterAsyncCommands<String,String> asyncCommands = connect.async();
RedisFuture<String> future = asyncCommands.get("hello");
try {
String str1 = future.get();
System.out.println(str1);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
connect.close();
client.shutdown();
}
网友评论