以前的Redis集羣搭建(在虛擬機上)文檔,放博客上以避免丟失,僅供參考java
Redis是基於內存,也能夠基於磁盤持久化nosql數據庫,使用c語言開發。node
數據存儲結構:key-valueredis
Redis使用c語言開發,須要使用gcc編譯程序進行編譯。spring
1) 安裝gccsql
安裝命令:yum -y install gcc數據庫
2) 安裝上傳文件插件spring-mvc
使用rz命令上傳redis安裝包,到/usr/local/soft目錄下:ruby
[root@localhost soft]# ll服務器
-rw-r--r--. 1 root root 1358081 May 14 2015 redis-3.0.0.tar.gzmvc
解壓命令:tar -zxvf redis-3.0.0.tar.gz
1) 編譯
a) 進入redis解壓目錄:執行編譯命令: make
b) 執行編譯程序:生成編譯文件在src目錄下
2) 安裝
a) 命令:make install PREFIX=/usr/local/soft/redis
b) 進入redis解壓目錄:執行安裝命令
[root@localhost redis-3.0.0]# make install PREFIX=/usr/local/soft/redis
cd src && make install
make[1]: Entering directory `/usr/local/soft/redis-3.0.0/src'
Hint: It's a good idea to run 'make test' ;)
INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
make[1]: Leaving directory `/usr/local/soft/redis-3.0.0/src'
啓動命令:./redis-server
特色:默認啓動前臺服務,進程一種阻塞,不能直接退出,使用客戶端進行登陸。
修改redis配置文件,redis.conf配置文件,此時bin安裝目錄沒有配置文件,須要從解壓目錄中拷貝一份配置文件便可。
1) 拷貝redis.conf配置文件
a) redis.conf在redis解壓目錄中
b) 拷貝:cp redis.conf ../redis/bin/
[root@localhost bin]# ll
total 15520
-rw-r--r--. 1 root root 18 Jul 26 17:14 dump.rdb
-rwxr-xr-x. 1 root root 4587078 Jul 26 17:09 redis-benchmark
-rwxr-xr-x. 1 root root 22185 Jul 26 17:09 redis-check-aof
-rwxr-xr-x. 1 root root 45403 Jul 26 17:09 redis-check-dump
-rwxr-xr-x. 1 root root 4689993 Jul 26 17:09 redis-cli
-rw-r--r--. 1 root root 41403 Jul 26 17:16 redis.conf
lrwxrwxrwx. 1 root root 12 Jul 26 17:09 redis-sentinel -> redis-server
-rwxr-xr-x. 1 root root 6448257 Jul 26 17:09 redis-server
2) 修改redis配置文件
a) daemonize no==daemonize yes
3) 啓動redis,加載配置文件
a) 命令:./redis-server redis.conf
4) 登陸redis
a) 登陸命令:./redis-cli –h ip –p port
b) 登陸:./redis-cli (默認登陸6379端口redis服務)
redis集羣管理工具redis-trib.rb依賴ruby環境,首先須要安裝ruby環境:
安裝ruby
執行:
yum install ruby
再執行:
yum install rubygems
安裝ruby和redis的接口程序
拷貝redis-3.0.0.gem(ruby和redis接口)至/usr/local下
執行:
gem install /usr/local/redis-3.0.0.gem
搭建redis集羣,必須的有多臺redis服務器:保證一主一備(4臺主機,4臺從機),搭建6臺redis服務器集羣。
保證有8個redis服務:模擬8臺redis服務(端口不衝突,開啓集羣配置)
搭建集羣步驟:
1) 建立6個目錄:7001,7002,7003。。。。。。。7006
目錄名稱和redis服務端口一致
2) 開啓redis集羣配置
3) 修改redis服務器端口
4) 刪除持久化配置文件 (初始化集羣必須保持純潔性)
5) 啓動6臺服務器
6) 建立集羣
建立目錄:redis-cluster
安裝集羣安裝到redis-cluster目錄下面。
1) 建立8個目錄:
[root@localhost redis-cluster]# mkdir 700{1..6}
[root@localhost redis-cluster]# ll
total 32
drwxr-xr-x. 2 root root 4096 Jul 27 00:10 7001
drwxr-xr-x. 2 root root 4096 Jul 27 00:10 7002
drwxr-xr-x. 2 root root 4096 Jul 27 00:10 7003
drwxr-xr-x. 2 root root 4096 Jul 27 00:10 7004
drwxr-xr-x. 2 root root 4096 Jul 27 00:10 7005
drwxr-xr-x. 2 root root 4096 Jul 27 00:10 7006
2) 拷貝redis安裝目錄bin到7001:
[root@localhost redis]# cp -r bin/ ../redis-cluster/7001/
3) 修改配置文件,開啓集羣
cluster-enabled yes 把註釋#去掉便可
4) 修改端口(redis配置文件:端口和目錄名稱相同)
port 7001
注意:
5) 拷貝7001目錄下修改後的bin目錄到7002…..7006等7個目錄
drwxr-xr-x. 2 root root 4096 Jul 27 00:15 bin
[root@localhost 7001]# cp -r bin/ ../7002/
[root@localhost 7001]# cp -r bin/ ../7003/
[root@localhost 7001]# cp -r bin/ ../7004/
[root@localhost 7001]# cp -r bin/ ../7005/
[root@localhost 7001]# cp -r bin/ ../7006
6) 修改7002,7003………..7006redis服務端口
a) 要求redis服務端口和目錄文件一致。
使用redis-trib.rb插件建立集羣:
redis-trib.rb插件位置在redis解壓目錄下的編譯目錄src中。
去src中拷貝插件到redis-Cluster集羣目錄中。
[root@localhost src]# cp redis-trib.rb /usr/local/soft/redis-cluster/
1) 啓動redis服務(啓動6臺redis服務)
[root@localhost redis-cluster]# cd 7001/bin/
[root@localhost bin]# ./redis-server redis.conf
[root@localhost bin]# cd ..
[root@localhost 7001]# cd ..
[root@localhost redis-cluster]# cd 7002/bin/
[root@localhost bin]# ./redis-server redis.conf
[root@localhost bin]# cd ../../7003/bin/
[root@localhost bin]# ./redis-server redis.conf
[root@localhost bin]# cd ../../7004/bin/
[root@localhost bin]# ./redis-server redis.conf
[root@localhost bin]# cd ../../7005/bin/
[root@localhost bin]# ./redis-server redis.conf
[root@localhost bin]# cd ../../7006/bin/
[root@localhost bin]# ./redis-server redis.conf
[root@localhost bin]# ps -ef | grep redis
root 6926 1 0 Jul26 ? 00:00:21 ./redis-server *:6379
root 7637 1 0 00:25 ? 00:00:00 ./redis-server *:7001 [cluster]
root 7642 1 0 00:25 ? 00:00:00 ./redis-server *:7002 [cluster]
root 7646 1 0 00:25 ? 00:00:00 ./redis-server *:7003 [cluster]
root 7650 1 0 00:25 ? 00:00:00 ./redis-server *:7004 [cluster]
root 7655 1 0 00:26 ? 00:00:00 ./redis-server *:7005 [cluster]
root 7659 1 0 00:26 ? 00:00:00 ./redis-server *:7006 [cluster]
root 7663 6287 0 00:26 pts/0 00:00:00 grep redis
2) 建立集羣
使用插件redis-trib.rb建立集羣:
建立命令:
./redis-trib.rb create --replicas 1 192.168.253.131:7001 192.168.253.131:7002 192.168.253.131:7003 192.168.253.131:7004 192.168.253.131:7005 192.168.253.131:7006
Create:建立集羣
--replicas 1:每一臺主機至少有一臺從機。
集羣成功建立信息:
>>> Creating cluster
Connecting to node 192.168.253.131:7001: OK
Connecting to node 192.168.253.131:7002: OK
Connecting to node 192.168.253.131:7003: OK
Connecting to node 192.168.253.131:7004: OK
Connecting to node 192.168.253.131:7005: OK
Connecting to node 192.168.253.131:7006: OK
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.253.131:7001
192.168.253.131:7002
192.168.253.131:7003
Adding replica 192.168.253.131:7004 to 192.168.253.131:7001
Adding replica 192.168.253.131:7005 to 192.168.253.131:7002
Adding replica 192.168.253.131:7006 to 192.168.253.131:7003
M: 1b807d0b0bb849632e93853728f8ed3e51d0dd48 192.168.253.131:7001
slots:0-5460 (5461 slots) master
M: bd79a369ef0af24aa5e54455cae1f93e0cca636d 192.168.253.131:7002
slots:5461-10922 (5462 slots) master
M: 2dc7730fab2a36ad00937bde4ec70421e7656f40 192.168.253.131:7003
slots:10923-16383 (5461 slots) master
S: 532103b67844ae7120f96ff8f7bb77914ae14d02 192.168.253.131:7004
replicates 1b807d0b0bb849632e93853728f8ed3e51d0dd48
S: 7606279a8b2c9768d1bfc168da2e99ccb09d9b96 192.168.253.131:7005
replicates bd79a369ef0af24aa5e54455cae1f93e0cca636d
S: bd1e2d83fa458c79ac60519896861bdc9e2248d4 192.168.253.131:7006
replicates 2dc7730fab2a36ad00937bde4ec70421e7656f40
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.253.131:7001)
M: 1b807d0b0bb849632e93853728f8ed3e51d0dd48 192.168.253.131:7001
slots:0-5460 (5461 slots) master
M: bd79a369ef0af24aa5e54455cae1f93e0cca636d 192.168.253.131:7002
slots:5461-10922 (5462 slots) master
M: 2dc7730fab2a36ad00937bde4ec70421e7656f40 192.168.253.131:7003
slots:10923-16383 (5461 slots) master
M: 532103b67844ae7120f96ff8f7bb77914ae14d02 192.168.253.131:7004
slots: (0 slots) master
replicates 1b807d0b0bb849632e93853728f8ed3e51d0dd48
M: 7606279a8b2c9768d1bfc168da2e99ccb09d9b96 192.168.253.131:7005
slots: (0 slots) master
replicates bd79a369ef0af24aa5e54455cae1f93e0cca636d
M: bd1e2d83fa458c79ac60519896861bdc9e2248d4 192.168.253.131:7006
slots: (0 slots) master
replicates 2dc7730fab2a36ad00937bde4ec70421e7656f40
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
在redis-Cluster目錄下建立啓動集羣批處理文件:start-all.sh
文件內容以下:
cd 7001/bin
./redis-server redis.conf
cd ../../7002/bin
./redis-server redis.conf
cd ../../7003/bin
./redis-server redis.conf
cd ../../7004/bin
./redis-server redis.conf
cd ../../7005/bin
./redis-server redis.conf
cd ../../7006/bin
./redis-server redis.conf
1) 運行啓動sh文件
./start-all.sh
登陸命令:./redis-cli –c –h ip –p port
cd 7001/bin
查詢集羣狀態:
[root@localhost bin]# ./redis-cli -c -h 192.168.253.131 -p 7001
192.168.253.131: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:433
cluster_stats_messages_received:433
查詢集羣節點信息:
192.168.253.131:7001> cluster nodes
bd79a369ef0af24aa5e54455cae1f93e0cca636d 192.168.253.131:7002 master - 0 1469550879424 2 connected 5461-10922
2dc7730fab2a36ad00937bde4ec70421e7656f40 192.168.253.131:7003 master - 0 1469550877393 3 connected 10923-16383
7606279a8b2c9768d1bfc168da2e99ccb09d9b96 192.168.253.131:7005 slave bd79a369ef0af24aa5e54455cae1f93e0cca636d 0 1469550880434 5 connected
bd1e2d83fa458c79ac60519896861bdc9e2248d4 192.168.253.131:7006 slave 2dc7730fab2a36ad00937bde4ec70421e7656f40 0 1469550881443 6 connected
532103b67844ae7120f96ff8f7bb77914ae14d02 192.168.253.131:7004 slave 1b807d0b0bb849632e93853728f8ed3e51d0dd48 0 1469550878404 4 connected
1b807d0b0bb849632e93853728f8ed3e51d0dd48 192.168.253.131:7001 myself,master - 0 0 1 connected 0-5460
配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <!-- Jedis配置 --> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="20"></property> <property name="maxTotal" value="1000"></property> </bean> <!-- 集羣版Jedis --> <bean class="redis.clients.jedis.JedisCluster"> <constructor-arg name="nodes"> <set> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.253.131"></constructor-arg> <constructor-arg name="port" value="7001"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.253.131"></constructor-arg> <constructor-arg name="port" value="7002"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.253.131"></constructor-arg> <constructor-arg name="port" value="7003"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.253.131"></constructor-arg> <constructor-arg name="port" value="7004"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.253.131"></constructor-arg> <constructor-arg name="port" value="7005"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.253.131"></constructor-arg> <constructor-arg name="port" value="7006"></constructor-arg> </bean> </set> </constructor-arg> <constructor-arg name="poolConfig" ref="poolConfig"></constructor-arg> </bean> </beans>
import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisCluster; import redis.clients.jedis.JedisPool; public class SpringClusterJedis { /** * jedis鏈接集羣版redis */ @Test public void jedisSpringRedis() { // 加載spring配置文件 ApplicationContext app = new ClassPathXmlApplicationContext( "applicationContext-jedisCluster.xml"); //獲取jedisCluster對象 JedisCluster JedisCluster = app.getBean(JedisCluster.class); JedisCluster.set("username", "張三"); String username = JedisCluster.get("username"); System.out.println(username); } }