springboot1.5集成redis哨兵模式

首先 centos7 环境安装redis
 

wget ......

cp -r redis.5  reids1, cp -r redis.5  reids2, cp -r redis.5  reids3 , 
make & make install

......

主要的两个配置文件修改
#########################6379#########################
 

###redis.conf
daemonize yes 
masterauth mypassword
requirepass mypassword




###sentinel.conf

port 26379
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel auth-pass mymaster mypassword
sentinel down-after-milliseconds mymaster 15000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 80000
 protected-mode yes

#########################6380#########################
 

 ###redis-6380.conf

daemonize yes 
masterauth mypassword
requirepass mypassword
slaveof 127.0.0.1 6379



###sentinel-6380.conf

port 26380
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel auth-pass mymaster mypassword
sentinel down-after-milliseconds mymaster 15000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 80000
 protected-mode yes 

#########################6381#########################

###redis-6381.conf
daemonize yes 
masterauth mypassword
requirepass mypassword
slaveof 127.0.0.1 6379


###sentinel-6381.conf

port 26381
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel auth-pass mymaster mypassword
sentinel down-after-milliseconds mymaster 15000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 80000
 protected-mode yes 

配置完成 ,然后分别启动  

redis1/src > ./redis-server ../redis.conf   ./redis-sentinel ../sentinel.conf
redis2/src > ./redis-server ../redis.conf   ./redis-sentinel ../sentinel.conf
redis3/src > ./redis-server ../redis.conf   ./redis-sentinel ../sentinel.conf

ps -ef | grep redis  查看进程号是否都启动起来

 

具体按照流程在此略过。。。。。

环境准备好了,下面就要集成代码了, (springboot1.5 + redis哨兵)
首先引入必要的jar包
 gradle引入  

compile group: 'org.springframework.boot', name: 'spring-boot-starter-redis', version:'1.4.7.RELEASE'

application.properties 增加修改
 

###哨兵模式不起作用
spring.redis.host=127.0.0.1    
###哨兵模式不起作用
spring.redis.port=63791
spring.redis.password=myredis
spring.redis.sentinel.master=mymaster
spring.redis.sentinel.nodes=127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381
#连接超时时间
spring.redis.timeout=6000
#Redis数据库索引(默认为0)
spring.redis.database=0
# 连接池配置,springboot2.0中直接使用jedis或者lettuce配置连接池,默认为lettuce连接池
#连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=8
#连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1s
#连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=8
#接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0
@Configuration
@EnableAutoConfiguration
public class RedisConfig {

	@Value("${spring.redis.host}")
	private String host;
	@Value("${spring.redis.port}")
	private int port;
	@Value("${spring.redis.password}")
	private String passowrd;

	@Value("#{'${spring.redis.sentinel.nodes}'.split(',')}")
	private List nodes;

	@Bean
	@ConfigurationProperties(prefix="spring.redis")
	public JedisPoolConfig getRedisConfig(){
		JedisPoolConfig config = new JedisPoolConfig();
		return config;
	}
	@Bean
	public RedisSentinelConfiguration sentinelConfiguration(){
		RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration();
		//配置matser的名称
		redisSentinelConfiguration.master("mymaster");
		//配置redis的哨兵sentinel
		Set redisNodeSet = new HashSet<>();
		nodes.forEach(x->{
			redisNodeSet.add(new RedisNode(x.split(":")[0],Integer.parseInt(x.split(":")[1])));
		});
		redisSentinelConfiguration.setSentinels(redisNodeSet);
		return redisSentinelConfiguration;
	}


	@Bean
	public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig,RedisSentinelConfiguration sentinelConfig) {
		JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(sentinelConfig,jedisPoolConfig);
		jedisConnectionFactory.setHostName(host);
		jedisConnectionFactory.setPort(port);
		jedisConnectionFactory.setPassword(passowrd);
		return jedisConnectionFactory;
	}

}

启动项目测试 ,完美启动。 自此springboot集成redis哨兵结束。
如有疑问请给我留言

你可能感兴趣的:(SpringBoot)