Redis集群與SSM整合使用方法
首先是創(chuàng)建redis-cluster文件夾:
因?yàn)閞edis最少需要6個(gè)節(jié)點(diǎn)(三主三從),為了更好的理解,我這里創(chuàng)建了兩臺(tái)虛擬機(jī)(192.168.0.109 192.168.0.110),分別在兩臺(tái)虛擬機(jī)的/opt/redis-4.0.1/redis-cluster下創(chuàng)建三個(gè)節(jié)點(diǎn)文件夾
192.168.0.109:
192.168.0.110:
以上6個(gè)節(jié)點(diǎn)全部創(chuàng)建完成,分別再在這六個(gè)文件夾下創(chuàng)建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修改成節(jié)點(diǎn)端口號(hào)一致,bind改成本機(jī)ip,以便遠(yuǎn)程訪問,全部修改完后,即可啟動(dòng)redis服務(wù):
啟動(dòng)命令:
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”
可以看到后臺(tái)模式啟動(dòng)成功的日志打印,兩天機(jī)器都需要依次啟動(dòng)所有節(jié)點(diǎn)。節(jié)點(diǎn)啟動(dòng)完成后,即可創(chuàng)建集群服務(wù):
在其中一臺(tái)虛擬機(jī)上執(zhí)行如下命令“/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”
千萬記住只需要在一臺(tái)上執(zhí)行即可,如果卡在join處不能往下執(zhí)行,一般情況是出在防火墻端口被禁導(dǎo)致,有兩種方式可以解決:
1、不但需要開啟7000對(duì)外端口,還需要開啟17000(因?yàn)閞edis總線端口需要加1000)。
2、直接關(guān)閉所有防火墻(因我這里是自己的環(huán)境,所以直接關(guān)閉了防火墻服務(wù))。
出現(xiàn)上圖運(yùn)行日志,基本就成功搭建好了集群服務(wù),可以清晰的看到各個(gè)節(jié)點(diǎn)的主從關(guān)系,環(huán)境搭建好后,這里我們就和我上篇寫到的SSM架構(gòu)進(jìn)行聯(lián)合使用。
上次整合的mybaits二級(jí)緩存是個(gè)單機(jī)版本,由于這種方式不支持集群,所以這里從新使用jedis-cluster進(jìn)行另外一種redis集群與java整合使用的方式。
首先在redis.properties文件中新增集群機(jī)器的配置,將6個(gè)節(jié)點(diǎn)依次加入配置:
#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配置文件中也與之前改動(dòng)比較多,我直接列出來,可以直接拷去用了。
spring-redis.xml
<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:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- 連接池基本參數(shù)配置,類似數(shù)據(jù)庫連接池 --> <context:property-placeholder location="classpath*:redis.properties" /> <bean name="genericObjectPoolConfig" class="org.apache.commons.pool2.impl.GenericObjectPoolConfig" > <property name="maxWaitMillis" value="-1" /> <property name="maxTotal" value="1000" /> <property name="minIdle" value="8" /> <property name="maxIdle" value="100" /> </bean> <!-- 連接池配置,類似數(shù)據(jù)庫連接池 --> <!-- <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" > <property name="hostName" value="${redis.host}"></property> <property name="port" value="${redis.port}"></property> <property name="password" value="${redis.pass}"></property> <property name="poolConfig" ref="poolConfig"></property> </bean> --> <!-- 調(diào)用連接池工廠配置 --> <!-- <bean id="redisTemplate" class=" org.springframework.data.redis.core.RedisTemplate"> <property name="jedisConnectionFactory" ref="jedisConnectionFactory"></property> 如果不配置Serializer,那么存儲(chǔ)的時(shí)候智能使用String,如果用User類型存儲(chǔ),那么會(huì)提示錯(cuò)誤User can't cast to String?。?! <property name="keySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> </property> </bean> --> <bean id="jedisCluster" class="com.cjl.util.JedisClusterFactory"> <property name="addressConfig"> <value>classpath:redis.properties</value> </property> <property name="addressKeyPrefix" value="cluster" /> <property name="timeout" value="300000" /> <property name="maxRedirections" value="6" /> <property name="genericObjectPoolConfig" ref="genericObjectPoolConfig" /> </bean> </beans>
將上篇SSM+redis整合中mybatis的開啟緩存配置全部禁用,即可啟動(dòng)服務(wù)測(cè)試了
首先直接注入jedisCluster獲取一個(gè)集群對(duì)象。
這里為了方便,我數(shù)據(jù)同步直接用了java寫了個(gè)簡(jiǎn)單思想,其他方法也可實(shí)現(xiàn),例如Spring AOP方式實(shí)現(xiàn),使用第三方插件,或者數(shù)據(jù)庫層面實(shí)現(xiàn)都可行。
啟動(dòng)成功后,反復(fù)調(diào)用方法??梢钥吹娇刂婆_(tái)并未打印sql語句,而是直接在redis集群中直接獲取得到數(shù)據(jù)。以上簡(jiǎn)單的redis集群實(shí)例已經(jīng)完成,因?yàn)闀r(shí)間關(guān)系,其中l(wèi)inux中有些坑我沒有細(xì)細(xì)寫出。
總結(jié)
以上所述是小編給大家介紹的Redis集群與SSM整合使用方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
java 快速實(shí)現(xiàn)異步調(diào)用的操作方法
這篇文章主要介紹了java 如何快速實(shí)現(xiàn)異步調(diào)用方法,今天我們就來了解下 CompletableFuture,它Java 8引入的一種功能強(qiáng)大的異步編程工具,可以用于實(shí)現(xiàn)復(fù)雜的異步操作和處理鏈?zhǔn)降漠惒饺蝿?wù),需要的朋友可以參考下2023-07-07java實(shí)現(xiàn)emqx設(shè)備上下線監(jiān)聽詳解
這篇文章主要為大家介紹了java實(shí)現(xiàn)emqx設(shè)備上下線監(jiān)聽詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07Spring Data JPA踩坑記錄(@id @GeneratedValue)
這篇文章主要介紹了Spring Data JPA踩坑記錄(@id @GeneratedValue),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07

Executor攔截器高級(jí)教程QueryInterceptor的規(guī)范