|
@@ -1,22 +1,27 @@
|
|
|
package com.persagy.apm.diagnose.config;
|
|
|
|
|
|
|
|
|
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
|
|
+import com.fasterxml.jackson.annotation.PropertyAccessor;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
|
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
|
|
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.RedisPassword;
|
|
|
-import org.springframework.data.redis.connection.RedisSentinelConfiguration;
|
|
|
-import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
|
|
|
+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.connection.lettuce.LettuceConnectionFactory;
|
|
|
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
+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;
|
|
|
|
|
@@ -27,159 +32,244 @@ import java.util.Set;
|
|
|
public class RedisConfig {
|
|
|
|
|
|
|
|
|
- @Bean
|
|
|
- @ConfigurationProperties(prefix = "spring.redis.pool")
|
|
|
- public JedisPoolConfig getRedisConfig(){
|
|
|
- return new JedisPoolConfig();
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
- * 主机地址
|
|
|
+ * 连接池中的最小空闲连接
|
|
|
*/
|
|
|
- @Value("${spring.redis.host}")
|
|
|
- private String springRedisHost;
|
|
|
-
|
|
|
+ @Value("${spring.redis.pool.min-idle}")
|
|
|
+ private int minIdle;
|
|
|
+
|
|
|
/**
|
|
|
- * 端口号
|
|
|
+ * 连接池中的最大空闲连接
|
|
|
*/
|
|
|
- @Value("${spring.redis.port}")
|
|
|
- private Integer springRedisPort;
|
|
|
-
|
|
|
+ @Value("${spring.redis.pool.max-idle}")
|
|
|
+ private int maxIdle;
|
|
|
|
|
|
+ /**
|
|
|
+ * 连接池最大阻塞等待时间
|
|
|
+ */
|
|
|
+ @Value("${spring.redis.pool.max-wait}")
|
|
|
+ private int maxWait;
|
|
|
|
|
|
/**
|
|
|
- * 密码(如果有的话)
|
|
|
+ * 连接池最大连接数
|
|
|
*/
|
|
|
- @Value("${spring.redis.password.sentinel}")
|
|
|
- private String springRedisPassword;
|
|
|
+ @Value("${spring.redis.pool.max-active}")
|
|
|
+ private int maxActive;
|
|
|
|
|
|
/**
|
|
|
- * 单机密码
|
|
|
+ * 是否使用哨兵集群模式
|
|
|
*/
|
|
|
- @Value("${spring.redis.password.standalone}")
|
|
|
- private String springRedisPasswordStandalone;
|
|
|
+ @Value("${spring.redis.use.cluster}")
|
|
|
+ private boolean useCluster;
|
|
|
|
|
|
+ /****** 单机模式 ******/
|
|
|
+ /**
|
|
|
+ * 连接地址
|
|
|
+ */
|
|
|
+ @Value("${spring.redis.host}")
|
|
|
+ private String host;
|
|
|
|
|
|
- @Value("${spring.redis.database.ten}")
|
|
|
- private Integer springRedisDatabaseTen;
|
|
|
-
|
|
|
/**
|
|
|
- * 哨兵集群名称
|
|
|
+ * 连接端口号
|
|
|
*/
|
|
|
- @Value("${spring.redis.sentinel.master}")
|
|
|
- private String springRedisSentinelMaster;
|
|
|
-
|
|
|
+ @Value("${spring.redis.port}")
|
|
|
+ private int port;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 认证密钥
|
|
|
+ */
|
|
|
+ @Value("${spring.redis.password.standalone}")
|
|
|
+ private String passwordStandalone;
|
|
|
+
|
|
|
+ /****** 哨兵集群模式 ******/
|
|
|
/**
|
|
|
* 哨兵集群节点地址
|
|
|
*/
|
|
|
@Value("${spring.redis.sentinel.nodes}")
|
|
|
- private String springRedisSentinelNodes;
|
|
|
+ private String sentinelNodes;
|
|
|
|
|
|
-
|
|
|
- @Value("${spring.redis.use.cluster}")
|
|
|
- private Boolean redisUseCluster;
|
|
|
+ /**
|
|
|
+ * 哨兵集群监控主节点名称
|
|
|
+ */
|
|
|
+ @Value("${spring.redis.sentinel.master}")
|
|
|
+ private String sentinelMaster;
|
|
|
|
|
|
+ /**
|
|
|
+ * 认证密钥
|
|
|
+ */
|
|
|
+ @Value("${spring.redis.password.sentinel}")
|
|
|
+ private String passwordSentinel;
|
|
|
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
- *
|
|
|
- *<p>Description: saas:使用索引为10的Redis库 .</p>
|
|
|
- *<p>@return RedisTemplate<String,Object>
|
|
|
- *@since 2019年5月10日: 下午5:18:27
|
|
|
- *@author gezhanbin
|
|
|
+ * 索引1
|
|
|
*/
|
|
|
- @Bean
|
|
|
- public RedisTemplate<String, Object> redisTemplate() {
|
|
|
- RedisTemplate<String, Object> template = new RedisTemplate<>();
|
|
|
- template.setConnectionFactory(redisConnectionFactory(springRedisDatabaseTen));
|
|
|
- template.setDefaultSerializer(new StringRedisSerializer());
|
|
|
- return template;
|
|
|
+ @Value("${spring.redis.database.ten}")
|
|
|
+ private Integer databaseOne;
|
|
|
+
|
|
|
+ @Value("${integrated.redisson.password}")
|
|
|
+ private String password;
|
|
|
+
|
|
|
+ @Value("${integrated.redisson.single-server-config.address}")
|
|
|
+ private String address;
|
|
|
+
|
|
|
+ @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;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- *
|
|
|
- *<p>Description: redis连接工厂 .</p>
|
|
|
- *<p>@param index redis库的索引
|
|
|
- *<p>@return RedisConnectionFactory
|
|
|
- *@since 2019年5月10日: 下午4:49:24
|
|
|
- *@author gezhanbin
|
|
|
- */
|
|
|
- private LettuceConnectionFactory redisConnectionFactory(int index) {
|
|
|
- LettuceConnectionFactory redisConnectionFactory;
|
|
|
- if(redisUseCluster) {
|
|
|
- redisConnectionFactory = new LettuceConnectionFactory(redisSentinelConfiguration(index), jedisClientConfiguration());
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 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 {
|
|
|
- redisConnectionFactory = new LettuceConnectionFactory(redisStandaloneConfiguration(index), jedisClientConfiguration());
|
|
|
+ jedisConnectionFactory = new JedisConnectionFactory(standaloneConfiguration(databaseIndex),
|
|
|
+ getJedisClientConfiguration());
|
|
|
}
|
|
|
- redisConnectionFactory.afterPropertiesSet();
|
|
|
- return redisConnectionFactory;
|
|
|
+ jedisConnectionFactory.afterPropertiesSet();
|
|
|
+ return jedisConnectionFactory;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
- *
|
|
|
- *<p>Description: jedis 客户端 连接池配置.</p>
|
|
|
- *<p>@return JedisClientConfiguration
|
|
|
- *@since 2019年5月10日: 下午4:47:59
|
|
|
- *@author gezhanbin
|
|
|
+ * Description: Redis单机模式配置
|
|
|
+ *
|
|
|
+ * @param databaseIndex
|
|
|
+ * @return RedisStandaloneConfiguration
|
|
|
+ * @author shaohongbo
|
|
|
+ * @since 2019年7月15日:上午11:19:55
|
|
|
*/
|
|
|
- private LettucePoolingClientConfiguration jedisClientConfiguration() {
|
|
|
- return LettucePoolingClientConfiguration.builder().poolConfig(getRedisConfig()).build();
|
|
|
+ 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;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- *
|
|
|
- *<p>Description: 哨兵模式的redis配置.</p>
|
|
|
- *<p>@param index
|
|
|
- *<p>@return RedisSentinelConfiguration
|
|
|
- *@since 2019年5月10日: 下午4:20:43
|
|
|
- *@author gezhanbin
|
|
|
- */
|
|
|
- private RedisSentinelConfiguration redisSentinelConfiguration(int index) {
|
|
|
- RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration();
|
|
|
- redisSentinelConfiguration.setDatabase(index);
|
|
|
- redisSentinelConfiguration.setMaster(springRedisSentinelMaster);
|
|
|
- //配置redis的哨兵sentinel
|
|
|
- String[] hosts = StringUtils.delimitedListToStringArray(springRedisSentinelNodes,",");
|
|
|
- Set<RedisNode> redisNodeSet = new HashSet<>();
|
|
|
- for(String redisHost : hosts){
|
|
|
- String[] item = redisHost.split(":");
|
|
|
- String ip = item[0];
|
|
|
- String port = item[1];
|
|
|
- redisNodeSet.add(new RedisNode(ip, Integer.parseInt(port)));
|
|
|
- }
|
|
|
- redisSentinelConfiguration.setSentinels(redisNodeSet);
|
|
|
- if(!StringUtils.isEmpty(springRedisPassword)) {
|
|
|
- redisSentinelConfiguration.setPassword(redisPassword(springRedisPassword));
|
|
|
+ /**
|
|
|
+ * 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)));
|
|
|
}
|
|
|
- return redisSentinelConfiguration;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- *
|
|
|
- *<p>Description: 单机模式的redis配置 .</p>
|
|
|
- *<p>@param index redis库的索引
|
|
|
- *<p>@return RedisStandaloneConfiguration
|
|
|
- *@since 2019年5月10日: 下午4:05:20
|
|
|
- *@author gezhanbin
|
|
|
- */
|
|
|
- private RedisStandaloneConfiguration redisStandaloneConfiguration(int index) {
|
|
|
- RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
|
|
|
- redisStandaloneConfiguration.setDatabase(index);
|
|
|
- redisStandaloneConfiguration.setHostName(springRedisHost);
|
|
|
- redisStandaloneConfiguration.setPort(springRedisPort);
|
|
|
- if(!StringUtils.isEmpty(springRedisPasswordStandalone)) {
|
|
|
- redisStandaloneConfiguration.setPassword(redisPassword(springRedisPasswordStandalone));
|
|
|
+ sentinelConfiguration.setSentinels(redisNodeSet);
|
|
|
+ if (!StringUtils.isEmpty(passwordSentinel)) {
|
|
|
+ sentinelConfiguration.setPassword(RedisPassword.of(passwordSentinel));
|
|
|
}
|
|
|
- return redisStandaloneConfiguration;
|
|
|
+ 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:
|
|
|
+ *
|
|
|
+ * @return RedisTemplate
|
|
|
+ * @author maxinyu
|
|
|
+ * @since 2019年3月5日:下午2:06:37
|
|
|
+ */
|
|
|
+ @Bean(value = "redisTemplate")
|
|
|
+ public RedisTemplate<Object, Object> redisTemplate() {
|
|
|
+ RedisTemplate<Object, Object> template = new RedisTemplate<>();
|
|
|
+ // initDomainRedisTemplate(template,
|
|
|
+ // lettuceConnectionFactory(databaseZero));
|
|
|
+ initDomainRedisTemplate(template, jedisConnectionFactory(databaseOne));
|
|
|
+ return template;
|
|
|
}
|
|
|
|
|
|
- private RedisPassword redisPassword(String password) {
|
|
|
- return RedisPassword.of(password);
|
|
|
+ @Bean
|
|
|
+ public JedisConnectionFactory redisConnectionFactory() throws UnknownHostException {
|
|
|
+ JedisConnectionFactory jedisConnectionFactory = null;
|
|
|
+ if (useCluster) {
|
|
|
+ // 哨兵模式
|
|
|
+ jedisConnectionFactory = new JedisConnectionFactory(sentinelConfiguration(databaseOne),
|
|
|
+ getJedisClientConfiguration());
|
|
|
+ } else {
|
|
|
+ // 单机模式
|
|
|
+ jedisConnectionFactory = new JedisConnectionFactory(standaloneConfiguration(databaseOne),
|
|
|
+ getJedisClientConfiguration());
|
|
|
+ }
|
|
|
+ jedisConnectionFactory.afterPropertiesSet();
|
|
|
+ return jedisConnectionFactory;
|
|
|
}
|
|
|
+
|
|
|
}
|