spring整合redis哨兵配置
时间: 2023-10-28 07:07:09 浏览: 181
Spring整合Redis哨兵
以下是Spring整合Redis哨兵配置的步骤:
1. 在pom.xml中添加redis和jedis依赖:
```
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.4.5</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.6.0</version>
</dependency>
```
2. 在applicationContext.xml中配置RedisConnectionFactory:
```
<bean id="redisSentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
<property name="master" value="${redis.master}"/>
<property name="sentinels">
<set>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg value="${redis.sentinel1.host}:${redis.sentinel1.port}"/>
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg value="${redis.sentinel2.host}:${redis.sentinel2.port}"/>
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg value="${redis.sentinel3.host}:${redis.sentinel3.port}"/>
</bean>
</set>
</property>
</bean>
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.pool.maxTotal}"/>
<property name="maxIdle" value="${redis.pool.maxIdle}"/>
<property name="minIdle" value="${redis.pool.minIdle}"/>
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/>
<property name="testOnReturn" value="${redis.pool.testOnReturn}"/>
<property name="testWhileIdle" value="${redis.pool.testWhileIdle}"/>
<property name="timeBetweenEvictionRunsMillis" value="${redis.pool.timeBetweenEvictionRunsMillis}"/>
<property name="numTestsPerEvictionRun" value="${redis.pool.numTestsPerEvictionRun}"/>
<property name="minEvictableIdleTimeMillis" value="${redis.pool.minEvictableIdleTimeMillis}"/>
</bean>
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="sentinelConfiguration" ref="redisSentinelConfiguration"/>
<property name="poolConfig" ref="jedisPoolConfig"/>
</bean>
```
3. 在application.properties中配置Redis哨兵相关信息:
```
redis.master=master
redis.sentinel1.host=127.0.0.1
redis.sentinel1.port=26379
redis.sentinel2.host=127.0.0.1
redis.sentinel2.port=26380
redis.sentinel3.host=127.0.0.1
redis.sentinel3.port=26381
redis.pool.maxTotal=100
redis.pool.maxIdle=50
redis.pool.minIdle=10
redis.pool.testOnBorrow=true
redis.pool.testOnReturn=false
redis.pool.testWhileIdle=true
redis.pool.timeBetweenEvictionRunsMillis=30000
redis.pool.numTestsPerEvictionRun=10
redis.pool.minEvictableIdleTimeMillis=60000
```
4. 在需要使用Redis的类中注入RedisTemplate:
```
@Autowired
private RedisTemplate<String, Object> redisTemplate;
```
阅读全文