Redis集群的配置

集群配置推荐实践

Sentinel 127.0.0.1:26379,127.0.0.1:26479 
Master 127.0.0.1:10001 
Slave 127.0.0.1:10002,127.0.0.1:10003

Slave 的配置需要指定一个默认master

slaveof 127.0.0.1 10001

sentinel配置只需要指定一个监控的默认master即可

重点是端口和master

port26379

sentinel monitor mymaster127.0.0.1100011


客户端实践

<!-- redis属性文件 -->

<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:config/redis.properties</value>
</list>
</property>
</bean>


<!-- 启动缓存注解功能,否则缓解不会生效 -->
<cache:annotation-driven cache-manager="cacheManager" />


<!-- redis属性配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.pool.maxTotal}" />
<property name="maxIdle" value="${redis.pool.maxIdle}" />
<property name="numTestsPerEvictionRun" value="${redis.pool.numTestsPerEvictionRun}" />
<property name="timeBetweenEvictionRunsMillis" value="${redis.pool.timeBetweenEvictionRunsMillis}" />
<property name="minEvictableIdleTimeMillis" value="${redis.pool.minEvictableIdleTimeMillis}" />
<property name="softMinEvictableIdleTimeMillis" value="${redis.pool.softMinEvictableIdleTimeMillis}" />
<property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}" />
<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="mymaster"></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>
</set>
</property>
</bean>
<bean id="redisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg name="sentinelConfig" ref="sentinelConfiguration"></constructor-arg>
<constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="redisConnectionFactory"></property>
</bean>
<!-- 缓存管理器 -->
<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
<constructor-arg ref="redisTemplate" />

</bean>


正常日志打印如下,从配置的哨兵获取到master,再为master创建连接池

注意配置redis哨兵的时候使用本机的实际地址,而不要使用127.0.0.1,否则会拿不到连接,因为客户端会将地址127.0.0.1视为redis服务器地址

当客户端和服务器不在同一台机器时就搞出事情了

五月 15, 2017 5:49:03 下午 redis.clients.jedis.JedisSentinelPool initSentinels
信息: Trying to find master from available Sentinels...
五月 15, 2017 5:49:03 下午 redis.clients.jedis.JedisSentinelPool initSentinels
信息: Redis master running at 10.95.18.144:6379, starting Sentinel listeners...
五月 15, 2017 5:49:03 下午 redis.clients.jedis.JedisSentinelPool initPool
信息: Created JedisPool to master at 10.95.18.144:6379

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值