generated from root/miduo_server
连接redis哨兵
parent
c2c9492488
commit
32f619431b
|
|
@ -2,6 +2,7 @@ package com;
|
||||||
|
|
||||||
import com.jmfy.redisProperties.RedisAutoConfiguration;
|
import com.jmfy.redisProperties.RedisAutoConfiguration;
|
||||||
import com.jmfy.thrift.pool.ThriftPoolUtils;
|
import com.jmfy.thrift.pool.ThriftPoolUtils;
|
||||||
|
import com.jmfy.utils.MyStringRedisTemplate;
|
||||||
import com.jmfy.utils.RedisUtil;
|
import com.jmfy.utils.RedisUtil;
|
||||||
import com.jmfy.utils.STableManager;
|
import com.jmfy.utils.STableManager;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
|
|
@ -20,8 +21,8 @@ import org.springframework.context.ConfigurableApplicationContext;
|
||||||
public class Application extends SpringBootServletInitializer {
|
public class Application extends SpringBootServletInitializer {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
ConfigurableApplicationContext configurableApplicationContext = SpringApplication.run(Application.class, args);
|
ConfigurableApplicationContext configurableApplicationContext = SpringApplication.run(Application.class, args);
|
||||||
RedisAutoConfiguration redisAutoConfiguration = configurableApplicationContext.getBean(RedisAutoConfiguration.class);
|
MyStringRedisTemplate myStringRedisTemplate = configurableApplicationContext.getBean(MyStringRedisTemplate.class);
|
||||||
RedisUtil.getInstence().init(redisAutoConfiguration.getRedisProperties());
|
RedisUtil.getInstence().init(myStringRedisTemplate.getRedisObjectTemplate());
|
||||||
//初使化连接池util类
|
//初使化连接池util类
|
||||||
ThriftPoolUtils.getInstance().initContext(configurableApplicationContext);
|
ThriftPoolUtils.getInstance().initContext(configurableApplicationContext);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import com.jmfy.model.CFamilyUser;
|
||||||
import com.jmfy.redisProperties.RedisUserKey;
|
import com.jmfy.redisProperties.RedisUserKey;
|
||||||
import com.jmfy.utils.Connect;
|
import com.jmfy.utils.Connect;
|
||||||
import com.jmfy.utils.MongoName;
|
import com.jmfy.utils.MongoName;
|
||||||
|
import com.jmfy.utils.MyStringRedisTemplate;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||||
import org.springframework.data.mongodb.core.query.Criteria;
|
import org.springframework.data.mongodb.core.query.Criteria;
|
||||||
|
|
@ -14,13 +15,20 @@ import org.springframework.data.mongodb.core.query.Query;
|
||||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@Component
|
@Component
|
||||||
public class FamilyDaoImpl implements FamilyDao {
|
public class FamilyDaoImpl implements FamilyDao {
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private StringRedisTemplate stringTemplate;
|
private StringRedisTemplate stringTemplate;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MyStringRedisTemplate myStringRedisTemplate;
|
||||||
|
@PostConstruct
|
||||||
|
public void init() {
|
||||||
|
stringTemplate = myStringRedisTemplate.getRedisObjectTemplate();
|
||||||
|
}
|
||||||
private static final String ARENA_RANK = "CHAOS_AND_DISPUTE_ARENA_RANK";
|
private static final String ARENA_RANK = "CHAOS_AND_DISPUTE_ARENA_RANK";
|
||||||
@Resource
|
@Resource
|
||||||
private Connect connect;
|
private Connect connect;
|
||||||
|
|
|
||||||
|
|
@ -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 void setSentine(String sentine) {
|
||||||
|
this.sentine = sentine;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSentine() {
|
||||||
|
return sentine;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,93 @@
|
||||||
|
package com.jmfy.utils;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -49,71 +49,8 @@ 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);
|
|
||||||
LOGGER.info("redis Host={},port={},redis init ..", redisProperties.getHost(), redisProperties.getPort());
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
LOGGER.error("------------------Redis 未启动------------------");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private void initUserCacheDb(int dbIndex, RedisProperties redisProperties) {
|
|
||||||
//实例化链接工厂
|
|
||||||
JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
|
|
||||||
//设置host
|
|
||||||
connectionFactory.setHostName(redisProperties.getHost());
|
|
||||||
//设置端口
|
|
||||||
connectionFactory.setPort(redisProperties.getPort());
|
|
||||||
// 分库
|
|
||||||
connectionFactory.setDatabase(dbIndex);
|
|
||||||
//设置密码
|
|
||||||
connectionFactory.setPassword(redisProperties.getPassword());
|
|
||||||
// 设置参数
|
|
||||||
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(8);
|
|
||||||
//最大连接数, 默认8个
|
|
||||||
config.setMaxTotal(1000);
|
|
||||||
//获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间, 默认-1
|
|
||||||
config.setMaxWaitMillis(-1);
|
|
||||||
//逐出连接的最小空闲时间 默认1800000毫秒(30分钟)
|
|
||||||
config.setMinEvictableIdleTimeMillis(1800000);
|
|
||||||
//最小空闲连接数, 默认0
|
|
||||||
config.setMinIdle(0);
|
|
||||||
//每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3
|
|
||||||
config.setNumTestsPerEvictionRun(3);
|
|
||||||
//对象空闲多久后逐出, 当空闲时间>该值 且 空闲连接>最大空闲数 时直接逐出,不再根据MinEvictableIdleTimeMillis判断 (默认逐出策略)
|
|
||||||
config.setSoftMinEvictableIdleTimeMillis(1800000);
|
|
||||||
//在获取连接的时候检查有效性, 默认false
|
|
||||||
config.setTestOnBorrow(false);
|
|
||||||
//在空闲时检查有效性, 默认false
|
|
||||||
config.setTestWhileIdle(false);
|
|
||||||
//逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1
|
|
||||||
config.setTimeBetweenEvictionRunsMillis(-1);
|
|
||||||
|
|
||||||
connectionFactory.setPoolConfig(config);
|
|
||||||
|
|
||||||
//初始化connectionFactory
|
|
||||||
connectionFactory.afterPropertiesSet();
|
|
||||||
//实例化
|
|
||||||
redisObjectTemplate = new StringRedisTemplate(connectionFactory);
|
|
||||||
// redisObjectTemplate.setConnectionFactory(connectionFactory);
|
|
||||||
redisObjectTemplate.afterPropertiesSet();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -11,8 +11,13 @@ spring.data.oldMongodb.uri= mongodb://60.1.1.14:27017
|
||||||
|
|
||||||
# redis config
|
# redis config
|
||||||
spring.redis.host = 60.1.1.14
|
spring.redis.host = 60.1.1.14
|
||||||
|
#spring.redis.port = 6379
|
||||||
|
#spring.redis.password =
|
||||||
|
#spring.redis.expireTime = -1
|
||||||
|
|
||||||
spring.redis.port = 6379
|
spring.redis.port = 6379
|
||||||
spring.redis.password =
|
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.mvc.static-path-pattern=/**
|
spring.mvc.static-path-pattern=/**
|
||||||
Loading…
Reference in New Issue