Compare commits
127 Commits
Author | SHA1 | Date |
---|---|---|
|
d21aab7ef9 | |
|
761a186777 | |
|
f68c4ca325 | |
|
24b8fe4085 | |
|
13a67257fc | |
|
b04f697547 | |
|
585656aa46 | |
|
55ed020ac7 | |
|
cedad4d7b9 | |
|
c8f912393d | |
|
0be030416f | |
|
40fccdbc56 | |
|
b8a7658ce5 | |
|
a8cdd55797 | |
|
1960d9dd90 | |
|
2308d974be | |
|
b26bb59183 | |
|
1a62dd6f96 | |
|
6779e2120e | |
|
b9ce76ca15 | |
|
2e940349ef | |
|
cf286d48f9 | |
|
ec57579533 | |
|
4cbfb65109 | |
|
acd7ad9fb4 | |
|
b266460a0a | |
|
d7fa72be80 | |
|
88fdcfcbab | |
|
79f9309c0f | |
|
0aa33b36a0 | |
|
d5d17321ff | |
|
6e5e07af85 | |
|
d0e51b14b1 | |
|
4548e2c781 | |
|
59c16de298 | |
|
efa6861d2f | |
|
0ebf046e25 | |
|
2773a13270 | |
|
2a0bd1178e | |
|
97994a0452 | |
|
cd5e46b650 | |
|
f6baa83c94 | |
|
2235097f05 | |
|
9e94cf5674 | |
|
ca505dc2c4 | |
|
c90845d508 | |
|
c1f24c8d03 | |
|
f45e6865d9 | |
|
abf186e99c | |
|
2186c58122 | |
|
b395a24329 | |
|
e2897c4bb7 | |
|
c539a55f1e | |
|
9499d9738e | |
|
ae7294da6f | |
|
fe3ce0e291 | |
|
bd0214ccd9 | |
|
d01a3d5ce1 | |
|
705414778c | |
|
dd319599b6 | |
|
875391757f | |
|
b9c4334cb5 | |
|
87d104cf9b | |
|
a63bde6c84 | |
|
c5f0d52aa6 | |
|
e2fc097b14 | |
|
30782882c3 | |
|
8c90364eaa | |
|
dc26138929 | |
|
4399609c9e | |
|
a769d4da40 | |
|
a3613176f3 | |
|
d82ae07d37 | |
|
89825669dc | |
|
f56a0e8b7f | |
|
9846c72b3b | |
|
53ff3d70c9 | |
|
709878fbab | |
|
6dd3d73f62 | |
|
2623669eb0 | |
|
3a8b35b710 | |
|
cb8867d0ee | |
|
ba8009fd8d | |
|
954a8939c6 | |
|
3393804735 | |
|
59050208a0 | |
|
008efdde25 | |
|
217af96abc | |
|
b5a2687491 | |
|
370a91773e | |
|
5589803de6 | |
|
708214b86e | |
|
c79a39f54e | |
|
476d6ae0b7 | |
|
4adb06459e | |
|
a13ba8199a | |
|
fe90a341ff | |
|
d99edc16d1 | |
|
50a655d888 | |
|
e8bc92c658 | |
|
76440e1c98 | |
|
b879a1768d | |
|
370fdd9fd5 | |
|
f46db057b5 | |
|
82ec79ccaf | |
|
551f5f5496 | |
|
8e0e492555 | |
|
6545fc79ef | |
|
78747b7d03 | |
|
d6ff8ad340 | |
|
8bc54f4662 | |
|
e6639bc7f9 | |
|
a0ddc45e67 | |
|
051fe490d6 | |
|
31df6d32db | |
|
798f509dec | |
|
f93af8b4a6 | |
|
1272d0e0b7 | |
|
7149ea8f1c | |
|
1f072d4ca2 | |
|
a6521e46c9 | |
|
cc99d65102 | |
|
29cb1ffadb | |
|
2488248967 | |
|
dea0f07c90 | |
|
688154709d | |
|
3f9a930c95 |
|
@ -16,6 +16,13 @@
|
|||
|
||||
<dependencies>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
<version>3.5.5</version>
|
||||
</dependency>
|
||||
|
||||
<!-- SpringCloud Alibaba Nacos -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
|
@ -34,6 +41,8 @@
|
|||
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
|
||||
<!-- SpringBoot Web -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
@ -57,6 +66,19 @@
|
|||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-api-doc</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-saas</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-all</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
|
|
@ -1,8 +1,13 @@
|
|||
package com.muyu.auth.controller;
|
||||
|
||||
import com.muyu.auth.form.EnterpriseSettlement;
|
||||
import com.muyu.auth.form.Firm;
|
||||
import com.muyu.auth.form.LoginBody;
|
||||
import com.muyu.auth.form.RegisterBody;
|
||||
import com.muyu.auth.service.SysFirmService;
|
||||
import com.muyu.auth.service.SysLoginService;
|
||||
import com.muyu.cloud.common.many.datasource.constents.DatasourceContent;
|
||||
import com.muyu.common.core.constant.SecurityConstants;
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.common.core.utils.JwtUtils;
|
||||
import com.muyu.common.core.utils.StringUtils;
|
||||
|
@ -10,7 +15,15 @@ import com.muyu.common.security.auth.AuthUtil;
|
|||
import com.muyu.common.security.service.TokenService;
|
||||
import com.muyu.common.security.utils.SecurityUtils;
|
||||
import com.muyu.common.system.domain.LoginUser;
|
||||
import com.muyu.common.system.domain.SysUser;
|
||||
import com.muyu.common.system.remote.RemoteUserService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.support.EncodedResource;
|
||||
import org.springframework.jdbc.datasource.init.ScriptUtils;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
|
@ -18,12 +31,20 @@ import org.springframework.web.bind.annotation.RestController;
|
|||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* token 控制
|
||||
*
|
||||
* @author muyu
|
||||
*/
|
||||
@Log4j2
|
||||
@RestController
|
||||
@Tag(name = "auth",description = "auth")
|
||||
public class TokenController {
|
||||
@Autowired
|
||||
private TokenService tokenService;
|
||||
|
@ -31,8 +52,20 @@ public class TokenController {
|
|||
@Autowired
|
||||
private SysLoginService sysLoginService;
|
||||
|
||||
@Autowired
|
||||
private SysFirmService sysFirmService;
|
||||
|
||||
@Autowired
|
||||
private RemoteUserService remoteUserService;
|
||||
|
||||
@PostMapping("login")
|
||||
@Operation(summary = "登录", description = "登录")
|
||||
public Result<?> login (@RequestBody LoginBody form) {
|
||||
//查询企业是否存在
|
||||
Firm firm = sysFirmService.findFirmByName(form.getFirmName());
|
||||
if (firm.getDatabaseName() == null){
|
||||
return Result.error(null,"企业不存在");
|
||||
}
|
||||
// 用户登录
|
||||
LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword());
|
||||
// 获取登录token
|
||||
|
@ -40,6 +73,7 @@ public class TokenController {
|
|||
}
|
||||
|
||||
@DeleteMapping("logout")
|
||||
@Operation(summary = "退出", description = "退出")
|
||||
public Result<?> logout (HttpServletRequest request) {
|
||||
String token = SecurityUtils.getToken(request);
|
||||
if (StringUtils.isNotEmpty(token)) {
|
||||
|
@ -53,6 +87,7 @@ public class TokenController {
|
|||
}
|
||||
|
||||
@PostMapping("refresh")
|
||||
@Operation(summary = "刷新token", description = "刷新token")
|
||||
public Result<?> refresh (HttpServletRequest request) {
|
||||
LoginUser loginUser = tokenService.getLoginUser(request);
|
||||
if (StringUtils.isNotNull(loginUser)) {
|
||||
|
@ -64,9 +99,65 @@ public class TokenController {
|
|||
}
|
||||
|
||||
@PostMapping("register")
|
||||
@Operation(summary = "注册用户", description = "注册用户")
|
||||
public Result<?> register (@RequestBody RegisterBody registerBody) {
|
||||
// 用户注册
|
||||
sysLoginService.register(registerBody.getUsername(), registerBody.getPassword());
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 企业入入驻
|
||||
* @param settlement
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/enterprise")
|
||||
@Operation(summary = "企业入驻", description = "企业入驻")
|
||||
public Result<?> enterprise( @RequestBody EnterpriseSettlement settlement){
|
||||
|
||||
String createDatabaseUrl="jdbc:mysql://"+ DatasourceContent.IP+":"+DatasourceContent.PORT+"?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
|
||||
String createDatabaseSql = "CREATE DATABASE IF NOT EXISTS " + settlement.getDatabaseName() + ";";
|
||||
try (Connection adminConn = DriverManager.getConnection(createDatabaseUrl, DatasourceContent.USER_NAME, DatasourceContent.PASSWORD);
|
||||
Statement stmt = adminConn.createStatement()) {
|
||||
|
||||
boolean success = stmt.execute(createDatabaseSql);
|
||||
if (success) {
|
||||
log.info("数据库 {} 创建失败", settlement.getDatabaseName());
|
||||
|
||||
} else {
|
||||
log.warn("数据库 {} 创建成功", settlement.getDatabaseName());
|
||||
|
||||
|
||||
SysUser sysUser = new SysUser();
|
||||
sysUser.setCreateTime(new Date());
|
||||
sysUser.setUserName(settlement.getFirmName());
|
||||
sysUser.setDatabaseName(settlement.getDatabaseName());
|
||||
remoteUserService.addUser(sysUser, SecurityConstants.INNER);
|
||||
|
||||
// 切换到新的数据库连接
|
||||
Connection connection = null;
|
||||
try {
|
||||
String url = "jdbc:mysql://47.101.53.251:3306/" + settlement.getDatabaseName() + "?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
|
||||
String user = "root";
|
||||
String pwd = "Lw030106";
|
||||
String driverClassName = "com.mysql.cj.jdbc.Driver";
|
||||
Class.forName(driverClassName);
|
||||
connection = DriverManager.getConnection(url, user, pwd);
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
ClassPathResource rc = new ClassPathResource("init-file/saas.sql");
|
||||
EncodedResource er = new EncodedResource(rc, "utf-8");
|
||||
ScriptUtils.executeSqlScript(connection, er);
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
log.error("连接数据库时发生错误或创建数据库失败", e);
|
||||
}
|
||||
//企业入驻
|
||||
sysLoginService.enterprise(settlement.getDatabaseName(),settlement.getFirmName());
|
||||
|
||||
return Result.success();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
package com.muyu.auth.form;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 企业入驻
|
||||
* @author 袁子龙
|
||||
* @package com.muyu.auth.form
|
||||
* @name EnterpriseSettlement
|
||||
* @date 2024/9/30 11:25
|
||||
*/
|
||||
@Data
|
||||
public class EnterpriseSettlement {
|
||||
/**
|
||||
* 企业名称
|
||||
*/
|
||||
private String firmName;
|
||||
/**
|
||||
* 数据库名称
|
||||
*/
|
||||
private String databaseName;
|
||||
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package com.muyu.auth.form;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 企业登录对象
|
||||
* @author 袁子龙
|
||||
* @package com.muyu.auth.form
|
||||
* @name Enterprise
|
||||
* @date 2024/9/30 10:30
|
||||
*/
|
||||
@Data
|
||||
public class Firm {
|
||||
|
||||
/**
|
||||
* 企业id
|
||||
*/
|
||||
private Integer id;
|
||||
/**
|
||||
* 企业名称
|
||||
*/
|
||||
private String firmName;
|
||||
/**
|
||||
* 数据库名称
|
||||
*/
|
||||
private String databaseName;
|
||||
|
||||
}
|
|
@ -1,10 +1,14 @@
|
|||
package com.muyu.auth.form;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 用户登录对象
|
||||
*
|
||||
* @author muyu
|
||||
*/
|
||||
|
||||
@Data
|
||||
public class LoginBody {
|
||||
/**
|
||||
* 用户名
|
||||
|
@ -16,20 +20,11 @@ public class LoginBody {
|
|||
*/
|
||||
private String password;
|
||||
|
||||
/**
|
||||
* 企业名称
|
||||
*/
|
||||
private String firmName;
|
||||
|
||||
public String getUsername () {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername (String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword () {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword (String password) {
|
||||
this.password = password;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,57 @@
|
|||
package com.muyu.auth.service;
|
||||
|
||||
import com.muyu.auth.form.Firm;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
|
||||
/**
|
||||
* 数据源
|
||||
* @author 袁子龙
|
||||
* @package com.muyu.auth.service
|
||||
* @name SysFirmService
|
||||
* @date 2024/9/30 11:05
|
||||
*/
|
||||
@Component
|
||||
public class SysFirmService {
|
||||
//数据库账号
|
||||
static final String USER="root";
|
||||
//数据库密码
|
||||
static final String PASSWORD="Lw030106";
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate redisTemplate;
|
||||
public Firm findFirmByName(String firmName){
|
||||
Firm firm = new Firm();
|
||||
try {
|
||||
DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
|
||||
Connection connection= DriverManager.getConnection("jdbc:mysql://47.101.53.251:3306/datasource?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT&useSSL=false",USER,PASSWORD);
|
||||
String sql="select * from `datasource` where firm_name = '"+firmName+"'";
|
||||
|
||||
Statement stmt = connection.createStatement();
|
||||
ResultSet rs = stmt.executeQuery(sql);
|
||||
|
||||
|
||||
while (rs.next()){
|
||||
firm.setId(rs.getInt("id"));
|
||||
firm.setFirmName(rs.getString("firm_name"));
|
||||
firm.setDatabaseName(rs.getString("database_name"));
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
//数据源不为空
|
||||
if (firm!=null){
|
||||
redisTemplate.opsForValue().set("datasource",firm.getDatabaseName());
|
||||
}
|
||||
return firm;
|
||||
};
|
||||
}
|
|
@ -1,5 +1,7 @@
|
|||
package com.muyu.auth.service;
|
||||
|
||||
|
||||
import com.muyu.cloud.common.many.datasource.ManyDataSource;
|
||||
import com.muyu.common.core.constant.CacheConstants;
|
||||
import com.muyu.common.core.constant.Constants;
|
||||
import com.muyu.common.core.constant.SecurityConstants;
|
||||
|
@ -12,7 +14,10 @@ import com.muyu.common.core.utils.StringUtils;
|
|||
import com.muyu.common.core.utils.ip.IpUtils;
|
||||
import com.muyu.common.redis.service.RedisService;
|
||||
import com.muyu.common.security.utils.SecurityUtils;
|
||||
|
||||
|
||||
import com.muyu.common.system.remote.RemoteUserService;
|
||||
import com.muyu.common.system.domain.Business;
|
||||
import com.muyu.common.system.domain.SysUser;
|
||||
import com.muyu.common.system.domain.LoginUser;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -37,6 +42,8 @@ public class SysLoginService {
|
|||
@Autowired
|
||||
private RedisService redisService;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 登录
|
||||
*/
|
||||
|
@ -88,6 +95,8 @@ public class SysLoginService {
|
|||
}
|
||||
passwordService.validate(user, password);
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_SUCCESS, "登录成功");
|
||||
|
||||
|
||||
return userInfo;
|
||||
}
|
||||
|
||||
|
@ -124,4 +133,29 @@ public class SysLoginService {
|
|||
}
|
||||
recordLogService.recordLogininfor(username, Constants.REGISTER, "注册成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 企业入驻
|
||||
* @param databaseName
|
||||
* @param fileName
|
||||
*/
|
||||
public void enterprise (String databaseName,String fileName) {
|
||||
// 参数校验 数据库名或企业名称不能为空
|
||||
if (StringUtils.isAnyBlank(databaseName, fileName)) {
|
||||
throw new ServiceException("数据库名或企业名称不能为空");
|
||||
}
|
||||
|
||||
if (databaseName.length() < UserConstants.PASSWORD_MIN_LENGTH || databaseName.length() > UserConstants.PASSWORD_MAX_LENGTH) {
|
||||
throw new ServiceException("数据库名长度必须在5到20个字符之间");
|
||||
}
|
||||
if (fileName.length() < UserConstants.Firm_NAME_MIN_LENGTH || fileName.length() > UserConstants.Firm_NAME_MAX_LENGTH) {
|
||||
throw new ServiceException("企业名称长度必须在2到20个字符之间");
|
||||
}
|
||||
Business settlement = new Business();
|
||||
settlement.setDatabaseName(databaseName);
|
||||
settlement.setFirmName(fileName);
|
||||
|
||||
remoteUserService.settlementEnterpriseInfo(settlement, SecurityConstants.INNER);
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,374 @@
|
|||
/*
|
||||
Navicat Premium Data Transfer
|
||||
|
||||
Source Server : 组
|
||||
Source Server Type : MySQL
|
||||
Source Server Version : 80401
|
||||
Source Host : 47.101.53.251:3306
|
||||
Source Schema : saas
|
||||
|
||||
Target Server Type : MySQL
|
||||
Target Server Version : 80401
|
||||
File Encoding : 65001
|
||||
|
||||
Date: 04/10/2024 16:01:07
|
||||
*/
|
||||
|
||||
SET NAMES utf8mb4;
|
||||
SET FOREIGN_KEY_CHECKS = 0;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for car_type
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `car_type`;
|
||||
CREATE TABLE `car_type` (
|
||||
`id` int NOT NULL AUTO_INCREMENT COMMENT '车辆类型Id',
|
||||
`type_name` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '车辆类型',
|
||||
`template_id` int NULL DEFAULT NULL COMMENT '报文模版Id',
|
||||
PRIMARY KEY (`id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '车辆类型' ROW_FORMAT = Dynamic;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for data_type
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `data_type`;
|
||||
CREATE TABLE `data_type` (
|
||||
`data_type_id` int NOT NULL AUTO_INCREMENT COMMENT '报文数据类型Id',
|
||||
`data_type_name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '报文数据类型',
|
||||
PRIMARY KEY (`data_type_id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '报文数据类型' ROW_FORMAT = Dynamic;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for electronic_fence
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `electronic_fence`;
|
||||
CREATE TABLE `electronic_fence` (
|
||||
`id` int NOT NULL AUTO_INCREMENT COMMENT '围栏主键',
|
||||
`name` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '围栏名称',
|
||||
`fence_type` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '围栏类型(驶入,驶出)',
|
||||
`longitude_latitude` text CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL COMMENT '经纬度信息',
|
||||
`status` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '电子围栏状态(正常,停用)',
|
||||
`fence_desc` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '描述信息',
|
||||
`create_by` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '创建人',
|
||||
`create_time` datetime NULL DEFAULT NULL COMMENT '创建时间',
|
||||
`update_by` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '更新人',
|
||||
`update_time` datetime NULL DEFAULT NULL COMMENT '更新时间',
|
||||
`remark` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '备注',
|
||||
PRIMARY KEY (`id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 11 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin COMMENT = '电子围栏' ROW_FORMAT = DYNAMIC;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for electronic_fence_group
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `electronic_fence_group`;
|
||||
CREATE TABLE `electronic_fence_group` (
|
||||
`id` int NOT NULL AUTO_INCREMENT COMMENT '自增主键',
|
||||
`group_name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '围栏组名称',
|
||||
`group_type` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '围栏组类型',
|
||||
`priority` int NULL DEFAULT NULL COMMENT '围栏组优先级(0-99)',
|
||||
`status` varchar(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '启用状态',
|
||||
`create_by` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '创建人',
|
||||
`create_time` datetime NULL DEFAULT NULL COMMENT '创建时间',
|
||||
`update_by` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '更新人',
|
||||
`update_time` datetime NULL DEFAULT NULL COMMENT '更新时间',
|
||||
`remark` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '备注',
|
||||
PRIMARY KEY (`id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 11 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin COMMENT = '围栏组' ROW_FORMAT = DYNAMIC;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for fence_group_mid
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `fence_group_mid`;
|
||||
CREATE TABLE `fence_group_mid` (
|
||||
`id` int NOT NULL AUTO_INCREMENT,
|
||||
`group_id` int NULL DEFAULT NULL COMMENT '围栏组外键',
|
||||
`fence_id` int NULL DEFAULT NULL COMMENT '电子围栏外键',
|
||||
PRIMARY KEY (`id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 23 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '电子围栏-围栏组(中间表)' ROW_FORMAT = DYNAMIC;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for message_template
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `message_template`;
|
||||
CREATE TABLE `message_template` (
|
||||
`message_id` int(10) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT,
|
||||
`vin_code` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT 'VIN 码',
|
||||
`time_stamp` datetime NULL DEFAULT NULL COMMENT '时间戳',
|
||||
`long_itude` double NULL DEFAULT NULL COMMENT '经度',
|
||||
`latitude` double NULL DEFAULT NULL COMMENT '纬度',
|
||||
`speed_vehicle` double NULL DEFAULT NULL COMMENT '车速',
|
||||
`total_mileage` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '总里程',
|
||||
`total_voltage` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '总电压',
|
||||
`combined_current` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '总电流',
|
||||
`insulation_resistance` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '绝缘电阻',
|
||||
`gear_position` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '档位',
|
||||
`accelerator_pedal_travel_value` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '加速踏板行程值',
|
||||
`brake_pedal_travel_value` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '制动踏板行程值',
|
||||
`specific_fuel_consumption` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '燃料消耗率',
|
||||
`motor_controller_temperature` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '电机控制器温度',
|
||||
`motor_speed` double NULL DEFAULT NULL COMMENT '电机转速',
|
||||
`motor_torque` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '电机转矩',
|
||||
`motor_temperature` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '电机温度',
|
||||
`motor_voltage` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '电机电压',
|
||||
`motor_current` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '电机电流',
|
||||
`power_battery_remaining_soc` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '剩余电池电量',
|
||||
`maximum_feedback_power` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '最大反馈功率',
|
||||
`maximum_discharge_power` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '最大放电功率',
|
||||
`bms_self_check_counter` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT 'BMS自检计数器',
|
||||
`power_battery` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '动力电池',
|
||||
`total_voltage_load_side` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '总电压负载侧',
|
||||
`maximum_voltage` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '最大电压',
|
||||
`minimum_voltage` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '最小电压',
|
||||
`maximum_temperature` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '最高温度',
|
||||
`minimum_temperature` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '最低温度',
|
||||
`available_capacity` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '可用功率',
|
||||
`vehicle_status` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '车辆状态',
|
||||
`charging_state` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '充电状态',
|
||||
`operational_status` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '行驶状态',
|
||||
`soc` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '单芯片系统',
|
||||
`energy_storage_devices` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '能源存储设备',
|
||||
`drive_motor_condition` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '驱动电机转态',
|
||||
`whether_works` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '是否有效',
|
||||
`eas` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '电子防窃系统',
|
||||
`ptc` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '正常温度系数',
|
||||
`eps` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '蓄电池',
|
||||
`abs` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '防抱死制动系统',
|
||||
`mcu` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '单片机',
|
||||
`heating_state` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '加热状态',
|
||||
`power_battery_status` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT '' COMMENT '电源电池状态',
|
||||
`state_battery_insulation` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '状态电池绝缘',
|
||||
`dcdc` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '直流直流变换器',
|
||||
`chg` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '变化',
|
||||
`check_digit` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '校验数位',
|
||||
`cutoff_bit` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '截止点',
|
||||
PRIMARY KEY (`message_id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '报文模版' ROW_FORMAT = Dynamic;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for message_template_type
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `message_template_type`;
|
||||
CREATE TABLE `message_template_type` (
|
||||
`message_template_type_id` int NOT NULL AUTO_INCREMENT,
|
||||
`message_code` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '编码',
|
||||
`message_field` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '标签',
|
||||
`start_index` int NULL DEFAULT NULL COMMENT '起始位',
|
||||
`end_index` int NULL DEFAULT NULL COMMENT '终值位',
|
||||
`data_type_id` int NULL DEFAULT NULL COMMENT '报文数据类型Id',
|
||||
`data_type_name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '报文数据类型名称',
|
||||
`fixed_value` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '最小值',
|
||||
`range_value` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '最大值',
|
||||
`template_id` int NULL DEFAULT NULL COMMENT '模版Id',
|
||||
`message_class` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '报文类别',
|
||||
PRIMARY KEY (`message_template_type_id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 76 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '报文模版类型' ROW_FORMAT = Dynamic;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for message_value
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `message_value`;
|
||||
CREATE TABLE `message_value` (
|
||||
`message_id` bigint NOT NULL AUTO_INCREMENT COMMENT '报文主键',
|
||||
`template_id` bigint NULL DEFAULT NULL COMMENT '模版外键',
|
||||
`message_code` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '报文编码',
|
||||
`message_label` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '报文标签',
|
||||
`message_start_index` int NULL DEFAULT NULL COMMENT '起始位',
|
||||
`message_end_index` int NULL DEFAULT NULL COMMENT '终止位',
|
||||
`create_by` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '创建人',
|
||||
`create_time` datetime NULL DEFAULT NULL COMMENT '创建时间',
|
||||
`update_by` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '更新人',
|
||||
`update_time` datetime NULL DEFAULT NULL COMMENT '更新时间',
|
||||
`remark` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '备注',
|
||||
`message_class` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
|
||||
PRIMARY KEY (`message_id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 61 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '报文' ROW_FORMAT = DYNAMIC;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for sys_car
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `sys_car`;
|
||||
CREATE TABLE `sys_car` (
|
||||
`id` int NOT NULL AUTO_INCREMENT,
|
||||
`car_vin` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT 'VIN码',
|
||||
`car_type_id` int NULL DEFAULT NULL COMMENT '车辆类型Id',
|
||||
`state` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '状态',
|
||||
`fence_id` int NULL DEFAULT NULL COMMENT '电子围栏',
|
||||
`car_motor_manufacturer` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '车辆电机厂商',
|
||||
`car_motor_model` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '电机型号',
|
||||
`car_battery_manufacturer` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '车辆电池厂商',
|
||||
`car_battery_model` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '电池型号',
|
||||
`strategy_id` int NULL DEFAULT NULL COMMENT '策略ID',
|
||||
`group_id` int NULL DEFAULT NULL COMMENT '围栏组ID',
|
||||
`create_by` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '创建人',
|
||||
`create_time` datetime NULL DEFAULT NULL COMMENT '创建时间',
|
||||
`update_by` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '修改人',
|
||||
`update_time` datetime NULL DEFAULT NULL COMMENT '修改时间',
|
||||
`remark` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '备注',
|
||||
`template_id` int NULL DEFAULT NULL COMMENT '报文模版ID',
|
||||
PRIMARY KEY (`id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '车辆基础信息表' ROW_FORMAT = DYNAMIC;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for sys_car_enterprise
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `sys_car_enterprise`;
|
||||
CREATE TABLE `sys_car_enterprise` (
|
||||
`id` int NOT NULL AUTO_INCREMENT,
|
||||
`enterprise_name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '企业名称',
|
||||
`name` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '用户姓名',
|
||||
`position` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '用户职位',
|
||||
`province` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '公司所在省',
|
||||
`city` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '公司所在市',
|
||||
`county` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '公司所在县/区',
|
||||
`address` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '公司详细地址',
|
||||
`credit_code` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '统一社会信用代码',
|
||||
`business_license` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '营业执照',
|
||||
PRIMARY KEY (`id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '企业' ROW_FORMAT = Dynamic;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for sys_car_fault
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `sys_car_fault`;
|
||||
CREATE TABLE `sys_car_fault` (
|
||||
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '参数主键',
|
||||
`fault_code` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '故障码编码',
|
||||
`fault_name` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '故障名',
|
||||
`type_id` int NULL DEFAULT NULL COMMENT '故障类型',
|
||||
`fault_label` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '故障标签',
|
||||
`fault_bit` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '故障位',
|
||||
`fault_value` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '故障值',
|
||||
`fault_rank` int NULL DEFAULT NULL COMMENT '故障级别 (0.低 ,1.中 ,2.高)',
|
||||
`fault_desc` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '故障描述信息',
|
||||
`fault_min_threshold` int NULL DEFAULT NULL COMMENT '故障最小阈值',
|
||||
`fault_max_threshold` int NULL DEFAULT NULL COMMENT '故障最大阈值',
|
||||
`status` int NULL DEFAULT 1 COMMENT '启用状态(1.待处理 2.处理中 3.已处理 4.忽略)',
|
||||
`warn_status` int NULL DEFAULT 0 COMMENT '是否警告(0.开启 1.禁止)',
|
||||
`remark` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '备注',
|
||||
`create_by` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '创建者',
|
||||
`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
|
||||
`update_by` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '更新者',
|
||||
`update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
|
||||
`car_type_id` int NULL DEFAULT NULL COMMENT '车辆类型',
|
||||
PRIMARY KEY (`id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 17 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '故障码' ROW_FORMAT = Dynamic;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for sys_car_fault_log
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `sys_car_fault_log`;
|
||||
CREATE TABLE `sys_car_fault_log` (
|
||||
`id` int NOT NULL AUTO_INCREMENT,
|
||||
`sys_car_fault_id` int NOT NULL COMMENT '故障码编号',
|
||||
`create_by` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '创建人',
|
||||
`create_time` datetime NULL DEFAULT NULL COMMENT '开始时间',
|
||||
`update_by` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '修改人',
|
||||
`update_time` datetime NULL DEFAULT NULL COMMENT '结束时间',
|
||||
`vin` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT 'VIN码',
|
||||
`remark` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '内容',
|
||||
`status` int NULL DEFAULT 2 COMMENT '1- 解决 2- 处理中 3-忽略',
|
||||
PRIMARY KEY (`id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '故障日志表' ROW_FORMAT = Dynamic;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for sys_car_fault_message
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `sys_car_fault_message`;
|
||||
CREATE TABLE `sys_car_fault_message` (
|
||||
`id` int NOT NULL AUTO_INCREMENT,
|
||||
`content` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '内容',
|
||||
`status` int NULL DEFAULT NULL COMMENT '1-已读 2-未读',
|
||||
`create_by` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '创建人',
|
||||
`create_time` datetime NULL DEFAULT NULL COMMENT '开始时间',
|
||||
`update_by` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '修改人',
|
||||
`update_time` datetime NULL DEFAULT NULL COMMENT '结束时间',
|
||||
`remark` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '备注',
|
||||
PRIMARY KEY (`id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '站内信' ROW_FORMAT = Dynamic;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for sys_car_log
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `sys_car_log`;
|
||||
CREATE TABLE `sys_car_log` (
|
||||
`id` int NOT NULL AUTO_INCREMENT,
|
||||
`car_vin` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT 'VIN码',
|
||||
`start_time` datetime NULL DEFAULT NULL COMMENT '创建时间',
|
||||
`end_time` datetime NULL DEFAULT NULL COMMENT '结束时间',
|
||||
PRIMARY KEY (`id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '车辆日志表' ROW_FORMAT = Dynamic;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for t_template
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `t_template`;
|
||||
CREATE TABLE `t_template` (
|
||||
`template_id` int NOT NULL AUTO_INCREMENT,
|
||||
`house_id` int NULL DEFAULT NULL,
|
||||
`template_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '报文模版名称',
|
||||
`template_describe` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '报文模版描述',
|
||||
`create_time` datetime NULL DEFAULT NULL COMMENT '创建时间',
|
||||
PRIMARY KEY (`template_id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '报文模版表' ROW_FORMAT = Dynamic;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for tb_enterprise
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `tb_enterprise`;
|
||||
CREATE TABLE `tb_enterprise` (
|
||||
`enterprise_id` int NOT NULL AUTO_INCREMENT COMMENT '企业编号',
|
||||
`enterprise_name` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '企业名称',
|
||||
`enterprise_car_count` int NULL DEFAULT 0 COMMENT '企业车辆数量',
|
||||
`enterprise_fence_count` int NULL DEFAULT 0 COMMENT '企业电子围栏数量',
|
||||
`enterprise_database_name` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '企业数据库',
|
||||
PRIMARY KEY (`enterprise_id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 18 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '企业运营' ROW_FORMAT = Dynamic;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for warn_logs
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `warn_logs`;
|
||||
CREATE TABLE `warn_logs` (
|
||||
`id` int NOT NULL AUTO_INCREMENT COMMENT '预警日志id',
|
||||
`vin` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '车辆vin码',
|
||||
`warn_rule_id` int NULL DEFAULT NULL COMMENT '规则id',
|
||||
`start_time` datetime NULL DEFAULT NULL COMMENT '开始时间',
|
||||
`end_time` datetime NULL DEFAULT NULL COMMENT '结束时间',
|
||||
`max_value` int NULL DEFAULT NULL COMMENT '最大值',
|
||||
`min_value` int NULL DEFAULT NULL COMMENT '最小值',
|
||||
`avg_value` int NULL DEFAULT NULL COMMENT '平均值',
|
||||
`median_value` int NULL DEFAULT NULL COMMENT '中位数',
|
||||
`status` int NULL DEFAULT NULL COMMENT '是否发送预警',
|
||||
PRIMARY KEY (`id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '车辆预警记录表' ROW_FORMAT = DYNAMIC;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for warn_rule
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `warn_rule`;
|
||||
CREATE TABLE `warn_rule` (
|
||||
`id` int NOT NULL AUTO_INCREMENT COMMENT '规则id',
|
||||
`rule_name` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '规则名称',
|
||||
`strategy_id` int NULL DEFAULT NULL COMMENT '策略id',
|
||||
`msg_type_id` int NULL DEFAULT NULL COMMENT '报文数据类型id',
|
||||
`slide_time` int NULL DEFAULT NULL COMMENT '滑窗时间',
|
||||
`slide_frequency` int NULL DEFAULT NULL COMMENT '滑窗频率',
|
||||
`growth_rate` int NULL DEFAULT NULL COMMENT '增长率',
|
||||
`volatility_rate` int NULL DEFAULT NULL COMMENT '波动率',
|
||||
`decrease_rate` int NULL DEFAULT NULL COMMENT '下降率',
|
||||
PRIMARY KEY (`id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '车辆预警规则表' ROW_FORMAT = DYNAMIC;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for warn_strategy
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `warn_strategy`;
|
||||
CREATE TABLE `warn_strategy` (
|
||||
`id` int NOT NULL AUTO_INCREMENT COMMENT '策略id',
|
||||
`car_type_id` int NULL DEFAULT NULL COMMENT '车辆类型id',
|
||||
`strategy_name` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '策略名称',
|
||||
`template_id` int NULL DEFAULT NULL COMMENT '报文模版id',
|
||||
PRIMARY KEY (`id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 8 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '车辆预警策略表' ROW_FORMAT = DYNAMIC;
|
||||
|
||||
SET FOREIGN_KEY_CHECKS = 1;
|
|
@ -8,6 +8,10 @@ package com.muyu.cache;/**
|
|||
import com.muyu.common.redis.service.RedisService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static cn.hutool.core.lang.ansi.AnsiEncoder.encode;
|
||||
|
||||
/**
|
||||
|
@ -23,7 +27,7 @@ public abstract class CacheAbsBasic<K,V> implements CacheBasic<K,V>{
|
|||
|
||||
@Override
|
||||
public void put(K key, V value) {
|
||||
redisService.setCacheObject(encodeKey(key), value); // 编码 --> 缓存基础的对象 Integer String 实体类等
|
||||
redisService.setCacheObject(encodeKey(key), value,24L, TimeUnit.HOURS); // 编码 --> 缓存基础的对象 Integer String 实体类等
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package com.muyu.cache;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author yuping
|
||||
* @package com.muyu.cache
|
||||
|
|
|
@ -138,10 +138,10 @@
|
|||
</dependency>
|
||||
|
||||
<!-- Java Specification Requests 标准库-->
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>javax.annotation</groupId>-->
|
||||
<!-- <artifactId>jsr250-api</artifactId>-->
|
||||
<!-- </dependency>-->
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>javax.annotation</groupId>-->
|
||||
<!-- <artifactId>jsr250-api</artifactId>-->
|
||||
<!-- </dependency>-->
|
||||
|
||||
<dependency>
|
||||
<groupId>javax.annotation</groupId>
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
package com.muyu.common.core.constant;
|
||||
|
||||
/**
|
||||
* kafka常量信息
|
||||
* @program: cloud-server
|
||||
* @author: cuiyongxing
|
||||
* @create: 2024-09-28 12:18
|
||||
**/
|
||||
public class KafkaConstant {
|
||||
|
||||
public static final String BOOTSTRAP_SERVERS = "bootstrap.servers";
|
||||
|
||||
public static final String RETRIES = "retries";
|
||||
|
||||
public static final String ACKS = "acks";
|
||||
|
||||
public static final String BATCH_SIZE = "batch.size";
|
||||
|
||||
public static final String BUFFER_MEMORY = "buffer-memory";
|
||||
|
||||
public static final String KEY_SERIALIZER = "key.serializer";
|
||||
|
||||
public static final String VALUE_SERIALIZER = "value.serializer";
|
||||
|
||||
public static final String ENABLE_AUTO_COMMIT = "enable.auto.commit";
|
||||
|
||||
public static final String AUTO_COMMIT_INTERVAL = "auto.commit.interval.ms";
|
||||
|
||||
public static final String AUTO_OFFSET_RESET = "auto.offset.reset";
|
||||
|
||||
public static final String FETCH_MAX_WAIT = "fetch.max.wait";
|
||||
|
||||
public static final String FETCH_MIN_SIZE = "fetch.min.size";
|
||||
|
||||
public static final String HEARTBEAT_INTERVAL = "heartbeat.interval";
|
||||
|
||||
public static final String MAX_POLL_RECORDS = "max.poll.records";
|
||||
|
||||
public static final String KEY_DESERIALIZER = "key.deserializer";
|
||||
|
||||
public static final String VALUE_DESERIALIZER = "value.deserializer";
|
||||
|
||||
public static final String TOPIC = "topic";
|
||||
|
||||
public static final String GROUP_ID = "group.id";
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -110,4 +110,16 @@ public class UserConstants {
|
|||
public static final int PASSWORD_MIN_LENGTH = 5;
|
||||
|
||||
public static final int PASSWORD_MAX_LENGTH = 20;
|
||||
|
||||
/**
|
||||
* 企业名称长度限制
|
||||
*/
|
||||
public static final int Firm_NAME_MIN_LENGTH = 2;
|
||||
public static final int Firm_NAME_MAX_LENGTH = 20;
|
||||
|
||||
/**
|
||||
* 数据库名称长度限制
|
||||
*/
|
||||
public static final int DATABASE_NAME_MIN_LENGTH = 2;
|
||||
public static final int DATABASE_NAME_MAX_LENGTH = 20;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
<?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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common</artifactId>
|
||||
<version>3.6.3</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>cloud-common-kafka</artifactId>
|
||||
<description>
|
||||
cloud-common-kafka消息队列
|
||||
</description>
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>org.apache.kafka</groupId>-->
|
||||
<!-- <artifactId>kafka-clients</artifactId>-->
|
||||
<!-- <version>3.0.0</version>-->
|
||||
<!-- </dependency>-->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.kafka</groupId>
|
||||
<artifactId>kafka-clients</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-core</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -0,0 +1,94 @@
|
|||
package com.muyu.common.kafka.config;
|
||||
|
||||
import com.muyu.common.core.constant.KafkaConstant;
|
||||
import org.apache.kafka.clients.consumer.KafkaConsumer;
|
||||
import org.apache.kafka.common.serialization.StringDeserializer;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* kafka消费者配置类
|
||||
* @program: cloud-server
|
||||
* @author: cuiyongxing
|
||||
* @create: 2024-09-28 14:28
|
||||
**/
|
||||
@Configuration
|
||||
public class KafkaConsumerConfig {
|
||||
|
||||
/**
|
||||
* 服务端id+端口号
|
||||
*/
|
||||
@Value("${spring.kafka.consumer.bootstrap-servers}")
|
||||
private String bootstrapServers;
|
||||
|
||||
/**
|
||||
* 偏移量
|
||||
*/
|
||||
@Value("${spring.kafka.consumer.enable-auto-commit}")
|
||||
private Boolean enableAutoCommit;
|
||||
|
||||
/**
|
||||
* 自动提交时间间隔
|
||||
*/
|
||||
@Value("${spring.kafka.consumer.auto-commit-interval}")
|
||||
private Integer autoCommitInterval;
|
||||
|
||||
/**
|
||||
* 偏移量配置
|
||||
*/
|
||||
@Value("${spring.kafka.consumer.auto-offset-reset}")
|
||||
private String autoOffsetReset;
|
||||
|
||||
/**
|
||||
* 阻塞最大时间
|
||||
*/
|
||||
@Value("${spring.kafka.consumer.fetch-max-wait}")
|
||||
private Integer fetchMaxWait;
|
||||
|
||||
/**
|
||||
* 请求最小字节
|
||||
*/
|
||||
@Value("${spring.kafka.consumer.fetch-min-size}")
|
||||
private Integer fetchMinSize;
|
||||
|
||||
/**
|
||||
* 心跳间隔时间
|
||||
*/
|
||||
@Value("${spring.kafka.consumer.heartbeat-interval}")
|
||||
private Integer heartbeatInterval;
|
||||
|
||||
/**
|
||||
* 最大记录条数
|
||||
*/
|
||||
@Value("${spring.kafka.consumer.max-poll-records}")
|
||||
private Integer maxPollRecords;
|
||||
|
||||
/**
|
||||
* 消费组
|
||||
*/
|
||||
@Value("${spring.kafka.consumer.group-id}")
|
||||
private String groupId;
|
||||
|
||||
|
||||
@Bean
|
||||
public KafkaConsumer kafkaConsumer(){
|
||||
Map<String,Object> configs = new HashMap<>();
|
||||
configs.put(KafkaConstant.BOOTSTRAP_SERVERS, bootstrapServers);
|
||||
configs.put(KafkaConstant.ENABLE_AUTO_COMMIT, enableAutoCommit);
|
||||
configs.put(KafkaConstant.AUTO_COMMIT_INTERVAL, autoCommitInterval);
|
||||
configs.put(KafkaConstant.AUTO_OFFSET_RESET, autoOffsetReset);
|
||||
configs.put(KafkaConstant.FETCH_MAX_WAIT, fetchMaxWait);
|
||||
configs.put(KafkaConstant.FETCH_MIN_SIZE, fetchMinSize);
|
||||
configs.put(KafkaConstant.HEARTBEAT_INTERVAL, heartbeatInterval);
|
||||
configs.put(KafkaConstant.MAX_POLL_RECORDS, maxPollRecords);
|
||||
configs.put(KafkaConstant.GROUP_ID, groupId);
|
||||
StringDeserializer keyDeserializer = new StringDeserializer();
|
||||
StringDeserializer valueDeserializer = new StringDeserializer();
|
||||
return new KafkaConsumer(configs, keyDeserializer, valueDeserializer);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
package com.muyu.common.kafka.config;
|
||||
|
||||
import com.muyu.common.core.constant.KafkaConstant;
|
||||
import org.apache.kafka.clients.producer.KafkaProducer;
|
||||
import org.apache.kafka.common.serialization.StringSerializer;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* kafka生产者配置信息
|
||||
* @program: cloud-server
|
||||
* @author: cuiyongxing
|
||||
* @create: 2024-09-28 12:03
|
||||
**/
|
||||
@Configuration
|
||||
public class KafkaProducerConfig {
|
||||
|
||||
/**
|
||||
* 服务端ip+端口号
|
||||
*/
|
||||
@Value("${spring.kafka.producer.bootstrap-servers}")
|
||||
private String bootstrapServers;
|
||||
/**
|
||||
* 重试次数
|
||||
*/
|
||||
@Value("${spring.kafka.producer.retries}")
|
||||
private Integer retries;
|
||||
|
||||
/**
|
||||
* 默认批量大小
|
||||
*/
|
||||
@Value("${spring.kafka.producer.batch-size}")
|
||||
private Integer batchSize;
|
||||
/**
|
||||
* 总内存字节数
|
||||
*/
|
||||
@Value("${spring.kafka.producer.buffer-memory}")
|
||||
private Integer bufferMemory;
|
||||
|
||||
/**
|
||||
* 偏移量
|
||||
*/
|
||||
@Value("${spring.kafka.producer.acks}")
|
||||
private String acks;
|
||||
|
||||
|
||||
@Bean
|
||||
public KafkaProducer kafkaProducer() {
|
||||
Map<String, Object> configs = new HashMap<>();
|
||||
configs.put(KafkaConstant.BOOTSTRAP_SERVERS, bootstrapServers);
|
||||
configs.put(KafkaConstant.RETRIES, retries);
|
||||
configs.put(KafkaConstant.BATCH_SIZE, batchSize);
|
||||
configs.put(KafkaConstant.BUFFER_MEMORY, bufferMemory);
|
||||
configs.put(KafkaConstant.ACKS, acks);
|
||||
StringSerializer keySerializer = new StringSerializer();
|
||||
StringSerializer valueSerializer = new StringSerializer();
|
||||
KafkaProducer kafkaProducer = new KafkaProducer<>(configs, keySerializer, valueSerializer);
|
||||
return kafkaProducer;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
com.muyu.common.kafka.config.KafkaConsumerConfig
|
||||
com.muyu.common.kafka.config.KafkaProducerConfig
|
|
@ -17,6 +17,10 @@
|
|||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<description>
|
||||
cloud-common-rabbit 消息队列服务
|
||||
</description>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- rabbitMq 消息队列 -->
|
||||
|
@ -28,8 +32,10 @@
|
|||
<!-- 项目公共核心 -->
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-core</artifactId>
|
||||
<artifactId>cloud-common-redis</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -1,41 +0,0 @@
|
|||
package com.muyu.common.rabbit;
|
||||
|
||||
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
|
||||
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistrar;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
|
||||
import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory;
|
||||
|
||||
@Configuration
|
||||
public class RabbitListenerConfigurer implements org.springframework.amqp.rabbit.annotation.RabbitListenerConfigurer {
|
||||
|
||||
static {
|
||||
System.setProperty("spring.amqp.deserialization.trust.all", "true");
|
||||
}
|
||||
|
||||
//以下配置RabbitMQ消息服务
|
||||
@Autowired
|
||||
public ConnectionFactory connectionFactory;
|
||||
|
||||
|
||||
/**
|
||||
* 处理器方法工厂
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public DefaultMessageHandlerMethodFactory handlerMethodFactory() {
|
||||
DefaultMessageHandlerMethodFactory factory = new DefaultMessageHandlerMethodFactory();
|
||||
// 这里的转换器设置实现了 通过 @Payload 注解 自动反序列化message body
|
||||
factory.setMessageConverter(new MappingJackson2MessageConverter());
|
||||
return factory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configureRabbitListeners(RabbitListenerEndpointRegistrar rabbitListenerEndpointRegistrar) {
|
||||
rabbitListenerEndpointRegistrar.setMessageHandlerMethodFactory(handlerMethodFactory());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
package com.muyu.rabbitmq.config;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.amqp.rabbit.connection.CorrelationData;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
/**
|
||||
* @ClassName:
|
||||
* @Description: 消息发送到 交换机的确认 回调方法
|
||||
*/
|
||||
@Component
|
||||
@AllArgsConstructor
|
||||
public class MyConfirmCallback implements RabbitTemplate.ConfirmCallback {
|
||||
|
||||
|
||||
private RabbitTemplate rabbitTemplate;
|
||||
|
||||
// public MyConfirmCallback(RabbitTemplate rabbitTemplate) {
|
||||
// this.rabbitTemplate = rabbitTemplate;
|
||||
// // 设置 消息发送到交换机成功 的回调
|
||||
// this.rabbitTemplate.setConfirmCallback(this);
|
||||
// }
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
this.rabbitTemplate.setConfirmCallback(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送消息到交换机的回调方法 消息发送成功或者失败都会执行
|
||||
*
|
||||
* @param correlationData correlation data for the callback. 消息的元数据
|
||||
* @param ack true for ack, false for nack
|
||||
* @param cause An optional cause, for nack, when available, otherwise null.
|
||||
*/
|
||||
@Override
|
||||
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
|
||||
if (ack) {
|
||||
System.out.println("消息发送到交换机成功~");
|
||||
} else {
|
||||
System.out.println("消息发送到交换机失败,失败的原因:" + cause);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
package com.muyu.rabbitmq.config;
|
||||
|
||||
|
||||
|
||||
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
|
||||
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
|
||||
import org.springframework.amqp.rabbit.core.RabbitAdmin;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @ClassName: RabbitAdminConfig
|
||||
* @Description: RabbitAdmin配置类
|
||||
*/
|
||||
@Configuration
|
||||
public class RabbitAdminConfig {
|
||||
@Value("${spring.rabbitmq.host}")
|
||||
private String host;
|
||||
@Value("${spring.rabbitmq.username}")
|
||||
private String username;
|
||||
@Value("${spring.rabbitmq.password}")
|
||||
private String password;
|
||||
@Value("${spring.rabbitmq.port}")
|
||||
private Integer port;
|
||||
|
||||
@Bean
|
||||
public ConnectionFactory connectionFactory() {
|
||||
CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
|
||||
cachingConnectionFactory.setHost(host);
|
||||
cachingConnectionFactory.setPort(port);
|
||||
cachingConnectionFactory.setUsername(username);
|
||||
cachingConnectionFactory.setPassword(password);
|
||||
return cachingConnectionFactory;
|
||||
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
|
||||
RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
|
||||
rabbitAdmin.setAutoStartup(true);
|
||||
return rabbitAdmin;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
package com.muyu.rabbitmq.config;
|
||||
|
||||
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
|
||||
import org.springframework.amqp.support.converter.MessageConverter;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* JSON 消息转换器 自动将发送的消息转换成 json 字符串 并且 消费者接收到消息的时候自动反序列化 成需要的对象
|
||||
*/
|
||||
@Configuration
|
||||
public class RabbitmqConfig {
|
||||
|
||||
|
||||
// 消息转换配置
|
||||
@Bean
|
||||
public MessageConverter jsonMessageConverter() {
|
||||
return new Jackson2JsonMessageConverter();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package com.muyu.rabbitmq.config;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.amqp.core.ReturnedMessage;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
/**
|
||||
* 消息发送到 队列的确认
|
||||
*/
|
||||
@Component
|
||||
@AllArgsConstructor
|
||||
public class ReturnCallbackConfig implements RabbitTemplate.ReturnsCallback {
|
||||
|
||||
|
||||
private final RabbitTemplate rabbitTemplate;
|
||||
|
||||
@PostConstruct // @PostContruct是spring框架的注解,在⽅法上加该注解会在项⽬启动的时候执⾏该⽅法,也可以理解为在spring容器初始化的时候执
|
||||
public void init() {
|
||||
rabbitTemplate.setReturnsCallback(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 消息发送到 队列失败的时候执行
|
||||
*
|
||||
* @param returnedMessage the returned message and metadata.
|
||||
*/
|
||||
@Override
|
||||
public void returnedMessage(ReturnedMessage returnedMessage) {
|
||||
System.out.println("消息" + returnedMessage.getMessage().toString() +
|
||||
"被交换机" + returnedMessage.getExchange() + "回退!"
|
||||
+ "退回原因为:" + returnedMessage.getReplyText());
|
||||
// 回退了所有的信息,可做补偿机制 记录发送的日志
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package com.muyu.rabbitmq.constants;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author: 刘武
|
||||
* @date: 2024/7/10
|
||||
* @Description: rabbitmq常量
|
||||
* @Version 1.0.0
|
||||
*/
|
||||
public interface RabbitmqConstants {
|
||||
|
||||
//普通队列
|
||||
String BASIC_QUEUE_NAME = "BASIC_QUEUE_NAME";
|
||||
|
||||
String lOG_QUEUE_NAME = "LOG_QUEUE_NAME";
|
||||
//延迟队列
|
||||
//队列名称
|
||||
String DELAYED_QUEUE_NAME = "delayed_queue";
|
||||
//交换机名称
|
||||
String DELAYED_EXCHANGE_NAME = "DELAYED_EXCHANGE";
|
||||
//交换机
|
||||
String DELAYED_ROUTING_KEY = "delayed";
|
||||
/**
|
||||
* 上下线监听交换机
|
||||
*/
|
||||
String TOP_BOTTOM_STITCHING = "top_bottom_stitching";
|
||||
/**
|
||||
* 上线规则
|
||||
*/
|
||||
String TOP_RULE = "car.top.data";
|
||||
/**
|
||||
* 车辆下线规则
|
||||
*/
|
||||
String BOTTOM_RULE = "car.bottom.data";
|
||||
}
|
|
@ -0,0 +1,150 @@
|
|||
package com.muyu.rabbitmq.consumer;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.muyu.common.redis.service.RedisService;
|
||||
//import com.muyu.rabbitmq.util.CacheUtil;
|
||||
import com.rabbitmq.client.Channel;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.amqp.core.Message;
|
||||
import org.springframework.amqp.rabbit.annotation.Queue;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @ClassName: RabbitMQConsumerUtil
|
||||
* @Description: rabbitmq消费者
|
||||
*/
|
||||
@Component
|
||||
@Log4j2
|
||||
@AllArgsConstructor
|
||||
public class RabbitMQConsumerUtil {
|
||||
|
||||
private final RedisService redisService;
|
||||
|
||||
// @Autowired
|
||||
// private CacheUtil cacheUtil;
|
||||
|
||||
|
||||
/**
|
||||
* 普通消费者
|
||||
* @param data 数据类型
|
||||
* @param message
|
||||
* @param channel
|
||||
*/
|
||||
// @RabbitListener(queuesToDeclare = @Queue(name = "basic"))
|
||||
public void rabbitMQBasicConsumer(String data ,Message message , Channel channel) {
|
||||
log.info("当前时间:{} :RabbitMQConsumerUtil : {}", new Date(), message);
|
||||
try {
|
||||
// 获取到消息 开始消费
|
||||
log.info("消息消费者接收到消息,消息内容:{}", JSONObject.toJSONString(data));
|
||||
|
||||
|
||||
Long add = redisService.redisTemplate.opsForSet().add(data, message.getMessageProperties().getMessageId());
|
||||
|
||||
if (add != 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* -----------------------------------以下为异步业务操作----------------------------
|
||||
*/
|
||||
String carList = (String) redisService.redisTemplate.opsForValue().get("carList");
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* ------------------------------------------------------------------------------
|
||||
*/
|
||||
// 消费消息成功之后需要确认
|
||||
// long deliveryTag 消息投递序号 自增的数字 在整个队列中唯一 拿到这个序号就相当于拿到这条消息
|
||||
// boolean multiple 是否批量确认 true 批量 确认小于等于当前投递序号的消息 false 单个确认
|
||||
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
|
||||
log.info("xxx消费者接收到消息,消息内容:{},消费成功...", message);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("xxx消费者接收到消息,消息内容:{},消费消息异常,异常信息:{}", message, e);
|
||||
// 消息回退 拒绝消费消息
|
||||
// long deliveryTag 消息投递序号 自增的数字 在整个队列中唯一 拿到这个序号就相当于拿到这条消息
|
||||
// boolean requeue 是否回到原来的队列
|
||||
try {
|
||||
channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
|
||||
// channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
|
||||
} catch (IOException ex) {
|
||||
log.error("xxx消费者接收到消息,消息内容:{},回退消息异常,异常信息:{}", message, ex);
|
||||
}
|
||||
}finally {
|
||||
try {
|
||||
channel.close();
|
||||
} catch (Exception e) {
|
||||
log.error("xxx消费者关闭Channel异常,消息内容:{},异常信息:{}", message, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 普通消费者
|
||||
* @param data 数据类型
|
||||
* @param message
|
||||
* @param channel
|
||||
*/
|
||||
public void carUpConsumer(String data,Message message , Channel channel) {
|
||||
log.info("当前时间:{} :RabbitMQConsumerUtil : {}", new Date(), message);
|
||||
try {
|
||||
// 获取到消息 开始消费
|
||||
log.info("消息消费者接收到消息,消息内容:{}", JSONObject.toJSONString(data));
|
||||
|
||||
|
||||
Long add = redisService.redisTemplate.opsForSet().add(data, message.getMessageProperties().getMessageId());
|
||||
|
||||
if (add != 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* -----------------------------------以下为异步业务操作----------------------------
|
||||
*/
|
||||
log.info("[ 根据vin拿到缓存 ] vin为 --》 {}",data);
|
||||
log.info("[ 存入本地缓存 ] 数据为 --》 {}",data);
|
||||
log.info("[ 存入本地缓存 ] 数据为 --》 {}",data);
|
||||
/**
|
||||
* ------------------------------------------------------------------------------
|
||||
*/
|
||||
// 消费消息成功之后需要确认
|
||||
// long deliveryTag 消息投递序号 自增的数字 在整个队列中唯一 拿到这个序号就相当于拿到这条消息
|
||||
// boolean multiple 是否批量确认 true 批量 确认小于等于当前投递序号的消息 false 单个确认
|
||||
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
|
||||
log.info("xxx消费者接收到消息,消息内容:{},消费成功...", message);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("xxx消费者接收到消息,消息内容:{},消费消息异常,异常信息:{}", message, e);
|
||||
// 消息回退 拒绝消费消息
|
||||
// long deliveryTag 消息投递序号 自增的数字 在整个队列中唯一 拿到这个序号就相当于拿到这条消息
|
||||
// boolean requeue 是否回到原来的队列
|
||||
try {
|
||||
channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
|
||||
// channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
|
||||
} catch (IOException ex) {
|
||||
log.error("xxx消费者接收到消息,消息内容:{},回退消息异常,异常信息:{}", message, ex);
|
||||
}
|
||||
}finally {
|
||||
try {
|
||||
channel.close();
|
||||
} catch (Exception e) {
|
||||
log.error("xxx消费者关闭Channel异常,消息内容:{},异常信息:{}", message, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,174 @@
|
|||
package com.muyu.rabbitmq.producer;
|
||||
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.rabbitmq.constants.RabbitmqConstants;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.amqp.core.MessageProperties;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @ClassName: RabbitMQProducer
|
||||
* @Description: rabbitmq生产者
|
||||
*/
|
||||
@Component
|
||||
@AllArgsConstructor
|
||||
@Log4j2
|
||||
public class RabbitMQProducerUtil {
|
||||
//redis工具类对象
|
||||
|
||||
//rabbit
|
||||
private final RabbitTemplate rabbitTemplate;
|
||||
|
||||
|
||||
/**
|
||||
* 简单模型
|
||||
*
|
||||
* @param param 传递的消息 (如果是对象需要序列化)
|
||||
* @return 结果集
|
||||
* 一对一消费,只有一个消费者能接收到
|
||||
*/
|
||||
public void basicSendMessage(String queueName, String param) {
|
||||
|
||||
log.info("【简单模型mq】 : method: 【 basicSendMessage 】 - ages: 【 String : {}, Object : {}】 ---> 【 消息发送中。。。 】", RabbitmqConstants.BASIC_QUEUE_NAME, param);
|
||||
// 发送简单模型消息
|
||||
// 第一个参数: 绑定规则 相当于 队列名称
|
||||
// 第二个参数:消息内容
|
||||
rabbitTemplate.convertAndSend(queueName, param, message -> {
|
||||
message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
|
||||
return message;
|
||||
} );
|
||||
|
||||
log.info("【简单模型mq】 : method: 【 basicSendMessage 】- queue: 【 {} 】 ---> 【 消息发送成功 】", RabbitmqConstants.BASIC_QUEUE_NAME);
|
||||
|
||||
|
||||
}
|
||||
|
||||
// /**
|
||||
// * Work queue 工作模型
|
||||
// *
|
||||
// * @param obj 传递的消息 (如果是对象需要序列化)
|
||||
// * @return 结果集
|
||||
// * 多个消费者,你一个我一个分配消费消息,有预取机制,默认公平消费,可配置 能者多劳模式(),谁完成的快,谁多做一点
|
||||
// */
|
||||
// public Result<?> workSendMessage(String queueName, Object obj, String msg) {
|
||||
//
|
||||
// log.info("【工作模型mq】 : method: 【 workSendMessage 】 - ages: 【 String : {}, Object : {}, String : {} 】 ---> 【 消息发送中。。。 】", queueName, obj, msg);
|
||||
// // 发送简单模型消息
|
||||
// // 第一个参数: 绑定规则 相当于 队列名称
|
||||
// // 第二个参数:消息内容
|
||||
// rabbitTemplate.convertAndSend(queueName, obj, message -> {
|
||||
// message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
|
||||
// return message;
|
||||
// } );
|
||||
//
|
||||
// log.info("【工作模型mq】 : method: 【 workSendMessage 】- queue: 【 {} 】 ---> 【 消息发送成功 】", queueName);
|
||||
//
|
||||
// return Result.success("消息发送成功");
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * Publish/Subscribe 发布订阅者模型
|
||||
// * 多个消费者,多个消费者可以同时接收到消息 有交换机 类型 fanout
|
||||
// *
|
||||
// * @param exchange 交换机名称
|
||||
// * @param obj 发送的消息Object
|
||||
// * @param msg 响应的内容
|
||||
// * @return 结果集
|
||||
// */
|
||||
// public Result<?> publishSubscribeSendMessage(String exchange, Object obj, String msg) {
|
||||
//
|
||||
// log.info("【订阅模型mq】 : method: 【 workSendMessage 】 - ages: 【 String : {}, Object : {}, String : {} 】 ---> 【 消息发送中。。。 】", exchange, obj, msg);
|
||||
// // 发送简单模型消息
|
||||
// // 第一个参数: exchange 交换机的名称
|
||||
// // 第二个参数: 绑定规则 发布订阅者模型 不写 默认 "" 只要绑定就行 不需要规则
|
||||
// // 第三个参数:消息内容
|
||||
// rabbitTemplate.convertAndSend(exchange, "", obj, message -> {
|
||||
// message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
|
||||
// return message;
|
||||
// } );
|
||||
//
|
||||
// log.info("【订阅模型mq】 : method: 【 workSendMessage 】- exchange: 【 {} 】 ---> 【 消息发送成功 】", exchange);
|
||||
//
|
||||
// return Result.success("消息发送成功");
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * Routing路由模型
|
||||
// * 使用的是 Direct 类型的交换机,会将接收到的消息根据 规则 路由到指定的Queue(队列),因此称为路由模式
|
||||
// *
|
||||
// * @param exchange 交换机名称
|
||||
// * @param rule 绑定规则 一个字符串即可
|
||||
// * @param obj 发送的消息Object
|
||||
// * @param msg 响应的内容
|
||||
// * @return 结果集
|
||||
// */
|
||||
// public Result<?> routingSendMessage(String exchange, String rule, Object obj, String msg) {
|
||||
//
|
||||
// log.info("【路由模型mq】 : method: 【 workSendMessage 】 - ages: 【 String : {}, Object : {}, String : {} 】 ---> 【 消息发送中。。。 】", exchange, obj, msg);
|
||||
// // 发送简单模型消息
|
||||
// // 第一个参数: 绑定规则 相当于 队列名称
|
||||
// // 第二个参数:消息内容
|
||||
// rabbitTemplate.convertAndSend(exchange, rule, obj, message -> {
|
||||
// message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
|
||||
// return message;
|
||||
// } );
|
||||
//
|
||||
// log.info("【路由模型mq】 : method: 【 workSendMessage 】- exchange: 【 {} 】 ---> 【 消息发送成功 】", exchange);
|
||||
//
|
||||
// return Result.success("消息发送成功");
|
||||
// }
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * Topic主题模型模型
|
||||
// * 使用的是 topic 类型的交换机
|
||||
// *
|
||||
// * @param exchange 交换机名称
|
||||
// * @param rule 绑定规则 可以绑定多个单词以 . 拼接 也可以使用 #(匹配 零个 一个 或 多个 单词) 或 *(匹配 一个 单词) 通配符(例如:name.msg, *.msg, age.# )
|
||||
// * @param obj 发送的消息Object
|
||||
// * @param msg 响应的内容
|
||||
// * @return 结果集
|
||||
// */
|
||||
// public Result<?> topicSendMessage(String exchange, String rule, Object obj) {
|
||||
//
|
||||
// log.info("【主题模型mq】 : method: 【 workSendMessage 】 - ages: 【 String : {}, Object : {} 】 ---> 【 消息发送中。。。 】", exchange, obj);
|
||||
// // 发送简单模型消息
|
||||
// // 第一个参数: 绑定规则 相当于 队列名称
|
||||
// // 第二个参数:消息内容
|
||||
// rabbitTemplate.convertAndSend(exchange, rule, obj, message -> {
|
||||
// message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
|
||||
// return message;
|
||||
// } );
|
||||
//
|
||||
// log.info("【主题模型mq】 : method: 【 workSendMessage 】- exchange: 【 {} 】 ---> 【 消息发送成功 】", exchange);
|
||||
//
|
||||
// return Result.success(obj,"消息发送成功");
|
||||
// }
|
||||
|
||||
|
||||
/**
|
||||
* 延迟队列模型
|
||||
* @param param 传输内容
|
||||
* @param delayTime 延迟时间
|
||||
* @return 结果集
|
||||
*/
|
||||
// public Result<?> delayedSendMessage(Long delayTime, Object param) {
|
||||
// log.info("【延迟队列模型】 : method: 【 delayedSendMessage 】 消息内容:{}---> 【 消息发送中。。。 】",param);
|
||||
//
|
||||
// rabbitTemplate.convertAndSend(RabbitmqConstants.DELAYED_EXCHANGE_NAME, RabbitmqConstants.DELAYED_ROUTING_KEY,param, message -> {
|
||||
// MessageProperties messageProperties = message.getMessageProperties();
|
||||
// messageProperties.setMessageId(UUID.randomUUID().toString());
|
||||
// messageProperties.setDelayLong(delayTime);
|
||||
// return message;
|
||||
// });
|
||||
// log.info("【延迟队列模型】 : method: 【 delayedSendMessage 】 消息内容:{}---> 【 消息发送成功 】",param);
|
||||
//
|
||||
// return Result.success(param,"消息发送成功");
|
||||
//
|
||||
// }
|
||||
|
||||
}
|
|
@ -1 +1,6 @@
|
|||
com.muyu.common.rabbit.RabbitListenerConfigurer
|
||||
com.muyu.rabbitmq.producer.RabbitMQProducerUtil
|
||||
com.muyu.rabbitmq.consumer.RabbitMQConsumerUtil
|
||||
com.muyu.rabbitmq.config.RabbitmqConfig
|
||||
com.muyu.rabbitmq.config.MyConfirmCallback
|
||||
com.muyu.rabbitmq.config.RabbitAdminConfig
|
||||
com.muyu.rabbitmq.config.ReturnCallbackConfig
|
||||
|
|
|
@ -11,6 +11,10 @@
|
|||
|
||||
<artifactId>cloud-common-saas</artifactId>
|
||||
|
||||
<description>
|
||||
cloud-common-saas 模块
|
||||
</description>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
|
@ -29,8 +33,10 @@
|
|||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-security</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
|
|
@ -1,33 +1,30 @@
|
|||
package com.muyu.cloud.common.many.datasource;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;
|
||||
import com.muyu.cloud.common.many.datasource.constents.DatasourceContent;
|
||||
import com.muyu.cloud.common.many.datasource.domain.model.DataSourceInfo;
|
||||
import com.muyu.cloud.common.many.datasource.factory.DruidDataSourceFactory;
|
||||
import com.muyu.cloud.common.many.datasource.init.InitDataSource;
|
||||
import com.muyu.cloud.common.many.datasource.role.DynamicDataSource;
|
||||
import com.muyu.cloud.common.saas.domain.Datasource;
|
||||
import com.muyu.cloud.common.saas.domain.model.EntInfo;
|
||||
import com.muyu.cloud.common.saas.exception.SaaSException;
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.common.core.utils.SpringUtils;
|
||||
import com.muyu.common.system.domain.Datasource;
|
||||
import com.muyu.common.system.domain.SysUser;
|
||||
import com.muyu.common.system.remote.RemoteSaaSService;
|
||||
import com.muyu.common.system.remote.RemoteUserService;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author: DongZeLiang
|
||||
* @date: 2024/6/3
|
||||
|
@ -38,9 +35,14 @@ import java.util.Map;
|
|||
@Component
|
||||
@AutoConfiguration(before = {MybatisPlusAutoConfiguration.class, MybatisAutoConfiguration.class})
|
||||
public class ManyDataSource implements ApplicationRunner{
|
||||
private List<EntInfo> dataSourceInfoList(){
|
||||
|
||||
@Autowired
|
||||
private InitDataSource initDataSource;
|
||||
|
||||
|
||||
public List<EntInfo> dataSourceInfoList(){
|
||||
RemoteSaaSService remoteSaaSService = SpringUtils.getBean(RemoteSaaSService.class);
|
||||
Result<List<Datasource>> tableDataInfoResult = remoteSaaSService.findDatabaseList();
|
||||
Result<List<Datasource>> tableDataInfoResult = initDataSource.initDatasource();
|
||||
if (tableDataInfoResult==null){
|
||||
throw new SaaSException("saas远调数据源错误");
|
||||
}
|
||||
|
@ -62,7 +64,6 @@ Result<List<Datasource>> tableDataInfoResult = remoteSaaSService.findDatabaseLis
|
|||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// private List<EntInfo> dataPrimarySourceInfoList(){
|
||||
// List<EntInfo> list = new ArrayList<>();
|
||||
// list.add(
|
||||
|
@ -74,7 +75,6 @@ Result<List<Datasource>> tableDataInfoResult = remoteSaaSService.findDatabaseLis
|
|||
// );
|
||||
// return list;
|
||||
// }
|
||||
|
||||
@Bean
|
||||
public DynamicDataSource dynamicDataSource(DruidDataSourceFactory druidDataSourceFactory) {
|
||||
// 企业列表 企业CODE,端口,IP
|
||||
|
@ -93,7 +93,6 @@ Result<List<Datasource>> tableDataInfoResult = remoteSaaSService.findDatabaseLis
|
|||
dynamicDataSource.setDefineTargetDataSources(dataSourceMap);
|
||||
return dynamicDataSource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) {
|
||||
DruidDataSourceFactory druidDataSourceFactory = SpringUtils.getBean(DruidDataSourceFactory.class);
|
||||
|
|
|
@ -0,0 +1,58 @@
|
|||
package com.muyu.cloud.common.many.datasource.init;
|
||||
|
||||
|
||||
import com.muyu.cloud.common.saas.domain.Datasource;
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
@Log4j2
|
||||
public class InitDataSource {
|
||||
|
||||
public static final String USER="root";
|
||||
public static final String PASSWORD="Lw030106";
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
public Result<List<Datasource>> initDatasource(){
|
||||
ArrayList<Datasource> list = new ArrayList<>();
|
||||
|
||||
try {
|
||||
DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
|
||||
Connection connection= DriverManager.getConnection("jdbc:mysql://47.101.53.251:3306/datasource?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT&useSSL=false",USER,PASSWORD);
|
||||
String sql="select * from `datasource` ";
|
||||
|
||||
Statement stmt = connection.createStatement();
|
||||
ResultSet rs = stmt.executeQuery(sql);
|
||||
|
||||
while (rs.next()){
|
||||
Datasource datasource = new Datasource();
|
||||
datasource.setId(rs.getInt("id"));
|
||||
datasource.setFirmName(rs.getString("firm_name"));
|
||||
datasource.setDatabaseName(rs.getString("database_name"));
|
||||
list.add(datasource);
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return Result.success(list);
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -1,10 +1,9 @@
|
|||
package com.muyu.cloud.common.saas.interceptor;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.muyu.cloud.common.saas.contents.SaaSConstant;
|
||||
import com.muyu.cloud.common.many.datasource.holder.DynamicDataSourceHolder;
|
||||
import com.muyu.cloud.common.saas.exception.SaaSException;
|
||||
import com.muyu.cloud.common.many.datasource.role.DynamicDataSource;
|
||||
import com.muyu.cloud.common.saas.contents.SaaSConstant;
|
||||
import com.muyu.cloud.common.saas.exception.SaaSException;
|
||||
import com.muyu.common.core.utils.ServletUtils;
|
||||
import com.muyu.common.core.utils.SpringUtils;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
com.muyu.cloud.common.saas.interceptor.WebMvcSaaSConfig
|
||||
com.muyu.cloud.common.many.datasource.ManyDataSource
|
||||
com.muyu.cloud.common.many.datasource.factory.DruidDataSourceFactory
|
||||
com.muyu.cloud.common.many.datasource.init.InitDataSource
|
||||
|
|
|
@ -1,23 +1,26 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
<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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common</artifactId>
|
||||
<version>3.6.3</version>
|
||||
</parent>
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.bwie</groupId>
|
||||
<artifactId>cloud-common-swagger</artifactId>
|
||||
|
||||
<description>
|
||||
cloud-common-swagger swagger2文档聚合
|
||||
cloud-common-swagger系统接口
|
||||
</description>
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- SpringBoot Web -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
|
|
@ -1,10 +1,21 @@
|
|||
package com.muyu.common.swagger.annotation;
|
||||
|
||||
import com.muyu.common.swagger.config.SwaggerAutoConfiguration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* @author 袁子龙
|
||||
* @package:com.muyu.common.swagger.annotation
|
||||
* @name:EnableCustomSwagger2
|
||||
* @date:2024/9/29 10:01
|
||||
*/
|
||||
@Target({ElementType.TYPE})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Inherited
|
||||
@Import({SwaggerAutoConfiguration.class})
|
||||
public @interface EnableCustomSwagger2 {
|
||||
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@ import java.util.List;
|
|||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 在 springboot 2.6.x 不兼容问题的处理
|
||||
* @author 袁子龙
|
||||
* @package:com.muyu.common.swagger.config
|
||||
* @name:SwaggerBeanPostProcessor
|
||||
|
|
|
@ -300,4 +300,3 @@ public class SwaggerProperties {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
package com.muyu.common.system.domain;
|
||||
|
||||
import com.muyu.common.core.annotation.Excel;
|
||||
import com.muyu.common.core.annotation.Excel.ColumnType;
|
||||
import com.muyu.common.core.web.domain.BaseEntity;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
/**
|
||||
* 企业对象
|
||||
* @author 袁子龙
|
||||
* @package com.muyu.common.system.domain
|
||||
* @name Enterprise
|
||||
* @date 2024/9/30 12:05
|
||||
*/
|
||||
@Data
|
||||
@SuperBuilder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Business {
|
||||
|
||||
/**
|
||||
* 企业Id
|
||||
*/
|
||||
@Excel(name = "企业序号",cellType = ColumnType.NUMERIC, prompt = "企业编号")
|
||||
private Integer id;
|
||||
/**
|
||||
* 企业名称
|
||||
*/
|
||||
@Excel(name = "企业名称")
|
||||
private String firmName;
|
||||
/**
|
||||
* 数据库名称
|
||||
*/
|
||||
@Excel(name = "数据库名称")
|
||||
private String databaseName;
|
||||
}
|
|
@ -1,26 +0,0 @@
|
|||
package com.muyu.common.system.domain;
|
||||
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@TableName(value = "datasource", autoResultMap = true)
|
||||
public class Datasource {
|
||||
|
||||
@TableId(value = "id",type = IdType.AUTO)
|
||||
private Integer id;
|
||||
|
||||
public String firmName;
|
||||
|
||||
public String databaseName;
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -1,25 +0,0 @@
|
|||
package com.muyu.common.system.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
/**
|
||||
* @Author WangXin
|
||||
* @Data 2024/9/18
|
||||
* @Description 企业用户
|
||||
* @Version 1.0.0
|
||||
*/
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
@SuperBuilder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class SysFirmUser extends SysUser {
|
||||
/**
|
||||
* 用户数据库
|
||||
*/
|
||||
private String databaseName;
|
||||
}
|
|
@ -1,13 +1,10 @@
|
|||
package com.muyu.common.system.remote;
|
||||
|
||||
import com.muyu.common.core.constant.SecurityConstants;
|
||||
|
||||
import com.muyu.common.core.constant.ServiceNameConstants;
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.common.system.domain.Datasource;
|
||||
import com.muyu.common.system.domain.LoginUser;
|
||||
import com.muyu.common.system.domain.SysUser;
|
||||
import com.muyu.common.system.domain.Business;
|
||||
import com.muyu.common.system.remote.factory.RemoteSaaSFallbackFactory;
|
||||
import com.muyu.common.system.remote.factory.RemoteUserFallbackFactory;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
|
@ -22,7 +19,7 @@ import java.util.List;
|
|||
public interface RemoteSaaSService {
|
||||
|
||||
@GetMapping("/saas/findDatabaseList")
|
||||
Result<List<Datasource>> findDatabaseList();
|
||||
Result<List<Business>> findDatabaseList();
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -3,12 +3,12 @@ package com.muyu.common.system.remote;
|
|||
import com.muyu.common.core.constant.SecurityConstants;
|
||||
import com.muyu.common.core.constant.ServiceNameConstants;
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.common.core.web.page.TableDataInfo;
|
||||
import com.muyu.common.system.domain.SysFirmUser;
|
||||
import com.muyu.common.system.domain.Business;
|
||||
import com.muyu.common.system.domain.SysUser;
|
||||
import com.muyu.common.system.remote.factory.RemoteUserFallbackFactory;
|
||||
import com.muyu.common.system.domain.LoginUser;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
@ -44,4 +44,21 @@ public interface RemoteUserService {
|
|||
|
||||
@GetMapping("/user/companyList")
|
||||
Result<List<SysUser>> companyList();
|
||||
|
||||
/**
|
||||
* 入驻企业信息
|
||||
* @param enterprise
|
||||
* @param source
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/user/enterprise")
|
||||
Result<Boolean>settlementEnterpriseInfo(@RequestBody Business enterprise, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
|
||||
|
||||
/**
|
||||
* 用户添加
|
||||
* @param sysUser
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/user/addUser")
|
||||
public Result<Integer> addUser(@RequestBody SysUser sysUser,@RequestHeader(SecurityConstants.FROM_SOURCE)String source);
|
||||
}
|
||||
|
|
|
@ -1,13 +1,8 @@
|
|||
package com.muyu.common.system.remote.factory;
|
||||
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.common.system.domain.Datasource;
|
||||
import com.muyu.common.system.domain.LoginUser;
|
||||
import com.muyu.common.system.domain.SysUser;
|
||||
import com.muyu.common.system.domain.Business;
|
||||
import com.muyu.common.system.remote.RemoteSaaSService;
|
||||
import com.muyu.common.system.remote.RemoteUserService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.cloud.openfeign.FallbackFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
|
@ -25,7 +20,7 @@ public class RemoteSaaSFallbackFactory implements FallbackFactory<RemoteSaaSServ
|
|||
public RemoteSaaSService create(Throwable cause) {
|
||||
return new RemoteSaaSService() {
|
||||
@Override
|
||||
public Result<List<Datasource>> findDatabaseList() {
|
||||
public Result<List<Business>> findDatabaseList() {
|
||||
return Result.error("查询数据库失败:" + cause.getMessage());
|
||||
}
|
||||
};
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
package com.muyu.common.system.remote.factory;
|
||||
|
||||
import com.muyu.common.core.domain.Result;
|
||||
import com.muyu.common.core.web.page.TableDataInfo;
|
||||
import com.muyu.common.system.domain.SysFirmUser;
|
||||
import com.muyu.common.system.domain.Business;
|
||||
import com.muyu.common.system.remote.RemoteUserService;
|
||||
import com.muyu.common.system.domain.SysUser;
|
||||
import com.muyu.common.system.domain.LoginUser;
|
||||
|
@ -41,6 +40,17 @@ public class RemoteUserFallbackFactory implements FallbackFactory<RemoteUserServ
|
|||
return Result.error("获取企业列表失败:" + throwable.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Boolean> settlementEnterpriseInfo(Business enterprise, String source) {
|
||||
return Result.error("入驻企业失败");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Integer> addUser(SysUser sysUser, String source) {
|
||||
return Result.error("用户添加失败");
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
<module>cloud-common-saas</module>
|
||||
<module>cloud-common-swagger</module>
|
||||
<module>cloud-common-cache</module>
|
||||
<module>cloud-common-kafka</module>
|
||||
</modules>
|
||||
|
||||
<artifactId>cloud-common</artifactId>
|
||||
|
@ -32,4 +33,5 @@
|
|||
cloud-common通用模块
|
||||
</description>
|
||||
|
||||
|
||||
</project>
|
||||
|
|
|
@ -54,13 +54,6 @@
|
|||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.kafka</groupId>
|
||||
<artifactId>spring-kafka</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- Mysql Connector -->
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
|
@ -110,11 +103,35 @@
|
|||
<artifactId>node-commons</artifactId>
|
||||
<version>1.3.2</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.kafka</groupId>
|
||||
<artifactId>spring-kafka</artifactId>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-kafka</artifactId>
|
||||
<version>3.6.3</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-rabbit</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.muyu.common</groupId>
|
||||
<artifactId>saas-common</artifactId>
|
||||
<version>3.6.3</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>saas-cache</artifactId>
|
||||
<version>3.6.3</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>saas-cache</artifactId>
|
||||
<version>3.6.3</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
package com.muyu.event.basic;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
/**
|
||||
* 事件类型
|
||||
* @author 刘武
|
||||
* @package:com.muyu.event.Basic
|
||||
* @name:EventCustom
|
||||
* @date:2024/9/29 21:17
|
||||
*/
|
||||
public class EventCustom extends ApplicationEvent {
|
||||
|
||||
private JSONObject data;
|
||||
|
||||
public EventCustom(Object source,JSONObject data) {
|
||||
super(source);
|
||||
this.data=data;
|
||||
}
|
||||
|
||||
public JSONObject getData(){
|
||||
return data;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.muyu.event.basic;
|
||||
|
||||
|
||||
import org.springframework.context.ApplicationListener;
|
||||
|
||||
/**
|
||||
* 事件监听接口
|
||||
* @author 刘武
|
||||
* @package:com.muyu.event.basic
|
||||
* @name:EventListener
|
||||
* @date:2024/9/29 21:21
|
||||
*/
|
||||
public interface EventListener extends ApplicationListener<EventCustom> {
|
||||
|
||||
void onEvent(EventCustom event);
|
||||
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.muyu.event.basic;
|
||||
|
||||
import com.alibaba.fastjson2.JSONB;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.context.ApplicationEventPublisherAware;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 策略发送事件
|
||||
* @author 刘武
|
||||
* @package:com.muyu.event.basic
|
||||
* @name:EventPublisher
|
||||
* @date:2024/9/29 22:01
|
||||
*/
|
||||
@Component
|
||||
public class EventPublisher implements ApplicationEventPublisherAware {
|
||||
|
||||
private ApplicationEventPublisher publisher;
|
||||
|
||||
@Override
|
||||
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
|
||||
this.publisher=applicationEventPublisher;
|
||||
}
|
||||
|
||||
public void publishEvent(JSONObject jsonObject){
|
||||
EventCustom event = new EventCustom(this, jsonObject);
|
||||
publisher.publishEvent(event);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,25 +0,0 @@
|
|||
package com.muyu.event.basics;
|
||||
|
||||
/**
|
||||
* @author 刘武
|
||||
* @package:
|
||||
* @name:EventHandler
|
||||
* @date:2024/9/29
|
||||
*/
|
||||
public class EventHandler {
|
||||
|
||||
private static final ThreadLocal<EventQueueConfig> EVENT_THREAD = new ThreadLocal<>();
|
||||
|
||||
public static void set(final EventQueueConfig handler) {
|
||||
EVENT_THREAD.set(handler);
|
||||
}
|
||||
|
||||
public static EventQueueConfig get() {
|
||||
return EVENT_THREAD.get();
|
||||
}
|
||||
|
||||
public static void remove(){
|
||||
EVENT_THREAD.remove();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,25 +0,0 @@
|
|||
package com.muyu.event.basics;
|
||||
|
||||
|
||||
public abstract class EventProcessBasics {
|
||||
|
||||
/**
|
||||
* 下一个事件对象
|
||||
*/
|
||||
protected EventProcessBasics nextEvent;
|
||||
|
||||
/**
|
||||
* 下一个事件
|
||||
* @param nextHandler 下一个事件处理
|
||||
*/
|
||||
public void setNextHandler(EventProcessBasics nextHandler) {
|
||||
this.nextEvent = nextHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* 事件处理抽象类
|
||||
* @param eventKey 事件唯一key
|
||||
*/
|
||||
public abstract void handleEvent(String eventKey);
|
||||
|
||||
}
|
|
@ -1,35 +0,0 @@
|
|||
package com.muyu.event.basics;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.concurrent.LinkedBlockingDeque;
|
||||
|
||||
/**
|
||||
* @author 刘武
|
||||
* @package:
|
||||
* @name:EventQueueConfig
|
||||
* @date:2024/9/29
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class EventQueueConfig {
|
||||
|
||||
private LinkedBlockingDeque<EventProcessBasics> taskNodeQueue = new LinkedBlockingDeque<>();
|
||||
|
||||
public void addEvent(EventProcessBasics obj){
|
||||
this.taskNodeQueue.add(obj);
|
||||
}
|
||||
|
||||
public boolean hashEventNext(){
|
||||
return !taskNodeQueue.isEmpty();
|
||||
}
|
||||
|
||||
private EventProcessBasics nextTaskNode(){
|
||||
return taskNodeQueue.poll();
|
||||
}
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
package com.muyu.event.basics;
|
||||
|
||||
|
||||
import com.muyu.event.domian.EventActuate;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author 刘武
|
||||
* @package:
|
||||
* @name:StartEvent
|
||||
* @date:2024/9/29
|
||||
*/
|
||||
|
||||
public class StartEvent extends ApplicationEvent {
|
||||
|
||||
private EventActuate eventActuate;
|
||||
|
||||
public StartEvent(EventActuate source) {
|
||||
super(source);
|
||||
this.eventActuate = source;
|
||||
}
|
||||
|
||||
public EventActuate getEventActuate() {
|
||||
return eventActuate;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.muyu.event.config;
|
||||
|
||||
|
||||
import com.muyu.event.listener.AddDatabaseListener;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author 刘武
|
||||
* @package:com.muyu.event.config
|
||||
* @name:EventConfig
|
||||
* @date:2024/9/29 21:13
|
||||
*/
|
||||
@Configuration
|
||||
public class EventConfig {
|
||||
|
||||
@Bean
|
||||
public AddDatabaseListener addDatabaseListener() {
|
||||
return new AddDatabaseListener();
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -66,9 +66,7 @@ public class IoTDBConfig {
|
|||
measurements.add("car_vin");
|
||||
measurements.add("information");
|
||||
|
||||
|
||||
session.insertRecord(TABLENAME,System.currentTimeMillis(),measurements,list);
|
||||
|
||||
//关闭连接
|
||||
session.close();
|
||||
} catch (IoTDBConnectionException e) {
|
||||
|
|
|
@ -1,129 +0,0 @@
|
|||
package com.muyu.event.config;
|
||||
|
||||
|
||||
import org.apache.kafka.clients.consumer.ConsumerConfig;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.SpringBootConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
|
||||
import org.springframework.kafka.config.KafkaListenerContainerFactory;
|
||||
import org.springframework.kafka.core.ConsumerFactory;
|
||||
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
|
||||
import org.springframework.kafka.listener.ConcurrentMessageListenerContainer;
|
||||
import org.springframework.kafka.listener.ContainerProperties;
|
||||
import org.springframework.kafka.support.serializer.JsonDeserializer;
|
||||
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author 徐一杰
|
||||
* @date 2022/10/31 18:05
|
||||
* kafka配置,也可以写在yml,这个文件会覆盖yml
|
||||
*/
|
||||
@SpringBootConfiguration
|
||||
public class KafkaConsumerConfig {
|
||||
|
||||
/**
|
||||
* 配置 Kafka的 主机地址
|
||||
*/
|
||||
@Value("${spring.kafka.consumer.bootstrap-servers}")
|
||||
private String bootstrapServers;
|
||||
/**
|
||||
* 配置分分组
|
||||
*/
|
||||
@Value("${spring.kafka.consumer.group-id}")
|
||||
private String groupId;
|
||||
/**
|
||||
* 是否自动提交 偏移量
|
||||
*/
|
||||
@Value("${spring.kafka.consumer.enable-auto-commit}")
|
||||
private boolean enableAutoCommit;
|
||||
/**
|
||||
* 消费者与Kafka的心跳续约的会话超时时间
|
||||
*/
|
||||
@Value("${spring.kafka.properties.session.timeout.ms}")
|
||||
private String sessionTimeout;
|
||||
/**
|
||||
* 两次poll之间的最大间隔,默认值为5分钟。如果超过这个间隔会触发reBalance
|
||||
*/
|
||||
@Value("${spring.kafka.properties.max.poll.interval.ms}")
|
||||
private String maxPollIntervalTime;
|
||||
|
||||
@Value("${spring.kafka.consumer.max-poll-records}")
|
||||
private String maxPollRecords;
|
||||
|
||||
@Value("${spring.kafka.consumer.auto-offset-reset}")
|
||||
private String autoOffsetReset;
|
||||
|
||||
@Value("${spring.kafka.listener.concurrency}")
|
||||
private Integer concurrency;
|
||||
|
||||
@Value("${spring.kafka.listener.missing-topics-fatal}")
|
||||
private boolean missingTopicsFatal;
|
||||
|
||||
@Value("${spring.kafka.listener.poll-timeout}")
|
||||
private long pollTimeout;
|
||||
|
||||
@Bean
|
||||
public Map<String, Object> consumerConfigs() {
|
||||
Map<String, Object> propsMap = new HashMap<>(16);
|
||||
propsMap.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
|
||||
propsMap.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
|
||||
//是否自动提交偏移量,默认值是true,为了避免出现重复数据和数据丢失,可以把它设置为false,然后手动提交偏移量
|
||||
propsMap.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, enableAutoCommit);
|
||||
//自动提交的时间间隔,自动提交开启时生效
|
||||
propsMap.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, "2000");
|
||||
//该属性指定了消费者在读取一个没有偏移量的分区或者偏移量无效的情况下该作何处理:
|
||||
//earliest:当各分区下有已提交的offset时,从提交的offset开始消费;无提交的offset时,从头开始消费分区的记录
|
||||
//latest:当各分区下有已提交的offset时,从提交的offset开始消费;无提交的offset时,消费新产生的该分区下的数据(在消费者启动之后生成的记录)
|
||||
//none:当各分区都存在已提交的offset时,从提交的offset开始消费;只要有一个分区不存在已提交的offset,则抛出异常
|
||||
propsMap.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, autoOffsetReset);
|
||||
//两次poll之间的最大间隔,默认值为5分钟。如果超过这个间隔会触发reBalance
|
||||
propsMap.put(ConsumerConfig.MAX_POLL_INTERVAL_MS_CONFIG, maxPollIntervalTime);
|
||||
//这个参数定义了poll方法最多可以拉取多少条消息,默认值为500。如果在拉取消息的时候新消息不足500条,那有多少返回多少;如果超过500条,每次只返回500。
|
||||
//这个默认值在有些场景下太大,有些场景很难保证能够在5min内处理完500条消息,
|
||||
//如果消费者无法在5分钟内处理完500条消息的话就会触发reBalance,
|
||||
//然后这批消息会被分配到另一个消费者中,还是会处理不完,这样这批消息就永远也处理不完。
|
||||
//要避免出现上述问题,提前评估好处理一条消息最长需要多少时间,然后覆盖默认的max.poll.records参数
|
||||
//注:需要开启BatchListener批量监听才会生效,如果不开启BatchListener则不会出现reBalance情况
|
||||
propsMap.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, maxPollRecords);
|
||||
//当broker多久没有收到consumer的心跳请求后就触发reBalance,默认值是10s
|
||||
propsMap.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, sessionTimeout);
|
||||
//序列化(建议使用Json,这种序列化方式可以无需额外配置传输实体类)
|
||||
propsMap.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class);
|
||||
propsMap.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class);
|
||||
return propsMap;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ConsumerFactory<Object, Object> consumerFactory() {
|
||||
// 配置消费者的 Json 反序列化的可信赖包,反序列化实体类需要
|
||||
try (JsonDeserializer<Object> deserializer = new JsonDeserializer<>()) {
|
||||
deserializer.trustedPackages("*");
|
||||
return new DefaultKafkaConsumerFactory<>(consumerConfigs(), new JsonDeserializer<>(), deserializer);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* kafka监听容器工厂 负责 从 Kafka的主题中 取出消息进行消费 可以设置消费者的配置
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<Object, Object>> kafkaListenerContainerFactory() {
|
||||
ConcurrentKafkaListenerContainerFactory<Object, Object> factory = new ConcurrentKafkaListenerContainerFactory<>();
|
||||
factory.setConsumerFactory(consumerFactory());
|
||||
//在侦听器容器中运行的线程数,一般设置为 机器数*分区数
|
||||
factory.setConcurrency(concurrency);
|
||||
// 消费监听接口监听的主题不存在时,默认会报错,所以设置为false忽略错误
|
||||
factory.setMissingTopicsFatal(missingTopicsFatal);
|
||||
// 自动提交关闭,需要设置手动消息确认
|
||||
factory.getContainerProperties().setAckMode(ContainerProperties.AckMode.MANUAL_IMMEDIATE);
|
||||
factory.getContainerProperties().setPollTimeout(pollTimeout);
|
||||
// 设置为批量监听,需要用List接收
|
||||
// factory.setBatchListener(true);
|
||||
return factory;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,127 +0,0 @@
|
|||
package com.muyu.event.config;
|
||||
|
||||
import org.apache.kafka.clients.producer.ProducerConfig;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
|
||||
import org.springframework.kafka.core.KafkaTemplate;
|
||||
import org.springframework.kafka.core.ProducerFactory;
|
||||
import org.springframework.kafka.transaction.KafkaTransactionManager;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 主题生产者的配置类
|
||||
*/
|
||||
@Configuration
|
||||
public class KafkaProviderConfig {
|
||||
|
||||
/**
|
||||
* kafka 的主机地址
|
||||
*/
|
||||
@Value("${spring.kafka.producer.bootstrap-servers}")
|
||||
private String bootstrapServers;
|
||||
/**
|
||||
* 配置 Kafka的事务
|
||||
*/
|
||||
@Value("${spring.kafka.producer.transaction-id-prefix}")
|
||||
private String transactionIdPrefix;
|
||||
/**
|
||||
* 发送确认机制
|
||||
*/
|
||||
@Value("${spring.kafka.producer.acks}")
|
||||
private String acks;
|
||||
/**
|
||||
* 发送重试
|
||||
*/
|
||||
@Value("${spring.kafka.producer.retries}")
|
||||
private String retries;
|
||||
/**
|
||||
* 发送消息的批次大小
|
||||
*/
|
||||
@Value("${spring.kafka.producer.batch-size}")
|
||||
private String batchSize;
|
||||
/**
|
||||
* 消息的缓冲区内存大小
|
||||
*/
|
||||
@Value("${spring.kafka.producer.buffer-memory}")
|
||||
private String bufferMemory;
|
||||
|
||||
/**
|
||||
* 设置 健的序列化方式
|
||||
*/
|
||||
@Value("${spring.kafka.producer.key-serializer}")
|
||||
private String keySerializer;
|
||||
|
||||
/**
|
||||
* 设置 值的序列化方式
|
||||
*/
|
||||
@Value("${spring.kafka.producer.value-serializer}")
|
||||
private String valueSerializer;
|
||||
|
||||
/**
|
||||
* 构建 map 配置消息生产者对象的配置
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public Map<String, Object> producerConfigs() {
|
||||
Map<String, Object> props = new HashMap<>(16);
|
||||
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
|
||||
//acks=0 : 生产者在成功写入消息之前不会等待任何来自服务器的响应。
|
||||
//acks=1 : 只要集群的首领节点收到消息,生产者就会收到一个来自服务器成功响应。
|
||||
//acks=all :只有当所有参与复制的节点全部收到消息时,生产者才会收到一个来自服务器的成功响应。
|
||||
//开启事务必须设为all
|
||||
props.put(ProducerConfig.ACKS_CONFIG, acks);
|
||||
//发生错误后,消息重发的次数,开启事务必须大于0
|
||||
props.put(ProducerConfig.RETRIES_CONFIG, retries);
|
||||
//当多个消息发送到相同分区时,生产者会将消息打包到一起,以减少请求交互. 而不是一条条发送
|
||||
//批次的大小可以通过batch.size 参数设置.默认是16KB
|
||||
//较小的批次大小有可能降低吞吐量(批次大小为0则完全禁用批处理)。
|
||||
//比如说,kafka里的消息5秒钟Batch才凑满了16KB,才能发送出去。那这些消息的延迟就是5秒钟
|
||||
//实测batchSize这个参数没有用
|
||||
props.put(ProducerConfig.BATCH_SIZE_CONFIG, batchSize);
|
||||
//有的时刻消息比较少,过了很久,比如5min也没有凑够16KB,这样延时就很大,所以需要一个参数. 再设置一个时间,到了这个时间,
|
||||
//即使数据没达到16KB,也将这个批次发送出去
|
||||
props.put(ProducerConfig.LINGER_MS_CONFIG, "5000");
|
||||
//生产者内存缓冲区的大小
|
||||
props.put(ProducerConfig.BUFFER_MEMORY_CONFIG, bufferMemory);
|
||||
//反序列化,和生产者的序列化方式对应
|
||||
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, keySerializer);
|
||||
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, valueSerializer);
|
||||
return props;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建 主题生产者工厂
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public ProducerFactory<Object, Object> producerFactory() {
|
||||
DefaultKafkaProducerFactory<Object, Object> factory = new DefaultKafkaProducerFactory<>(producerConfigs());
|
||||
//开启事务,会导致 LINGER_MS_CONFIG 配置失效
|
||||
factory.setTransactionIdPrefix(transactionIdPrefix);
|
||||
return factory;
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置 Kafka的事务管理器
|
||||
* @param producerFactory
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public KafkaTransactionManager<Object, Object> kafkaTransactionManager(ProducerFactory<Object, Object> producerFactory) {
|
||||
return new KafkaTransactionManager<>(producerFactory);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建 KafkaTemplate
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public KafkaTemplate<Object, Object> kafkaTemplate() {
|
||||
return new KafkaTemplate<>(producerFactory());
|
||||
}
|
||||
|
||||
}
|
|
@ -1,65 +0,0 @@
|
|||
package com.muyu.event.config;
|
||||
|
||||
import jakarta.annotation.Nullable;
|
||||
import org.apache.kafka.clients.producer.ProducerRecord;
|
||||
import org.apache.kafka.clients.producer.RecordMetadata;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.kafka.core.KafkaTemplate;
|
||||
import org.springframework.kafka.support.ProducerListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
@Component
|
||||
public class KafkaSendResultHandler implements ProducerListener<Object,Object> {
|
||||
|
||||
@Autowired
|
||||
private KafkaTemplate<Object,Object> kafkaTemplate;
|
||||
|
||||
/**
|
||||
* bean 初始化方法
|
||||
*/
|
||||
@PostConstruct
|
||||
public void init(){
|
||||
this.kafkaTemplate.setProducerListener(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 消息发送到Kafka成功的回调
|
||||
* @param producerRecord
|
||||
* @param recordMetadata
|
||||
*/
|
||||
@Override
|
||||
public void onSuccess(ProducerRecord producerRecord, RecordMetadata recordMetadata){
|
||||
System.out.println("信息发送成功:"+ producerRecord.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* 息发送到 Kafka 失败的回调
|
||||
* @param producerRecord the failed record
|
||||
* @param recordMetadata The metadata for the record that was sent (i.e. the partition
|
||||
* and offset). If an error occurred, metadata will contain only valid topic and maybe
|
||||
* the partition. If the partition is not provided in the ProducerRecord and an error
|
||||
* occurs before partition is assigned, then the partition will be set to
|
||||
* RecordMetadata.UNKNOWN_PARTITION.
|
||||
* @param exception the exception thrown
|
||||
*/
|
||||
@Override
|
||||
public void onError(ProducerRecord producerRecord, @Nullable RecordMetadata recordMetadata,
|
||||
Exception exception){
|
||||
System.out.println("消息发送失败: "+ producerRecord.toString());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -1,34 +0,0 @@
|
|||
package com.muyu.event.config;
|
||||
|
||||
|
||||
import lombok.NonNull;
|
||||
import org.apache.kafka.clients.consumer.Consumer;
|
||||
import org.springframework.kafka.listener.KafkaListenerErrorHandler;
|
||||
import org.springframework.kafka.listener.ListenerExecutionFailedException;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class MyKafkaListenerErrorHandler implements KafkaListenerErrorHandler {
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
public Object handleError(@NonNull Message<?> message,
|
||||
ListenerExecutionFailedException exception) {
|
||||
return new Object();
|
||||
}
|
||||
|
||||
@Override
|
||||
@NonNull
|
||||
public Object handleError(@NonNull Message<?> message,
|
||||
@NonNull ListenerExecutionFailedException exception,
|
||||
Consumer<?, ?> consumer) {
|
||||
System.out.println("消息详情:"+ message);
|
||||
System.out.println("异常信息:"+ exception);
|
||||
System.out.println("消费者详情:" +consumer.groupMetadata());
|
||||
System.out.println("监听主题:"+ consumer.listTopics());
|
||||
return KafkaListenerErrorHandler.super.handleError(message, exception, consumer);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package com.muyu.event.constant;
|
||||
|
||||
|
||||
/**
|
||||
* 事件常量
|
||||
* @author 刘武
|
||||
* @package:com.muyu.event.constant
|
||||
* @name:EventConstant
|
||||
* @date:2024/9/28 19:25
|
||||
*/
|
||||
|
||||
public interface EventConstant {
|
||||
|
||||
String STORAGE_EVENT = "storageEvent";
|
||||
}
|
|
@ -1,31 +0,0 @@
|
|||
package com.muyu.event.consumer;
|
||||
|
||||
import org.apache.kafka.clients.consumer.ConsumerRecord;
|
||||
import org.springframework.kafka.annotation.KafkaListener;
|
||||
import org.springframework.kafka.support.Acknowledgment;
|
||||
|
||||
/**
|
||||
* kafka监听
|
||||
* @author 刘武
|
||||
* @package:com.muyu.event.consumer
|
||||
* @name:KafkaConsumer
|
||||
* @date:2024/9/28 23:34
|
||||
*/
|
||||
|
||||
public class KafkaConsumer {
|
||||
|
||||
|
||||
@KafkaListener(topics = "data")
|
||||
public void dataKafkaConsumer(ConsumerRecord<Object,Object> consumerRecord, Acknowledgment acknowledgment){
|
||||
Object key = consumerRecord.key();
|
||||
Object value = consumerRecord.value();
|
||||
|
||||
//事件调用
|
||||
|
||||
|
||||
//消息确认消费
|
||||
acknowledgment.acknowledge();
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
package com.muyu.event.consumer;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.muyu.event.basic.EventPublisher;
|
||||
import com.muyu.event.service.IncidentService;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.apache.kafka.clients.consumer.ConsumerRecords;
|
||||
import org.apache.kafka.clients.consumer.KafkaConsumer;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
import java.time.Duration;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import static org.bouncycastle.asn1.x500.style.RFC4519Style.l;
|
||||
/**
|
||||
* kafka监听
|
||||
* @author 刘武
|
||||
* @package:com.muyu.event.consumer
|
||||
* @name:KafkaConsumer
|
||||
* @date:2024/9/28 23:34
|
||||
*/
|
||||
@Component
|
||||
@Log4j2
|
||||
public class MessageConsumer implements ApplicationRunner {
|
||||
|
||||
@Autowired
|
||||
public KafkaConsumer consumer;
|
||||
|
||||
private EventPublisher eventPublisher;
|
||||
|
||||
private final String topic="kafka-topic";
|
||||
|
||||
@Autowired
|
||||
private IncidentService incidentService;
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
List<String> list = Collections.singletonList(topic);
|
||||
consumer.subscribe(list);
|
||||
while (true){
|
||||
ConsumerRecords<String,String> consumerRecords = consumer.poll(Duration.ofMillis(100));
|
||||
consumerRecords.forEach(record -> {
|
||||
String value = record.value();
|
||||
JSONObject jsonObject = JSONObject.parseObject(value);
|
||||
log.info("value:{}",value);
|
||||
eventPublisher.publishEvent(jsonObject);
|
||||
try {
|
||||
//数据预警
|
||||
incidentService.warnEventProcess(jsonObject);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,90 @@
|
|||
package com.muyu.event.consumer;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.muyu.cache.ElectronicFenceGroupCacheService;
|
||||
import com.muyu.cache.SysCarCacheService;
|
||||
import com.muyu.common.domain.database.ElectronicFenceGroup;
|
||||
import com.muyu.common.domain.resp.SysCarVo;
|
||||
import com.muyu.common.redis.service.RedisService;
|
||||
import com.muyu.rabbitmq.consumer.RabbitMQConsumerUtil;
|
||||
import com.rabbitmq.client.Channel;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.amqp.core.Message;
|
||||
import org.springframework.amqp.rabbit.annotation.Queue;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* rabbitmq 监听器
|
||||
* @author 刘武
|
||||
* @package:com.muyu.event.consumer
|
||||
* @name:MqConsumer
|
||||
* @date:2024/10/2 14:17
|
||||
*/
|
||||
|
||||
@Component
|
||||
@Log4j2
|
||||
public class MqConsumer {
|
||||
|
||||
@Autowired
|
||||
private RedisService redisService;
|
||||
|
||||
@Autowired
|
||||
private SysCarCacheService sysCarCacheService;
|
||||
@Autowired
|
||||
private ElectronicFenceGroupCacheService electronicFenceGroupCacheService;
|
||||
|
||||
@RabbitListener(queuesToDeclare = @Queue(name = "basic"))
|
||||
public void rabbitMQBasicConsumer(String data , Message message , Channel channel) {
|
||||
log.info("当前时间:{} :RabbitMQConsumerUtil : {}", new Date(), message);
|
||||
try {
|
||||
// 获取到消息 开始消费
|
||||
log.info("消息消费者接收到消息,消息内容:{}", JSONObject.toJSONString(data));
|
||||
|
||||
Long add = redisService.redisTemplate.opsForSet().add(data, message.getMessageProperties().getMessageId());
|
||||
|
||||
if (add != 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* -----------------------------------以下为异步业务操作----------------------------
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* ------------------------------------------------------------------------------
|
||||
*/
|
||||
// 消费消息成功之后需要确认
|
||||
// long deliveryTag 消息投递序号 自增的数字 在整个队列中唯一 拿到这个序号就相当于拿到这条消息
|
||||
// boolean multiple 是否批量确认 true 批量 确认小于等于当前投递序号的消息 false 单个确认
|
||||
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
|
||||
log.info("xxx消费者接收到消息,消息内容:{},消费成功...", message);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("xxx消费者接收到消息,消息内容:{},消费消息异常,异常信息:{}", message, e);
|
||||
// 消息回退 拒绝消费消息
|
||||
// long deliveryTag 消息投递序号 自增的数字 在整个队列中唯一 拿到这个序号就相当于拿到这条消息
|
||||
// boolean requeue 是否回到原来的队列
|
||||
try {
|
||||
channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
|
||||
// channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
|
||||
} catch (IOException ex) {
|
||||
log.error("xxx消费者接收到消息,消息内容:{},回退消息异常,异常信息:{}", message, ex);
|
||||
}
|
||||
}finally {
|
||||
try {
|
||||
channel.close();
|
||||
} catch (Exception e) {
|
||||
log.error("xxx消费者关闭Channel异常,消息内容:{},异常信息:{}", message, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.muyu.event.consumer;
|
||||
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 车辆上线监听
|
||||
* @author 刘武
|
||||
* @package:com.muyu.event.consumer
|
||||
* @name:OnlineConsumer
|
||||
* @date:2024/9/30 11:40
|
||||
*/
|
||||
@Component
|
||||
@Log4j2
|
||||
public class OnlineConsumer {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.muyu.event.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 数据处理
|
||||
* @author 刘武
|
||||
* @package:com.muyu.event.controller
|
||||
* @name:DataController
|
||||
* @date:2024/9/29 20:16
|
||||
*/
|
||||
|
||||
@RestController
|
||||
@RequestMapping("data")
|
||||
public class DataController {
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -19,13 +19,12 @@ import java.util.List;
|
|||
* @name:ItodbController
|
||||
* @date:2024/9/28 19:17
|
||||
*/
|
||||
@RestController()
|
||||
@RestController
|
||||
public class IoTDBController {
|
||||
|
||||
@Autowired
|
||||
private IoTDBService tdbService;
|
||||
|
||||
|
||||
/**
|
||||
* 查询实时车辆信息列表
|
||||
* @return list
|
||||
|
@ -47,7 +46,6 @@ public class IoTDBController {
|
|||
return Result.success(carInformation);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* 车辆添加
|
||||
* @param addCarInformation
|
||||
|
@ -59,16 +57,4 @@ public class IoTDBController {
|
|||
return Result.success("添加成功");
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,68 @@
|
|||
package com.muyu.event.controller;
|
||||
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.muyu.rabbitmq.producer.RabbitMQProducerUtil;
|
||||
import org.apache.kafka.clients.producer.KafkaProducer;
|
||||
import org.apache.kafka.clients.producer.ProducerRecord;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
|
||||
/**
|
||||
* 测试
|
||||
* @author 刘武
|
||||
* @package:com.muyu.event.controller
|
||||
* @name:TestController
|
||||
* @date:2024/9/29 20:58
|
||||
*/
|
||||
@RestController("test")
|
||||
public class TestController {
|
||||
|
||||
@Resource
|
||||
private KafkaProducer kafkaProducer;
|
||||
@Resource
|
||||
private RabbitMQProducerUtil rabbitMQProducerUtil;
|
||||
|
||||
|
||||
private static final String topic="four_car";
|
||||
|
||||
@GetMapping("sendKafka")
|
||||
public String sendKafka(){
|
||||
|
||||
String message="发送一条信息";
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("cj","hh");
|
||||
ProducerRecord<String, String> producerRecord = new ProducerRecord<String, String>(topic,jsonObject.toString());
|
||||
kafkaProducer.send(producerRecord);
|
||||
|
||||
return "success";
|
||||
}
|
||||
|
||||
@GetMapping("sendMq")
|
||||
public String sendMq(){
|
||||
String message="发送一条信息-mq";
|
||||
rabbitMQProducerUtil.basicSendMessage("basic",message);
|
||||
return "success-mq";
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -1,8 +1,6 @@
|
|||
package com.muyu.event.domian;
|
||||
|
||||
import com.muyu.common.core.annotation.Excel;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 数据添加实体类
|
||||
* @author 刘武
|
||||
|
|
|
@ -13,7 +13,13 @@ import org.springframework.format.annotation.DateTimeFormat;
|
|||
|
||||
import java.util.Date;
|
||||
|
||||
|
||||
/**
|
||||
* 事件实体类
|
||||
* @author 刘武
|
||||
* @package:com.muyu.event.domain
|
||||
* @name:Event
|
||||
* @date:2024/9/28 23:10
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
|
|
|
@ -1,28 +0,0 @@
|
|||
package com.muyu.event.domian;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.SuperBuilder;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 事件驱动对象
|
||||
* @Author 刘武
|
||||
* @Data 2024/9/29
|
||||
*/
|
||||
@Data
|
||||
@SuperBuilder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class EventActuate {
|
||||
/**
|
||||
* json数据
|
||||
*/
|
||||
private String jsonData;
|
||||
/**
|
||||
* 事件驱动key集合
|
||||
*/
|
||||
private List<String> eventKeys;
|
||||
}
|
|
@ -1,25 +0,0 @@
|
|||
package com.muyu.event.eventDispose;
|
||||
|
||||
|
||||
import com.muyu.event.basics.StartEvent;
|
||||
import com.muyu.event.domian.EventActuate;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author 刘武
|
||||
* @package:
|
||||
* @name:AutoStartupEventListener
|
||||
* @date:2024/9/29
|
||||
*/
|
||||
@Component
|
||||
public class AutoStartupEventListener implements ApplicationListener<StartEvent> {
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(StartEvent event) {
|
||||
|
||||
EventActuate eventActuate = event.getEventActuate();
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -1,37 +0,0 @@
|
|||
package com.muyu.event.eventDispose;
|
||||
|
||||
|
||||
import com.muyu.event.basics.EventProcessBasics;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
|
||||
/**
|
||||
* @author 刘武
|
||||
* @package:
|
||||
* @name:StorageEvent
|
||||
* @date:2024/9/29
|
||||
*/
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Log4j2
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class StorageEvent extends EventProcessBasics {
|
||||
/**
|
||||
* 事件名称
|
||||
*/
|
||||
private String eventName;
|
||||
|
||||
@Override
|
||||
public void handleEvent(String eventKey) {
|
||||
if (eventKey.equals(eventName)){
|
||||
log.info("开始执行 [{}] 事件", eventKey);
|
||||
|
||||
}else if (nextEvent != null){
|
||||
nextEvent.handleEvent(eventKey);
|
||||
}else {
|
||||
log.info("处理结束,最后处理的事件为 [{}]", eventKey);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.muyu.event.listener;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.muyu.event.basic.EventCustom;
|
||||
import com.muyu.event.basic.EventListener;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
/**
|
||||
* 添加数据库事件
|
||||
* @program: cloud-server
|
||||
* @author: cuiyongxing
|
||||
* @create: 2024-09-29 17:34
|
||||
**/
|
||||
public class AddDatabaseListener implements EventListener {
|
||||
|
||||
|
||||
@Override
|
||||
public void onEvent(EventCustom event) {
|
||||
|
||||
JSONObject jsonObject = event.getData();
|
||||
List<String> keys = new ArrayList<>();
|
||||
List<String> values = new ArrayList<>();
|
||||
|
||||
jsonObject.forEach((key, value) -> {
|
||||
keys.add(key);
|
||||
values.add((String) value);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(EventCustom event) {
|
||||
onEvent(event);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.muyu.event.service;
|
||||
|
||||
/**
|
||||
* @author 刘武
|
||||
* @package:com.muyu.event.service.impl
|
||||
* @name:DataService
|
||||
* @date:2024/9/29 20:23
|
||||
*/
|
||||
public interface DataService {
|
||||
|
||||
void warnData(String data);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.muyu.event.service;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
|
||||
/**
|
||||
* @author liuxinyue
|
||||
* @Package:com.muyu.event.service
|
||||
* @name:IncidentService
|
||||
* @Date:2024/10/9 15:02
|
||||
*/
|
||||
public interface IncidentService {
|
||||
|
||||
void warnEventProcess(JSONObject jsonObject) throws Exception;
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.muyu.event.service;
|
||||
|
||||
/**
|
||||
* @author 刘武
|
||||
* @package:com.muyu.event.service
|
||||
* @name:TestService
|
||||
* @date:2024/9/29 20:59
|
||||
*/
|
||||
public interface TestService {
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.muyu.event.service.impl;
|
||||
|
||||
import com.muyu.event.service.DataService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* @author 刘武
|
||||
* @package:com.muyu.event.service.impl
|
||||
* @name:DataServiceImpl
|
||||
* @date:2024/9/29 20:24
|
||||
*/
|
||||
@Service
|
||||
public class DataServiceImpl implements DataService {
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void warnData(String data) {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,152 @@
|
|||
package com.muyu.event.service.impl;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.muyu.cache.*;
|
||||
import com.muyu.common.domain.MessageTemplateType;
|
||||
import com.muyu.common.domain.SysCar;
|
||||
import com.muyu.common.domain.SysCarFault;
|
||||
import com.muyu.common.domain.resp.SysCarVo;
|
||||
import com.muyu.common.domain.resp.WarnRuleResp;
|
||||
import com.muyu.common.domain.resp.WarnStrategyResp;
|
||||
import com.muyu.event.consumer.MessageConsumer;
|
||||
import com.muyu.event.service.IncidentService;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.apache.kafka.clients.consumer.ConsumerRecord;
|
||||
import org.apache.kafka.clients.consumer.ConsumerRecords;
|
||||
import org.apache.kafka.clients.consumer.KafkaConsumer;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.stereotype.Service;
|
||||
import javax.annotation.Resource;
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
/**
|
||||
* @author liuxinyue
|
||||
* @Package:com.muyu.event.service.impl
|
||||
* @name:IncidentServiceImpl
|
||||
* @Date:2024/10/9 15:02
|
||||
*/
|
||||
@Log4j2
|
||||
@Service
|
||||
public class IncidentServiceImpl implements IncidentService {
|
||||
|
||||
private static int DURATION_SECONDS = 5;
|
||||
private static List<JSONObject> receivedStrings = new ArrayList<>();
|
||||
private static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
|
||||
private static List<MessageTemplateType> messageTemplateTypes=null;
|
||||
private static Long msgTypeId=null;
|
||||
//滑窗时间
|
||||
private static Long slideTime=null;
|
||||
//增长率
|
||||
private static Long slideFrequency=null;
|
||||
//预警策略
|
||||
@Resource
|
||||
private WarnStrategyCacheService warnStrategyCacheService;
|
||||
//车辆
|
||||
@Resource
|
||||
private SysCarCacheService sysCarCacheService;
|
||||
//预警规则
|
||||
@Resource
|
||||
private WarnRuleCacheService warnRuleCacheService;
|
||||
//报文模版
|
||||
@Resource
|
||||
private MessageTemplateTypeCacheService messageTemplateTypeCacheService;
|
||||
//kafka的主题名称
|
||||
private final String topic="kafka-topic";
|
||||
@Autowired
|
||||
private SysCarFaultCacheService sysCarFaultCacheService;
|
||||
@Override
|
||||
public void warnEventProcess(JSONObject jsonObject) throws Exception {
|
||||
receivedStrings.add(jsonObject);
|
||||
//协议解析:每秒穿过来一个JSONObject jsonObject; 添加进receivedStrings
|
||||
//根据这个车辆VIN查询出他对应的车辆类型
|
||||
String carVin=null;
|
||||
//报文模版的ID
|
||||
Integer templateId=null;
|
||||
//取出这辆车的carVin
|
||||
for (JSONObject receivedString : receivedStrings) {
|
||||
carVin = (String) receivedString.get("carVin");
|
||||
}
|
||||
//这辆车的信息
|
||||
SysCar carByVin = null;
|
||||
List<SysCarVo> carVoList = sysCarCacheService.get(sysCarCacheService.keyPre());
|
||||
Map<String, SysCarVo> carMap = carVoList.stream()
|
||||
.collect(Collectors.toMap(SysCarVo::getCarVin, Function.identity()));
|
||||
//获取到了这个车辆的信息
|
||||
carByVin = carMap.get(carVin);
|
||||
//获取到这辆车绑定的报文模版
|
||||
templateId=carByVin.getTemplateId();
|
||||
//这个是这辆车对应的所有策略
|
||||
List<WarnStrategyResp> carWithWarnStrategyList=null;
|
||||
List<WarnStrategyResp> warnStrategyResps = warnStrategyCacheService.get(warnStrategyCacheService.keyPre());
|
||||
for (WarnStrategyResp warnStrategyResp : warnStrategyResps) {
|
||||
//策略中有绑定的车辆ID
|
||||
if(warnStrategyResp.getCarTypeId()==carByVin.getCarTypeId()){
|
||||
carWithWarnStrategyList.add(warnStrategyResp);
|
||||
}
|
||||
}
|
||||
//该车对应的所有预警规则
|
||||
List<WarnRuleResp> warnRuleResp=null;
|
||||
List<WarnRuleResp> warnRuleResps = warnRuleCacheService.get(warnRuleCacheService.keyPre());
|
||||
for (WarnStrategyResp warnStrategyResp : carWithWarnStrategyList) {
|
||||
for (WarnRuleResp ruleResp : warnRuleResps) {
|
||||
if(warnStrategyResp.getId().equals(ruleResp.getStrategyId())){
|
||||
warnRuleResp.add(ruleResp);
|
||||
}
|
||||
}
|
||||
}
|
||||
//报文模版
|
||||
messageTemplateTypes = messageTemplateTypeCacheService.get(messageTemplateTypeCacheService.keyPre());
|
||||
for (WarnRuleResp ruleResp : warnRuleResp) {
|
||||
//每一个规则他绑定了报文模版里面对应的一个配置 比如:电池,或者车速
|
||||
msgTypeId = ruleResp.getMsgTypeId();
|
||||
//将规则中对应的滑窗时间赋值为DURATION_SECONDS
|
||||
DURATION_SECONDS = Math.toIntExact(ruleResp.getSlideTime());
|
||||
slideFrequency = ruleResp.getSlideFrequency();
|
||||
}
|
||||
// 定义一个任务,每秒执行一次
|
||||
Runnable task = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// 清理超过的数据
|
||||
cleanUpOldStrings();
|
||||
// 检查超速条件
|
||||
checkForSpeeding();
|
||||
}
|
||||
};
|
||||
// 每隔1秒执行一次任务
|
||||
scheduler.scheduleAtFixedRate(task, 0, 1, TimeUnit.SECONDS);
|
||||
}
|
||||
// 清理超过60秒的数据
|
||||
private static void cleanUpOldStrings() {
|
||||
long currentTime = System.currentTimeMillis();
|
||||
receivedStrings.removeIf(jsonObject ->
|
||||
currentTime - jsonObject.getLong("time") > TimeUnit.SECONDS.toMillis(DURATION_SECONDS)
|
||||
);
|
||||
}
|
||||
// 检查是否有超速情况
|
||||
private static void checkForSpeeding() {
|
||||
if (receivedStrings.size() < 2) return; // 如果数据不足,直接返回
|
||||
for (int i = 0; i < receivedStrings.size(); i++) {
|
||||
JSONObject current = receivedStrings.get(i);
|
||||
JSONObject next = receivedStrings.get(i + 1);
|
||||
for (MessageTemplateType messageTemplateType : messageTemplateTypes) {
|
||||
if(messageTemplateType.getMessageTemplateTypeId().equals(msgTypeId)){
|
||||
Short currentElapsed = current.getShort(messageTemplateType.getMessageField());
|
||||
Short nextElapsed = next.getShort(messageTemplateType.getMessageField());
|
||||
if (nextElapsed > currentElapsed + slideFrequency) {
|
||||
log.info("出错啦,出错啦,您的"+messageTemplateType.getMessageField()+"不正常,请检查!!!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.muyu.event.service.impl;
|
||||
|
||||
import com.muyu.event.service.TestService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* @author 刘武
|
||||
* @package:com.muyu.event.service.impl
|
||||
* @name:TestServiceImpl
|
||||
* @date:2024/9/29 21:00
|
||||
*/
|
||||
@Service
|
||||
public class TestServiceImpl implements TestService {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package com.muyu.event.util;
|
||||
|
||||
import com.github.benmanes.caffeine.cache.Cache;
|
||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 缓存工具类
|
||||
*
|
||||
* @program: cloud-server
|
||||
* @author: 刘武
|
||||
* @create: 2024-09-30 10:08
|
||||
**/
|
||||
@Component
|
||||
public class CacheUtil<T> {
|
||||
|
||||
private final Cache<String, T> cache;
|
||||
|
||||
public CacheUtil() {
|
||||
this.cache = Caffeine.newBuilder()
|
||||
.maximumSize(500L)
|
||||
.build();
|
||||
}
|
||||
|
||||
public T get(String key) {
|
||||
return cache.getIfPresent(key);
|
||||
}
|
||||
|
||||
public void put(String key, T value) {
|
||||
cache.put(key, value);
|
||||
}
|
||||
|
||||
public void remove(String key) {
|
||||
cache.invalidate(key);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,14 +1,16 @@
|
|||
# Tomcat
|
||||
server:
|
||||
port: 10009
|
||||
|
||||
# nacos线上地址
|
||||
nacos:
|
||||
addr: 47.101.53.251:8848
|
||||
user-name: nacos
|
||||
password: nacos
|
||||
namespace: four
|
||||
namespace: lxy
|
||||
# SPRING_AMQP_DESERIALIZATION_TRUST_ALL=true spring.amqp.deserialization.trust.all
|
||||
# Spring
|
||||
spring:
|
||||
amqp:
|
||||
deserialization:
|
||||
trust:
|
||||
|
@ -51,9 +53,8 @@ nacos:
|
|||
- application-config-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
||||
# xxl-job 配置文件
|
||||
- application-xxl-config-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
||||
# rabbit 配置文件
|
||||
- application-rabbit-config-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
||||
|
||||
logging:
|
||||
level:
|
||||
com.muyu.fence.mapper: DEBUG
|
||||
com.muyu.system.mapper: DEBUG
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ nacos:
|
|||
addr: 47.101.53.251:8848
|
||||
user-name: nacos
|
||||
password: nacos
|
||||
namespace: four
|
||||
namespace: lxy
|
||||
|
||||
# Spring
|
||||
spring:
|
||||
|
|
|
@ -9,7 +9,7 @@ import org.springframework.stereotype.Service;
|
|||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 业务字段 服务层实现
|
||||
* 业务字段 业务逻辑层实现
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
|
|
|
@ -40,7 +40,7 @@ import java.util.zip.ZipEntry;
|
|||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
/**
|
||||
* 业务 服务层实现
|
||||
* 业务 业务逻辑层实现
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
|
|
|
@ -7,7 +7,7 @@ nacos:
|
|||
addr: 47.101.53.251:8848
|
||||
user-name: nacos
|
||||
password: nacos
|
||||
namespace: four
|
||||
namespace: lxy
|
||||
# SPRING_AMQP_DESERIALIZATION_TRUST_ALL=true spring.amqp.deserialization.trust.all
|
||||
# Spring
|
||||
spring:
|
||||
|
|
|
@ -78,6 +78,7 @@
|
|||
<artifactId>cloud-common-xxl</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -10,10 +10,7 @@ import com.muyu.common.log.enums.BusinessType;
|
|||
import com.muyu.common.security.annotation.InnerAuth;
|
||||
import com.muyu.common.security.annotation.RequiresPermissions;
|
||||
import com.muyu.common.security.utils.SecurityUtils;
|
||||
import com.muyu.common.system.domain.SysDept;
|
||||
import com.muyu.common.system.domain.SysRole;
|
||||
import com.muyu.common.system.domain.SysUser;
|
||||
import com.muyu.common.system.domain.LoginUser;
|
||||
import com.muyu.common.system.domain.*;
|
||||
import com.muyu.system.domain.resp.AuthRoleResp;
|
||||
import com.muyu.system.domain.resp.UserDetailInfoResp;
|
||||
import com.muyu.system.domain.resp.UserInfoResp;
|
||||
|
@ -136,6 +133,15 @@ public class SysUserController extends BaseController {
|
|||
return Result.success(userService.registerUser(sysUser));
|
||||
}
|
||||
|
||||
/**
|
||||
* 入驻企业信息
|
||||
*/
|
||||
@InnerAuth
|
||||
@PostMapping("/enterprise")
|
||||
public Result<Boolean> enterprise (@RequestBody Business enterprise){
|
||||
|
||||
return Result.success(userService.enterprise(enterprise));
|
||||
}
|
||||
/**
|
||||
* 获取用户信息
|
||||
*
|
||||
|
@ -296,4 +302,11 @@ public class SysUserController extends BaseController {
|
|||
public Result deptTree (SysDept dept) {
|
||||
return success(deptService.selectDeptTreeList(dept));
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("/addUser")
|
||||
public Result<Integer> addUser(@RequestBody SysUser sysUser){
|
||||
Integer i = userService.addUser(sysUser);
|
||||
return Result.success(i);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.muyu.system.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.muyu.common.system.domain.Business;
|
||||
import com.muyu.common.system.domain.SysUser;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
|
@ -66,6 +67,20 @@ public interface SysUserMapper extends BaseMapper<SysUser> {
|
|||
*/
|
||||
int insertUser(SysUser user);
|
||||
|
||||
/**
|
||||
* 企业入驻
|
||||
* @param enterprise
|
||||
* @return
|
||||
*/
|
||||
int enterprise(Business enterprise);
|
||||
|
||||
/**
|
||||
* 企业管理添加
|
||||
* @param enterprise
|
||||
* @return
|
||||
*/
|
||||
int enterPriseAdd(Business enterprise);
|
||||
|
||||
/**
|
||||
* 修改用户信息
|
||||
*
|
||||
|
@ -142,4 +157,9 @@ public interface SysUserMapper extends BaseMapper<SysUser> {
|
|||
|
||||
List<SysUser> selectCompanyList();
|
||||
|
||||
|
||||
Integer addUser(SysUser sysUser);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ import com.muyu.system.domain.SysConfig;
|
|||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 配置plus业务层
|
||||
* @author DongZl
|
||||
* @description: 配置plus业务层
|
||||
* @Date 2023-11-13 上午 10:06
|
||||
|
|
|
@ -7,7 +7,7 @@ import com.muyu.system.domain.vo.TreeSelect;
|
|||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 部门管理 服务层
|
||||
* 部门管理 业务逻辑层
|
||||
*
|
||||
* @author muyu
|
||||
*/
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.muyu.system.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.muyu.common.system.domain.Business;
|
||||
import com.muyu.common.system.domain.SysUser;
|
||||
|
||||
import java.util.List;
|
||||
|
@ -133,6 +134,7 @@ public interface SysUserService extends IService<SysUser> {
|
|||
*/
|
||||
boolean registerUser(SysUser user);
|
||||
|
||||
boolean enterprise(Business enterprise);
|
||||
/**
|
||||
* 修改用户信息
|
||||
*
|
||||
|
@ -228,4 +230,6 @@ public interface SysUserService extends IService<SysUser> {
|
|||
|
||||
List<SysUser> selectCompanyList();
|
||||
|
||||
Integer addUser(SysUser sysUser);
|
||||
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@ import java.util.List;
|
|||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 配置plus业务实现层
|
||||
* @author DongZl
|
||||
* @description: 配置plus业务实现层
|
||||
* @Date 2023-11-13 上午 10:06
|
||||
|
|
|
@ -8,6 +8,7 @@ import com.muyu.common.core.utils.StringUtils;
|
|||
import com.muyu.common.core.utils.bean.BeanValidators;
|
||||
import com.muyu.common.datascope.annotation.DataScope;
|
||||
import com.muyu.common.security.utils.SecurityUtils;
|
||||
import com.muyu.common.system.domain.Business;
|
||||
import com.muyu.common.system.domain.SysRole;
|
||||
import com.muyu.common.system.domain.SysUser;
|
||||
import com.muyu.system.domain.SysPost;
|
||||
|
@ -257,6 +258,17 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
|
|||
return userMapper.insertUser(user) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 企业入驻
|
||||
* @param enterprise
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean enterprise(Business enterprise){
|
||||
userMapper.enterPriseAdd(enterprise);
|
||||
return userMapper.enterprise(enterprise) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改保存用户信息
|
||||
*
|
||||
|
@ -506,4 +518,9 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
|
|||
return userMapper.selectCompanyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer addUser(SysUser sysUser) {
|
||||
return userMapper.addUser(sysUser);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
<result property="updateBy" column="update_by"/>
|
||||
<result property="updateTime" column="update_time"/>
|
||||
<result property="remark" column="remark"/>
|
||||
<result property="databaseName" column="database_name"/>
|
||||
<association property="dept" javaType="com.muyu.common.system.domain.SysDept" resultMap="deptResult"/>
|
||||
<collection property="roles" javaType="java.util.List" resultMap="RoleResult"/>
|
||||
</resultMap>
|
||||
|
@ -204,6 +205,7 @@
|
|||
<if test="status != null and status != ''">status,</if>
|
||||
<if test="createBy != null and createBy != ''">create_by,</if>
|
||||
<if test="remark != null and remark != ''">remark,</if>
|
||||
<if test="databaseName !=null and databaseName!=''">database_name,</if>
|
||||
create_time
|
||||
)values(
|
||||
<if test="userId != null and userId != ''">#{userId},</if>
|
||||
|
@ -218,9 +220,22 @@
|
|||
<if test="status != null and status != ''">#{status},</if>
|
||||
<if test="createBy != null and createBy != ''">#{createBy},</if>
|
||||
<if test="remark != null and remark != ''">#{remark},</if>
|
||||
<if test="databaseName!=null and databaseName!=''">#{databaseName}</if>
|
||||
sysdate()
|
||||
)
|
||||
</insert>
|
||||
<insert id="enterprise">
|
||||
INSERT INTO `datasource`.`datasource` (`id`, `firm_name`, `database_name`) VALUES (NULL, #{firmName}, #{databaseName});
|
||||
</insert>
|
||||
<insert id="enterPriseAdd">
|
||||
INSERT INTO `saas`.`tb_enterprise` (`enterprise_id`, `enterprise_name`, `enterprise_car_count`, `enterprise_fence_count`, `enterprise_database_name`)
|
||||
VALUES (NULL, #{firmName}, 0, 0, #{databaseName});
|
||||
</insert>
|
||||
<insert id="addUser">
|
||||
INSERT INTO `four`.`sys_user`
|
||||
(`dept_id`, `user_name`, `nick_name`, `user_type`, `email`, `phonenumber`, `sex`, `avatar`, `password`, `status`, `del_flag`, `login_ip`, `login_date`, `create_by`, `create_time`, `update_by`, `update_time`, `remark`, `database_name`)
|
||||
VALUES ( 105, #{userName}, '若依', '00', 'ry@qq.com', '15666666666', '1', '', '$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2', '0', '0', '47.101.53.251', '2024-05-23 15:08:18', 'admin', #{createTime}, '', NULL, '测试员', #{databaseName});
|
||||
</insert>
|
||||
|
||||
<update id="updateUser" parameterType="com.muyu.common.system.domain.SysUser">
|
||||
update sys_user
|
||||
|
|
|
@ -0,0 +1,109 @@
|
|||
<?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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-modules</artifactId>
|
||||
<version>3.6.3</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>cloud-modules-template</artifactId>
|
||||
|
||||
<description>
|
||||
cloud-modules-template 协议解析模块
|
||||
</description>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>com.muyu</groupId>-->
|
||||
<!-- <artifactId>cloud-common-kafka</artifactId>-->
|
||||
<!-- <version>3.6.3</version>-->
|
||||
<!-- </dependency>-->
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>com.muyu.server</groupId>
|
||||
<artifactId>saas-server</artifactId>
|
||||
<version>3.6.3</version>
|
||||
</dependency>
|
||||
|
||||
<!-- SpringCloud Alibaba Nacos -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- SpringCloud Alibaba Nacos Config -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- SpringCloud Alibaba Sentinel -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- SpringBoot Actuator -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Mysql Connector -->
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- MuYu Common DataSource -->
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-datasource</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- MuYu Common DataScope -->
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-datascope</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- MuYu Common Log -->
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-log</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 接口模块 -->
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-api-doc</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- XllJob定时任务 -->
|
||||
<dependency>
|
||||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-xxl</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>redis.clients</groupId>
|
||||
<artifactId>jedis</artifactId> <!-- or lettuce -->
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,19 @@
|
|||
package com.muyu.template;
|
||||
import com.muyu.common.security.annotation.EnableCustomConfig;
|
||||
import com.muyu.common.security.annotation.EnableMyFeignClients;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
/**
|
||||
* @author liuxinyue
|
||||
* @Package:com.muyu.template
|
||||
* @name:CloudTemplateApplication
|
||||
* @Date:2024/9/30 10:36
|
||||
*/
|
||||
@EnableCustomConfig
|
||||
@EnableMyFeignClients
|
||||
@SpringBootApplication
|
||||
public class CloudTemplateApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(CloudTemplateApplication.class, args);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,170 @@
|
|||
package com.muyu.template.config;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.muyu.cache.MessageTemplateCacheService;
|
||||
import com.muyu.cache.MessageTemplateTypeCacheService;
|
||||
import com.muyu.cache.SysCarCacheService;
|
||||
import com.muyu.cache.TemplateCacheService;
|
||||
import com.muyu.common.domain.MessageTemplate;
|
||||
import com.muyu.common.domain.MessageTemplateType;
|
||||
import com.muyu.common.domain.SysCar;
|
||||
import com.muyu.common.domain.Template;
|
||||
import com.muyu.common.domain.resp.SysCarVo;
|
||||
import com.muyu.common.redis.service.RedisService;
|
||||
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.apache.kafka.clients.producer.KafkaProducer;
|
||||
import org.apache.kafka.clients.producer.ProducerRecord;
|
||||
import org.eclipse.paho.client.mqttv3.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
/**
|
||||
* @author liuxinyue
|
||||
* @Package:com.muyu.mqtt.configure
|
||||
* @Project:cloud-server
|
||||
* @name:MqttConfigure
|
||||
* @Date:2024/9/28 16:10
|
||||
*/
|
||||
@Log4j2
|
||||
@Component
|
||||
public class MqttConfigure {
|
||||
|
||||
@Autowired
|
||||
private RedisService redisService;
|
||||
|
||||
@Autowired
|
||||
private SysCarCacheService service;
|
||||
|
||||
@Autowired
|
||||
private MessageTemplateCacheService messageTemplateCacheService;
|
||||
|
||||
@Autowired
|
||||
private MessageTemplateTypeCacheService messageTemplateTypeCacheService;
|
||||
|
||||
@Autowired
|
||||
private KafkaProducer kafkaProducer;
|
||||
|
||||
@PostConstruct
|
||||
public void MQTTMonitoring(){
|
||||
String topic = "car";
|
||||
int qos = 2;
|
||||
String broker = "tcp://47.101.53.251:1883";
|
||||
String clientId = "lxy";
|
||||
try {
|
||||
MqttClient sampleClient = new MqttClient(broker, clientId);
|
||||
MqttConnectOptions connOpts = new MqttConnectOptions();
|
||||
//是否清空session
|
||||
connOpts.setCleanSession(false);
|
||||
log.info("Connecting to broker: " + broker);
|
||||
//连接
|
||||
sampleClient.connect(connOpts);
|
||||
sampleClient.subscribe(topic,0);
|
||||
sampleClient.setCallback(new MqttCallback() {
|
||||
//连接丢失(报错)
|
||||
@Override
|
||||
public void connectionLost(Throwable throwable) {
|
||||
log.error("error:"+throwable.getMessage());
|
||||
}
|
||||
//消息已经接收到
|
||||
@Override
|
||||
public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
|
||||
// 将MQTT消息转换为字符串
|
||||
String messageContent = new String(mqttMessage.getPayload());
|
||||
// 解析JSON字符串
|
||||
JSONObject jsonObject = new JSONObject(messageContent);
|
||||
// 从JSON对象中获取"msg"字段的值
|
||||
String msgValue = jsonObject.getStr("msg");
|
||||
log.info("接收到的报文为:"+msgValue);
|
||||
messageParsing(msgValue);
|
||||
}
|
||||
//交付完成
|
||||
@Override
|
||||
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
|
||||
|
||||
}
|
||||
});
|
||||
} catch(MqttException me) {
|
||||
System.out.println("reason "+me.getReasonCode());
|
||||
System.out.println("msg "+me.getMessage());
|
||||
System.out.println("loc "+me.getLocalizedMessage());
|
||||
System.out.println("cause "+me.getCause());
|
||||
System.out.println("excep "+me);
|
||||
me.printStackTrace();
|
||||
}
|
||||
}
|
||||
public JSONObject messageParsing(String templateMessage) {
|
||||
//给一个JSON对象
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
//先截取出VIN码 然后根据VIN码查询这个车属于什么类型
|
||||
if (templateMessage.length() < 18) {
|
||||
throw new RuntimeException("The vehicle message is incorrect");
|
||||
}
|
||||
//将 templateMessage 按空格切割成多个字符串,得到一个字符串数组 hexArray。接着,
|
||||
// 将每个十六进制字符串转换成十进制整数,然后再转换为字符,最后将所有字符拼接成一个完整的字符串 result。
|
||||
String[] hexArray = templateMessage.split(" ");
|
||||
StringBuilder result = new StringBuilder();
|
||||
for (String hex : hexArray) {
|
||||
int decimal = Integer.parseInt(hex, 16);
|
||||
result.append((char) decimal);
|
||||
}
|
||||
//取出VIN码
|
||||
String carVin = result.substring(1, 18 );
|
||||
log.info("carVin码为:" + carVin);
|
||||
//根据VIN码获取车辆信息
|
||||
SysCar carByVin = null;
|
||||
List<SysCarVo> carList = service.get("carList");
|
||||
if(carList==null){
|
||||
throw new RuntimeException("Redis未获取到车辆数据!!!");
|
||||
}else{
|
||||
// 使用 HashMap 存储车信息,以 VIN 作为键
|
||||
Map<String, SysCarVo> carMap = carList.stream()
|
||||
.collect(Collectors.toMap(SysCarVo::getCarVin, Function.identity()));
|
||||
carByVin = carMap.get(carVin);
|
||||
}
|
||||
|
||||
log.info("车辆信息为:" + carByVin);
|
||||
//对应车辆所对应的报文模版
|
||||
Integer templateId = carByVin.getTemplateId();
|
||||
List<MessageTemplateType> messageTemplateTypes=null;
|
||||
String redisKey = "messageTemplateTypeList";
|
||||
//key存在
|
||||
Boolean b = redisService.hasKey(redisKey);
|
||||
if (b) {
|
||||
messageTemplateTypes = messageTemplateTypeCacheService.get(redisKey);
|
||||
}else{
|
||||
throw new RuntimeException("请先将配置存入Redis!!!");
|
||||
}
|
||||
//将模版里面有的配置进行循环
|
||||
for (MessageTemplateType messageTemplateType : messageTemplateTypes) {
|
||||
//开始位置
|
||||
Integer startIndex = messageTemplateType.getStartIndex() - 1;
|
||||
//结束位置
|
||||
Integer endIndex = messageTemplateType.getEndIndex();
|
||||
//将每个解析后的字段都存入到JSON对象中
|
||||
jsonObject.put(messageTemplateType.getMessageField(), result.substring(startIndex, endIndex));
|
||||
}
|
||||
log.info("解析后的报文是:" + jsonObject);
|
||||
sendKafka(jsonObject);
|
||||
log.info("发送kafka成功");
|
||||
return jsonObject;
|
||||
}
|
||||
//kafka发送消息
|
||||
public void sendKafka(JSONObject jsonObject){
|
||||
ProducerRecord<String, String> stringStringProducerRecord = new ProducerRecord<>("four_car", jsonObject.toString());
|
||||
kafkaProducer.send(stringStringProducerRecord);
|
||||
log.info("kafka发送成功");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,135 @@
|
|||
package com.muyu.template;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import com.muyu.cache.MessageTemplateTypeCacheService;
|
||||
import com.muyu.cache.SysCarCacheService;
|
||||
import com.muyu.cache.WarnRuleCacheService;
|
||||
import com.muyu.cache.WarnStrategyCacheService;
|
||||
import com.muyu.common.domain.MessageTemplateType;
|
||||
import com.muyu.common.domain.SysCar;
|
||||
import com.muyu.common.domain.resp.SysCarVo;
|
||||
import com.muyu.common.domain.resp.WarnRuleResp;
|
||||
import com.muyu.common.domain.resp.WarnStrategyResp;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import javax.annotation.Resource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
/**
|
||||
* @author liuxinyue
|
||||
* @Package:com.muyu.template
|
||||
* @name:test2
|
||||
* @Date:2024/10/6 10:34
|
||||
*/
|
||||
@Log4j2
|
||||
public class test {
|
||||
private static int DURATION_SECONDS = 5;
|
||||
private static List<JSONObject> receivedStrings = new ArrayList<>();
|
||||
private static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
|
||||
private static int elapsedSeconds = 0;
|
||||
private static String file="elapsed";
|
||||
private static List<MessageTemplateType> messageTemplateTypes=null;
|
||||
private static Long msgTypeId=null;
|
||||
//滑窗时间
|
||||
private static Long slideTime=null;
|
||||
//增长率
|
||||
private static Long slideFrequency=null;
|
||||
//预警策略
|
||||
@Resource
|
||||
private WarnStrategyCacheService warnStrategyCacheService;
|
||||
//车辆
|
||||
@Resource
|
||||
private SysCarCacheService sysCarCacheService;
|
||||
//预警规则
|
||||
@Resource
|
||||
private WarnRuleCacheService warnRuleCacheService;
|
||||
//报文模版
|
||||
@Resource
|
||||
private MessageTemplateTypeCacheService messageTemplateTypeCacheService;
|
||||
|
||||
public void main(String[] args) {
|
||||
//协议解析:每秒穿过来一个JSONObject jsonObject; 添加进receivedStrings
|
||||
//根据这个车辆VIN查询出他对应的车辆类型
|
||||
String carVin=null;
|
||||
//报文模版的ID
|
||||
Integer templateId=null;
|
||||
for (JSONObject receivedString : receivedStrings) {
|
||||
carVin = receivedString.getStr("carVin");
|
||||
}
|
||||
SysCar carByVin = null;
|
||||
List<SysCarVo> carVoList = sysCarCacheService.get(sysCarCacheService.keyPre());
|
||||
Map<String, SysCarVo> carMap = carVoList.stream()
|
||||
.collect(Collectors.toMap(SysCarVo::getCarVin, Function.identity()));
|
||||
//获取到了这个车辆的信息
|
||||
carByVin = carMap.get(carVin);
|
||||
//获取到这辆车绑定的报文模版
|
||||
templateId=carByVin.getTemplateId();
|
||||
//这个是这辆车对应的所有策略
|
||||
List<WarnStrategyResp> carWithWarnStrategyList=null;
|
||||
List<WarnStrategyResp> warnStrategyResps = warnStrategyCacheService.get(warnStrategyCacheService.keyPre());
|
||||
for (WarnStrategyResp warnStrategyResp : warnStrategyResps) {
|
||||
if(warnStrategyResp.getCarTypeId()==carByVin.getCarTypeId()){
|
||||
carWithWarnStrategyList.add(warnStrategyResp);
|
||||
}
|
||||
}
|
||||
//该车对应的所有预警规则
|
||||
List<WarnRuleResp> warnRuleResp=null;
|
||||
List<WarnRuleResp> warnRuleResps = warnRuleCacheService.get(warnRuleCacheService.keyPre());
|
||||
for (WarnStrategyResp warnStrategyResp : carWithWarnStrategyList) {
|
||||
for (WarnRuleResp ruleResp : warnRuleResps) {
|
||||
if(warnStrategyResp.getId().equals(ruleResp.getStrategyId())){
|
||||
warnRuleResp.add(ruleResp);
|
||||
}
|
||||
}
|
||||
}
|
||||
//报文模版
|
||||
messageTemplateTypes = messageTemplateTypeCacheService.get(messageTemplateTypeCacheService.keyPre());
|
||||
for (WarnRuleResp ruleResp : warnRuleResp) {
|
||||
//每一个规则他绑定了报文模版里面对应的一个配置 比如:电池,或者车速
|
||||
msgTypeId = ruleResp.getMsgTypeId();
|
||||
//将规则中对应的滑窗时间赋值为DURATION_SECONDS
|
||||
DURATION_SECONDS = Math.toIntExact(ruleResp.getSlideTime());
|
||||
slideFrequency = ruleResp.getSlideFrequency();
|
||||
}
|
||||
// 定义一个任务,每秒执行一次
|
||||
Runnable task = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// 清理超过的数据
|
||||
cleanUpOldStrings();
|
||||
// 检查超速条件
|
||||
checkForSpeeding();
|
||||
}
|
||||
};
|
||||
// 每隔1秒执行一次任务
|
||||
scheduler.scheduleAtFixedRate(task, 0, 1, TimeUnit.SECONDS);
|
||||
}
|
||||
// 清理超过60秒的数据
|
||||
private static void cleanUpOldStrings() {
|
||||
long currentTime = System.currentTimeMillis();
|
||||
receivedStrings.removeIf(jsonObject ->
|
||||
currentTime - jsonObject.getLong("time") > TimeUnit.SECONDS.toMillis(DURATION_SECONDS)
|
||||
);
|
||||
}
|
||||
// 检查是否有超速情况
|
||||
private static void checkForSpeeding() {
|
||||
if (receivedStrings.size() < 2) return; // 如果数据不足,直接返回
|
||||
for (int i = 0; i < receivedStrings.size(); i++) {
|
||||
JSONObject current = receivedStrings.get(i);
|
||||
JSONObject next = receivedStrings.get(i + 1);
|
||||
for (MessageTemplateType messageTemplateType : messageTemplateTypes) {
|
||||
if(messageTemplateType.getMessageTemplateTypeId().equals(msgTypeId)){
|
||||
Short currentElapsed = current.getShort(messageTemplateType.getMessageField());
|
||||
Short nextElapsed = next.getShort(messageTemplateType.getMessageField());
|
||||
if (nextElapsed > currentElapsed + slideFrequency) {
|
||||
log.info("出错啦,出错啦,您的"+messageTemplateType.getMessageField()+"不正常,请检查!!!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
Spring Boot Version: ${spring-boot.version}
|
||||
Spring Application Name: ${spring.application.name}
|
|
@ -0,0 +1,62 @@
|
|||
# Tomcat
|
||||
server:
|
||||
port: 15277
|
||||
|
||||
# nacos线上地址
|
||||
nacos:
|
||||
addr: 47.101.53.251:8848
|
||||
user-name: nacos
|
||||
password: nacos
|
||||
namespace: lxy
|
||||
# SPRING_AMQP_DESERIALIZATION_TRUST_ALL=true spring.amqp.deserialization.trust.all
|
||||
# Spring
|
||||
spring:
|
||||
mvc:
|
||||
pathmatch:
|
||||
matching-strategy: ant_path_matcher
|
||||
amqp:
|
||||
deserialization:
|
||||
trust:
|
||||
all: true
|
||||
main:
|
||||
allow-bean-definition-overriding: true
|
||||
application:
|
||||
# 应用名称
|
||||
name: cloud-template
|
||||
profiles:
|
||||
# 环境配置
|
||||
active: dev
|
||||
cloud:
|
||||
nacos:
|
||||
discovery:
|
||||
# 服务注册地址
|
||||
server-addr: ${nacos.addr}
|
||||
# nacos用户名
|
||||
username: ${nacos.user-name}
|
||||
# nacos密码
|
||||
password: ${nacos.password}
|
||||
# 命名空间
|
||||
namespace: ${nacos.namespace}
|
||||
config:
|
||||
# 服务注册地址
|
||||
server-addr: ${nacos.addr}
|
||||
# nacos用户名
|
||||
username: ${nacos.user-name}
|
||||
# nacos密码
|
||||
password: ${nacos.password}
|
||||
# 命名空间
|
||||
namespace: ${nacos.namespace}
|
||||
# 配置文件格式
|
||||
file-extension: yml
|
||||
# 共享配置
|
||||
shared-configs:
|
||||
# 系统共享配置
|
||||
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
||||
# 系统环境Config共享配置
|
||||
- application-config-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
||||
# xxl-job 配置文件
|
||||
- application-xxl-config-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
|
||||
|
||||
logging:
|
||||
level:
|
||||
com.muyu.system.mapper: DEBUG
|
|
@ -0,0 +1,74 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration scan="true" scanPeriod="60 seconds" debug="false">
|
||||
<!-- 日志存放路径 -->
|
||||
<property name="log.path" value="logs/cloud-saas"/>
|
||||
<!-- 日志输出格式 -->
|
||||
<property name="log.pattern" value="%d{HH:mm:ss.SSS} [%thread] %-5level %logger{20} - [%method,%line] - %msg%n"/>
|
||||
|
||||
<!-- 控制台输出 -->
|
||||
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>${log.pattern}</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<!-- 系统日志输出 -->
|
||||
<appender name="file_info" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<file>${log.path}/info.log</file>
|
||||
<!-- 循环政策:基于时间创建日志文件 -->
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<!-- 日志文件名格式 -->
|
||||
<fileNamePattern>${log.path}/info.%d{yyyy-MM-dd}.log</fileNamePattern>
|
||||
<!-- 日志最大的历史 60天 -->
|
||||
<maxHistory>60</maxHistory>
|
||||
</rollingPolicy>
|
||||
<encoder>
|
||||
<pattern>${log.pattern}</pattern>
|
||||
</encoder>
|
||||
<filter class="ch.qos.logback.classic.filter.LevelFilter">
|
||||
<!-- 过滤的级别 -->
|
||||
<level>INFO</level>
|
||||
<!-- 匹配时的操作:接收(记录) -->
|
||||
<onMatch>ACCEPT</onMatch>
|
||||
<!-- 不匹配时的操作:拒绝(不记录) -->
|
||||
<onMismatch>DENY</onMismatch>
|
||||
</filter>
|
||||
</appender>
|
||||
|
||||
<appender name="file_error" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<file>${log.path}/error.log</file>
|
||||
<!-- 循环政策:基于时间创建日志文件 -->
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<!-- 日志文件名格式 -->
|
||||
<fileNamePattern>${log.path}/error.%d{yyyy-MM-dd}.log</fileNamePattern>
|
||||
<!-- 日志最大的历史 60天 -->
|
||||
<maxHistory>60</maxHistory>
|
||||
</rollingPolicy>
|
||||
<encoder>
|
||||
<pattern>${log.pattern}</pattern>
|
||||
</encoder>
|
||||
<filter class="ch.qos.logback.classic.filter.LevelFilter">
|
||||
<!-- 过滤的级别 -->
|
||||
<level>ERROR</level>
|
||||
<!-- 匹配时的操作:接收(记录) -->
|
||||
<onMatch>ACCEPT</onMatch>
|
||||
<!-- 不匹配时的操作:拒绝(不记录) -->
|
||||
<onMismatch>DENY</onMismatch>
|
||||
</filter>
|
||||
</appender>
|
||||
|
||||
<!-- 系统模块日志级别控制 -->
|
||||
<logger name="com.muyu" level="info"/>
|
||||
<!-- Spring日志级别控制 -->
|
||||
<logger name="org.springframework" level="warn"/>
|
||||
|
||||
<root level="info">
|
||||
<appender-ref ref="console"/>
|
||||
</root>
|
||||
|
||||
<!--系统操作日志-->
|
||||
<root level="info">
|
||||
<appender-ref ref="file_info"/>
|
||||
<appender-ref ref="file_error"/>
|
||||
</root>
|
||||
</configuration>
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue