diff --git a/pom.xml b/pom.xml index ef534d503..092453741 100644 --- a/pom.xml +++ b/pom.xml @@ -37,27 +37,27 @@ + + 192.168.1.13 + + 192.168.1.111 - 192.168.1.14:18848 + ${middle.server.url}:18848 - - 6b3de6af-da16-41c7-a482-58902c408c0d + + fd74182b-1fce-4dba-afa7-2623b0376205 - - 192.168.1.139:8080 - - - 192.168.1.139 + + ${middle.server.url}:8080 - - 192.168.1.139:10215 + ${service.server.url}:10215 - http://${server.url}:2375 + http://${middle.server.url}:2375 - ${server.url}:8090 + ${middle.server.url}:8090 njcn - site + deploy 1.8 8 8 diff --git a/pqs-auth/src/main/resources/bootstrap.yml b/pqs-auth/src/main/resources/bootstrap.yml index 91954b70a..888084dbe 100644 --- a/pqs-auth/src/main/resources/bootstrap.yml +++ b/pqs-auth/src/main/resources/bootstrap.yml @@ -21,7 +21,7 @@ spring: cloud: nacos: discovery: - ip: @server.url@ + ip: @service.server.url@ server-addr: @nacos.url@ namespace: @nacos.namespace@ config: @@ -49,4 +49,8 @@ mybatis-plus: #别名扫描 type-aliases-package: com.njcn.user.pojo - +mqtt: + uri: tcp://@middle.server.url@:1883 + client-id: @artifactId@ + username: t_user + password: njcnpqs diff --git a/pqs-common/common-core/pom.xml b/pqs-common/common-core/pom.xml index 39250ebb7..4a734c031 100644 --- a/pqs-common/common-core/pom.xml +++ b/pqs-common/common-core/pom.xml @@ -64,10 +64,10 @@ commons-codec commons-codec - - - - + + + + com.github.xiaoymin @@ -91,6 +91,11 @@ com.fasterxml.jackson.core jackson-annotations + + + com.github.tocrhz + mqtt-spring-boot-starter + \ No newline at end of file diff --git a/pqs-common/common-core/src/main/java/com/njcn/common/pojo/dto/LogInfoDTO.java b/pqs-common/common-core/src/main/java/com/njcn/common/pojo/dto/LogInfoDTO.java index b5b3b5175..96c9c7bdf 100644 --- a/pqs-common/common-core/src/main/java/com/njcn/common/pojo/dto/LogInfoDTO.java +++ b/pqs-common/common-core/src/main/java/com/njcn/common/pojo/dto/LogInfoDTO.java @@ -29,12 +29,26 @@ public class LogInfoDTO implements Serializable { private String operateType; - private String result; + /** + * 操作结果 0.失败 1.成功 + */ + private Integer result; - private String level; + /** + * 严重度 0.普通 1.中等 2.严重 + */ + private Integer level; - private String type; + /** + * 事件类型 0.业务事件 1.系统事件 + */ + private Integer type; private String serviceName; + /** + * 0 未登录; 1 已登录 + */ + private Integer isLogin; + } diff --git a/pqs-common/common-core/src/main/java/com/njcn/common/utils/PubUtils.java b/pqs-common/common-core/src/main/java/com/njcn/common/utils/PubUtils.java index 2af3e3535..4cf248a46 100644 --- a/pqs-common/common-core/src/main/java/com/njcn/common/utils/PubUtils.java +++ b/pqs-common/common-core/src/main/java/com/njcn/common/utils/PubUtils.java @@ -2,6 +2,7 @@ package com.njcn.common.utils; import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.StrUtil; +import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; @@ -120,6 +121,21 @@ public class PubUtils { } } + /** + * 将实体对象转为JSON + * + * @param object 实体对象 + */ + public static String obj2json(Object object) { + try { + MAPPER.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); + return MAPPER.writeValueAsString(object); + } catch (IOException e) { + throw new IllegalArgumentException("将将实体对象转为JSON时发生错误:" + object, e); + } + } + + /** * 判断一个数字是否在区间内 * diff --git a/pqs-common/common-web/src/main/java/com/njcn/web/service/impl/LogServiceImpl.java b/pqs-common/common-web/src/main/java/com/njcn/web/service/impl/LogServiceImpl.java index 01c2e0633..ed2ca072c 100644 --- a/pqs-common/common-web/src/main/java/com/njcn/web/service/impl/LogServiceImpl.java +++ b/pqs-common/common-web/src/main/java/com/njcn/web/service/impl/LogServiceImpl.java @@ -2,11 +2,15 @@ package com.njcn.web.service.impl; import cn.hutool.core.text.StrFormatter; import cn.hutool.core.util.StrUtil; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.github.tocrhz.mqtt.publisher.MqttPublisher; import com.njcn.common.config.GeneralInfo; import com.njcn.common.pojo.constant.LogInfo; import com.njcn.common.pojo.dto.LogInfoDTO; import com.njcn.common.pojo.enums.response.CommonResponseEnum; import com.njcn.common.pojo.response.HttpResult; +import com.njcn.common.utils.PubUtils; import com.njcn.common.utils.ReflectCommonUtil; import com.njcn.web.service.ILogService; import com.njcn.web.utils.RequestUtil; @@ -46,6 +50,8 @@ public class LogServiceImpl implements ILogService { private final GeneralInfo generalInfo; + private final MqttPublisher publisher; + /** * 异步记录controller中返回的信息内容 * @@ -59,19 +65,21 @@ public class LogServiceImpl implements ILogService { public void recodeAdviceLog(ServerHttpRequest request, MethodParameter returnType, HttpResult httpResult, String methodDescribe) { //处理审计日志 String userName; + int isLogin = 0; HttpServletRequest httpServletRequest = RequestUtil.getRequest(request); if (UN_LOGIN_METHOD.contains(methodDescribe)) { userName = RequestUtil.getLoginName(httpServletRequest); } else { userName = RequestUtil.getUserNickname(request); + isLogin = 1; } String result = httpResult.getCode().equalsIgnoreCase(CommonResponseEnum.FAIL.getCode()) ? CommonResponseEnum.FAIL.getMessage() : CommonResponseEnum.SUCCESS.getMessage(); String ip = RequestUtil.getRealIp(request); String type = ReflectCommonUtil.getOperateInfoByMethod(returnType.getMethod()).getOperateType(); String level = ReflectCommonUtil.getOperateInfoByMethod(returnType.getMethod()).getOperateLevel(); String operateType = ReflectCommonUtil.getOperateTypeByMethod(returnType.getMethod()); - LogInfoDTO logInfoDTO = new LogInfoDTO(userName, methodDescribe, result, ip, type, level, operateType, generalInfo.getMicroServiceName()); - System.out.println(logInfoDTO); + LogInfoDTO logInfoDTO = new LogInfoDTO(userName, ip, methodDescribe, operateType, result.equalsIgnoreCase("失败") ? 0 : 1, levelStringToNumber(level), type.equalsIgnoreCase("业务事件") ? 0 : 1, generalInfo.getMicroServiceName(), isLogin); + publisher.send("/userLog", PubUtils.obj2json(logInfoDTO), 2, false); } @@ -87,8 +95,10 @@ public class LogServiceImpl implements ILogService { public void recodeBusinessExceptionLog(Exception exception, HttpServletRequest request, String message) { LogInfoDTO tempLogInfo = RequestUtil.initLogInfo(request); //认证前,获取用户信息 + int isLogin = 1; if (Objects.equals(tempLogInfo.getUserName(), LogInfo.UNKNOWN_USER)) { tempLogInfo.setUserName(RequestUtil.getLoginName(request)); + isLogin = 0; } //根据异常获取method方法 Method method = ReflectCommonUtil.getMethod(exception); @@ -101,9 +111,8 @@ public class LogServiceImpl implements ILogService { String type = ReflectCommonUtil.getOperateInfoByMethod(method).getOperateType(); String level = ReflectCommonUtil.getOperateInfoByMethod(method).getOperateLevel(); String operateType = ReflectCommonUtil.getOperateTypeByMethod(method); - LogInfoDTO logInfoDTO = new LogInfoDTO(tempLogInfo.getUserName(), methodDescribe, result, tempLogInfo.getIp(), type, level, operateType, generalInfo.getMicroServiceName()); - System.out.println(logInfoDTO); - + LogInfoDTO logInfoDTO = new LogInfoDTO(tempLogInfo.getUserName(), tempLogInfo.getIp(), methodDescribe, operateType, result.equalsIgnoreCase("失败") ? 0 : 1, levelStringToNumber(level), type.equalsIgnoreCase("业务事件") ? 0 : 1, generalInfo.getMicroServiceName(), isLogin); + publisher.send("/userLog", PubUtils.obj2json(logInfoDTO), 1, false); } /** @@ -129,10 +138,37 @@ public class LogServiceImpl implements ILogService { String type = ReflectCommonUtil.getOperateInfoByMethod(method).getOperateType(); String level = ReflectCommonUtil.getOperateInfoByMethod(method).getOperateLevel(); String operateType = ReflectCommonUtil.getOperateTypeByMethod(method); - LogInfoDTO logInfoDTO = new LogInfoDTO(userName, methodDescribe, result, ip, type, level, operateType, generalInfo.getMicroServiceName()); - System.out.println(logInfoDTO); + LogInfoDTO logInfoDTO = new LogInfoDTO(userName, ip, methodDescribe, operateType, result.equalsIgnoreCase("失败") ? 0 : 1, levelStringToNumber(level), type.equalsIgnoreCase("业务事件") ? 0 : 1, generalInfo.getMicroServiceName(), 0); + publisher.send("/userLog", PubUtils.obj2json(logInfoDTO), 1, false); + } + /** + * 严重度 文字 转 数字 + */ + private Integer levelStringToNumber(String level) { + switch (level) { + case "中等": + return 1; + case "严重": + return 2; + default: + return 0; + } } + /** + * 严重度 数字 转 文字 + */ + private String levelNumberToString(Integer level) { + switch (level) { + case 1: + return "中等"; + case 2: + return "严重"; + default: + return "普通"; + } + } + } diff --git a/pqs-common/pom.xml b/pqs-common/pom.xml index 18d1a1a61..3c900be94 100644 --- a/pqs-common/pom.xml +++ b/pqs-common/pom.xml @@ -21,6 +21,7 @@ common-autocode common-influxdb common-poi + diff --git a/pqs-device/device-boot/src/main/resources/bootstrap.yml b/pqs-device/device-boot/src/main/resources/bootstrap.yml index 42e6dccc8..fd22405fb 100644 --- a/pqs-device/device-boot/src/main/resources/bootstrap.yml +++ b/pqs-device/device-boot/src/main/resources/bootstrap.yml @@ -21,7 +21,7 @@ spring: cloud: nacos: discovery: - ip: @server.url@ + ip: @service.server.url@ server-addr: @nacos.url@ namespace: @nacos.namespace@ config: @@ -54,3 +54,11 @@ logging: mybatis-plus: #别名扫描 type-aliases-package: com.njcn.device.pojo + + +mqtt: + uri: tcp://@middle.server.url@:1883 + client-id: @artifactId@ + username: t_user + password: njcnpqs + diff --git a/pqs-energy/energy-boot/pom.xml b/pqs-energy/energy-boot/pom.xml index ae424daa8..0bdd127ab 100644 --- a/pqs-energy/energy-boot/pom.xml +++ b/pqs-energy/energy-boot/pom.xml @@ -54,10 +54,7 @@ ${project.version} compile - - com.github.tocrhz - mqtt-spring-boot-starter - + org.jetbrains annotations diff --git a/pqs-energy/energy-boot/src/main/resources/bootstrap.yml b/pqs-energy/energy-boot/src/main/resources/bootstrap.yml index 287ed4362..1eda55f48 100644 --- a/pqs-energy/energy-boot/src/main/resources/bootstrap.yml +++ b/pqs-energy/energy-boot/src/main/resources/bootstrap.yml @@ -47,8 +47,9 @@ mybatis-plus: #别名扫描 type-aliases-package: com.njcn.energy.pojo + mqtt: - uri: tcp://192.168.1.14:1883 - client-id: energy_cdf + uri: tcp://@middle.server.url@:1883 + client-id: @artifactId@_cdf username: t_user - password: njcnpqs + password: njcnpqs \ No newline at end of file diff --git a/pqs-event/event-boot/src/main/resources/bootstrap.yml b/pqs-event/event-boot/src/main/resources/bootstrap.yml index 9326b133c..c1303615f 100644 --- a/pqs-event/event-boot/src/main/resources/bootstrap.yml +++ b/pqs-event/event-boot/src/main/resources/bootstrap.yml @@ -20,6 +20,7 @@ spring: cloud: nacos: discovery: + ip: @service.server.url@ server-addr: @nacos.url@ namespace: @nacos.namespace@ config: @@ -47,6 +48,11 @@ mybatis-plus: type-aliases-package: com.njcn.event.pojo +mqtt: + uri: tcp://@middle.server.url@:1883 + client-id: @artifactId@ + username: t_user + password: njcnpqs diff --git a/pqs-gateway/src/main/resources/bootstrap.yml b/pqs-gateway/src/main/resources/bootstrap.yml index a14c30de4..c27231d12 100644 --- a/pqs-gateway/src/main/resources/bootstrap.yml +++ b/pqs-gateway/src/main/resources/bootstrap.yml @@ -18,7 +18,7 @@ spring: cloud: nacos: discovery: - ip: @server.url@ + ip: @service.server.url@ server-addr: @nacos.url@ namespace: @nacos.namespace@ config: @@ -133,9 +133,11 @@ whitelist: - /quality-boot/** - - - +mqtt: + uri: tcp://@middle.server.url@:1883 + client-id: @artifactId@ + username: t_user + password: njcnpqs diff --git a/pqs-harmonic/harmonic-boot/src/main/resources/bootstrap.yml b/pqs-harmonic/harmonic-boot/src/main/resources/bootstrap.yml index 70ca4de85..ae528020e 100644 --- a/pqs-harmonic/harmonic-boot/src/main/resources/bootstrap.yml +++ b/pqs-harmonic/harmonic-boot/src/main/resources/bootstrap.yml @@ -20,7 +20,7 @@ spring: cloud: nacos: discovery: - ip: @server.url@ + ip: @service.server.url@ server-addr: @nacos.url@ namespace: @nacos.namespace@ config: @@ -48,7 +48,11 @@ mybatis-plus: type-aliases-package: com.njcn.harmonic.pojo - +mqtt: + uri: tcp://@middle.server.url@:1883 + client-id: @artifactId@ + username: t_user + password: njcnpqs diff --git a/pqs-job/job-admin/src/main/resources/bootstrap.yml b/pqs-job/job-admin/src/main/resources/bootstrap.yml index 5eb7fdfbe..ac91c4565 100644 --- a/pqs-job/job-admin/src/main/resources/bootstrap.yml +++ b/pqs-job/job-admin/src/main/resources/bootstrap.yml @@ -22,6 +22,7 @@ spring: cloud: nacos: discovery: + ip: @service.server.url@ server-addr: @nacos.url@ namespace: @nacos.namespace@ config: @@ -118,4 +119,3 @@ management: - diff --git a/pqs-job/job-executor/src/main/resources/bootstrap.yml b/pqs-job/job-executor/src/main/resources/bootstrap.yml index 107705fde..288fd560a 100644 --- a/pqs-job/job-executor/src/main/resources/bootstrap.yml +++ b/pqs-job/job-executor/src/main/resources/bootstrap.yml @@ -50,7 +50,7 @@ xxl: #执行器注册 [选填]:优先使用该配置作为注册地址,为空时使用内嵌服务 ”IP:PORT“ 作为注册地址。从而更灵活的支持容器类型执行器动态IP和动态映射端口问题。 address: #执行器IP [选填]:默认为空表示自动获取IP,多网卡时可手动设置指定IP,该IP不会绑定Host仅作为通讯实用;地址信息用于 "执行器注册" 和 "调度中心请求并触发任务"; - ip: @server.url@ + ip: @service.server.url@ #执行器端口号 [选填]:小于等于0则自动获取;默认端口为9999,单机部署多个执行器时,注意要配置不同执行器端口; port: 10219 #执行器运行日志文件存储磁盘路径 [选填] :需要对该路径拥有读写权限;为空则使用默认路径; diff --git a/pqs-quality/quality-boot/src/main/resources/bootstrap.yml b/pqs-quality/quality-boot/src/main/resources/bootstrap.yml index 879619902..a79ebe0e3 100644 --- a/pqs-quality/quality-boot/src/main/resources/bootstrap.yml +++ b/pqs-quality/quality-boot/src/main/resources/bootstrap.yml @@ -21,6 +21,7 @@ spring: cloud: nacos: discovery: + ip: @service.server.url@ server-addr: @nacos.url@ namespace: @nacos.namespace@ config: @@ -50,7 +51,7 @@ mybatis-plus: mqtt: - uri: tcp://192.168.1.18:1883 - client-id: energy + uri: tcp://@middle.server.url@:1883 + client-id: @artifactId@ username: t_user password: njcnpqs \ No newline at end of file diff --git a/pqs-system/system-api/src/main/java/com/njcn/system/pojo/po/UserLog.java b/pqs-system/system-api/src/main/java/com/njcn/system/pojo/po/UserLog.java index 5d782b38a..8d29b210b 100644 --- a/pqs-system/system-api/src/main/java/com/njcn/system/pojo/po/UserLog.java +++ b/pqs-system/system-api/src/main/java/com/njcn/system/pojo/po/UserLog.java @@ -68,4 +68,5 @@ public class UserLog extends BaseEntity { */ private Integer state; + } diff --git a/pqs-system/system-boot/src/main/java/com/njcn/system/controller/UserLogController.java b/pqs-system/system-boot/src/main/java/com/njcn/system/controller/UserLogController.java index bf1ffb777..383f5364d 100644 --- a/pqs-system/system-boot/src/main/java/com/njcn/system/controller/UserLogController.java +++ b/pqs-system/system-boot/src/main/java/com/njcn/system/controller/UserLogController.java @@ -49,11 +49,8 @@ public class UserLogController extends BaseController { @ApiImplicitParam(name = "logInfoDTO", value = "插入日志参数", required = true) public HttpResult addUserLog(@RequestBody LogInfoDTO logInfoDTO) { String methodDescribe = getMethodDescribe("addUserLog"); - boolean res = userLogService.addUserLog(logInfoDTO); - if (res) { - return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS,null,methodDescribe); - } - return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL,null,methodDescribe); + userLogService.addUserLog(logInfoDTO); + return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe); } diff --git a/pqs-system/system-boot/src/main/java/com/njcn/system/handler/MqttMessageHandler.java b/pqs-system/system-boot/src/main/java/com/njcn/system/handler/MqttMessageHandler.java new file mode 100644 index 000000000..3bcb3de56 --- /dev/null +++ b/pqs-system/system-boot/src/main/java/com/njcn/system/handler/MqttMessageHandler.java @@ -0,0 +1,35 @@ +package com.njcn.system.handler; + +import com.github.tocrhz.mqtt.annotation.MqttSubscribe; +import com.github.tocrhz.mqtt.annotation.Payload; +import com.njcn.common.pojo.dto.LogInfoDTO; +import com.njcn.common.utils.PubUtils; +import com.njcn.system.service.IUserLogService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.eclipse.paho.client.mqttv3.MqttMessage; +import org.springframework.stereotype.Component; + +import java.nio.charset.StandardCharsets; + +/** + * @author hongawen + * @version 1.0.0 + * @date 2022年07月13日 13:35 + */ +@Slf4j +@Component +@RequiredArgsConstructor +public class MqttMessageHandler { + + private final IUserLogService userLogService; + + /** + * 订阅审计日志的记录,并进行入库操作 + */ + @MqttSubscribe(value = "/userLog") + public void subUserLog(String topic, MqttMessage message, @Payload String payload) { + LogInfoDTO logInfoDTO = PubUtils.json2obj(new String(message.getPayload(), StandardCharsets.UTF_8),LogInfoDTO.class); + userLogService.addUserLog(logInfoDTO); + } +} diff --git a/pqs-system/system-boot/src/main/resources/bootstrap.yml b/pqs-system/system-boot/src/main/resources/bootstrap.yml index b3a855323..2c3017264 100644 --- a/pqs-system/system-boot/src/main/resources/bootstrap.yml +++ b/pqs-system/system-boot/src/main/resources/bootstrap.yml @@ -20,7 +20,7 @@ spring: cloud: nacos: discovery: - ip: @server.url@ + ip: @service.server.url@ server-addr: @nacos.url@ namespace: @nacos.namespace@ config: @@ -47,3 +47,10 @@ logging: mybatis-plus: #别名扫描 type-aliases-package: com.njcn.system.pojo + + +mqtt: + uri: tcp://@middle.server.url@:1883 + client-id: @artifactId@ + username: t_user + password: njcnpqs \ No newline at end of file diff --git a/pqs-user/user-boot/src/main/resources/bootstrap.yml b/pqs-user/user-boot/src/main/resources/bootstrap.yml index 1a9f461b8..ec5837071 100644 --- a/pqs-user/user-boot/src/main/resources/bootstrap.yml +++ b/pqs-user/user-boot/src/main/resources/bootstrap.yml @@ -21,7 +21,7 @@ spring: cloud: nacos: discovery: - ip: @server.url@ + ip: @service.server.url@ server-addr: @nacos.url@ namespace: @nacos.namespace@ config: @@ -48,3 +48,9 @@ logging: mybatis-plus: #别名扫描 type-aliases-package: com.njcn.user.pojo + +mqtt: + uri: tcp://@middle.server.url@:1883 + client-id: @artifactId@ + username: t_user + password: njcnpqs