spring与redis sentinel模式集成

本文介绍了Spring框架下Redis的两种配置方式:普通配置和哨兵模式配置,并提供了详细的XML配置示例,展示了如何创建连接池、配置模板等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、普通的redis配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">


<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.pool.maxTotal}" />
<property name="maxIdle" value="${redis.pool.maxIdle}" />
<property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}" />
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
</bean>
<!-- Jedis ConnectionFactory -->
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.host}" p:port="${redis.port}" p:database="${redis.dbIndex}"
p:use-pool="true">
<property name="poolConfig" ref="jedisPoolConfig"></property>
</bean>


<!-- redis template definition -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="jedisConnectionFactory">
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
</bean>


<bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"
p:connection-factory-ref="jedisConnectionFactory">
<property name="enableTransactionSupport" value="false" />
<property name="exposeConnection" value="true" />
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
</bean>
</beans>


2、sentinel模式配置文件:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">


   <!-- 配置 jedis pool -->
   <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
      <!-- 最大连接数 -->
      <property name="maxTotal" value="${redis.pool.maxTotal}" />
      <!-- 最大空闲时间 -->
      <property name="maxIdle" value="${redis.pool.maxIdle}" />
      <!-- 获得链接时的最大等待毫秒数,小于0:阻塞不确定时间,默认-1 -->
      <property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}" />
      <!-- 在获得链接的时候检查有效性,默认false -->
      <property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
   </bean>


   <!-- redis集群配置 哨兵模式 -->
   <bean id="sentinelConfiguration"
        class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
      <property name="master">
         <bean class="org.springframework.data.redis.connection.RedisNode">
            <!--这个值要和Sentinel中指定的master的值一致,不然启动时找不到Sentinel会报错的 -->
            <property name="name" value="${redis.sentinel.master.name}"></property>
         </bean>
      </property>
      <!--记住了,这里是指定Sentinel的IP和端口,不是Master和Slave的 -->
      <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>


   <!-- redisConnectionFactory -->
   <bean id="jedisConnFactory"
        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
      <constructor-arg name="sentinelConfig" ref="sentinelConfiguration"></constructor-arg>
      <constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
      <property name="database" value="${redis.dbIndex}"/>
   </bean>
   <!-- redis template definition -->
   <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
      <property name="connectionFactory" ref="jedisConnFactory"/>
      <property name="keySerializer">
         <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
      </property>
      <property name="hashKeySerializer">
         <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
      </property>
      <property name="valueSerializer">
         <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
      </property>
   </bean>


   <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
      <property name="connectionFactory" ref="jedisConnFactory" />
      <property name="enableTransactionSupport" value="false" />
      <property name="exposeConnection" value="true" />
      <property name="keySerializer">
         <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
      </property>
      <property name="valueSerializer">
         <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
      </property>
   </bean>

</beans>


3、应用使用:

@Autowired
@Qualifier("redisTemplate")
private RedisTemplate redisTemplate;
@Autowired
private StringRedisTemplate redisTemplate;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值