1. 程式人生 > >linux系統中搭建redis叢集

linux系統中搭建redis叢集

環境:在window作業系統下使用vmware虛擬機器開啟linux系統搭建redis叢集
映象:CentOS 6.6
所需軟體:redis-3.0.0.tar.gz
ruby
redis-3.0.0.gem

將redis通過xftp傳到linux中進行解壓安裝
tar -zxvf redis-3.0.0.tar.gz
複製一份redis.conf
vi redis.conf
將diaemond no 改為 yes (允許redis伺服器在後臺執行,否則必須開啟一個終端用來開啟伺服器)

步驟:
開啟linux系統終端(可以使用xshell等工具開啟)
(有些安裝的步驟需要輸入yes或y,這裡先說明一下,不再贅述)
1.輸入yum install ruby (安裝ruby指令碼語言)
2.yum install rubygems
3.將redis-3.0.0.gem檔案通過xftp工具傳到linux的root目錄下
4.輸入命令gen install redis-3.0.0.gem
5. cd /usr/local/ (進入/usr/local目錄)
6. mkdir redis-cluster (建立一個目錄用來存放redis叢集)
7. 進入redis/bin目錄,確認bin目錄中沒有rdb檔案,否則會建立叢集失敗。有rdb就要刪除掉,使用命令rm -rf *.rdb
8. cp -r redis/bin/ redis-cluster/redis01 (將redis的bin目錄所有東西複製到redis-cluster下並命名為redis01)
9. cd redis-cluster
10. vi redis01/redis.conf (進入redis.conf的編輯模式,按i,把埠號改成7001,搜尋找到cluster-enabled yes,將前面的註釋的#號去掉,儲存退出wq)
11. 將redis01複製6份
cp -r redis01 redis02
cp -r redis01 redis03
cp -r redis01 redis04
cp -r redis01 redis05
cp -r redis01 redis06
12.將每個redis中的redis.conf裡的埠號改一下
vi redis02/redis.conf 的埠號改成port 7002
…以此類推,一直改到7006

13.vi redis-cluster-start-all.sh (寫一個啟動的指令碼用來一次性啟動全部redis)
寫入:
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

13.chmod +x redis-cluster-start-all.sh (給sh檔案加入許可權,否則無法啟動)
14. ./redis-cluster-start-all.sh 啟動
15. ps aux|grep redis 檢視啟動狀態
16. 此時redis叢集並沒有真正啟動,還需要做一些事情
17. cd …
18. cd redis-3.0.0/
19. cd src
20. ll *.rb
21. 會出現一個redis-trib.rb 此檔案是專門使用ruby來搭建redis叢集的
22. cp *.rb /usr/local/redis-cluster (將此檔案拷貝到/usr/local/redis-cluster下)
23. cd /usr/local/redis-cluster
24. 使用ruby搭建叢集
輸入 ./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
注意:這裡的ip需要根據你自己的伺服器ip地址進行設定,這裡我設定的自己的ip
25.等待幾個ok之後,提示輸入yes,然後成功搭建redis叢集了。
在這裡插入圖片描述


26.測試一下
cd redis01
./redis-cli -h 192.168.25.129 -p 7004 -c (-c是通過叢集連線)
set key1 abc
–>ok
get key1
–>“abc”

在這裡插入圖片描述

使用eclipse連線redis叢集進行測試
1、引入jedis客戶端依賴

	<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>2.7.2</version>
		</dependency>
<dependency>

2、測試類:

	@Test
	public void testJedisCluster(){
		Set<HostAndPort> nodes=new HashSet<>();
		nodes.add(new HostAndPort("192.168.25.129", 7001));
		nodes.add(new HostAndPort("192.168.25.129", 7002));
		nodes.add(new HostAndPort("192.168.25.129", 7003));
		nodes.add(new HostAndPort("192.168.25.129", 7004));
		nodes.add(new HostAndPort("192.168.25.129", 7005));
		nodes.add(new HostAndPort("192.168.25.129", 7006));
		JedisCluster jedisCluster=new JedisCluster(nodes);
		jedisCluster.set("key6", "testJedisCluster");
		String string = jedisCluster.get("key6");
		System.out.println(string);
	}


輸出:
在這裡插入圖片描述

連線正常。

三、使用spring整合redis叢集

1、匯入spring相關的依賴
2、加入配置檔案applicationContext-rediscluster.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
		
	<bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
		<constructor-arg>
			<set>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host" value="192.168.25.129"></constructor-arg>
					<constructor-arg name="port" value="7001"></constructor-arg>
				</bean>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host" value="192.168.25.129"></constructor-arg>
					<constructor-arg name="port" value="7002"></constructor-arg>
				</bean>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host" value="192.168.25.129"></constructor-arg>
					<constructor-arg name="port" value="7003"></constructor-arg>
				</bean>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host" value="192.168.25.129"></constructor-arg>
					<constructor-arg name="port" value="7004"></constructor-arg>
				</bean>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host" value="192.168.25.129"></constructor-arg>
					<constructor-arg name="port" value="7005"></constructor-arg>
				</bean>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host" value="192.168.25.129"></constructor-arg>
					<constructor-arg name="port" value="7006"></constructor-arg>
				</bean>
			</set>
		</constructor-arg>
	</bean>	
</beans>

測試類:

@Test
	public void testJedisCluster2(){
		ApplicationContext app=new ClassPathXmlApplicationContext("classpath:applicationContext-rediscluster.xml");
		JedisCluster jedisCluster=(JedisCluster)app.getBean("jedisCluster");
		jedisCluster.set("key7", "hello jedisCluster");
		String string = jedisCluster.get("key7");
		System.out.println(string);
	}

輸出:
在這裡插入圖片描述

連線正常

注意:redis叢集是3.0版本加入的特性,3.0以前版本不能使用redis叢集。
redis是用C語言編寫的,資料儲存在記憶體中,單執行緒安全,key-value結構,讀取速度非常的快,應用場景諸如排行榜,評論,點贊,訂單等頻繁操作資料庫導致效能下降,此時可以使用redis進行儲存。

redis有五種資料結構
1、字串
2、hash雜湊
3、list列表
4、set集合
5、zset

list列表採用雙向連結串列結構,對於兩端新增和刪除速度很快,查詢時兩邊快,中間資料較慢。
set集合底層是hash,用的比較少
zet底層也是hash,但是會給每個key增加一個分數,用來排序。效能很好,但是會消耗一定的記憶體,為了提示效能,這些記憶體消耗還是很值的。