環境:在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增加一個分數,用來排序。性能很好,但是會消耗一定的內存,爲了提示性能,這些內存消耗還是很值的。