1: application.properties增加配置:

spring 改造redis哨兵模式

 

#哨兵模式配置
redis.masterName=sentinel-10.0.30.149-6380

redis.sentinel.host1=10.0.30.149
redis.sentinel.port1=6381

redis.sentinel.host2=10.0.30.149
redis.sentinel.port2=6382

redis.sentinel.host3=10.0.41.16
redis.sentinel.port3=6383

redis.masterName 必须跟服务器名称对应,这个一般是搭建这个机器的人定义的

2:修改application.xml 文件,看你需要用到什么bean,我这里需要用到:

jedisPoolConfig,sentinelConfiguration,jedisSentinelPool

jedisPoolConfig一般不用修改,跟单机模式的一样 

配置如下:

<!-- jedis 连接池配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
   <!-- 最大分配的对象数 -->
   <property name="maxTotal" value="${redis.maxActive}" />
   <!-- 最大能够保持idel状态的对象数 -->
   <property name="maxIdle" value="${redis.maxIdle}" />
   <!-- 当池内没有返回对象时,最大等待时间 -->
   <property name="maxWaitMillis" value="${redis.maxWait}" />
   <!-- 当调用borrow Object方法时,是否进行有效性检查 -->
   <property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>
<!-- 哨兵模式 -->
<bean id="sentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
   <!-- 服务名称 -->
   <property name="master">
      <bean class="org.springframework.data.redis.connection.RedisNode">
         <property name="name" value="${redis.masterName}"></property>
      </bean>
   </property>
   <!-- 哨兵服务IP和端口 -->
   <property name="sentinels">
      <set>
         <bean class="org.springframework.data.redis.connection.RedisNode">
            <constructor-arg name="host" value="${redis.sentinel.host1}"></constructor-arg>
            <constructor-arg name="port" value="${redis.sentinel.port1}"></constructor-arg>
         </bean>
         <bean class="org.springframework.data.redis.connection.RedisNode">
            <constructor-arg name="host" value="${redis.sentinel.host2}"></constructor-arg>
            <constructor-arg name="port" value="${redis.sentinel.port2}"></constructor-arg>
         </bean>
         <bean class="org.springframework.data.redis.connection.RedisNode">
            <constructor-arg name="host" value="${redis.sentinel.host3}"></constructor-arg>
            <constructor-arg name="port" value="${redis.sentinel.port3}"></constructor-arg>
         </bean>
      </set>
   </property>
</bean>

<!-- 连接池配置 -->
<bean id="jedisSentinelPool" class="redis.clients.jedis.JedisSentinelPool">
   <constructor-arg index="0" value="${redis.masterName}" />
   <constructor-arg index="1">
      <set>
         <value>${redis.sentinel.host1}:${redis.sentinel.port1}</value>
         <value>${redis.sentinel.host2}:${redis.sentinel.port2}</value>
         <value>${redis.sentinel.host3}:${redis.sentinel.port3}</value>
      </set>
   </constructor-arg>
   <constructor-arg index="2" ref="jedisPoolConfig" />
</bean>

 

一般如果报错,就看下是不是少包或者redis依赖版本太低 一般其实推荐是使用templateredis,但因为是改造不想动太多原来的东西

 

3   redis 工具类修改:

public static JedisSentinelPool jedisPool = (JedisSentinelPool) ServiceLocator.getInstance().getBeanFactory().getBean("jedisSentinelPool");

public static Jedis getJedis() {
    Jedis jedis = jedisPool.getResource();
    return jedis;
}

只需要把原来的 JedisPool  换成  JedisSentinelPool对象就行了

相关文章:

  • 2021-10-20
猜你喜欢
  • 2021-06-20
  • 2021-10-24
  • 2022-12-23
  • 2021-05-10
  • 2021-08-01
相关资源
相似解决方案