僞集羣搭建練習:在一臺虛擬機上模擬多臺虛擬機的操作與多臺是一樣的但是省資源。
Linux 的操作
1 安裝
複製之前先要確保,沒有快照文件:dump.rdb。
[root@localhost bin]# ll
total 13896
-rw-r--r--. 1 root root 111 Aug 28 19:58 dump.rdb
-rwxr-xr-x. 1 root root 4167882 Aug 28 03:28 redis-benchmark
-rwxr-xr-x. 1 root root 16463 Aug 28 03:28 redis-check-aof
-rwxr-xr-x. 1 root root 37695 Aug 28 03:28 redis-check-dump
-rwxr-xr-x. 1 root root 4256652 Aug 28 03:28 redis-cli
-rw-r--r--. 1 root root 41404 Aug 28 04:03 redis.conf
lrwxrwxrwx. 1 root root 12 Aug 28 03:28 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 5686485 Aug 28 03:28 redis-server
複製多個bin
[root@localhost local]# mkdir redis-cluster
……
drwxr-xr-x. 3 root root 4096 Aug 28 03:28 redis
drwxr-xr-x. 2 root root 4096 Aug 28 20:06 redis-cluster
……
配置文件
[root@localhost redis01]# vim redis.conf
1 修改端口號
# Accept connections on the specified port, default is 6379.
# If port 0 is specified Redis will not listen on a TCP socket.
port 7001
同一個虛擬機要用不同端口號!
2 打開集羣
因爲是註釋不好找,先查找
/cluster
取消註釋即可
# Normal Redis instances can't be part of a Redis Cluster; only nodes that are
# started as cluster nodes can. In order to start a Redis instance as a
# cluster node enable the cluster support uncommenting the following:
#
# cluster-enabled yes
cluster-enabled yes
用批處理文件分別開啓服務
[root@localhost redis-cluster]# vim start-all-redis.sh
cd redis01
./redis-server redis.conf
cd ..
cd redis02
./redis-server redis.conf
cd ..
cd redis03
./redis-server redis.conf
cd ..
cd redis04
./redis-server redis.conf
cd ..
cd redis05
./redis-server redis.conf
cd ..
cd redis06
./redis-server redis.conf
cd ..
:wq
[root@localhost redis-cluster]# ll
total 28
drwxr-xr-x. 2 root root 4096 Aug 28 20:27 redis01
drwxr-xr-x. 2 root root 4096 Aug 28 20:33 redis02
drwxr-xr-x. 2 root root 4096 Aug 28 20:34 redis03
drwxr-xr-x. 2 root root 4096 Aug 28 20:34 redis04
drwxr-xr-x. 2 root root 4096 Aug 28 20:29 redis05
drwxr-xr-x. 2 root root 4096 Aug 28 20:35 redis06
-rw-r--r--. 1 root root 193 Aug 28 20:44 start-all-redis.sh
進入其中一個文件夾發現沒變綠,應添加可執行權限
[root@localhost redis-cluster]# chmod +x start-all-redis.sh
一般都是權限問題吧,查看進程,確認都開啓了
[root@localhost redis-cluster]# ps aux|grep redis
root 2416 0.1 0.3 33936 1928 ? Ssl 20:51 0:07 ./redis-server *:7001 [cluster]
root 2486 0.1 0.3 33936 1928 ? Ssl 21:17 0:05 ./redis-server *:7002 [cluster]
root 2534 0.1 0.3 33936 1964 ? Ssl 22:27 0:00 ./redis-server *:7003 [cluster]
root 2536 0.0 0.3 33936 1968 ? Ssl 22:27 0:00 ./redis-server *:7004 [cluster]
root 2540 0.0 0.3 33936 1964 ? Ssl 22:27 0:00 ./redis-server *:7005 [cluster]
root 2544 0.0 0.3 33936 1964 ? Ssl 22:27 0:00 ./redis-server *:7006 [cluster]
先安裝ruby
[root@localhost redis-cluster]# yum install ruby
……
……
……
Complete!
再安裝redis-3.0.0.gem(因爲redis-3.0.0.gem依賴ruby腳本)
[root@localhost redis]# ll
total 1392
drwxrwxr-x. 6 root root 4096 Apr 1 2015 redis-3.0.0
-rw-r--r--. 1 root root 57856 Aug 28 22:45 redis-3.0.0.gem
-rw-r--r--. 1 root root 1358081 Aug 28 02:11 redis-3.0.0.tar.gz
[root@localhost redis]# gem install redis-3.0.0.gem
Successfully installed redis-3.0.0
1 gem installed
Installing ri documentation for redis-3.0.0...
Installing RDoc documentation for redis-3.0.0...
[root@localhost redis]#
複製腳本到集羣
腳本在源碼裏 的一個 .rb文件
[root@localhost redis]# cd /root/redis/redis-3.0.0/src
[root@localhost src]# ll *.rb
-rwxrwxr-x. 1 root root 48141 Apr 1 2015 redis-trib.rb
只有一個文件複製後如下:
[root@localhost src]# cp /root/redis/redis-3.0.0/src/*.rb /usr/local/redis-cluster/
[root@localhost src]# cd /usr/local/redis-cluster
[root@localhost redis-cluster]# ll
total 76
drwxr-xr-x. 2 root root 4096 Aug 28 20:51 redis01
drwxrwxrwx. 2 root root 4096 Aug 28 21:17 redis02
drwxrwxrwx. 2 root root 4096 Aug 28 22:27 redis03
drwxrwxrwx. 2 root root 4096 Aug 28 22:27 redis04
drwxrwxrwx. 2 root root 4096 Aug 28 22:27 redis05
drwxrwxrwx. 2 root root 4096 Aug 28 22:27 redis06
-rwxr-xr-x. 1 root root 48141 Aug 28 23:15 redis-trib.rb
-rwxrwxrwx. 1 root root 457 Aug 28 21:17 start-all-redis.sh
[root@localhost redis-cluster]#
2 啓動
服務端:
運行rb命令,是否同意如下搭建,填yes
[root@localhost redis-cluster]# ./redis-trib.rb create --replicas 1 192.168.25.129:7001 192.168.25.129:7002 192.168.25.129:7003 192.168.25.129:7004 192.168.25.129:7005 192.168.25.129:7006
>>> Creating cluster
Connecting to node 192.168.25.129:7001: OK
Connecting to node 192.168.25.129:7002: OK
Connecting to node 192.168.25.129:7003: OK
Connecting to node 192.168.25.129:7004: OK
Connecting to node 192.168.25.129:7005: OK
Connecting to node 192.168.25.129:7006: OK
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.25.129:7001
192.168.25.129:7002
192.168.25.129:7003
Adding replica 192.168.25.129:7004 to 192.168.25.129:7001
Adding replica 192.168.25.129:7005 to 192.168.25.129:7002
Adding replica 192.168.25.129:7006 to 192.168.25.129:7003
M: 9a87db88f4dbccb6c1c7e1bc8ea48db95312c149 192.168.25.129:7001
slots:0-5460 (5461 slots) master
M: e539f4aa26f4ac22956d9aea76a5cb897e4884e8 192.168.25.129:7002
slots:5461-10922 (5462 slots) master
M: 9309491a6a07c2c2584009f4fa28d7d01adeeab0 192.168.25.129:7003
slots:10923-16383 (5461 slots) master
S: 6617e552e6bf014be3359ceb639b41365d352805 192.168.25.129:7004
replicates 9a87db88f4dbccb6c1c7e1bc8ea48db95312c149
S: 7d60df145c1183724b803f722aaa1c90b821f2e9 192.168.25.129:7005
replicates e539f4aa26f4ac22956d9aea76a5cb897e4884e8
S: c14e15af1c47aebf71db0c7f9a3ba67385a0d1cb 192.168.25.129:7006
replicates 9309491a6a07c2c2584009f4fa28d7d01adeeab0
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join.....
>>> Performing Cluster Check (using node 192.168.25.129:7001)
M: 9a87db88f4dbccb6c1c7e1bc8ea48db95312c149 192.168.25.129:7001
slots:0-5460 (5461 slots) master
M: e539f4aa26f4ac22956d9aea76a5cb897e4884e8 192.168.25.129:7002
slots:5461-10922 (5462 slots) master
M: 9309491a6a07c2c2584009f4fa28d7d01adeeab0 192.168.25.129:7003
slots:10923-16383 (5461 slots) master
M: 6617e552e6bf014be3359ceb639b41365d352805 192.168.25.129:7004
slots: (0 slots) master
replicates 9a87db88f4dbccb6c1c7e1bc8ea48db95312c149
M: 7d60df145c1183724b803f722aaa1c90b821f2e9 192.168.25.129:7005
slots: (0 slots) master
replicates e539f4aa26f4ac22956d9aea76a5cb897e4884e8
M: c14e15af1c47aebf71db0c7f9a3ba67385a0d1cb 192.168.25.129:7006
slots: (0 slots) master
replicates 9309491a6a07c2c2584009f4fa28d7d01adeeab0
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@localhost redis-cluster]#
客戶端
連接
[root@localhost redis-cluster]# redis01/redis-cli -p 7006 -c
127.0.0.1:7006> set key1 yup1212
-> Redirected to slot [9189] located at 192.168.25.129:7002
OK
192.168.25.129:7002> keys *
1) "key1"
192.168.25.129:7002> set key2 123
-> Redirected to slot [4998] located at 192.168.25.129:7001
OK
192.168.25.129:7001>
加-c的作用,如果不加-c在需要跳槽的時候報錯
[root@localhost redis-cluster]# redis01/redis-cli -p 7006
127.0.0.1:7006> set key 123
(error) MOVED 12539 192.168.25.129:7003
127.0.0.1:7006> quit
[root@localhost redis-cluster]#
3 常用命令
查看當前集羣狀態:
192.168.25.129:7001> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_sent:2859
cluster_stats_messages_received:2859
192.168.25.129:7001>
查看集羣節點:
192.168.25.129:7001> cluster nodes
e539f4aa26f4ac22956d9aea76a5cb897e4884e8 192.168.25.129:7002 master - 0 1535525342658 2 connected 5461-10922
9a87db88f4dbccb6c1c7e1bc8ea48db95312c149 192.168.25.129:7001 myself,master - 0 0 1 connected 0-5460
9309491a6a07c2c2584009f4fa28d7d01adeeab0 192.168.25.129:7003 master - 0 1535525345683 3 connected 10923-16383
6617e552e6bf014be3359ceb639b41365d352805 192.168.25.129:7004 slave 9a87db88f4dbccb6c1c7e1bc8ea48db95312c149 0 1535525346691 4 connected
7d60df145c1183724b803f722aaa1c90b821f2e9 192.168.25.129:7005 slave e539f4aa26f4ac22956d9aea76a5cb897e4884e8 0 1535525344674 5 connected
c14e15af1c47aebf71db0c7f9a3ba67385a0d1cb 192.168.25.129:7006 slave 9309491a6a07c2c2584009f4fa28d7d01adeeab0 0 1535525343665 6 connected
192.168.25.129:7001>
Java
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestJedisSpring {
@Test
public void testJedisClientPool() throws Exception {
//初始化spring容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-redis.xml");
//從容器中獲得JedisClient對象
JedisClient jedisClient = applicationContext.getBean(JedisClient.class);
//使用JedisClient對象操作redis
jedisClient.set("jedisclient", "mytest");
String result = jedisClient.get("jedisclient");
System.out.println(result);
}
}