ソースを参照

通过feign调用数据中台接口,使用fastjson来编码和解码

lixing 3 年 前
コミット
286da9b96e

+ 1 - 96
AlarmDataStarter/src/main/java/com/persagy/apm/energyalarmstarter/alarmdata/AutoConfiguration.java

@@ -1,26 +1,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;
-import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
-import org.springframework.cloud.openfeign.support.ResponseEntityDecoder;
-import org.springframework.cloud.openfeign.support.SpringDecoder;
-import org.springframework.cloud.openfeign.support.SpringEncoder;
-import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.ComponentScan;
 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;
 
 /**
  * @description: 配置类
@@ -32,81 +13,5 @@ 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.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;
-//    }
+
 }

+ 104 - 0
AlarmDataStarter/src/main/java/com/persagy/apm/energyalarmstarter/alarmdata/feign/AlarmClientConfig.java

@@ -0,0 +1,104 @@
+package com.persagy.apm.energyalarmstarter.alarmdata.feign;
+
+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;
+import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
+import org.springframework.cloud.openfeign.support.ResponseEntityDecoder;
+import org.springframework.cloud.openfeign.support.SpringDecoder;
+import org.springframework.cloud.openfeign.support.SpringEncoder;
+import org.springframework.context.annotation.Bean;
+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;
+
+/**
+ * 数据中台feign配置类
+ * 在通过feign调用接口时,spring默认的编码器springEncoder会过滤掉参数中的空数组,而空数组参数在数据中台极为常见
+ * 因此feign调用中台接口使用fastjson进行参数的编码和解码。
+ *
+ * @author lixing
+ * @version V1.0 2021/11/12 2:53 下午
+ **/
+public class AlarmClientConfig {
+    @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,
+                // 禁止循环引用
+                SerializerFeature.DisableCircularReferenceDetect
+        );
+        // 时间格式转换为时间戳
+        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;
+    }
+}

+ 3 - 2
AlarmDataStarter/src/main/java/com/persagy/apm/energyalarmstarter/alarmdata/feign/client/AlarmClient.java

@@ -3,8 +3,8 @@ package com.persagy.apm.energyalarmstarter.alarmdata.feign.client;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.persagy.apm.energyalarmstarter.alarmdata.constant.RequestUrlConstant;
+import com.persagy.apm.energyalarmstarter.alarmdata.feign.AlarmClientConfig;
 import com.persagy.apm.energyalarmstarter.alarmdata.feign.AlarmUrlParam;
-import com.persagy.apm.energyalarmstarter.alarmdata.model.dto.common.UrlParamDTO;
 import com.persagy.apm.energyalarmstarter.alarmdata.feign.DmpResult;
 import com.persagy.apm.energyalarmstarter.alarmdata.feign.fallback.AlarmClientFallbackFactory;
 import feign.Headers;
@@ -20,7 +20,8 @@ import org.springframework.web.bind.annotation.RequestBody;
  * @since: 2020/11/27 10:43 上午
  * @version: V1.0
  */
-@FeignClient(name = "dmp-alarm", fallbackFactory = AlarmClientFallbackFactory.class)
+@FeignClient(name = "dmp-alarm", fallbackFactory = AlarmClientFallbackFactory.class,
+        configuration = AlarmClientConfig.class)
 public interface AlarmClient {
     /**
      * @description: 查询报警定义

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

@@ -77,6 +77,7 @@ public class AlarmRecordServiceImpl {
         queryDTO.setWithColumns(withColumns);
         JSONObject jsonObject = Obj2JSONObject.convert(queryDTO);
         DmpResult<JSONArray> result = alarmClient.queryAlarmRecord(queryDTO.fetchParams(), jsonObject);
+//        DmpResult<JSONArray> result = alarmClient.queryAlarmRecord(queryDTO.fetchParams(), Obj2JSONObject.convert2Str(queryDTO));
         return DmpResultUtil.multiConvert(result, AlarmRecordItem.class);
     }
 

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

@@ -10,9 +10,14 @@ import com.alibaba.fastjson.JSONObject;
  **/
 public class Obj2JSONObject {
     public static JSONObject convert(Object o) throws Exception{
+        // 为空的字段会被过滤掉
         String jsonStr = JSONObject.toJSONString(o);
         return JSONObject.parseObject(jsonStr);
-//        return (JSONObject)JSONObject.toJSON(o);
+    }
+
+    public static String convert2Str(Object o) throws Exception{
+        // 为空的字段会被过滤掉
+        return JSONObject.toJSONString(o);
     }
 
     public static <T> T reverse(JSONObject jsonObject, Class<T> clazz) throws Exception{

+ 24 - 1
AlarmEngineStarter/src/main/java/com/persagy/apm/energyalarmstarter/alarmengine/service/NettyAlarmService.java

@@ -177,10 +177,11 @@ public abstract class NettyAlarmService extends BaseService {
         //报警分类(设备、空间、系统 Eq,Sp,Sy)
         String category = data.getString("category");
         String objId = data.getString("objId");
-        String classCode = data.getString("classCode");
         String projectId = data.getString("projectId");
         String itemCode = data.getString("itemCode");
 
+        data.put("classCode", getObjClassCode(getAlarmUrlParam(data), objId));
+        data.put("category", getAlarmCategory(getAlarmUrlParam(data), itemCode));
         data.put("name", getAlarmName(projectId,objId,category));
         data.put("remark", getAlarmRemark(getAlarmUrlParam(data), objId, itemCode));
         data.put("supplement", getAlarmSupplement(getAlarmUrlParam(data), data));
@@ -191,6 +192,28 @@ public abstract class NettyAlarmService extends BaseService {
     }
 
     /**
+     * 获取报警类型所属的报警系统
+     *
+     * @param urlParam 数据中台通用param参数
+     * @param itemCode 报警类型编码
+     * @return 报警系统编码
+     * @author lixing
+     * @version V1.0 2021/11/4 10:15 下午
+     */
+    protected abstract String getAlarmCategory(AlarmUrlParam urlParam, String itemCode);
+
+    /**
+     * 获取设备的设备类编码
+     *
+     * @param @param urlParam 数据中台通用param参数
+     * @param objId 设备id
+     * @return 设备编码
+     * @author lixing
+     * @version V1.0 2021/11/4 10:14 下午
+     */
+    protected abstract String getObjClassCode(AlarmUrlParam urlParam, String objId);
+
+    /**
      * @description: 更新报警记录
      * @param: data
      * @return: void