Home >Web Front-end >JS Tutorial >Learn more about how to use the redis cluster function in node [detailed configuration]
How to use the redis cluster function in node? The following article will give you a detailed explanation of the redis cluster configuration based on node. I hope it will be helpful to you!
Regarding the use of redis cluster function in
nodejs, I have not found a relatively complete and easy-to-understand article. I understand the article, so I also took many detours during the development and debugging process.
This article will introduce in detail how to build a redis cluster locally, how to use the cluster on the client, and summarize and explain the problems and errors encountered during the construction process to avoid using it next time When necessary, we will take detours to improve development and work efficiency.
The background for using the cluster is: in Redis singleton mode, with the increase in the number of users and visits, the qps value rises sharply??, and a large number of io
operations lead to a certain amount of occupancy at a certain moment When the cpu (100%)
is full, there is a risk of downtime at any time. At the same time, batch processing of redis and other methods only treat the symptoms but not the root cause, and cannot break through the bottleneck of server performance. Therefore, it is imperative to use a cluster solution or add redis instances.
Cluster generally refers to a server cluster, which is different from a distributed system. It is a collection of many servers that perform the same service together. From the client's perspective, it looks like There is only one server. The cluster can use multiple computers for parallel computing to obtain a very high computing speed. It can also use multiple computers for backup, so that if any machine breaks down, the entire system can still function normally. run. (Before redis3.0, the sentinel mode was generally used, but the configuration of the sentinel is slightly complicated, and the performance and high availability are average)
Since the voting fault tolerance mechanism requires more than half of the nodes to think that a node is down, the node is down, so 2 nodes cannot form a cluster, so the Redis cluster requires at least 3 nodes.
To ensure the high availability of the cluster, each node needs to have a slave node (that is, a backup node), so the Redis
cluster requires at least 6 servers. (Three masters and three slaves, three deposits and three retrievals, high availability, backup)
Of course, it is impossible for us to use so many servers when debugging locally, so we can simulate running locally 6 redis
instances, In fact, the Redis cluster setup in the production environment is basically the same as here.
You can choose to install it on the official website, or you can use the named line Installation
#安装 brew install redis #启动 redis-server #进入redis客户端 redis-cli
First, find the location of the redis configuration file
brew list redis
# Check the location of redis installation cd /opt/homebrew/Cellar/redis/6.2.4
# Enter the folder where the version number is located based on the location open .
# Open the folder Xcode.app
to open homebrew.mxcl.redis.plist
, that is You can find the location where redis.conf
is located, as follows: Create Six service configuration files
cd /opt/homebrew/etc/
(the configuration file directory found in the previous step)
# 需要在 /opt/homebrew/etc/ 路径下 mkdir -p redis/cluster/7000 mkdir -p redis/cluster/7001 mkdir -p redis/cluster/7002 mkdir -p redis/cluster/7003 mkdir -p redis/cluster/7004 mkdir -p redis/cluster/7005
Modify the configuration file
/opt/homebrew/etc/redis.conf
The configuration file under the path does not need to be modified, just copy it to the redis/cluster/7000 created above
directory, and then modify it, the steps are as follows
cd /opt/homebrew/etc/ # 进入配置文件目录 cp redis.conf redis/cluster/7000/7000.conf code redis/cluster/7000/7000.conf # 用编辑器打开或者用vim打开配置文件来进行修改
7000.conf
After that, modify the following properties# Redis端口号(7000-7005每个配置文件都要修改) port 7000 # 开启集群模式运行 cluster-enabled yes # 集群内部配置文件配置文件路径,默认nodes-6379.conf(7000-7005每个配置文件都要修改) cluster-config-file nodes-7000.conf # 节点间通信的超时时间 cluster-node-timeout 5000 # 数据持久化 appendonly yes
cd /opt/homebrew/etc/redis/cluster # 进入配置文件目录 cp 7000/7000.conf 7001/7001.conf cp 7000/7000.conf 7002/7002.conf cp 7000/7000.conf 7003/7003.conf cp 7000/7000.conf 7004/7004.conf cp 7000/7000.conf 7005/7005.conf
7001.conf-7005.conf
The port and cluster-config-file attributes of each configuration fileNote:Each configuration file must be configured differently port and cluster-config-file
values (otherwise the cluster will not take effect), the above are distinguished by ports.
The directory of the configuration file can be found through the find /opt/homebrew -name nodes-7000.conf
command
Since we have configured 6 services, it is impossible to start or stop them one by one. We need to use shell scripts to achieve this
进入/opt/homebrew/etc/redis/cluster
目录,创建start.sh和stop.sh文件
# start.sh 文件 #!/bin/sh redis-server /opt/homebrew/etc/redis/cluster/7000/7000.conf & redis-server /opt/homebrew/etc/redis/cluster/7001/7001.conf & redis-server /opt/homebrew/etc/redis/cluster/7002/7002.conf & redis-server /opt/homebrew/etc/redis/cluster/7003/7003.conf & redis-server /opt/homebrew/etc/redis/cluster/7004/7004.conf & redis-server /opt/homebrew/etc/redis/cluster/7005/7005.conf & # stop.sh 文件 #!/bin/sh redis-cli -p 7000 shutdown & redis-cli -p 7001 shutdown & redis-cli -p 7002 shutdown & redis-cli -p 7003 shutdown & redis-cli -p 7004 shutdown & redis-cli -p 7005 shutdown &
执行./start.sh
或者./stop.sh
来启停服务
执行ps -ef |grep redis
来查看已启动的redis服务
注意: 第一次执行./start.sh需要通过sudo chmod +x start.sh
授权执行权限
redis-cli -p 7000 # 单个客户端启动 redis-server 7000/7000.conf # 启动单个服务端 redis-cli -p 7000 shutdown # 关闭服务端 sudo chmod +x start.sh # 开启脚本执行权限 # 设置redis主从关系(三主三从) redis-cli --cluster create --cluster-replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 cluster nodes #查看集群节点情况(进入某个客户端执行) cluster info #查看集群信息(进入某个客户端执行) 查看所有key值:keys * 删除指定索引的值:del key 清空整个 Redis 服务器的数据:flushall 清空当前库中的所有 key:flushdb
Redis.Cluster提供了在多个Redis节点上自动分片的功能,使用前面搭建好的六个redis服务器,然后在本地启动node redis.js
,就可以测试集群的效果了。ioredis
// redis.js const Redis = require("ioredis"); const cluster = new Redis.Cluster([ { port: 7000, host: "127.0.0.1", }, { port: 7001, host: "127.0.0.1", }, ]); cluster.set("foo", "bar"); cluster.get("foo", (err, res) => { // res === 'bar' });
import Queue from 'bull' // 创建redis队列实例 const instance = new Queue('custom', { prefix : '{myprefix}', createClient(type) { // cluster 集群实例同上 return cluster } }) // 添加数据到redis队列(生产者) instance.add( 'request', { ...params }, { removeOnComplete: false } ).catch(e => { console.error(e) }) // 消费者回调 instance.process('request', 5, async (job, done) => { console.log('获取当前消费的数据:', job.data) // 执行异步操作 await new Promise((resolve)=>resolve()) done() })
使用bull
框架连接ioredis
集群时存在问题: 每次有数据push
到redis
队列时对应的回调函数可能会触发多次,目前无法确定是使用的问题还是框架本身的问题(如果有了解的欢迎大家留言告知)。
替代集群的方案:在不需要数据同步和数据迁移的情况下,可以在客户端使用多个redis
实例,结合Math.random()
使数据平分到其中的一个redis
,从而解决了单个实例硬件(cpu
等)瓶颈的问题。
1、Mac系统下连接redis报错?
控制台错误提示:Could not connect to Redis at 127.0.0.1:6379: Connection refused
原因:服务端没有开启或启动失败
解决办法:需要先启动redis服务端redis-server
参考链接
https://blog.csdn.net/qq_23347459/article/details/104257529
2、客户端启动、读写报错?
错误提示:ClusterAllFailedError: Failed to refresh slots cache.
原因:每个服务下的配置文件中的cluster-config-file属性一致。
处理:修改成唯一的属性值
参考链接1
https://stackoverflow.com/questions/57350961/ioredis-clusterallfailederror-failed-to-refresh-slots-cache
参考2
https://github.com/luin/ioredis/issues/711
3、执行创建主从redis语句失败?
执行语句:redis-cli --cluster create --cluster-replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
提示错误:[ERR] Node 127.0.0.1:7000 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0
原因:执行创建语句时,没有清空数据和重置集群
处理:清空数据和重置集群, 清除rdb和aof文件
参考清除redis数据
https://stackoverflow.com/questions/37206993/redis-server-cluster-not-working
# 以7000端口的服务为例,7001-7005重复以下操作 $redis-cli -p 7000 127.0.0.1:7000> flushall 127.0.0.1:7000> cluster reset 127.0.0.1:7000> exit # 使用find找到rdb和aof文件(也在rdb目录下) find /opt/homebrew -name dump.rdb # 重新执行创建语句成功 redis-cli --cluster create --cluster-replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
redis集群在客户端的使用是非常简单的,相比之下服务端的配置会比较繁琐。
客户端的具体使用只做了一下简单的说明,使用过程中要注意redis数据的同步和迁移等问题。
使用集群虽然能提升服务能力、支持主从复制、哨兵模式、读写分离、平分服务器的压力等特点。但不具备自动容错和恢复功能,如果出现宕机会使部分读写请求失败,降低了系统的可用性。在使用时根据业务情况分析、选择不同的方案。
本文转载自:https://juejin.cn/post/7079928963107127327
作者:tager
更多node相关知识,请访问:nodejs 教程!
The above is the detailed content of Learn more about how to use the redis cluster function in node [detailed configuration]. For more information, please follow other related articles on the PHP Chinese website!