springboot -redis哨兵模式集群配置

application.yml配置文件

server:
port: 6789
servlet:
context-path: /
my:
name: pc_demo

spring:
profiles:
active: test
#test测试环境 prod生产环境
redis:
port: 6379
database: 16
timeout: 0
password: 123456
pool:
max-active: 500
max-idle: 50
min-idle: 5
max-wait: 300
thymeleaf:
aop:
proxy-target-class: true
mybatis:
typeAliasesPackage: com.cms.demo.entity
mapperLocations: classpath:mapper/*.xml
configuration:
call-setters-on-nulls: true
file:
singleFileSize: 50MB
totalFileSize: 1000000MB
---
#test environment


spring:
profiles: test
datasource:
primary:
name: crm
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:oracle:thin:@a.b.c.d:1521:test
username: test
password: test
second:
name: crm
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:oracle:thin:@a.b.c.d:1521:test
username: test
password: test
redis:
#哨兵模式根据名称自动选择master
sentinel:
master: mymaster
nodes: 127.0.0.1:26379,127.0.0.1:26379,127.0.0.1:26379,
data:
mongodb:
uri: mongodb://admin:admin@127.0.0.1:27017,127.0.0.1:27017,127.0.0.1:27017
database: PcWeb

my:
name: test

---
#prod environment
MyParam:
systemName: abc
userAccount: aaa

spring:
profiles: prod

datasource:
name: crm
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:oracle:thin:@123.456.789.123:1521:hehe
username: aaa
password: bbb
redis:
sentinel: #哨兵模式根据名称自动选择master
master: mymaster
nodes: 127.0.0.1:26379,127.0.0.1:26379,127.0.0.1:26379,
data:
mongodb:
uri: mongodb://arthur:admin2018@127.0.0.1:27017,127.0.0.1:27017,127.0.0.1:27017
database: PcWeb

my:
name: prod

RedisConfig.java配置文件

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.RedisServer;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;

import java.util.HashSet;
import java.util.Set;

@Configuration
@Slf4j
public class RedisConfig {

@Value("${spring.redis.timeout}")
private int timeout;
@Value("${spring.redis.sentinel.master}")
private String master;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.sentinel.nodes}")
private String Sentinels;
@Value("${spring.redis.pool.max-active}")
private int maxActive;
@Value("${spring.redis.database}")
private int database;
@Value("${spring.redis.pool.max-idle}")
private int maxIdle;
@Value("${spring.redis.pool.min-idle}")
private int minIdle;
@Value("${spring.redis.pool.max-wait}")
private long maxWait;
@Value("${spring.redis.password}")
private String password;

@Bean
public JedisConnectionFactory jedisConnectionFactory(@Autowired RedisSentinelConfiguration redisSentinelConfiguration) {
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisSentinelConfiguration);
jedisConnectionFactory.setPassword(password);
return jedisConnectionFactory;
}

@Bean
public RedisSentinelConfiguration redisSentinelConfiguration() {
RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration();
redisSentinelConfiguration.setMaster(master);
String[] sens = Sentinels.split(",");
for (int i = 0; i < sens.length; i++) {
String[] str = sens[i].split(":");
RedisNode redisServer = new RedisServer(str[0], Integer.parseInt(str[1]));
redisSentinelConfiguration.sentinel(redisServer);
}
return redisSentinelConfiguration;
}

/**
* jedisPool连接池统一通过redisUtil来获取连接
*
* @param jedisSentinelPool
* @param jedisPoolConfig
* @return
*/
@Bean("jedisPool")
public JedisPool jedisPool(@Autowired JedisSentinelPool jedisSentinelPool, @Autowired JedisPoolConfig jedisPoolConfig) {
JedisPool jedisPool = new JedisPool(jedisPoolConfig, jedisSentinelPool.getCurrentHostMaster().getHost(), jedisSentinelPool.getCurrentHostMaster().getPort(), timeout, password);
return jedisPool;
}

/**
* 哨兵
*
* @param jedisPoolConfig
* @return
*/
@Bean
public JedisSentinelPool jedisSentinelPool(@Autowired JedisPoolConfig jedisPoolConfig) {
String[] strs = Sentinels.split(",");
Set set = new HashSet();
for (int i = 0; i < strs.length; i++) {
set.add(strs[i]);
}
JedisSentinelPool jedisSentinelPool = new JedisSentinelPool(master, set, jedisPoolConfig, password);
return jedisSentinelPool;
}

@Bean("jedisPoolConfig")
public JedisPoolConfig getJedisPoolConfig() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
//连接耗尽时是否阻塞, false报异常,true阻塞直到超时, 默认true
jedisPoolConfig.setBlockWhenExhausted(true);
//在空闲时检查有效性, 默认false
jedisPoolConfig.setTestWhileIdle(true);
//获取连接是检查有效性
jedisPoolConfig.setTestOnBorrow(true);
//最大连接数
jedisPoolConfig.setMaxTotal(maxActive);
//对象空闲多久后逐出, 当空闲时间>该值 且 空闲连接>最大空闲数 时直接逐出,不再根据MinEvictableIdleTimeMillis判断 (默认逐出策略)
jedisPoolConfig.setSoftMinEvictableIdleTimeMillis(1800000);
//最大空闲连接数, 默认8个
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setBlockWhenExhausted(true);
//最小空闲连接数, 默认0
jedisPoolConfig.setMinIdle(minIdle);
//逐出连接的最小空闲时间 默认1800000毫秒(15分钟)
jedisPoolConfig.setTimeBetweenEvictionRunsMillis(900000);
//获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间, 默认-1
jedisPoolConfig.setMaxWaitMillis(maxWait);
return jedisPoolConfig;
}
}

posted on 2019-12-11 11:46  卖小女孩的小男孩  阅读(1031)  评论(0)    收藏  举报