Forráskód Böngészése

去掉alarmDataStarter中的feign配置,避免对其他的feign接口有影响;同时处理了调用报警中台入参去掉null参数的问题

lixing 3 éve
szülő
commit
53aa9044a1

+ 80 - 67
AlarmDataStarter/src/main/java/com/persagy/apm/energyalarmstarter/alarmdata/AutoConfiguration.java

@@ -3,6 +3,7 @@ package com.persagy.apm.energyalarmstarter.alarmdata;
 import com.alibaba.fastjson.PropertyNamingStrategy;
 import com.alibaba.fastjson.serializer.SerializeConfig;
 import com.alibaba.fastjson.serializer.SerializerFeature;
+import com.alibaba.fastjson.serializer.ValueFilter;
 import com.alibaba.fastjson.support.config.FastJsonConfig;
 import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
 import org.springframework.beans.factory.ObjectFactory;
@@ -16,6 +17,8 @@ import org.springframework.context.annotation.Configuration;
 import org.springframework.http.MediaType;
 import org.springframework.http.converter.HttpMessageConverter;
 
+import java.time.LocalDateTime;
+import java.time.ZoneOffset;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -29,71 +32,81 @@ import java.util.List;
 @Configuration
 @ComponentScan(value = "com.persagy.apm.energyalarmstarter.alarmdata")
 public class AutoConfiguration {
-    @Bean
-    public ResponseEntityDecoder feignDecoder() {
-        HttpMessageConverter fastJsonConverter = createFastJsonConverter();
-        ObjectFactory<HttpMessageConverters> objectFactory = () -> new HttpMessageConverters(fastJsonConverter);
-        return new ResponseEntityDecoder(new SpringDecoder(objectFactory));
-    }
-
-    @Bean
-    public SpringEncoder feignEncoder(){
-        HttpMessageConverter fastJsonConverter = createFastJsonConverter();
-        ObjectFactory<HttpMessageConverters> objectFactory = () -> new HttpMessageConverters(fastJsonConverter);
-        return new SpringEncoder(objectFactory);
-    }
-
-    /**
-     * Description: 添加支持的类型
-     *
-     * @return List<MediaType>
-     * @author luoguangyi
-     * @since 2019年9月3日: 下午6:20:33 Update By luoguangyi 2019年9月3日: 下午6:20:33
-     */
-    private HttpMessageConverter createFastJsonConverter() {
-        //===========替换框架json为fastjson
-        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
-        List<MediaType> supportedMediaTypes = new ArrayList<>();
-        supportedMediaTypes.add(MediaType.APPLICATION_JSON);
-        supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
-        supportedMediaTypes.add(MediaType.APPLICATION_ATOM_XML);
-        supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
-        supportedMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
-        supportedMediaTypes.add(MediaType.APPLICATION_PDF);
-        supportedMediaTypes.add(MediaType.APPLICATION_RSS_XML);
-        supportedMediaTypes.add(MediaType.APPLICATION_XHTML_XML);
-        supportedMediaTypes.add(MediaType.APPLICATION_XML);
-        supportedMediaTypes.add(MediaType.IMAGE_GIF);
-        supportedMediaTypes.add(MediaType.IMAGE_JPEG);
-        supportedMediaTypes.add(MediaType.IMAGE_PNG);
-        supportedMediaTypes.add(MediaType.TEXT_EVENT_STREAM);
-        supportedMediaTypes.add(MediaType.TEXT_HTML);
-        supportedMediaTypes.add(MediaType.TEXT_MARKDOWN);
-        supportedMediaTypes.add(MediaType.TEXT_PLAIN);
-        supportedMediaTypes.add(MediaType.TEXT_XML);
-        fastConverter.setSupportedMediaTypes(supportedMediaTypes);
-
-        //创建配置类
-        FastJsonConfig fastJsonConfig = new FastJsonConfig();
-        //---下划线转驼峰
-        SerializeConfig serializeConfig = new SerializeConfig();
-        serializeConfig.propertyNamingStrategy = PropertyNamingStrategy.CamelCase;
-        fastJsonConfig.setSerializeConfig(serializeConfig);
-        //---序列化格式
-        fastJsonConfig.setSerializerFeatures(
-                SerializerFeature.PrettyFormat,
-                SerializerFeature.WriteDateUseDateFormat,
-                // List字段如果为null,输出为[],而非null
-//                SerializerFeature.WriteNullListAsEmpty,
-                // 是否显示为null的字段,加上会显示,取消就不会显示为空的字段
-                // SerializerFeature.WriteMapNullValue,
-                // 禁止循环引用
-                SerializerFeature.DisableCircularReferenceDetect
-                // SerializerFeature.WriteNullStringAsEmpty
-        );
-        fastJsonConfig.setDateFormat("yyyyMMddHHmmss");
-        fastConverter.setFastJsonConfig(fastJsonConfig);
-
-        return fastConverter;
-    }
+//    @Bean
+//    public ResponseEntityDecoder feignDecoder() {
+//        HttpMessageConverter fastJsonConverter = createFastJsonConverter();
+//        ObjectFactory<HttpMessageConverters> objectFactory = () -> new HttpMessageConverters(fastJsonConverter);
+//        return new ResponseEntityDecoder(new SpringDecoder(objectFactory));
+//    }
+//
+//    @Bean
+//    public SpringEncoder feignEncoder(){
+//        HttpMessageConverter fastJsonConverter = createFastJsonConverter();
+//        ObjectFactory<HttpMessageConverters> objectFactory = () -> new HttpMessageConverters(fastJsonConverter);
+//        return new SpringEncoder(objectFactory);
+//    }
+//
+//    /**
+//     * Description: 添加支持的类型
+//     *
+//     * @return List<MediaType>
+//     * @author luoguangyi
+//     * @since 2019年9月3日: 下午6:20:33 Update By luoguangyi 2019年9月3日: 下午6:20:33
+//     */
+//    private HttpMessageConverter createFastJsonConverter() {
+//        //===========替换框架json为fastjson
+//        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
+//        List<MediaType> supportedMediaTypes = new ArrayList<>();
+//        supportedMediaTypes.add(MediaType.APPLICATION_JSON);
+//        supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
+//        supportedMediaTypes.add(MediaType.APPLICATION_ATOM_XML);
+//        supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
+//        supportedMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
+//        supportedMediaTypes.add(MediaType.APPLICATION_PDF);
+//        supportedMediaTypes.add(MediaType.APPLICATION_RSS_XML);
+//        supportedMediaTypes.add(MediaType.APPLICATION_XHTML_XML);
+//        supportedMediaTypes.add(MediaType.APPLICATION_XML);
+//        supportedMediaTypes.add(MediaType.IMAGE_GIF);
+//        supportedMediaTypes.add(MediaType.IMAGE_JPEG);
+//        supportedMediaTypes.add(MediaType.IMAGE_PNG);
+//        supportedMediaTypes.add(MediaType.TEXT_EVENT_STREAM);
+//        supportedMediaTypes.add(MediaType.TEXT_HTML);
+//        supportedMediaTypes.add(MediaType.TEXT_MARKDOWN);
+//        supportedMediaTypes.add(MediaType.TEXT_PLAIN);
+//        supportedMediaTypes.add(MediaType.TEXT_XML);
+//        fastConverter.setSupportedMediaTypes(supportedMediaTypes);
+//
+//        //创建配置类
+//        FastJsonConfig fastJsonConfig = new FastJsonConfig();
+//        //---下划线转驼峰
+//        SerializeConfig serializeConfig = new SerializeConfig();
+//        serializeConfig.propertyNamingStrategy = PropertyNamingStrategy.CamelCase;
+//        fastJsonConfig.setSerializeConfig(serializeConfig);
+//        //---序列化格式
+//        fastJsonConfig.setSerializerFeatures(
+//                SerializerFeature.PrettyFormat,
+//                SerializerFeature.WriteDateUseDateFormat,
+//                // List字段如果为null,输出为[],而非null
+////                SerializerFeature.WriteNullListAsEmpty,
+//                // 是否显示为null的字段,加上会显示,取消就不会显示为空的字段
+//                // SerializerFeature.WriteMapNullValue,
+//                // 禁止循环引用
+//                SerializerFeature.DisableCircularReferenceDetect
+//                // SerializerFeature.WriteNullStringAsEmpty
+//        );
+//        // 时间格式转换为时间戳
+//        fastJsonConfig.setSerializeFilters((ValueFilter) (o, s, source) -> {
+//            if (source == null) {
+//                return "";
+//            }
+//            if (source instanceof LocalDateTime) {
+//                return ((LocalDateTime) source).toInstant(ZoneOffset.of("+8")).toEpochMilli();
+//            }
+//            return source;
+//        });
+//
+//        fastConverter.setFastJsonConfig(fastJsonConfig);
+//
+//        return fastConverter;
+//    }
 }

+ 7 - 2
AlarmDataStarter/src/main/java/com/persagy/apm/energyalarmstarter/alarmdata/model/dto/alarmrecord/QueryAlarmRecordDTO.java

@@ -76,12 +76,14 @@ public class QueryAlarmRecordDTO extends UrlParamDTO {
      */
     private Date triggerTime;
     /**
-     * 报警发生时间区间-起始时间
+     * 报警发生时间区间-起始时间(禁用get方法)
      */
+    @Getter(AccessLevel.NONE)
     private Date triggerDurationStart;
     /**
-     * 报警发生时间区间-结束时间
+     * 报警发生时间区间-结束时间(禁用get方法)
      */
+    @Getter(AccessLevel.NONE)
     private Date triggerDurationEnd;
     /**
      * 工单id
@@ -98,6 +100,7 @@ public class QueryAlarmRecordDTO extends UrlParamDTO {
     private List<String> projectIds;
 
     public List<String> getProjectId() {
+        // 使用projectIds字段来存储查询条件中包含的项目id,和UrlParamDTO中的projectId做一个区分
         if (!CollectionUtils.isEmpty(projectIds)) {
             return projectIds;
         }
@@ -105,6 +108,7 @@ public class QueryAlarmRecordDTO extends UrlParamDTO {
     }
 
     public JSONObject getName() {
+        // 名称模糊查询
         if (this.name == null) {
             return null;
         }
@@ -114,6 +118,7 @@ public class QueryAlarmRecordDTO extends UrlParamDTO {
     }
 
     public Object getTriggerTime() {
+        // 触发时间可以精确查询也可以根据区间查询
         if (this.triggerTime != null) {
             return DateUtils.date2Str(this.triggerTime, DateUtils.SDFSECOND);
         }

+ 3 - 3
AlarmDataStarter/src/main/java/com/persagy/apm/energyalarmstarter/alarmdata/service/AlarmRecordServiceImpl.java

@@ -19,6 +19,7 @@ import com.persagy.apm.energyalarmstarter.alarmdata.utils.Obj2JSONObject;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.util.Date;
 import java.util.List;
 import java.util.Set;
 
@@ -116,9 +117,8 @@ public class AlarmRecordServiceImpl {
     }
 
     public static void main(String[] args) {
-        QueryAlarmRecordDTO addAlarmRecordDTO = new QueryAlarmRecordDTO();
-        List<String> projectIds = Lists.newArrayList("1", "2", "3");
-        addAlarmRecordDTO.setProjectIds(projectIds);
+        AddAlarmRecordDTO addAlarmRecordDTO = new AddAlarmRecordDTO();
+        addAlarmRecordDTO.setTriggerTime(new Date());
         try {
             JSONObject convert = Obj2JSONObject.convert(addAlarmRecordDTO);
             System.out.println(convert);

+ 3 - 1
AlarmDataStarter/src/main/java/com/persagy/apm/energyalarmstarter/alarmdata/utils/Obj2JSONObject.java

@@ -10,7 +10,9 @@ import com.alibaba.fastjson.JSONObject;
  **/
 public class Obj2JSONObject {
     public static JSONObject convert(Object o) throws Exception{
-        return (JSONObject)JSONObject.toJSON(o);
+        String jsonStr = JSONObject.toJSONString(o);
+        return JSONObject.parseObject(jsonStr);
+//        return (JSONObject)JSONObject.toJSON(o);
     }
 
     public static <T> T reverse(JSONObject jsonObject, Class<T> clazz) throws Exception{

+ 37 - 3
AlarmEngineStarter/src/main/java/com/persagy/apm/energyalarmstarter/alarmengine/netty/NettyAlarmMsgBaseHandler.java

@@ -191,6 +191,9 @@ public class NettyAlarmMsgBaseHandler extends ChannelInboundHandlerAdapter {
         for (ProjectVO project : projects) {
             List<ObjConditionRel> objConditionRels = energyAlarmService.queryObjAlarmConditionRel(
                     userId, groupCode, project.getProjectId());
+            if (CollectionUtils.isEmpty(objConditionRels)) {
+                continue;
+            }
             sendMessage(channel, new NettyAlarmMessage(NettyMsgTypeEnum.ALL_OBJ_CONDITION_REL, objConditionRels).toString());
         }
     }
@@ -302,6 +305,10 @@ public class NettyAlarmMsgBaseHandler extends ChannelInboundHandlerAdapter {
                     // 报警仍在继续
                     alarmContinue(nettyMessage);
                     break;
+                case HEART_BEAT:
+                    // 返回一个心跳信息
+                    sendHeartBeatMsg(nettyMessage);
+                    break;
                 default:
                     log.info("边缘端发来的参数无效,参数值为:" + nettyMessage.getOpCode());
                     break;
@@ -313,6 +320,22 @@ public class NettyAlarmMsgBaseHandler extends ChannelInboundHandlerAdapter {
     }
 
     /**
+     * 发送一个心跳消息
+     *
+     * @param nettyMessage 接收到的心跳消息
+     * @author lixing
+     * @version V1.0 2021/10/28 7:23 下午
+     */
+    private void sendHeartBeatMsg(NettyAlarmMessage nettyMessage) {
+        if (nettyMessage == null) {
+            return;
+        }
+        String projectId = nettyMessage.getSource();
+
+        sendMessage(projectId, nettyMessage.toString());
+    }
+
+    /**
      * @description: 报警仍在持续的处理方法
      * @param: nettyMessage
      * @return: void
@@ -368,6 +391,14 @@ public class NettyAlarmMsgBaseHandler extends ChannelInboundHandlerAdapter {
         Set<Map.Entry<String, Channel>> channelList = socketChannelMap.entrySet();
         for (Map.Entry<String, Channel> channelEntry : channelList) {
             if (channelEntry.getValue() == channel) {
+                try {
+                    // 手动关闭通道,避免异常导致的连接断开,通道未关闭
+                    if (channel.isActive()) {
+                        channel.close();
+                    }
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
                 socketChannelMap.remove(channelEntry.getKey());
                 log.warn("----项目ID[{}],地址[{}] ----离开---", channelEntry.getKey(), channel.remoteAddress().toString());
             }
@@ -439,7 +470,8 @@ public class NettyAlarmMsgBaseHandler extends ChannelInboundHandlerAdapter {
      * @version V1.0 2021/10/26 7:51 下午
      */
     public void syncNewCondition(DmpMessage msg) {
-        JSONObject alarmCondition = JSONObject.parseObject(msg.getStr1());
+        AlarmCondition alarmCondition = JSONObject.parseObject(msg.getStr1(), AlarmCondition.class);
+//        JSONObject alarmCondition = JSONObject.parseObject(msg.getStr1());
         sendMessage(msg.getProjectId(), new NettyAlarmMessage(
                 NettyMsgTypeEnum.NEW_CONDITION, Lists.newArrayList(alarmCondition)).toString());
     }
@@ -452,7 +484,8 @@ public class NettyAlarmMsgBaseHandler extends ChannelInboundHandlerAdapter {
      * @version V1.0 2021/10/26 7:51 下午
      */
     public void syncUpdatedCondition(DmpMessage msg) {
-        JSONObject alarmCondition = JSONObject.parseObject(msg.getStr1());
+        AlarmCondition alarmCondition = JSONObject.parseObject(msg.getStr1(), AlarmCondition.class);
+//        JSONObject alarmCondition = JSONObject.parseObject(msg.getStr1());
         sendMessage(msg.getProjectId(), new NettyAlarmMessage(
                 NettyMsgTypeEnum.UPDATE_CONDITION, Lists.newArrayList(alarmCondition)).toString());
     }
@@ -465,7 +498,8 @@ public class NettyAlarmMsgBaseHandler extends ChannelInboundHandlerAdapter {
      * @version V1.0 2021/10/26 7:51 下午
      */
     public void syncDeletedCondition(DmpMessage msg) {
-        JSONObject alarmCondition = JSONObject.parseObject(msg.getStr1());
+        AlarmCondition alarmCondition = new AlarmCondition();
+        alarmCondition.setId(msg.getStr1());
         sendMessage(msg.getProjectId(), new NettyAlarmMessage(
                 NettyMsgTypeEnum.DELETE_CONDITION, Lists.newArrayList(alarmCondition)).toString());
     }

+ 1 - 0
AlarmEngineStarter/src/main/java/com/persagy/apm/energyalarmstarter/alarmengine/netty/NettyMsgTypeEnum.java

@@ -16,6 +16,7 @@ public enum NettyMsgTypeEnum {
     /**
      * netty的消息类型
      */
+    HEART_BEAT(10, "心跳包"),
     ACCEPTED(100, "已接收到消息"),
     CONNECT(200, "建立连接,此时的source == 项目id"),
     REQUEST_ALL_CONFIGS(10, "边缘端申请全量获取报警定义(报警条件、条件和设备的关联关系)"),