Initial commit

This commit is contained in:
jackyu66git
2026-03-23 11:52:51 +08:00
commit ea8d889fbe
1114 changed files with 92438 additions and 0 deletions
+46
View File
@@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>oneone-alert</artifactId>
<groupId>com.oneone.cloud</groupId>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>alert-api</artifactId>
<dependencies>
<dependency>
<groupId>com.oneone.cloud</groupId>
<artifactId>common-core</artifactId>
</dependency>
<dependency>
<groupId>com.oneone.cloud</groupId>
<artifactId>common-web</artifactId>
<scope>provided</scope>
</dependency>
<!-- Web 相关 -->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<scope>provided</scope> <!-- 设置为 provided,主要是 PageParam 使用到 -->
</dependency>
<!-- 参数校验 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<optional>true</optional>
</dependency>
<!-- RPC 远程调用相关 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,29 @@
package com.oneone.alert.api.client;
import com.oneone.alert.api.feign.AlertFeignClient;
import com.oneone.alert.api.param.ActionDataParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import javax.annotation.Resource;
/**
* @author mice
* @version 1.0
* @date 2023-05-30 16:34
*/
@Slf4j
public class ActionClient {
@Resource
private AlertFeignClient alertFeignClient;
@Async("asyncExecutor")
public void push(ActionDataParam actionDataParam) {
try {
// alertFeignClient.pushActionData(actionDataParam);
}catch (Exception e){
log.error("pushActionData error",e);
}
}
}
@@ -0,0 +1,66 @@
package com.oneone.alert.api.common;
public class AlterConstant {
public enum BusyCode {
UNKNOWN("-1", "未知"),
SEND_REWARD("send_reward", "奖励发放"),
SEND_BOOK_BACKPACK("send_book_backpack", "发送图书到背包"),
;
BusyCode(String code, String name) {
this.code = code;
this.name = name;
}
private String code;
private String name;
public String getCode() {
return code;
}
public String getName() {
return name;
}
}
public enum BusinessType {
LOGIN("login", "登录"),
BEGINNER_GUIDE("beginner_guide", "新手指引"),
GAME_PERFORMANCE_ISSUES("game_performance_issues", "游戏性能问题"),
MALL("mall", "商城"),
ELEVATOR("elevator", "电梯"),
GAME("game", "游戏"),
RACING_CAR("racing_car", "赛车"),
ENTERTAINMENT_SPACE("entertainment_space", "娱乐空间"),
SQUARE_BIG_SCREEN("square_big_screen", "广场大屏"),
STAR_ENERGY_COLLECTOR("star_energy_collector", "星能收集器"),
VIRTUAL_STORE("virtual_store", "虚拟店铺"),
ART_GALLERY("art_gallery", "艺术馆"),
CLICK_ON_ROLE("click_on_role", "点击角色"),
CHANGE_ROLE("change_role", "更换角色"),
TRANSMIT("transmit", "传送"),
ENTER_SCENE("enter_scene", "进入场景"),
CHANGE_DECORATION("change_decoration", "更换摆件"),
AI_TRAVEL("ai_travel", "AI出行"),
;
BusinessType(String code, String name) {
this.code = code;
this.name = name;
}
private String code;
private String name;
public String getCode() {
return code;
}
public String getName() {
return name;
}
}
}
@@ -0,0 +1,16 @@
package com.oneone.alert.api.event;
import com.oneone.alert.api.param.EventParam;
import org.springframework.context.ApplicationEvent;
import java.util.Map;
public class AlertEvent extends ApplicationEvent {
private static final long serialVersionUID = 1L;
public AlertEvent(EventParam source) {
super(source);
}
}
@@ -0,0 +1,39 @@
package com.oneone.alert.api.event;
import com.oneone.alert.api.feign.AlertFeignClient;
import com.oneone.alert.api.param.EventParam;
import com.oneone.alert.api.util.AlterEventUtil;
import com.oneone.common.server.ServerInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
@Slf4j
public class AlertEventListener {
@Autowired
@Lazy
private AlertFeignClient alertFeignClient;
@Autowired
private ServerInfo serverInfo;
@Async("asyncExecutor")
@Order
@EventListener(AlertEvent.class)
public void pushAlertEvent(AlertEvent event) {
// Map<String, Object> source = (Map<String, Object>) event.getSource();
// EventParam param = (EventParam) source.get(CommonConstant.EVENT_LOG);
// AlterEventUtil.addOtherInfo(param, serverInfo);
// alertFeignClient.pushEvent(param);
try {
EventParam param = (EventParam) event.getSource();
AlterEventUtil.addOtherInfo(param, serverInfo);
alertFeignClient.pushEvent(param);
}catch (Exception e){
log.error("pushAlertEvent error",e);
}
}
}
@@ -0,0 +1,33 @@
package com.oneone.alert.api.feign;
import com.oneone.alert.api.param.ActionDataParam;
import com.oneone.alert.api.param.EventParam;
import com.oneone.common.result.Result;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@FeignClient(name = "oneone-alert", contextId = "alert")
public interface AlertFeignClient {
/**
* 推送事件
* @param param
* @return
*/
@PostMapping("api/v1/event/pushEvent")
Result pushEvent(@RequestBody EventParam param);
/**
* 推送行为数据
* @param param
* @return
*/
@PostMapping("api/v1/actiondata/save")
Result pushActionData(@RequestBody ActionDataParam param);
}
@@ -0,0 +1,101 @@
package com.oneone.alert.api.param;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;
/**
* @desc 数据收集表
* @author mice
* @date 2023-05-09 16:58:58
* @version 1.0
*/
@Data
@Schema(description = "数据收集表")
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ActionDataParam {
private static final long serialVersionUID = 1L;
/**
*
*/
@TableId(type = IdType.AUTO)
@Schema
private Integer id;
/**
* 业务类型
*/
@Schema(description="业务类型")
private String businessType;
/**
* 用户id
*/
@Schema(description="用户id",hidden = true)
private Long memberId;
/**
* 数据字段节点
*/
@Schema(description="数据字段节点")
private String dataField1;
/**
* 数据字段节点
*/
@Schema(description="数据字段节点")
private String dataField2;
/**
* 数据字段节点
*/
@Schema(description="数据字段节点")
private String dataField3;
/**
* 数据字段节点
*/
@Schema(description="数据字段节点")
private String dataField4;
/**
* 数据字段节点
*/
@Schema(description="数据字段节点")
private String dataField5;
/**
* 数据字段节点
*/
@Schema(description="数据字段节点")
private String dataField6;
/**
* 数据字段节点
*/
@Schema(description="数据字段节点")
private String dataField7;
/**
* 数据字段节点
*/
@Schema(description="数据字段节点")
private String dataField8;
/**
* 数据字段节点
*/
@Schema(description="数据字段节点")
private String dataField9;
/**
* 数据字段节点
*/
@Schema(description="数据字段节点")
private String dataField10;
}
@@ -0,0 +1,75 @@
package com.oneone.alert.api.param;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.Length;
/**
* @desc 事件
* @author mice
* @date 2022-04-20 12:10:02
* @version 1.0
*/
@Data
@Schema(description = "事件")
@NoArgsConstructor
@AllArgsConstructor
public class EventParam extends EventParamAbstract{
private static final long serialVersionUID = 1L;
/**
* 标题
*/
@Schema(description="标题")
@Length(max = 8)
private String title;
/**
* 告警等级
*/
@Schema(description="告警等级")
private Integer level;
/**
* 应用
*/
@Schema(description="应用")
private String app;
/**
* 指定通知人"183xxxxx,132xxxxxx"
*/
@Schema(description="通知人手机号,不为空则不走策略 直接推送。")
@Length(max = 200)
private String notifyUser;
/**
* 内容
*/
@Schema(description="内容")
private String content;
/**
* 告警等级
*/
@Schema(description="业务编码")
private String code;
public EventParam(String code, String content) {
this.code = code;
this.content = content;
}
public EventParam(String title, Integer level, String app, String notifyUser, String content) {
this.title = title;
this.level = level;
this.app = app;
this.notifyUser = notifyUser;
this.content = content;
}
}
@@ -0,0 +1,103 @@
package com.oneone.alert.api.param;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import java.io.Serializable;
import java.util.Date;
/**
* logApi、logError、logUsual的父类,拥有相同的属性值
*
*/
@Data
public class EventParamAbstract implements Serializable {
protected static final long serialVersionUID = 1L;
/**
* 服务ID
*/
protected String serviceId;
/**
* 服务器 ip
*/
protected String serverIp;
/**
* 服务器名
*/
protected String serverHost;
/**
* 操作IP地址
*/
protected String remoteIp;
/**
* 用户代理
*/
protected String userAgent;
/**
* 请求URI
*/
protected String requestUri;
/**
* 操作方式
*/
protected String method;
/**
* 方法类
*/
protected String methodClass;
/**
* 方法名
*/
protected String methodName;
/**
* 操作提交的数据
*/
protected String params;
/**
* 执行时间
*/
protected String time;
/**
* 创建人
*/
protected String createBy;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
protected Date createTime;
/**
* 文件名
*/
private String fileName;
/**
* 代码行数
*/
private Integer lineNumber;
/**
* 堆栈信息
*/
private String stackTrace;
/**
* 异常名
*/
private String exceptionName;
/**
* 异常消息
*/
private String message;
/**
* 告警等级
*/
private String code;
}
@@ -0,0 +1,42 @@
package com.oneone.alert.api.publisher;
import cn.hutool.extra.spring.SpringUtil;
import com.oneone.alert.api.event.AlertEvent;
import com.oneone.alert.api.param.EventParam;
import com.oneone.alert.api.util.AlterEventUtil;
import com.oneone.common.util.Exceptions;
import com.oneone.common.util.Func;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
public class AlertEventPublisher {
public static void publishEvent(Throwable error, EventParam eventParam) {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
if (eventParam == null) {
eventParam = new EventParam();
}
if (Func.isNotEmpty(error)) {
eventParam.setStackTrace(Exceptions.getStackTraceAsString(error));
eventParam.setExceptionName(error.getClass().getName());
eventParam.setMessage(error.getMessage());
StackTraceElement[] elements = error.getStackTrace();
if (Func.isNotEmpty(elements)) {
StackTraceElement element = elements[0];
eventParam.setMethodName(element.getMethodName());
eventParam.setMethodClass(element.getClassName());
eventParam.setFileName(element.getFileName());
eventParam.setLineNumber(element.getLineNumber());
}
}
AlterEventUtil.addRequestInfo(request, eventParam);
SpringUtil.publishEvent(new AlertEvent(eventParam));
}
public static void publishEvent(EventParam eventParam) {
SpringUtil.publishEvent(new AlertEvent(eventParam));
}
}
@@ -0,0 +1,36 @@
package com.oneone.alert.api.util;
import com.oneone.alert.api.param.EventParam;
import com.oneone.common.server.ServerInfo;
import com.oneone.common.util.Func;
import com.oneone.common.web.util.UserContext;
import com.oneone.common.web.util.WebUtil;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
public class AlterEventUtil {
public static void addOtherInfo(EventParam param, ServerInfo serverInfo) {
param.setServiceId(serverInfo.getApplicationName());
param.setApp(serverInfo.getApplicationName());
param.setServerHost(serverInfo.getHostName());
param.setServerIp(serverInfo.getIpWithPort());
param.setCreateTime(new Date());
//这里判断一下params为null的情况,否则blade-log服务在解析该字段的时候,可能会报出NPE
if (param.getParams() == null) {
param.setParams("");
}
}
public static void addRequestInfo(HttpServletRequest request, EventParam eventParam) {
eventParam.setRemoteIp(WebUtil.getIP(request));
eventParam.setUserAgent(request.getHeader(WebUtil.USER_AGENT_HEADER));
eventParam.setRequestUri(WebUtil.getPath(request.getRequestURI()));
eventParam.setMethod(request.getMethod());
if(Func.isEmpty(eventParam.getParams())){
eventParam.setParams(WebUtil.getRequestParamString(request));
}
eventParam.setCreateBy(UserContext.getUserId()+"");
}
}
@@ -0,0 +1,4 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.oneone.common.server.ServerInfo,\
com.oneone.alert.api.event.AlertEventListener,\
com.oneone.alert.api.client.ActionClient