序言
首先说明下redis支持事务,同时redis事务能够跟spring事务一起使用@Transactional来进行控制.(但是集群不支持事务否则会报错 :MULTI is currently not supported in cluster mode.)
其次redis事务不支持并发操作一个属性或者变量.
再次redis集群很好搭建~同时会搭建该redis的可视化监控.
另外redis集群可以手动设置主从,不需要程序帮你设置.
搭建流程
具体操作如下.搭建6个服务,形成3主3备的集群.
[root@[email protected] cuiyaonan]# wget http://download.redis.io/releases/redis-5.0.5.tar.gz
[root@[email protected] cuiyaonan]# mkdir redis
[root@[email protected] cuiyaonan]# mv redis-5.0.5.tar.gz ./redis
[root@[email protected] cuiyaonan]# cd redis
[root@[email protected] redis]# tar -zxvf redis-5.0.5.tar.gz
[root@[email protected] redis]# cd redis-5.0.5/
[root@[email protected] redis-5.0.5]# make
[root@[email protected] redis-5.0.5]# make install PREFIX=/usr/local/redis
[root@[email protected] redis-5.0.5]# mkdir /home/cuiyaonan/redis/7001
[root@[email protected] redis-5.0.5]# mkdir /home/cuiyaonan/redis/7002
[root@[email protected] redis-5.0.5]# mkdir /home/cuiyaonan/redis/7003
[root@[email protected] redis-5.0.5]# mkdir /home/cuiyaonan/redis/7004
[root@[email protected] redis-5.0.5]# mkdir /home/cuiyaonan/redis/7005
[root@[email protected] redis-5.0.5]# mkdir /home/cuiyaonan/redis/7006
[root@[email protected] redis-5.0.5]# cp redis.conf ../7001/
[root@[email protected] redis-5.0.5]# cp redis.conf ../7002/
[root@[email protected] redis-5.0.5]# cp redis.conf ../7003/
[root@[email protected] redis-5.0.5]# cp redis.conf ../7004/
[root@[email protected] redis-5.0.5]# cp redis.conf ../7005/
[root@[email protected] redis-5.0.5]# cp redis.conf ../7006/
针对每个实例初始化对应conf文件
daemonize yes //redis后台运行
pidfile /home/cuiyaonan/redis/7001/7001.pid //pidfile文件对应7001,7002,7003,7004
port 7001 //端口7000,7002,7003,7004
cluster-enabled yes //开启集群 把注释#去掉
cluster-config-file /home/cuiyaonan/redis/7001/nodes-7001.conf //集群的配置 配置文件首次启动自动生成 7001,7001,7002
cluster-node-timeout 5000 //请求超时 设置5秒够了
appendonly yes //aof日志开启 有需要就开启,它会每次写操作都记录一条日志
protected-mode no //此时外部网络可以直接访问
bind 192.168.2.64 //表示监听ip
修改完conf就可以启动redis实例了.只有实例启动后,才能在搭建集群(这个流程有点怪).当然你也可以写个脚本一起启动.(命令在/usr/local/bin里执行)
[root@[email protected] bin]# ./redis-server /home/cuiyaonan/redis/7001/redis.conf
[root@[email protected] bin]# ./redis-server /home/cuiyaonan/redis/7002/redis.conf
[root@[email protected] bin]# ./redis-server /home/cuiyaonan/redis/7003/redis.conf
[root@[email protected] bin]# ./redis-server /home/cuiyaonan/redis/7004/redis.conf
[root@[email protected] bin]# ./redis-server /home/cuiyaonan/redis/7005/redis.conf
[root@[email protected] bin]# ./redis-server /home/cuiyaonan/redis/7006/redis.conf
启动成功后开启集群.--
[root@[email protected] bin]# redis-cli --cluster create 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 127.0.0.1:7006 --cluster-replicas 1
1) create
表示创建一个redis集群。
2) --cluster-replicas 1
表示为集群中的每一个主节点指定一个从节点,即一比一的复制。\
运行过程中,会有个提示,输入yes回车即可。从屏幕输出,可以很容易地看出哪些是主(master)节点,哪些是从(slave)节点.
故此redis集群搭建完成.
如下的命令是查看集群的部署情况,能看到哪些是主机,哪些是备用主机
[root@localhost bin]# redis-cli --cluster info 127.0.0.1:7001
redis监控工具安装
网上关于redis监控的工具的优缺点如下所示.(这个工具很久没有更新了,安装的时候肯定会有很多版本冲突.建议还是使用命令来查看服务的情况,同时如果出了新的开源Redis监控,也请你告知我[email protected])
工具 | 优点 | 缺点 |
---|---|---|
redis-stat | 基于redis的INFO 命令,较使用MONITOR 的监控工具对redis服务器性能影响较小 |
如果同时监控多个redis实例,不能单独显示每一个实例的数据信息,貌似是总和。 |
RedisLive | MONITOR 命令对于 Redis 实例的性能有较大影响 | RedisLive 支持同时监控多个 Redis 实例 |
由于Redis-Live是用Python写的,所以先检测Python环境,要求必须是2.7以上。CentOS7默认就是2.7.5,所以省去这一步。但是还是要检查下是不是?看到如下的内容就满足了继续执行.
[root@localhost ~]# python
Python 2.7.5 (default, Oct 30 2018, 23:45:53)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
继续按如下操作执行
[root@[email protected] cuiyaonan]# yum -y install epel-release
[root@[email protected] cuiyaonan]# yum -y install python-pip
[root@[email protected] cuiyaonan]# pip install --upgrade pip
[root@[email protected] cuiyaonan]# pip install tornado
[root@[email protected] cuiyaonan]# pip install redis
[root@[email protected] cuiyaonan]# pip install python-dateutil
[root@[email protected] cuiyaonan]# git clone https://github.com/nkrode/RedisLive.git
[root@localhost src]# pwd
/home/cuiyaonan/RedisLive/src
[root@localhost src]# vi redis-live.conf
{
"RedisServers":
[
{
"server": "192.168.2.64",
"port" : 7001
},
{
"server": "192.168.2.64",
"port" : 7002
},
{
"server": "192.168.2.64",
"port" : 7003
},
{
"server": "192.168.2.64",
"port" : 7004
},
{
"server": "192.168.2.64",
"port" : 7005
},
{
"server": "192.168.2.64",
"port" : 7006
}
],
"DataStoreType" : "redis",
"RedisStatsServer":
{
"server" : "192.168.2.64",
"port" : 7001
},
"SqliteStatsStore" :
{
"path": "to your sql lite file"
}
}
启动监控服务,每60秒监控一次
./redis-monitor.py --duration=60
再次开启一个终端,进入/root/RedisLive/src目录,启动web服务
./redis-live.py