您的位置:首页 > 数据库 > Redis

redis 哨兵模式配置 和 spring data redis 哨兵配置 总结

2018-01-30 14:20 781 查看
从帖子http://blog.csdn.net/men_wen/article/details/72724406学习了哨兵配置,但是在实践的过程中遇到了问题,主要是关于bind配置,密码认证,ip的问题。然后再次总结一下。

redis版本3.2.100

分别有3个Sentinel节点,1个主节点,2个从节点组成一个Redis Sentinel。

角色ip端口
master192.168.30.2496379
slave1192.168.30.1236379
slave2192.168.30.2546379
Sentinel1192.168.30.24926379
Sentinel2192.168.30.12326379
Sentinel3192.168.30.25426379

192.168.30.249 配置

redis.conf配置

主要修改的配置如下,其他配置默认。

##这里绑定真实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"


sentinel配置

#这里配置成真实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


192.168.30.254配置

redis.conf配置

主要修改的配置如下,其他配置默认。

##这里绑定真实ip。让客户端可以远程访问
bind 192.168.30.254

port 6379
slaveof 192.168.30.249 6379
##配置了master的认证密码,作为salve要同步master的数据,不配置就连不上master
masterauth "redistest@123"
##客户端连接的时候需要密码认证。
requirepass "redistest@123"


sentinel配置

#这里配置成真实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


192.168.30.123配置

redis.conf配置

主要修改的配置如下,其他配置默认。

##这里绑定真实ip。让客户端可以远程访问
bind 192.168.30.123

port 6379
slaveof 192.168.30.249 6379
##配置了master的认证密码,作为salve要同步master的数据,不配置就连不上master
masterauth "redistest@123"
##客户端连接的时候需要密码认证。
requirepass "redistest@123"


sentinel配置

#这里配置成真实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


spring data redis 哨兵配置

spring版本4.3.5.RELEASE

spring-data-redis版本1.8.9.RELEASE

jedis版本2.9.0

redis.properties配置

#主机和端口号
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


spring-redis.xml配置

<?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: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.properties</value>
</list>
</property>
</bean>
<!--配置 jedis pool-->
<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}"/>
<!-- 获得链接时的最大等待毫秒数,小于0:阻塞不确定时间,默认-1 -->
<property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}"/>
<!-- 在获得链接的时候检查有效性,默认false -->
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/>
<!-- 在空闲时检查有效性,默认false -->
<property name="testWhileIdle" value="${redis.pool.testWhileIdle}"/>
<!-- 连接耗尽时是否阻塞,false报异常,true阻塞超时 默认:true-->
<property name="blockWhenExhausted" value="${redis.pool.blockWhenExhausted}"/>
</bean>

<!--配置redisSentinelConfiguration 哨兵模式-->
<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>

<!-- redis template definition -->
<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"/>

<!-- 配置RedisCacheManager 实现spring框架提供的缓存 -->
<!-- <bean id="redisCacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
<constructor-arg name="redisOperations" ref="redisTemplate"/>
<property name="defaultExpiration" value="${redis.expiration}"/>
</bean>-->

</beans>


junit测试

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);
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息