redis测试集群部署手顺
官网文档; http://www.redis.cn/topics/cluster-tutorial.html
安装步骤:
-
进入/opt文件夹,创建cluster-test文件夹
cd /opt
mkdir cluster-test -
进入cluster-test文件夹,创建7000 7001 7002 7003 7004 7005文件夹,如下所示
cd cluster-test
mkdir 7000 7001 7002 7003 7004 7005 -
在7000 7001 7002 7003 7004 7005文件中创建redis.config文件,这里提供一个拷贝方法
i. 第一步在文件中创建文件
touch redis.config
ii. 赋值方法,这里需要将700X改为需要复制文件的目录
cp redis.config /opt/ cluster-test/7001
- 编辑redis.config
vi redis.config
按下i ,进入编辑模式
port 7000 换为每个文件夹名
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
protected-mode no 关闭保护模式
daemonize yes 默认后台运行这样就没有启动页了
- 下载redis-unstable.tar.gz
官网下载地址:http://www.redis.cn/download.html
但是这里我进不去
所以我找的别人网页进去下载的
网址:https://blog.csdn.net/weixin_30666753/article/details/96354166
- 编辑redis-server 可执行文件
要先安装make命令 yum –y install make
进入redis-unstable
执行make
make时报如下错误:
zmalloc.h:50:31: error: jemalloc/jemalloc.h: No such file or directory
zmalloc.h:55:2: error: #error “Newer version of jemalloc required”
make[1]: *** [adlist.o] Error 1
make[1]: Leaving directory `/data0/src/redis-2.6.2/src’
make: *** [all] Error 2
执行:make MALLOC=libc
但是我没好使。最后我是删除我当前解压的文件,重新解压文件,再次去执行make 就好了
make之后,会出现一句提示
Hint: To run ‘make test’ is a good idea 😉
但是不测试,通常是可以使用的。若我们运行make test ,会有如下提示
[devnote@devnote src]$ make test
You need tcl 8.5 or newer in order to run the Redis test
make: ***[test] Error_1
解决办法是用yum安装tcl8.5(或去tcl的官方网站http://www.tcl.tk/下载8.5版本,并参考官网介绍进行安装)
yum install tcl
-
安装redis
执行make PREFIX=/root/tools/redis/ install 这里PREFIX 是你redis-server等文件里的路径
你可以不要这个PREFIX,直接执行make install 这样生成的文件就在你redis-unstable/src下
拷贝redis-server到cluster-test文件中,也就是7000 7001 … 文件外一层
执行:cp redis-server /opt/cluster-test -
编写shell脚本,启动6个redis
iii. 创建startAll.sh脚本文件
Touch startAll.sh
vi startAll,sh
内容
cd /opt/cluster-test/7000/ 创建的文件目录
…/redis-server ./redis.config //redis
cd /opt/cluster-test/7001
…/redis-server ./redis.config
cd /opt/cluster-test/7002
…/redis-server ./redis.config
cd /opt/cluster-test/7003
…/redis-server ./redis.config
cd /opt/cluster-test/7004
…/redis-server ./redis.config
cd /opt/cluster-test/7005
…/redis-server ./redis.config -
创建集群命令如下:进入任意一个redis实例,此处进入redis01目录下:
注意:后续如果需要使用java代码连接redis集群,则创建集群时使用IP地址
./redis-cli --cluster create 192.168.142.129:7000 192.168.142.129:7001 192.168.142.129:7002 192.168.142.129:7003 192.168.142.129:7004 192.168.142.129:7005 --cluster-replicas 1
./redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 192.168.142.129:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 --cluster-replicas 1
或者将127.0.0.1替换为虚拟机ip()
出现
[OK] All 16384 slots covered
代表成功
-
测试集群状态:
./redis-cli -h 192.168.142.129 -p 7001 -c( -h表示host ip,-p表示端口,-c表示要连接的是集群的节点) -
关闭防火墙,否则会拦截外部请求
Systemctl status firewalld 查看防火墙状态
Systemctl start firewalld 开启防火墙
Systemctl stop firewalld 关闭防火墙 -
停止redis 进入redis后shutDown命令
-
java连接集群测试代码
@Test
public void testCluster(){
try { Set<HostAndPort> nodes = new HashSet<>();
String host = "192.168.142.129";
nodes.add(new HostAndPort(host,7000));
nodes.add(new HostAndPort(host,7001));
nodes.add(new HostAndPort(host,7002));
nodes.add(new HostAndPort(host,7003));
nodes.add(new HostAndPort(host,7004));
nodes.add(new HostAndPort(host,7005));
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(15);
config.setMinIdle(3);
config.setMaxTotal(200);
JedisCluster cluster = new JedisCluster(nodes,config);
cluster.hset("name","20162544","zhujuan");
String name = cluster.hget("name","20162544");
System.out.println(name);
cluster.close();
} catch (IOException e) {
e.printStackTrace();
}
}
代码:redisdemo
不需要积分