|
@@ -11,14 +11,12 @@ import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.data.redis.connection.*;
|
|
|
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
|
|
|
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
|
|
-
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
|
|
|
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
import redis.clients.jedis.JedisPoolConfig;
|
|
|
|
|
|
-import java.net.UnknownHostException;
|
|
|
import java.util.HashSet;
|
|
|
import java.util.Set;
|
|
|
|
|
@@ -29,245 +27,244 @@ import java.util.Set;
|
|
|
public class RedisConfig {
|
|
|
|
|
|
|
|
|
- /**
|
|
|
- * 连接池中的最小空闲连接
|
|
|
- */
|
|
|
- @Value("${spring.redis.pool.min-idle}")
|
|
|
- private int minIdle;
|
|
|
+ /**
|
|
|
+ * 连接池中的最小空闲连接
|
|
|
+ */
|
|
|
+ @Value("${spring.redis.pool.min-idle}")
|
|
|
+ private int minIdle;
|
|
|
|
|
|
- /**
|
|
|
- * 连接池中的最大空闲连接
|
|
|
- */
|
|
|
- @Value("${spring.redis.pool.max-idle}")
|
|
|
- private int maxIdle;
|
|
|
+ /**
|
|
|
+ * 连接池中的最大空闲连接
|
|
|
+ */
|
|
|
+ @Value("${spring.redis.pool.max-idle}")
|
|
|
+ private int maxIdle;
|
|
|
|
|
|
- /**
|
|
|
- * 连接池最大阻塞等待时间
|
|
|
- */
|
|
|
- @Value("${spring.redis.pool.max-wait}")
|
|
|
- private int maxWait;
|
|
|
+ /**
|
|
|
+ * 连接池最大阻塞等待时间
|
|
|
+ */
|
|
|
+ @Value("${spring.redis.pool.max-wait}")
|
|
|
+ private int maxWait;
|
|
|
|
|
|
- /**
|
|
|
- * 连接池最大连接数
|
|
|
- */
|
|
|
- @Value("${spring.redis.pool.max-active}")
|
|
|
- private int maxActive;
|
|
|
+ /**
|
|
|
+ * 连接池最大连接数
|
|
|
+ */
|
|
|
+ @Value("${spring.redis.pool.max-active}")
|
|
|
+ private int maxActive;
|
|
|
|
|
|
- /**
|
|
|
- * 是否使用哨兵集群模式
|
|
|
- */
|
|
|
- @Value("${spring.redis.use.cluster}")
|
|
|
- private boolean useCluster;
|
|
|
+ /**
|
|
|
+ * 是否使用哨兵集群模式
|
|
|
+ */
|
|
|
+ @Value("${spring.redis.use.cluster}")
|
|
|
+ private boolean useCluster;
|
|
|
|
|
|
- /****** 单机模式 ******/
|
|
|
- /**
|
|
|
- * 连接地址
|
|
|
- */
|
|
|
- @Value("${spring.redis.host}")
|
|
|
- private String host;
|
|
|
+ /****** 单机模式 ******/
|
|
|
+ /**
|
|
|
+ * 连接地址
|
|
|
+ */
|
|
|
+ @Value("${spring.redis.host}")
|
|
|
+ private String host;
|
|
|
|
|
|
- /**
|
|
|
- * 连接端口号
|
|
|
- */
|
|
|
- @Value("${spring.redis.port}")
|
|
|
- private int port;
|
|
|
+ /**
|
|
|
+ * 连接端口号
|
|
|
+ */
|
|
|
+ @Value("${spring.redis.port}")
|
|
|
+ private int port;
|
|
|
|
|
|
- /**
|
|
|
- * 认证密钥
|
|
|
- */
|
|
|
- @Value("${spring.redis.password.standalone}")
|
|
|
- private String passwordStandalone;
|
|
|
+ /**
|
|
|
+ * 认证密钥
|
|
|
+ */
|
|
|
+ @Value("${spring.redis.password.standalone}")
|
|
|
+ private String passwordStandalone;
|
|
|
|
|
|
- /****** 哨兵集群模式 ******/
|
|
|
- /**
|
|
|
- * 哨兵集群节点地址
|
|
|
- */
|
|
|
- @Value("${spring.redis.sentinel.nodes}")
|
|
|
- private String sentinelNodes;
|
|
|
+ /****** 哨兵集群模式 ******/
|
|
|
+ /**
|
|
|
+ * 哨兵集群节点地址
|
|
|
+ */
|
|
|
+ @Value("${spring.redis.sentinel.nodes}")
|
|
|
+ private String sentinelNodes;
|
|
|
|
|
|
- /**
|
|
|
- * 哨兵集群监控主节点名称
|
|
|
- */
|
|
|
- @Value("${spring.redis.sentinel.master}")
|
|
|
- private String sentinelMaster;
|
|
|
+ /**
|
|
|
+ * 哨兵集群监控主节点名称
|
|
|
+ */
|
|
|
+ @Value("${spring.redis.sentinel.master}")
|
|
|
+ private String sentinelMaster;
|
|
|
|
|
|
- /**
|
|
|
- * 认证密钥
|
|
|
- */
|
|
|
- @Value("${spring.redis.password.sentinel}")
|
|
|
- private String passwordSentinel;
|
|
|
+ /**
|
|
|
+ * 认证密钥
|
|
|
+ */
|
|
|
+ @Value("${spring.redis.password.sentinel}")
|
|
|
+ private String passwordSentinel;
|
|
|
|
|
|
|
|
|
- /**
|
|
|
- * 索引1
|
|
|
- */
|
|
|
- @Value("${spring.redis.database.fourteen:14}")
|
|
|
- private Integer databaseFourteen;
|
|
|
+ /**
|
|
|
+ * 索引1
|
|
|
+ */
|
|
|
+ @Value("${spring.redis.database.fourteen:14}")
|
|
|
+ private Integer databaseFourteen;
|
|
|
|
|
|
- @Value("${integrated.redisson.password}")
|
|
|
- private String password;
|
|
|
+ @Value("${integrated.redisson.password}")
|
|
|
+ private String password;
|
|
|
|
|
|
- @Value("${integrated.redisson.single-server-config.address}")
|
|
|
- private String address;
|
|
|
+ @Value("${integrated.redisson.single-server-config.address}")
|
|
|
+ private String address;
|
|
|
|
|
|
- @Value("${integrated.redisson.singleServerConfig.connectionMinimumIdleSize}")
|
|
|
- private Integer connectionMinimumIdleSize;
|
|
|
+ @Value("${integrated.redisson.singleServerConfig.connectionMinimumIdleSize}")
|
|
|
+ private Integer connectionMinimumIdleSize;
|
|
|
|
|
|
- /**
|
|
|
- * Description: 连接池配置信息
|
|
|
- *
|
|
|
- * @return GenericObjectPoolConfig
|
|
|
- * @author shaohongbo
|
|
|
- * @since 2019年7月18日:上午10:03:29
|
|
|
- */
|
|
|
- private JedisClientConfiguration getJedisClientConfiguration() {
|
|
|
- JedisClientConfiguration.JedisClientConfigurationBuilder builder = JedisClientConfiguration.builder();
|
|
|
- JedisPoolConfig jedisPool = new JedisPoolConfig();
|
|
|
- jedisPool.setMinIdle(minIdle);
|
|
|
- jedisPool.setMaxIdle(maxIdle);
|
|
|
- jedisPool.setMaxTotal(maxActive);
|
|
|
- jedisPool.setMaxWaitMillis(maxWait);
|
|
|
- JedisClientConfiguration build = builder.usePooling().poolConfig(jedisPool).build();
|
|
|
- return build;
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * Description: 连接池配置信息
|
|
|
+ *
|
|
|
+ * @return GenericObjectPoolConfig
|
|
|
+ * @author shaohongbo
|
|
|
+ * @since 2019年7月18日:上午10:03:29
|
|
|
+ */
|
|
|
+ private JedisClientConfiguration getJedisClientConfiguration() {
|
|
|
+ JedisClientConfiguration.JedisClientConfigurationBuilder builder = JedisClientConfiguration.builder();
|
|
|
+ JedisPoolConfig jedisPool = new JedisPoolConfig();
|
|
|
+ jedisPool.setMinIdle(minIdle);
|
|
|
+ jedisPool.setMaxIdle(maxIdle);
|
|
|
+ jedisPool.setMaxTotal(maxActive);
|
|
|
+ jedisPool.setMaxWaitMillis(maxWait);
|
|
|
+ JedisClientConfiguration build = builder.usePooling().poolConfig(jedisPool).build();
|
|
|
+ return build;
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * Description: Redis连接工厂
|
|
|
- *
|
|
|
- * @param databaseIndex
|
|
|
- * @return RedisConnectionFactory
|
|
|
- * @author shaohongbo
|
|
|
- * @since 2019年3月5日:下午12:07:13
|
|
|
- */
|
|
|
- private RedisConnectionFactory jedisConnectionFactory(int databaseIndex) {
|
|
|
- JedisConnectionFactory jedisConnectionFactory = null;
|
|
|
- if (useCluster) {
|
|
|
- // 哨兵模式
|
|
|
- jedisConnectionFactory = new JedisConnectionFactory(sentinelConfiguration(databaseIndex),
|
|
|
- getJedisClientConfiguration());
|
|
|
- } else {
|
|
|
- jedisConnectionFactory = new JedisConnectionFactory(standaloneConfiguration(databaseIndex),
|
|
|
- getJedisClientConfiguration());
|
|
|
- }
|
|
|
- jedisConnectionFactory.afterPropertiesSet();
|
|
|
- return jedisConnectionFactory;
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * Description: Redis连接工厂
|
|
|
+ *
|
|
|
+ * @param databaseIndex
|
|
|
+ * @return RedisConnectionFactory
|
|
|
+ * @author shaohongbo
|
|
|
+ * @since 2019年3月5日:下午12:07:13
|
|
|
+ */
|
|
|
+ private RedisConnectionFactory jedisConnectionFactory(int databaseIndex) {
|
|
|
+ JedisConnectionFactory jedisConnectionFactory = null;
|
|
|
+ if (useCluster) {
|
|
|
+ // 哨兵模式
|
|
|
+ jedisConnectionFactory = new JedisConnectionFactory(sentinelConfiguration(databaseIndex),
|
|
|
+ getJedisClientConfiguration());
|
|
|
+ } else {
|
|
|
+ jedisConnectionFactory = new JedisConnectionFactory(standaloneConfiguration(databaseIndex),
|
|
|
+ getJedisClientConfiguration());
|
|
|
+ }
|
|
|
+ jedisConnectionFactory.afterPropertiesSet();
|
|
|
+ return jedisConnectionFactory;
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * Description: Redis单机模式配置
|
|
|
- *
|
|
|
- * @param databaseIndex
|
|
|
- * @return RedisStandaloneConfiguration
|
|
|
- * @author shaohongbo
|
|
|
- * @since 2019年7月15日:上午11:19:55
|
|
|
- */
|
|
|
- private RedisStandaloneConfiguration standaloneConfiguration(int databaseIndex) {
|
|
|
- RedisStandaloneConfiguration standaloneConfiguration = new RedisStandaloneConfiguration();
|
|
|
- standaloneConfiguration.setDatabase(databaseIndex);
|
|
|
- standaloneConfiguration.setHostName(host);
|
|
|
- standaloneConfiguration.setPort(port);
|
|
|
- if (!StringUtils.isEmpty(passwordStandalone)) {
|
|
|
- standaloneConfiguration.setPassword(RedisPassword.of(passwordStandalone));
|
|
|
- }
|
|
|
- return standaloneConfiguration;
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * Description: Redis单机模式配置
|
|
|
+ *
|
|
|
+ * @param databaseIndex
|
|
|
+ * @return RedisStandaloneConfiguration
|
|
|
+ * @author shaohongbo
|
|
|
+ * @since 2019年7月15日:上午11:19:55
|
|
|
+ */
|
|
|
+ private RedisStandaloneConfiguration standaloneConfiguration(int databaseIndex) {
|
|
|
+ RedisStandaloneConfiguration standaloneConfiguration = new RedisStandaloneConfiguration();
|
|
|
+ standaloneConfiguration.setDatabase(databaseIndex);
|
|
|
+ standaloneConfiguration.setHostName(host);
|
|
|
+ standaloneConfiguration.setPort(port);
|
|
|
+ if (!StringUtils.isEmpty(passwordStandalone)) {
|
|
|
+ standaloneConfiguration.setPassword(RedisPassword.of(passwordStandalone));
|
|
|
+ }
|
|
|
+ return standaloneConfiguration;
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * Description: Redis哨兵集群模式配置
|
|
|
- *
|
|
|
- * @param databaseIndex
|
|
|
- * @return RedisSentinelConfiguration
|
|
|
- * @author maxinyu
|
|
|
- * @since 2019年7月15日:上午11:23:46
|
|
|
- */
|
|
|
- private RedisSentinelConfiguration sentinelConfiguration(int databaseIndex) {
|
|
|
- RedisSentinelConfiguration sentinelConfiguration = new RedisSentinelConfiguration();
|
|
|
- sentinelConfiguration.setDatabase(databaseIndex);
|
|
|
- sentinelConfiguration.setMaster(sentinelMaster);
|
|
|
- // 配置redis的哨兵sentinel
|
|
|
- String[] hosts = StringUtils.delimitedListToStringArray(sentinelNodes, ",");
|
|
|
- Set<RedisNode> redisNodeSet = new HashSet<RedisNode>();
|
|
|
- for (String redisHost : hosts) {
|
|
|
- String[] item = redisHost.split(":");
|
|
|
- String ip = item[0];
|
|
|
- String port = item[1];
|
|
|
- redisNodeSet.add(new RedisNode(ip, Integer.parseInt(port)));
|
|
|
- }
|
|
|
- sentinelConfiguration.setSentinels(redisNodeSet);
|
|
|
- if (!StringUtils.isEmpty(passwordSentinel)) {
|
|
|
- sentinelConfiguration.setPassword(RedisPassword.of(passwordSentinel));
|
|
|
- }
|
|
|
- return sentinelConfiguration;
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * Description: Redis哨兵集群模式配置
|
|
|
+ *
|
|
|
+ * @param databaseIndex
|
|
|
+ * @return RedisSentinelConfiguration
|
|
|
+ * @author maxinyu
|
|
|
+ * @since 2019年7月15日:上午11:23:46
|
|
|
+ */
|
|
|
+ private RedisSentinelConfiguration sentinelConfiguration(int databaseIndex) {
|
|
|
+ RedisSentinelConfiguration sentinelConfiguration = new RedisSentinelConfiguration();
|
|
|
+ sentinelConfiguration.setDatabase(databaseIndex);
|
|
|
+ sentinelConfiguration.setMaster(sentinelMaster);
|
|
|
+ // 配置redis的哨兵sentinel
|
|
|
+ String[] hosts = StringUtils.delimitedListToStringArray(sentinelNodes, ",");
|
|
|
+ Set<RedisNode> redisNodeSet = new HashSet<RedisNode>();
|
|
|
+ for (String redisHost : hosts) {
|
|
|
+ String[] item = redisHost.split(":");
|
|
|
+ String ip = item[0];
|
|
|
+ String port = item[1];
|
|
|
+ redisNodeSet.add(new RedisNode(ip, Integer.parseInt(port)));
|
|
|
+ }
|
|
|
+ sentinelConfiguration.setSentinels(redisNodeSet);
|
|
|
+ if (!StringUtils.isEmpty(passwordSentinel)) {
|
|
|
+ sentinelConfiguration.setPassword(RedisPassword.of(passwordSentinel));
|
|
|
+ }
|
|
|
+ return sentinelConfiguration;
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * Description: 初始化Redis连接模板
|
|
|
- *
|
|
|
- * @param template
|
|
|
- * @param connectionFactory
|
|
|
- * @return void
|
|
|
- * @author maxinyu
|
|
|
- * @since 2019年3月5日:下午2:10:34
|
|
|
- */
|
|
|
- @SuppressWarnings({ "rawtypes", "unchecked" })
|
|
|
- private void initDomainRedisTemplate(RedisTemplate<Object, Object> template,
|
|
|
- RedisConnectionFactory connectionFactory) {
|
|
|
- template.setConnectionFactory(connectionFactory);
|
|
|
- Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
|
|
|
- ObjectMapper om = new ObjectMapper();
|
|
|
- om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
|
|
- // om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
|
|
|
- jackson2JsonRedisSerializer.setObjectMapper(om);
|
|
|
- StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
|
|
|
- // key采用String的序列化方式
|
|
|
- template.setKeySerializer(stringRedisSerializer);
|
|
|
- // hash的key也采用String的序列化方式
|
|
|
- template.setHashKeySerializer(stringRedisSerializer);
|
|
|
- // value序列化方式采用jackson
|
|
|
- template.setValueSerializer(jackson2JsonRedisSerializer);
|
|
|
- // hash的value序列化方式采用jackson
|
|
|
- template.setHashValueSerializer(jackson2JsonRedisSerializer);
|
|
|
- // template.afterPropertiesSet();
|
|
|
- // 序列化模板
|
|
|
- // template.setDefaultSerializer(new StringRedisSerializer());
|
|
|
- // template.setKeySerializer(new StringRedisSerializer());
|
|
|
- // template.setValueSerializer(new StringRedisSerializer());
|
|
|
- // template.setHashKeySerializer(new StringRedisSerializer());
|
|
|
- // template.setHashValueSerializer(new StringRedisSerializer());
|
|
|
- // template.setConnectionFactory(connectionFactory);
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * Description: 初始化Redis连接模板
|
|
|
+ *
|
|
|
+ * @param template
|
|
|
+ * @param connectionFactory
|
|
|
+ * @return void
|
|
|
+ * @author maxinyu
|
|
|
+ * @since 2019年3月5日:下午2:10:34
|
|
|
+ */
|
|
|
+ @SuppressWarnings({"rawtypes", "unchecked"})
|
|
|
+ private void initDomainRedisTemplate(
|
|
|
+ RedisTemplate<Object, Object> template,
|
|
|
+ RedisConnectionFactory connectionFactory) {
|
|
|
+ template.setConnectionFactory(connectionFactory);
|
|
|
+ Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
|
|
|
+ ObjectMapper om = new ObjectMapper();
|
|
|
+ om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
|
|
|
+ // om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
|
|
|
+ jackson2JsonRedisSerializer.setObjectMapper(om);
|
|
|
+ StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
|
|
|
+ // key采用String的序列化方式
|
|
|
+ template.setKeySerializer(stringRedisSerializer);
|
|
|
+ // hash的key也采用String的序列化方式
|
|
|
+ template.setHashKeySerializer(stringRedisSerializer);
|
|
|
+ // value序列化方式采用jackson
|
|
|
+ template.setValueSerializer(jackson2JsonRedisSerializer);
|
|
|
+ // hash的value序列化方式采用jackson
|
|
|
+ template.setHashValueSerializer(jackson2JsonRedisSerializer);
|
|
|
+ // template.afterPropertiesSet();
|
|
|
+ // 序列化模板
|
|
|
+ // template.setDefaultSerializer(new StringRedisSerializer());
|
|
|
+ // template.setKeySerializer(new StringRedisSerializer());
|
|
|
+ // template.setValueSerializer(new StringRedisSerializer());
|
|
|
+ // template.setHashKeySerializer(new StringRedisSerializer());
|
|
|
+ // template.setHashValueSerializer(new StringRedisSerializer());
|
|
|
+ // template.setConnectionFactory(connectionFactory);
|
|
|
+ }
|
|
|
|
|
|
- /**
|
|
|
- * Description:
|
|
|
- *
|
|
|
- * @return RedisTemplate
|
|
|
- * @author maxinyu
|
|
|
- * @since 2019年3月5日:下午2:06:37
|
|
|
- */
|
|
|
- @Bean(value = "alarmRedisTemplate")
|
|
|
- public RedisTemplate<Object, Object> redisTemplate() {
|
|
|
- RedisTemplate<Object, Object> template = new RedisTemplate<>();
|
|
|
- // initDomainRedisTemplate(template,
|
|
|
- // lettuceConnectionFactory(databaseZero));
|
|
|
- initDomainRedisTemplate(template, jedisConnectionFactory(databaseFourteen));
|
|
|
- template.setDefaultSerializer(new StringRedisSerializer());
|
|
|
- return template;
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * Description:
|
|
|
+ *
|
|
|
+ * @return RedisTemplate
|
|
|
+ * @author maxinyu
|
|
|
+ * @since 2019年3月5日:下午2:06:37
|
|
|
+ */
|
|
|
+ @Bean(value = "alarmRedisTemplate")
|
|
|
+ public RedisTemplate<Object, Object> redisTemplate() {
|
|
|
+ RedisTemplate<Object, Object> template = new RedisTemplate<>();
|
|
|
+ template.setConnectionFactory(redisConnectionFactory());
|
|
|
+ template.setDefaultSerializer(new StringRedisSerializer());
|
|
|
+ return template;
|
|
|
+ }
|
|
|
|
|
|
- @Bean
|
|
|
- public JedisConnectionFactory redisConnectionFactory() throws UnknownHostException {
|
|
|
- JedisConnectionFactory jedisConnectionFactory = null;
|
|
|
- if (useCluster) {
|
|
|
- // 哨兵模式
|
|
|
- jedisConnectionFactory = new JedisConnectionFactory(sentinelConfiguration(databaseFourteen),
|
|
|
- getJedisClientConfiguration());
|
|
|
- } else {
|
|
|
- // 单机模式
|
|
|
- jedisConnectionFactory = new JedisConnectionFactory(standaloneConfiguration(databaseFourteen),
|
|
|
- getJedisClientConfiguration());
|
|
|
- }
|
|
|
- jedisConnectionFactory.afterPropertiesSet();
|
|
|
- return jedisConnectionFactory;
|
|
|
- }
|
|
|
+ @Bean
|
|
|
+ public JedisConnectionFactory redisConnectionFactory() {
|
|
|
+ JedisConnectionFactory jedisConnectionFactory = null;
|
|
|
+ if (useCluster) {
|
|
|
+ // 哨兵模式
|
|
|
+ jedisConnectionFactory = new JedisConnectionFactory(sentinelConfiguration(databaseFourteen),
|
|
|
+ getJedisClientConfiguration());
|
|
|
+ } else {
|
|
|
+ // 单机模式
|
|
|
+ jedisConnectionFactory = new JedisConnectionFactory(standaloneConfiguration(databaseFourteen),
|
|
|
+ getJedisClientConfiguration());
|
|
|
+ }
|
|
|
+ jedisConnectionFactory.afterPropertiesSet();
|
|
|
+ return jedisConnectionFactory;
|
|
|
+ }
|
|
|
|
|
|
}
|