美文网首页
spring redis单机配置和哨兵配置

spring redis单机配置和哨兵配置

作者: 菜菜1234 | 来源:发表于2017-06-05 13:55 被阅读0次

用spring redis做redis缓存的缓存服务器时,发现单机或单集群的案例很多。
市场上还有一种做法,是用哨兵(sentinel)做集群,但是spring redis怎么配置哨兵连接呢?没有资料。我通过翻源代码,摸索出如下配置,实践检验是可行的。供君参考。
sentinel资料介绍 点击查看

常规配置如下:

spring redis单机(或单集群)配置

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>

<!-- 默认的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 工厂类,存放各个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数据源配置
#redis主机IP
redis.hostName=10.253.6.124
#redis主机端口
redis.port=6379
#如果redis有密码,则设置。否则不设置
redis.password=hundsun@1
#超时时间,单位毫秒
redis.timeout=2000
#最大空闲数 
redis.maxIdle=500
#最大连接数 
redis.maxTotal=1000
#最大建立连接等待时间
redis.maxWaitMillis=5000

#redis业务配置
#是否使用redis,true|false
redis.use=true
#缓存名称,多个用逗号区分
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">
    <!-- 使用构造函数注入对象 -->  
    <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>

<!-- 默认的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数据源配置
#redis哨兵master名称
redis.masterName=master
#redis哨兵IP
redis.hostName=10.253.6.124
#redis哨兵端口
redis.port=6379
#如果redis有密码,则设置。否则不设置
redis.password=hundsun@1
#超时时间,单位毫秒
redis.timeout=2000
#最大空闲数 
redis.maxIdle=500
#最大连接数 
redis.maxTotal=1000
#最大建立连接等待时间
redis.maxWaitMillis=5000

#redis业务配置
#是否使用redis,true|false
redis.use=true
#缓存名称,多个用逗号区分
redis.cachename=app

相关文章

网友评论

      本文标题:spring redis单机配置和哨兵配置

      本文链接:https://www.haomeiwen.com/subject/ompifxtx.html