04.redis集群+SSM整合使用


 redis集群+SSM整合使用

首先是創建redis-cluster文件夾:

因為redis最少需要6個節點(三主三從),為了更好的理解,我這里創建了兩台虛擬機(192.168.0.109 192.168.0.110),分別在兩台虛擬機的/opt/redis-4.0.1/redis-cluster下創建三個節點文件夾

192.168.0.109:

 

192.168.0.110:

以上6個節點全部創建完成,分別再在這六個文件夾下創建redis.conf配置文件,其中配置如圖:

port 7000
bind 192.168.0.109
daemonize yes
pidfile /var/run/redis_7000.pid
cluster-enabled yes
cluster-config-file nodes_7000.conf
cluster-node-timeout 10000
appendonly yes

其中需要將port pidfile cluster-config-file修改成節點端口號一致,bind改成本機ip,以便遠程訪問,全部修改完后,即可啟動redis服務:

啟動命令:

192.168.0.109下的命令:“for((i=0;i<=2;i++)); do /opt/redis-4.0.1/src/redis-server /opt/redis-4.0.1/redis-cluster/700$i/redis.conf; done

192.168.0.110下的命令:“for((i=3;i<=5;i++)); do /opt/redis-4.0.1/src/redis-server /opt/redis-4.0.1/redis-cluster/700$i/redis.conf; done

可以看到后台模式啟動成功的日志打印,兩台機器都需要依次啟動所有節點。節點啟動完成后,即可創建集群服務:

在其中一台虛擬機上執行如下命令“/opt/redis-4.0.1/src/redis-trib.rb create --replicas 1 192.168.0.109:7000 192.168.0.109:7001 192.168.0.109:7002 192.168.0.110:7003 192.168.0.110:7004 192.168.0.110:7005” 

 千萬記住只需要在一台上執行即可,如果卡在join處不能往下執行,一般情況是出在防火牆端口被禁導致,有兩種方式可以解決:

1、不但需要開啟7000對外端口,還需要開啟17000(因為redis總線端口需要加10000)。

2、直接關閉所有防火牆(因我這里是自己的環境,所以直接關閉了防火牆服務)。

出現上圖運行日志,基本就成功搭建好了集群服務,可以清晰的看到各個節點的主從關系,環境搭建好后,這里我們就和我上篇寫到的SSM架構進行聯合使用。

上次整合的mybaits二級緩存是個單機版本,由於這種方式不支持集群,所以這里從新使用jedis-cluster進行另外一種redis集群與java整合使用的方式。

首先在redis.properties文件中新增集群機器的配置,將6個節點依次加入配置:

#cluster  
cluster1.host.port=192.168.0.109:7000
cluster2.host.port=192.168.0.109:7001
cluster3.host.port=192.168.0.109:7002
cluster4.host.port=192.168.0.110:7003
cluster5.host.port=192.168.0.110:7004
cluster6.host.port=192.168.0.110:7005

redis配置文件中也與之前改動比較多,我直接列出來,可以直接拷去用了。

spring-redis.xml

 1 <beans xmlns="http://www.springframework.org/schema/beans"
 2   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3   xmlns:p="http://www.springframework.org/schema/p"
 4   xmlns:mvc="http://www.springframework.org/schema/mvc"
 5   xmlns:util="http://www.springframework.org/schema/util"
 6   xmlns:aop="http://www.springframework.org/schema/aop"
 7   xmlns:context="http://www.springframework.org/schema/context"
 8   xmlns:task="http://www.springframework.org/schema/task" 
 9   xsi:schemaLocation="http://www.springframework.org/schema/beans
10       http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
11       http://www.springframework.org/schema/util
12       http://www.springframework.org/schema/util/spring-util-4.3.xsd
13       http://www.springframework.org/schema/mvc
14       http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
15       http://www.springframework.org/schema/aop
16       http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
17       http://www.springframework.org/schema/context
18       http://www.springframework.org/schema/context/spring-context-4.3.xsd">
19       
20       
21     <!-- 連接池基本參數配置,類似數據庫連接池 -->
22      <context:property-placeholder location="classpath*:redis.properties" />
23      
24     <bean name="genericObjectPoolConfig" class="org.apache.commons.pool2.impl.GenericObjectPoolConfig" >
25         <property name="maxWaitMillis" value="-1" />
26         <property name="maxTotal" value="1000" />
27         <property name="minIdle" value="8" />
28         <property name="maxIdle" value="100" />
29     </bean>
30     
31     <!-- 連接池配置,類似數據庫連接池 -->
32     <!-- <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" >
33         <property name="hostName" value="${redis.host}"></property>
34         <property name="port" value="${redis.port}"></property>
35         <property name="password" value="${redis.pass}"></property>
36         <property name="poolConfig"  ref="poolConfig"></property> 
37     </bean> -->
38     
39     <!-- 調用連接池工廠配置 -->
40     <!-- <bean id="redisTemplate" class=" org.springframework.data.redis.core.RedisTemplate">
41         <property name="jedisConnectionFactory" ref="jedisConnectionFactory"></property>
42         
43         如果不配置Serializer,那么存儲的時候智能使用String,如果用User類型存儲,那么會提示錯誤User can't cast  to String!!!  
44          <property name="keySerializer">  
45             <bean  
46             class="org.springframework.data.redis.serializer.StringRedisSerializer" />  
47         </property>  
48         <property name="valueSerializer">  
49             <bean  
50                 class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />  
51         </property> 
52     </bean> -->
53      <bean id="jedisCluster" class="com.cjl.util.JedisClusterFactory">
54         <property name="addressConfig">
55             <value>classpath:redis.properties</value>
56         </property>
57         <property name="addressKeyPrefix" value="cluster" />
58 
59         <property name="timeout" value="300000" />
60         <property name="maxRedirections" value="6" />
61         <property name="genericObjectPoolConfig" ref="genericObjectPoolConfig" />
62     </bean>
63 </beans>

將上篇SSM+redis整合中mybatis的開啟緩存配置全部禁用,即可啟動服務測試了

首先直接注入jedisCluster獲取一個集群對象。

這里為了方便,我數據同步直接用了java寫了個簡單思想,其他方法也可實現,例如Spring AOP方式實現,使用第三方插件,或者數據庫層面實現都可行。

啟動成功后,反復調用方法。可以看到控制台並未打印sql語句,而是直接在redis集群中直接獲取得到數據。以上簡單的redis集群實例已經完成,因為時間關系,其中linux中有些坑我沒有細細寫出,如有疑問可以留言。

如有不對的地方或者更好的建議,歡迎評論中指出。我會盡快學習修改。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM