generated from root/miduo_server
redis 连接哨兵
parent
4bf512fbfe
commit
afaf52cb59
|
@ -8,6 +8,7 @@ import com.jmfy.server.ServerConfiguration;
|
||||||
import com.jmfy.server.ServerProperties;
|
import com.jmfy.server.ServerProperties;
|
||||||
import com.jmfy.thrift.pool.ThriftPoolUtils;
|
import com.jmfy.thrift.pool.ThriftPoolUtils;
|
||||||
import com.jmfy.util.JsonUtil;
|
import com.jmfy.util.JsonUtil;
|
||||||
|
import com.jmfy.util.MyStringRedisTemplate;
|
||||||
import com.jmfy.util.RedisUtil;
|
import com.jmfy.util.RedisUtil;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
@ -33,8 +34,8 @@ public class Application {
|
||||||
// KProducer kProducer = configurableApplicationContext.getBean(KProducer.class);
|
// KProducer kProducer = configurableApplicationContext.getBean(KProducer.class);
|
||||||
// KProducer.init();
|
// KProducer.init();
|
||||||
//redis初始化
|
//redis初始化
|
||||||
RedisAutoConfiguration redisAutoConfiguration = configurableApplicationContext.getBean(RedisAutoConfiguration.class);
|
MyStringRedisTemplate myStringRedisTemplate = configurableApplicationContext.getBean(MyStringRedisTemplate.class);
|
||||||
RedisUtil.getInstence().init(redisAutoConfiguration.getRedisProperties());
|
RedisUtil.getInstence().init(myStringRedisTemplate.getRedisObjectTemplate());
|
||||||
|
|
||||||
RedisUtil.getInstence().initServerIPandProt(serverProperties);
|
RedisUtil.getInstence().initServerIPandProt(serverProperties);
|
||||||
// Map<String, BaseHandler> commandHanderMap = configurableApplicationContext.getBeansOfType(BaseHandler.class);
|
// Map<String, BaseHandler> commandHanderMap = configurableApplicationContext.getBeansOfType(BaseHandler.class);
|
||||||
|
|
|
@ -15,6 +15,8 @@ public class RedisProperties {
|
||||||
private String password;
|
private String password;
|
||||||
|
|
||||||
private int expireTime; // 过期时间,单位分钟
|
private int expireTime; // 过期时间,单位分钟
|
||||||
|
|
||||||
|
private String sentine;
|
||||||
private int database ;
|
private int database ;
|
||||||
//连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
|
//连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
|
||||||
private boolean blockWhenExhausted;
|
private boolean blockWhenExhausted;
|
||||||
|
@ -186,4 +188,12 @@ public class RedisProperties {
|
||||||
public void setDatabase(int database) {
|
public void setDatabase(int database) {
|
||||||
this.database = database;
|
this.database = database;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getSentine() {
|
||||||
|
return sentine;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSentine(String sentine) {
|
||||||
|
this.sentine = sentine;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,93 @@
|
||||||
|
package com.jmfy.util;
|
||||||
|
|
||||||
|
import com.jmfy.redisProperties.RedisProperties;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.redis.connection.RedisNode;
|
||||||
|
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
|
||||||
|
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
||||||
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import redis.clients.jedis.JedisPoolConfig;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class MyStringRedisTemplate {
|
||||||
|
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(MyStringRedisTemplate.class);
|
||||||
|
|
||||||
|
private StringRedisTemplate redisObjectTemplate;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
RedisProperties redisProperties;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void init() {
|
||||||
|
RedisSentinelConfiguration redisSentinelConfiguration = null;
|
||||||
|
String sentine = redisProperties.getSentine();
|
||||||
|
if (sentine != null && !sentine.isEmpty()) {
|
||||||
|
redisSentinelConfiguration = new RedisSentinelConfiguration();
|
||||||
|
String[] split = sentine.split("#");
|
||||||
|
for (int i = 0; i < split.length; i++) {
|
||||||
|
String[] split1 = split[i].split(":");
|
||||||
|
RedisNode redisNode = new RedisNode(split1[0], Integer.parseInt(split1[1]));
|
||||||
|
redisSentinelConfiguration.addSentinel(redisNode);
|
||||||
|
}
|
||||||
|
redisSentinelConfiguration.setMaster("mymaster");
|
||||||
|
}
|
||||||
|
// 设置参数
|
||||||
|
JedisPoolConfig config = new JedisPoolConfig();
|
||||||
|
//连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
|
||||||
|
config.setBlockWhenExhausted(true);
|
||||||
|
//设置的逐出策略类名, 默认DefaultEvictionPolicy(当连接超过最大空闲时间,或连接数超过最大空闲连接数)
|
||||||
|
config.setEvictionPolicyClassName("org.apache.commons.pool2.impl.DefaultEvictionPolicy");
|
||||||
|
//是否启用pool的jmx管理功能, 默认true
|
||||||
|
config.setJmxEnabled(true);
|
||||||
|
//MBean ObjectName = new ObjectName("org.apache.commons.pool2:type=GenericObjectPool,name=" + "pool" + i); 默 认为"pool", JMX不熟,具体不知道是干啥的...默认就好.
|
||||||
|
config.setJmxNamePrefix("pool");
|
||||||
|
//是否启用后进先出, 默认true
|
||||||
|
config.setLifo(true);
|
||||||
|
//最大空闲连接数, 默认8个
|
||||||
|
config.setMaxIdle(100);
|
||||||
|
//最大连接数, 默认8个
|
||||||
|
config.setMaxTotal(1000);
|
||||||
|
//获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间, 默认-1
|
||||||
|
config.setMaxWaitMillis(-1);
|
||||||
|
//逐出连接的最小空闲时间 默认1800000毫秒(30分钟)
|
||||||
|
config.setMinEvictableIdleTimeMillis(1800000);
|
||||||
|
//最小空闲连接数, 默认0
|
||||||
|
config.setMinIdle(100);
|
||||||
|
//每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3
|
||||||
|
config.setNumTestsPerEvictionRun(3);
|
||||||
|
//对象空闲多久后逐出, 当空闲时间>该值 且 空闲连接>最大空闲数 时直接逐出,不再根据MinEvictableIdleTimeMillis判断 (默认逐出策略)
|
||||||
|
config.setSoftMinEvictableIdleTimeMillis(1800000);
|
||||||
|
//在获取连接的时候检查有效性, 默认false
|
||||||
|
config.setTestOnBorrow(false);
|
||||||
|
//在空闲时检查有效性, 默认false
|
||||||
|
config.setTestWhileIdle(false);
|
||||||
|
//逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1
|
||||||
|
config.setTimeBetweenEvictionRunsMillis(-1);
|
||||||
|
//实例化链接工厂
|
||||||
|
JedisConnectionFactory connectionFactory = new JedisConnectionFactory(redisSentinelConfiguration, config);
|
||||||
|
if (redisSentinelConfiguration == null) {
|
||||||
|
//设置host
|
||||||
|
connectionFactory.setHostName(redisProperties.getHost());
|
||||||
|
//设置端口
|
||||||
|
connectionFactory.setPort(redisProperties.getPort());
|
||||||
|
}
|
||||||
|
//设置密码
|
||||||
|
connectionFactory.setPassword(redisProperties.getPassword());
|
||||||
|
|
||||||
|
//初始化connectionFactory
|
||||||
|
connectionFactory.afterPropertiesSet();
|
||||||
|
//实例化
|
||||||
|
redisObjectTemplate = new StringRedisTemplate(connectionFactory);
|
||||||
|
LOGGER.info("initUserCacheDb==>host={},port={} isSentine=>{} redis success !!!", redisProperties.getHost(), redisProperties.getPort(), connectionFactory.isRedisSentinelAware());
|
||||||
|
}
|
||||||
|
|
||||||
|
public StringRedisTemplate getRedisObjectTemplate() {
|
||||||
|
return redisObjectTemplate;
|
||||||
|
}
|
||||||
|
}
|
|
@ -55,19 +55,22 @@ public class RedisUtil {
|
||||||
private static final RedisUtil SINGLETON = new RedisUtil();
|
private static final RedisUtil SINGLETON = new RedisUtil();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void init(RedisProperties redisProperties) {
|
public void init(StringRedisTemplate stringRedisTemplate) {
|
||||||
try {
|
redisObjectTemplate = stringRedisTemplate;
|
||||||
//initStringCacheDb(0, redisProperties);
|
|
||||||
initUserCacheDb(0,redisProperties);
|
|
||||||
initOldUserCacheDb();
|
|
||||||
LOGGER.info("redis init ..");
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
LOGGER.error("------------------Redis 未启动------------------");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
// public void init(RedisProperties redisProperties) {
|
||||||
|
// try {
|
||||||
|
// //initStringCacheDb(0, redisProperties);
|
||||||
|
//// initUserCacheDb(0,redisProperties);
|
||||||
|
//// initOldUserCacheDb();
|
||||||
|
// LOGGER.info("redis init ..");
|
||||||
|
//
|
||||||
|
// } catch (Exception e) {
|
||||||
|
// LOGGER.error("------------------Redis 未启动------------------");
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
|
||||||
private void initOldUserCacheDb() {
|
private void initOldUserCacheDb() {
|
||||||
//实例化链接工厂
|
//实例化链接工厂
|
||||||
|
|
|
@ -7,8 +7,9 @@ server.deliveryAddress=60.1.1.12:9991
|
||||||
#spring.redis.host = 150.116.94.74
|
#spring.redis.host = 150.116.94.74
|
||||||
spring.redis.host = 60.1.1.14
|
spring.redis.host = 60.1.1.14
|
||||||
spring.redis.port = 6379
|
spring.redis.port = 6379
|
||||||
spring.redis.password = redis.ljsd.COM@
|
spring.redis.password =
|
||||||
spring.redis.expireTime = -1
|
spring.redis.expireTime = -1
|
||||||
|
#spring.redis.sentine = 10.10.13.3:26379#10.10.13.2:26379#10.10.13.4:26379
|
||||||
|
|
||||||
spring.data.mongodb.uri = mongodb://60.1.1.14:27017/ysj_core
|
spring.data.mongodb.uri = mongodb://60.1.1.14:27017/ysj_core
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue