Browse Source

增加redis

xujiaheng 3 years ago
parent
commit
7dac4625c8

+ 10 - 0
pom.xml

@@ -147,6 +147,16 @@
             <artifactId>hibernate-types-52</artifactId>
             <version>2.9.13</version>
         </dependency>
+        <!-- ======================Redis jar坐标========================= -->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-data-redis</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>redis.clients</groupId>
+            <artifactId>jedis</artifactId>
+        </dependency>
+        <!-- ======================Redis jar坐标========================= -->
    </dependencies>
 
    <build>

+ 44 - 0
src/main/java/com/persagy/cache/AlarmRedisCache.java

@@ -0,0 +1,44 @@
+package com.persagy.cache;
+
+import cn.hutool.core.collection.CollectionUtil;
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.persagy.constant.RedisConstant;
+import com.persagy.entity.AlarmConditionState;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.data.redis.core.HashOperations;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.logging.Logger;
+
+@Component
+@SuppressWarnings({"unchecked", "rawtypes"})
+public class AlarmRedisCache implements RedisCache {
+
+    private static final Logger log = Logger.getLogger(String.valueOf(AlarmRedisCache.class));
+
+    @Resource(name = "emsRedisTemplate")
+    private RedisTemplate<String, String> emsRedisTemplate;
+
+
+    public AlarmConditionState getAlarmDefineState(String defineId) {
+        try {
+            String str = emsRedisTemplate.opsForHash().get(RedisConstant.DEFINE_ID, defineId).toString();
+            if (StringUtils.isNotBlank(str)) {
+                JSONObject object = JSONObject.parseObject(str);
+                AlarmConditionState alarmConditionState = JSON.toJavaObject(object, AlarmConditionState.class);
+                return alarmConditionState;
+            }
+        } catch (Exception e) {
+            log.warning("从redis获取报警定义状态失败" + e);
+        }
+        return null;
+    }
+}

+ 5 - 0
src/main/java/com/persagy/cache/RedisCache.java

@@ -0,0 +1,5 @@
+package com.persagy.cache;
+
+public interface RedisCache {
+
+}

+ 415 - 0
src/main/java/com/persagy/config/RedisConfig.java

@@ -0,0 +1,415 @@
+package com.persagy.config;
+
+import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
+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.RedisPassword;
+import org.springframework.data.redis.connection.RedisSentinelConfiguration;
+import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
+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.serializer.StringRedisSerializer;
+import org.springframework.util.StringUtils;
+
+import java.util.HashSet;
+import java.util.Set;
+
+//被@RefreshScope修饰的Bean都是延迟加载的,只有在第一次访问时才会被初始化,刷新Bean也是同理,下次访问时会创建一个信息的对象
+//@RefreshScope //这里不能省略  不然不能手动触发
+@Configuration
+public class RedisConfig {
+	
+	/**
+	 * 主机地址
+	 */
+	@Value("${spring.redis.host}")
+	private String springRedisHost;
+	
+	/**
+	 * 端口号
+	 */
+	@Value("${spring.redis.port}")
+	private Integer springRedisPort;
+	
+//	@Value("${spring.redis.timeout}")
+//	private Integer springRedisTimeout;
+
+	/**
+	 * 密码(如果有的话)
+	 */
+	//@Value("${spring.redis.password}")
+	//private String springRedisPassword;
+	@Value("${spring.redis.password.sentinel}")
+	private String springRedisPassword;
+
+	/**
+	 * 单机密码
+	 */
+	@Value("${spring.redis.password.standalone}")
+	private String springRedisPasswordStandalone;
+	
+	/**
+	 * 置连接池中最大允许的空闲连接  建议maxidle与maxTotal一样 
+	 */
+	@Value("${spring.redis.pool.max-idle}")
+	private Integer springRedisPoolMaxIdle;
+	
+	/**
+	 * 设置连接池中最小允许的连接数
+	 */
+	@Value("${spring.redis.pool.min-idle}")
+	private Integer springRedisPoolMinIdle;
+	
+	/**
+	 *  没有获取资源时最长等待1秒,1秒后还没有的话就报错
+	 */
+	@Value("${spring.redis.pool.max-wait}")
+	private Integer springRedisPoolMaxWait;
+	
+	/**
+	 *  设置连接池中最大活跃连接
+	 */
+	@Value("${spring.redis.pool.max-active}")
+	private Integer springRedisPoolMaxActive;
+	
+	
+	@Value("${spring.redis.database.zero}")
+	private Integer springRedisDatabaseZero;
+	
+	@Value("${spring.redis.database.one}")
+	private Integer springRedisDatabaseOne;
+
+	@Value("${spring.redis.database.two}")
+	private Integer springRedisDatabaseTwo;
+	
+	@Value("${spring.redis.database.three}")
+	private Integer springRedisDatabaseThree;
+	
+	@Value("${spring.redis.database.four}")
+	private Integer springRedisDatabaseFour;
+
+	@Value("${spring.redis.database.five}")
+	private Integer springRedisDatabaseFive;
+
+	@Value("${spring.redis.database.six}")
+	private Integer springRedisDatabaseSix;
+
+	@Value("${spring.redis.database.seven}")
+	private Integer springRedisDatabaseSeven;
+
+	@Value("${spring.redis.database.eight}")
+	private Integer springRedisDatabaseEight;
+
+	@Value("${spring.redis.database.nine}")
+	private Integer springRedisDatabaseNine;
+
+	@Value("${spring.redis.database.ten}")
+	private Integer springRedisDatabaseTen;
+	
+	@Value("${spring.redis.database.eleven}")
+	private Integer springRedisDatabaseEleven;
+
+	@Value("${spring.redis.database.fifteen}")
+	private Integer springRedisDatabaseFifteen;
+	
+	/**
+	 * 哨兵集群名称
+	 */
+	@Value("${spring.redis.sentinel.master}")
+	private String springRedisSentinelMaster;
+	
+	/**
+	 * 哨兵集群节点地址
+	 */
+	@Value("${spring.redis.sentinel.nodes}")
+	private String springRedisSentinelNodes;
+	
+	@Value("${spring.redis.use.cluster}")
+	private Boolean redisUseCluster;
+
+	
+	/**
+	 * 
+	 *<p>Description: 运维平台:使用索引为0的Redis库 .</p> 
+	 *<p>@return RedisTemplate<String,Object>
+	 *@since 2019年5月10日: 下午5:14:20
+	 *@author gezhanbin
+	 *@version 1.0
+	 */
+	@Bean
+	public RedisTemplate<String, String> emsRedisTemplate() {
+		RedisTemplate<String, String> template = new RedisTemplate<String, String>();
+		template.setConnectionFactory(redisConnectionFactory(springRedisDatabaseZero));
+		template.setDefaultSerializer(new StringRedisSerializer());
+		return template;
+	}
+	
+	/**
+	 * 
+	 *<p>Description: saas:使用索引为1的Redis库 .</p> 
+	 *<p>@return RedisTemplate<String,Object>
+	 *@since 2019年5月10日: 下午5:18:27
+	 *@author gezhanbin
+	 *@version 1.0
+	 */
+	@Bean
+	public RedisTemplate<String, Object> saasRedisTemplate() {
+		RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
+		template.setConnectionFactory(redisConnectionFactory(springRedisDatabaseOne));
+		template.setDefaultSerializer(new StringRedisSerializer());
+		return template;
+	}
+	
+	/**
+	 * 
+	 *<p>Description:  workorder:使用索引为2的Redis库.</p> 
+	 *<p>@return RedisTemplate<String,Object>
+	 *@since 2019年5月10日: 下午5:19:56
+	 *@author gezhanbin
+	 *@version 1.0
+	 */
+	@Bean
+	public RedisTemplate<String, Object> workOrderRedisTemplate() {
+		RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
+		template.setConnectionFactory(redisConnectionFactory(springRedisDatabaseTwo));
+		template.setDefaultSerializer(new StringRedisSerializer());
+		return template;
+	}
+	
+	/**
+	 * 
+	 *<p>Description: 人员服务:使用索引为3的Redis库 .</p> 
+	 *<p>@return RedisTemplate<String,Object>
+	 *@since 2019年5月10日: 下午5:20:23
+	 *@author gezhanbin
+	 *@version 1.0
+	 */
+	@Bean
+	public RedisTemplate<String, Object> personServiceRedisTemplate() {
+		RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
+		template.setConnectionFactory(redisConnectionFactory(springRedisDatabaseThree));
+		template.setDefaultSerializer(new StringRedisSerializer());
+		return template;
+	}
+	
+	/**
+	 *<p>Description: 事件管理:使用索引为4的Redis库 .</p> 
+	 *<p>@return RedisTemplate<String,Object>
+	 *@since 2019年5月10日: 下午5:23:09
+	 *@author gezhanbin
+	 *@version 1.0
+	 */
+	@Bean
+	public RedisTemplate<String, Object> eventMgRedisTemplate() {
+		RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
+		template.setConnectionFactory(redisConnectionFactory(springRedisDatabaseFour));
+		template.setDefaultSerializer(new StringRedisSerializer());
+		return template;
+	}
+	
+	/**
+	 * 
+	 *<p>Description:  报表缓存:使用索引为5的Redis库.</p> 
+	 *<p>@return RedisTemplate<String,Object>
+	 *@since 2019年5月10日: 下午5:26:24
+	 *@author gezhanbin
+	 *@version 1.0
+	 */
+	@Bean
+	public RedisTemplate<String, Object> customRedisTemplate() {
+		RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
+		template.setConnectionFactory(redisConnectionFactory(springRedisDatabaseFive));
+		template.setDefaultSerializer(new StringRedisSerializer());
+		return template;
+	}
+	
+	/**
+	 * 
+	 *<p>Description:  数据平台:使用索引为 6 的Redis库 .</p> 
+	 *<p>@return RedisTemplate<String,Object>
+	 *@since 2019年7月10日: 上午9:18:06
+	 *@author gezhanbin
+	 *@version 1.0
+	 */
+	@Bean
+	public RedisTemplate<String, Object> dataPlatformRedisTemplate() {
+		RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
+		template.setConnectionFactory(redisConnectionFactory(springRedisDatabaseSix));
+		template.setDefaultSerializer(new StringRedisSerializer());
+		return template;
+	}
+	
+	/**
+	 * 
+	 *<p>Description:  抢单信息缓存:使用索引为10的Redis库.</p> 
+	 *<p>@return RedisTemplate<String,Object>
+	 *@since 2019年5月10日: 下午5:26:51
+	 *@author gezhanbin
+	 *@version 1.0
+	 */
+	@Bean
+	public RedisTemplate<String, Object> robbingRedisTemplate() {
+		RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
+		template.setConnectionFactory(redisConnectionFactory(springRedisDatabaseTen));
+		template.setDefaultSerializer(new StringRedisSerializer());
+		return template;
+	}
+	
+	@Bean
+    public RedisTemplate<String, Object> taskRedisTemplate() {
+        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
+		template.setConnectionFactory(redisConnectionFactory(springRedisDatabaseEleven));
+		template.setDefaultSerializer(new StringRedisSerializer());
+        return template;
+    }
+	
+	/**
+	 * 
+	 *<p>Description: 访问量统计:使用索引为15的Redis库 .</p> 
+	 *<p>@return RedisTemplate<String,Object>
+	 *@since 2019年5月10日: 下午5:27:24
+	 *@author gezhanbin
+	 *@version 1.0
+	 */
+	@Bean
+	public RedisTemplate<String, Object> pageViewRedisTemplate() {
+		RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
+		template.setConnectionFactory(redisConnectionFactory(springRedisDatabaseFifteen));
+		template.setDefaultSerializer(new StringRedisSerializer());
+		return template;
+	}
+
+	
+	/**
+	 * 
+	 *<p>Description:  redis连接工厂 .</p> 
+	 *<p>@param index redis库的索引
+	 *<p>@return RedisConnectionFactory
+	 *@since 2019年5月10日: 下午4:49:24
+	 *@author gezhanbin
+	 *@version 1.0
+	 */
+	private LettuceConnectionFactory redisConnectionFactory(int index) {
+		LettuceConnectionFactory redisConnectionFactory = null;
+		if(redisUseCluster) {
+			redisConnectionFactory = new LettuceConnectionFactory(redisSentinelConfiguration(index), jedisClientConfiguration());
+		} else {
+			redisConnectionFactory = new LettuceConnectionFactory(redisStandaloneConfiguration(index), jedisClientConfiguration());
+		}
+		redisConnectionFactory.afterPropertiesSet();
+		return redisConnectionFactory;
+	}
+	
+	
+	/**
+	 * 
+	 *<p>Description:  jedis 客户端 连接池配置.</p> 
+	 *<p>@return JedisClientConfiguration
+	 *@since 2019年5月10日: 下午4:47:59
+	 *@author gezhanbin
+	 *@version 1.0
+	 */
+	private LettucePoolingClientConfiguration  jedisClientConfiguration() {
+//		LettuceClientConfiguration.LettuceClientConfigurationBuilder builder = LettuceClientConfiguration.builder();
+//		JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jedisPoolingClientConfigurationBuilder = builder.usePooling();
+//		jedisPoolingClientConfigurationBuilder.poolConfig(jedisPoolConfig());
+//		return builder.build();
+		return LettucePoolingClientConfiguration.builder().poolConfig(jedisPoolConfig()).build();
+	}
+	
+	/**
+	 * 
+	 *<p>Description: jedis 连接池配置 .</p> 
+	 *<p>@return JedisPoolConfig
+	 *@since 2019年5月10日: 下午4:34:33
+	 *@author gezhanbin
+	 *@version 1.0
+	 */
+	private GenericObjectPoolConfig jedisPoolConfig() {
+//		JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
+//		jedisPoolConfig.setMaxIdle(springRedisPoolMaxIdle);
+//		jedisPoolConfig.setMinIdle(springRedisPoolMinIdle);
+//		jedisPoolConfig.setMaxTotal(springRedisPoolMaxActive);
+//		jedisPoolConfig.setMaxWaitMillis(springRedisPoolMaxWait);
+//		return jedisPoolConfig;
+		
+		GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
+		 
+        poolConfig.setMinIdle(springRedisPoolMinIdle);
+        poolConfig.setMaxIdle(springRedisPoolMaxIdle);
+        poolConfig.setMaxTotal(springRedisPoolMaxActive);
+        poolConfig.setMaxWaitMillis(springRedisPoolMaxWait);
+        return poolConfig;
+	}
+	
+	
+	
+	/**
+	 * 
+	 *<p>Description:  哨兵模式的redis配置.</p> 
+	 *<p>@param index
+	 *<p>@return RedisSentinelConfiguration
+	 *@since 2019年5月10日: 下午4:20:43
+	 *@author gezhanbin
+	 *@version 1.0
+	 */
+	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<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)));
+        }
+        redisSentinelConfiguration.setSentinels(redisNodeSet);
+		if(!StringUtils.isEmpty(springRedisPassword)) {
+			redisSentinelConfiguration.setPassword(redisPassword(springRedisPassword));
+		}
+		return redisSentinelConfiguration;
+	}
+	
+	
+	/**
+	 * 
+	 *<p>Description:  单机模式的redis配置 .</p> 
+	 *<p>@param index  redis库的索引
+	 *<p>@return RedisStandaloneConfiguration
+	 *@since 2019年5月10日: 下午4:05:20
+	 *@author gezhanbin
+	 *@version 1.0
+	 */
+	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));
+		}
+		return redisStandaloneConfiguration;
+	}
+	
+	
+	/**
+	 * 
+	 *<p>Description:  redis的访问密码.</p> 
+	 *<p>@return RedisPassword
+	 *@since 2019年5月10日: 下午4:02:48
+	 *@author gezhanbin
+	 *@version 1.0
+	 */
+	private RedisPassword redisPassword(String password) {
+		return RedisPassword.of(password);
+	}
+	
+	
+	
+}

+ 4 - 0
src/main/java/com/persagy/constant/RedisConstant.java

@@ -63,4 +63,8 @@ public class RedisConstant {
      */
     public static final String ALARM_DEFINITION_ALARM_INFO_KEY = "ALARM_DEFINITION_ALARM_INFO";
 
+    /**
+     * itemCode-objId拼接,用于根据defineId获取报警定义状态
+     */
+    public static final String DEFINE_ID = "DEFINE_ID";
 }

+ 7 - 1
src/main/java/com/persagy/service/impl/AlarmHandleServiceImpl.java

@@ -8,6 +8,7 @@ import com.googlecode.aviator.AviatorEvaluator;
 import com.googlecode.aviator.Expression;
 import com.persagy.cache.AlarmInfoCache;
 import com.persagy.cache.AlarmLastTimeCache;
+import com.persagy.cache.AlarmRedisCache;
 import com.persagy.cache.CreatedAlarmIdsCache;
 import com.persagy.client.GroupNettyClient;
 import com.persagy.entity.*;
@@ -60,6 +61,9 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
     CreatedAlarmIdsCache createdAlarmIdsCache;
     @Autowired
     AlarmLastTimeCache alarmLastTimeCache;
+    @Autowired
+    AlarmRedisCache alarmRedisCache;
+
     /**
      * 集团编码
      */
@@ -580,7 +584,9 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
             HashMap<String, Object> paramMap) throws Exception {
         // 根据defineId获取报警定义状态
         String defineId = AlarmInfoCache.getAlarmDefineId(alarmDefine);
-        AlarmConditionState alarmConditionState = getAlarmDefineStateFromCacheOrDb(defineId);
+//      AlarmConditionState alarmConditionState = getAlarmDefineStateFromCacheOrDb(defineId);
+
+        AlarmConditionState alarmConditionState = alarmRedisCache.getAlarmDefineState(defineId);
 
         Condition condition = alarmDefine.getCondition();
         // 判断报警是否在有效期内