用spring redis做redis緩存的緩存服務(wù)器時(shí),發(fā)現(xiàn)單機(jī)或單集群的案例很多园细。
市場(chǎng)上還有一種做法惦积,是用哨兵(sentinel)做集群,但是spring redis怎么配置哨兵連接呢猛频?沒(méi)有資料狮崩。我通過(guò)翻源代碼蛛勉,摸索出如下配置,實(shí)踐檢驗(yàn)是可行的睦柴。供君參考诽凌。
sentinel資料介紹 點(diǎn)擊查看
常規(guī)配置如下:
spring redis單機(jī)(或單集群)配置
web.xml
<!--========= spring配置 ========= -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/conf/spring/*-beans.xml</param-value>
</context-param>
application-redis-beans.xml
<context:property-placeholder file-encoding="UTF-8" location="WEB-INF/conf/redis.properties"/>
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}"/>
<property name="maxTotal" value="${redis.maxTotal}"/>
<property name="maxWaitMillis" value="${redis.maxWaitMillis}"/>
<property name="testOnBorrow" value="true"/>
</bean>
<bean id="tradeConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.hostName}"/>
<property name="port" value="${redis.port}"/>
<property name="password" value="${redis.password}"/>
<property name="timeout" value="${redis.timeout}"/>
<property name="poolConfig" ref="jedisPoolConfig"/>
</bean>
<!-- 默認(rèn)的redisTemplate -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="tradeConnectionFactory"/>
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="valueSerializer">
<!-- <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> -->
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="hashValueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
</bean>
<!-- redisTemplate 工廠類(lèi),存放各個(gè)redisTemplate -->
<bean id="redisTemplateFactory" class="cn.cloud.winner.oss.redis.manager.RedisTemplateFactory">
<property name="redisTemplates">
<map>
<entry key="${redis.cachename}" value-ref="redisTemplate" />
</map>
</property>
</bean>
redis.properties
##redis數(shù)據(jù)源配置
#redis主機(jī)IP
redis.hostName=10.253.6.124
#redis主機(jī)端口
redis.port=6379
#如果redis有密碼坦敌,則設(shè)置侣诵。否則不設(shè)置
redis.password=hundsun@1
#超時(shí)時(shí)間,單位毫秒
redis.timeout=2000
#最大空閑數(shù)
redis.maxIdle=500
#最大連接數(shù)
redis.maxTotal=1000
#最大建立連接等待時(shí)間
redis.maxWaitMillis=5000
#redis業(yè)務(wù)配置
#是否使用redis,true|false
redis.use=true
#緩存名稱(chēng)狱窘,多個(gè)用逗號(hào)區(qū)分
redis.cachename=app
sentinel配置如下:
spring redis哨兵(單哨兵或多哨兵)配置(sentinel)
web.xml
<!--========= spring配置 ========= -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/conf/spring/*-beans.xml</param-value>
</context-param>
application-redis-beans.xml
<context:property-placeholder file-encoding="UTF-8" location="WEB-INF/conf/redis.properties"/>
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}"/>
<property name="maxTotal" value="${redis.maxTotal}"/>
<property name="maxWaitMillis" value="${redis.maxWaitMillis}"/>
<property name="testOnBorrow" value="true"/>
</bean>
<!-- redis哨兵配置 start(如果不使用哨兵杜顺,注釋該段代碼)-->
<bean id="redisNode1" class="org.springframework.data.redis.connection.RedisNode">
<!-- 使用構(gòu)造函數(shù)注入對(duì)象 -->
<constructor-arg index="0" value="${redis.hostName}" />
<constructor-arg index="1" value="${redis.port}" />
<property name="name" value="${redis.masterName}"/>
</bean>
<bean id="sentinelConfig" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
<property name="master" ref="redisNode1"/>
<property name="sentinels">
<set>
<ref bean="redisNode1"/>
</set>
</property>
</bean>
<!-- redis哨兵配置 end-->
<bean id="tradeConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.hostName}"/>
<property name="port" value="${redis.port}"/>
<property name="password" value="${redis.password}"/>
<property name="timeout" value="${redis.timeout}"/>
<property name="poolConfig" ref="jedisPoolConfig"/>
<!-- redis哨兵配置,如果不使用哨兵训柴,注釋該行即可 -->
<constructor-arg index="0" ref="sentinelConfig" />
</bean>
<!-- 默認(rèn)的redisTemplate -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="tradeConnectionFactory"/>
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
</property>
<property name="valueSerializer">
<!-- <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> -->
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="hashValueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
</bean>
redis.properties
##redis數(shù)據(jù)源配置
#redis哨兵master名稱(chēng)
redis.masterName=master
#redis哨兵IP
redis.hostName=10.253.6.124
#redis哨兵端口
redis.port=6379
#如果redis有密碼哑舒,則設(shè)置。否則不設(shè)置
redis.password=hundsun@1
#超時(shí)時(shí)間幻馁,單位毫秒
redis.timeout=2000
#最大空閑數(shù)
redis.maxIdle=500
#最大連接數(shù)
redis.maxTotal=1000
#最大建立連接等待時(shí)間
redis.maxWaitMillis=5000
#redis業(yè)務(wù)配置
#是否使用redis,true|false
redis.use=true
#緩存名稱(chēng)洗鸵,多個(gè)用逗號(hào)區(qū)分
redis.cachename=app