从帖子http://blog.csdn.net/men_wen/article/details/72724406学习了哨兵配置,但是在实践的过程中遇到了问题,主要是关于bind配置,密码认证,ip的问题。然后再次总结一下。
redis版本3.2.100
分别有3个Sentinel节点,1个主节点,2个从节点组成一个Redis Sentinel。
角色 | ip | 端口 |
---|---|---|
master | 192.168.30.249 | 6379 |
slave1 | 192.168.30.123 | 6379 |
slave2 | 192.168.30.254 | 6379 |
Sentinel1 | 192.168.30.249 | 26379 |
Sentinel2 | 192.168.30.123 | 26379 |
Sentinel3 | 192.168.30.254 | 26379 |
主要修改的配置如下,其他配置默认。
##这里绑定真实ip。让客户端可以远程访问
bind 192.168.30.249
port 6379
##开启日志级别是debug,方便看日志调试。
loglevel debug
##日志位置
logfile "D:/myStuepss/redis-x64-3.2.100/redisserver.log"
##配置了master的认证密码,虽然当前角色是master,但是一旦挂掉恢复后,会变为salve,这个时候再连接新master的时候,如果master需要密码认证,就需要配置。
masterauth "redistest@123"
##客户端连接的时候需要密码认证。
requirepass "redistest@123"
#这里配置成真实ip。为了客户端可以远程访问,例如:我用了spring data redis. 这里如果不配置ip,会报错的。
bind 192.168.30.249
port 26379
dir "D:\\myStuepss\\Redis-x64-3.2.100\\temp"
#监控master
sentinel monitor mymaster 192.168.30.249 6379 2
#要配置上master的认证密码,否则监控不到
sentinel auth-pass mymaster redistest@123
#配置上日志,方便排除错误,查看状态。
logfile "D:/myStuepss/redis-x64-3.2.100/sentinellog.log"
loglevel debug
主要修改的配置如下,其他配置默认。
##这里绑定真实ip。让客户端可以远程访问
bind 192.168.30.254
port 6379
slaveof 192.168.30.249 6379
##配置了master的认证密码,作为salve要同步master的数据,不配置就连不上master
masterauth "redistest@123"
##客户端连接的时候需要密码认证。
requirepass "redistest@123"
#这里配置成真实ip。为了客户端可以远程访问,例如:我用了spring data redis. 这里如果不配置ip,会报错的。
bind 192.168.30.254
port 26379
dir "D:\\myStuepss\\Redis-x64-3.2.100\\temp"
#监控master
sentinel monitor mymaster 192.168.30.249 6379 2
#要配置上master的认证密码,否则监控不到
sentinel auth-pass mymaster redistest@123
#配置上日志,方便排除错误,查看状态。
logfile "D:/myStuepss/redis-x64-3.2.100/sentinellog.log"
loglevel debug
主要修改的配置如下,其他配置默认。
##这里绑定真实ip。让客户端可以远程访问
bind 192.168.30.123
port 6379
slaveof 192.168.30.249 6379
##配置了master的认证密码,作为salve要同步master的数据,不配置就连不上master
masterauth "redistest@123"
##客户端连接的时候需要密码认证。
requirepass "redistest@123"
#这里配置成真实ip。为了客户端可以远程访问,例如:我用了spring data redis. 这里如果不配置ip,会报错的。
bind 192.168.30.123
port 26379
dir "D:\\myStuepss\\Redis-x64-3.2.100\\temp"
#监控master
sentinel monitor mymaster 192.168.30.249 6379 2
#要配置上master的认证密码,否则监控不到
sentinel auth-pass mymaster redistest@123
#配置上日志,方便排除错误,查看状态。
logfile "D:/myStuepss/redis-x64-3.2.100/sentinellog.log"
loglevel debug
#主机和端口号
redis.host1.cloudq=192.168.30.254
redis.port1.cloudq=26379
redis.host2.cloudq=192.168.30.249
redis.port2.cloudq=26379
redis.host3.cloudq=192.168.30.123
redis.port3.cloudq=26379
#JedisPoolConfig的参数
#最大连接数
redis.pool.maxTotal=30
#最大空闲时间
redis.pool.maxIdle=10
#每次最大连接数
redis.pool.numTestsPerEvictionRun=1024
#释放扫描的扫描间隔
redis.pool.timeBetweenEvictionRunsMillis=30000
#连接的最小空闲时间
redis.pool.minEvictableIdleTimeMillis=1800000
#连接控歘按时间多久后释放,当空闲时间>该值且空闲连接>最大空闲连接数时直接释放
redis.pool.softMinEvictableIdleTimeMillis=10000
#获得链接时的最大等待毫秒数,小于0:阻塞不确定时间,默认-1
redis.pool.maxWaitMillis=1500
#在获得链接的时候检查有效性,默认false
redis.pool.testOnBorrow=true
#在空闲时检查有效性,默认false
redis.pool.testWhileIdle=true
#连接耗尽时是否阻塞,false报异常,true阻塞超时,默认true
redis.pool.blockWhenExhausted=false
#JedisConnectionFactory的参数
#超时时间,默认:2000
redis.timeout.cloudq=3000
#密码
redis.password.cloudq=redistest@123
#是否使用连接池,默认true
redis.usePool=true
#使用数据库的索引,0-15之间的数字,默认:0
redis.dbIndex=0
#是否使用数据类型的转换,默认:true
#redis.convertPipelineAndTxResults
#哨兵配置
#redis.sentinelConfig
redis.expiration=3000
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
">
<bean id="placeholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="1"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>classpath:redis.propertiesvalue>
list>
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="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}"/>
<property name="testWhileIdle" value="${redis.pool.testWhileIdle}"/>
<property name="blockWhenExhausted" value="${redis.pool.blockWhenExhausted}"/>
bean>
<bean id="redisSentinelConfiguration"
class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
<property name="master">
<bean class="org.springframework.data.redis.connection.RedisNode">
<property name="name" value="mymaster">
property>
bean>
property>
<property name="sentinels">
<set>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${redis.host1.cloudq}"/>
<constructor-arg name="port" value="${redis.port1.cloudq}"/>
bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${redis.host2.cloudq}"/>
<constructor-arg name="port" value="${redis.port2.cloudq}"/>
bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="${redis.host3.cloudq}"/>
<constructor-arg name="port" value="${redis.port3.cloudq}"/>
bean>
set>
property>
bean>
<bean id="redisConnectionFactorySentinel"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:password="${redis.password.cloudq}">
<constructor-arg name="sentinelConfig" ref="redisSentinelConfiguration">constructor-arg>
<constructor-arg name="poolConfig" ref="jedisPoolConfig">constructor-arg>
bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="redisConnectionFactorySentinel" p:keySerializer-ref="genericJackson2JsonRedisSerializer"
p:valueSerializer-ref="genericJackson2JsonRedisSerializer"
p:hashKeySerializer-ref="genericJackson2JsonRedisSerializer"
p:hashValueSerializer-ref="genericJackson2JsonRedisSerializer"
p:enableTransactionSupport="false"/>
<bean id="genericJackson2JsonRedisSerializer"
class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>
beans>
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/spring-redis.xml"}
)
public class LockTest {
@Autowired
private RedisTemplate redisTemplate;
@org.junit.Test
public void test() throws Exception{
ValueOperations valueOperations = redisTemplate.opsForValue();
for(int i =0;i<10000;i++){
try{
valueOperations.set("test:"+i,i);
}catch (Exception e){
e.printStackTrace();
}
Thread.sleep(1000);
}
}
}