指令下发优化

This commit is contained in:
qiminbao
2025-09-12 14:38:49 +08:00
parent 8272523126
commit 12442fde4a
8 changed files with 85 additions and 76 deletions
+9
View File
@@ -92,6 +92,15 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId> <artifactId>spring-boot-maven-plugin</artifactId>
</plugin> </plugin>
<!-- 打包跳过测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins> </plugins>
</build> </build>
@@ -1,6 +1,6 @@
package com.tongran.agent.server.controller; package com.tongran.agent.server.controller;
import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject;
import com.tongran.agent.server.netty.model.Message; import com.tongran.agent.server.netty.model.Message;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
@@ -15,17 +15,13 @@ public class TcpController {
@Resource @Resource
private TcpService tcpService; private TcpService tcpService;
@PostMapping("/create")
public void create(@Validated @RequestBody Message dto) {
tcpService.create(dto.getClientId());
}
@PostMapping("/send") @PostMapping("/send")
public void sendMessage(@Validated @RequestBody Message dto) { public void sendMessage(@Validated @RequestBody Message dto) {
String json = JSON.toJSONString(dto); JSONObject jsonObject = new JSONObject();
// ScriptPolicy policy = JSON.parseObject(dto.getData(), ScriptPolicy.class); jsonObject.put("clientId", dto.getClientId());
// System.out.println(policy.getFiles().get(0).getFileData()); jsonObject.put("dataType", dto.getDataType());
tcpService.sendMessage(json); jsonObject.put("data", dto.getData());
tcpService.sendMessage(jsonObject.toString());
} }
@@ -10,6 +10,8 @@ import com.tongran.agent.server.netty.annotation.AgentDispatcher;
import com.tongran.agent.server.netty.basics.AgentDispatcherManager; import com.tongran.agent.server.netty.basics.AgentDispatcherManager;
import com.tongran.agent.server.netty.basics.AgentHandler; import com.tongran.agent.server.netty.basics.AgentHandler;
import com.tongran.agent.server.netty.model.Message; import com.tongran.agent.server.netty.model.Message;
import com.tongran.agent.server.rockermq.AgentProducer;
import com.tongran.agent.server.rockermq.model.MqMsg;
import com.tongran.agent.server.utils.AssertLog; import com.tongran.agent.server.utils.AssertLog;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@@ -29,6 +31,9 @@ public class TcpService {
@Resource @Resource
private MultiTargetNettyClient client; private MultiTargetNettyClient client;
@Resource
private AgentProducer agentProducer;
public TcpService() { public TcpService() {
this.sessionManager = SessionManager.getInstance(); this.sessionManager = SessionManager.getInstance();
} }
@@ -60,58 +65,50 @@ public class TcpService {
AssertLog.info("consumer==> received down message: {}", message); AssertLog.info("consumer==> received down message: {}", message);
try { try {
Message dto = JSON.parseObject(message, Message.class); Message dto = JSON.parseObject(message, Message.class);
if(Objects.nonNull(dto)){
if(StringUtils.equals(dto.getDataType(), MsgEnum.注册.getValue())){ if(StringUtils.equals(dto.getDataType(), MsgEnum.注册.getValue())){
JSONObject jsonObject = JSONObject.parseObject(dto.getData()); JSONObject jsonObject = JSONObject.parseObject(dto.getData());
String clientIp = ""; String clientIp = "";
int port = 0; int clientPort = 0;
String switchBoard = "";
long timestamp = 0; long timestamp = 0;
if(jsonObject.containsKey("clientIp")){ if(jsonObject.containsKey("clientIp")){
clientIp = jsonObject.getString("clientIp"); clientIp = jsonObject.getString("clientIp");
} }
if(jsonObject.containsKey("port")){ if(jsonObject.containsKey("clientPort")){
port = jsonObject.getInteger("port"); clientPort = jsonObject.getInteger("clientPort");
}
if(jsonObject.containsKey("switchBoard")){
switchBoard = jsonObject.getString("switchBoard");
} }
if(jsonObject.containsKey("timestamp")){ if(jsonObject.containsKey("timestamp")){
timestamp = jsonObject.getLongValue("timestamp"); timestamp = jsonObject.getLongValue("timestamp");
} }
if(StringUtils.isBlank(clientIp) || port == 0){ if(StringUtils.isBlank(clientIp) || clientPort == 0){
//返回提醒ip 端口 空 //返回提醒ip 端口 空
JSONObject json = new JSONObject();
json.put("clientId",dto.getClientId());
json.put("dataType",MsgEnum.注册应答.getValue());
JSONObject dataJson = new JSONObject();
dataJson.put("resCode",0);
dataJson.put("resMag","客户端IP、端口不能为空");
dataJson.put("timestamp", System.currentTimeMillis());
json.put("data",dataJson.toString());
MqMsg mqMsg = MqMsg.builder().topic("tongran_agent_down").content(json.toString()).build();
agentProducer.asyncSend(mqMsg);
return; return;
} }
boolean success = client.createConnection(dto.getClientId(), clientIp, port, 5); boolean success = client.createConnection(dto.getClientId(), clientIp, clientPort, 5);
if(success){ if(success){
String community = "";
String ip = "";
int switchPort = 0;
String oids = "";
String params = "";
if(jsonObject.containsKey("switchBoard")){
String switchBoard = jsonObject.getString("switchBoard");
jsonObject = JSONObject.parseObject(switchBoard);
community = jsonObject.getString("community");
ip = jsonObject.getString("ip");
switchPort = jsonObject.getInteger("port");
oids = jsonObject.getString("oids");
params = jsonObject.getString("params");
}
//连接成功 //连接成功
// 连接建立后可以发送登录认证消息 // 连接建立后可以发送登录认证消息
JSONObject object = new JSONObject(); JSONObject object = new JSONObject();
object.put("clientId",dto.getClientId());
object.put("timestamp", timestamp); object.put("timestamp", timestamp);
JSONObject switchBoard = new JSONObject(); if(StringUtils.isNotBlank(switchBoard)){
switchBoard.put("community", community); object.put("switchBoard",switchBoard);
switchBoard.put("ip", ip); }
switchBoard.put("port", switchPort);
switchBoard.put("oids", oids);
switchBoard.put("params", params);
object.put("switchBoard",switchBoard.toString());
Message msg = Message.builder().clientId(dto.getClientId()).dataType(MsgEnum.注册.getValue()).data(object.toString()).build(); Message msg = Message.builder().clientId(dto.getClientId()).dataType(MsgEnum.注册.getValue()).data(object.toString()).build();
// 将对象转为 JSON 字符串 // 将对象转为 JSON 字符串
client.sendMessages(dto.getClientId(), msg); client.sendMessages(dto.getClientId(), msg);
}else{
//连接失败
} }
} else { } else {
AgentHandler msgHandler = agentDispatcherManager.getHandler(dto.getDataType() + "&" + AgentDispatcher.VersionEnum.V1.value); AgentHandler msgHandler = agentDispatcherManager.getHandler(dto.getDataType() + "&" + AgentDispatcher.VersionEnum.V1.value);
@@ -123,7 +120,6 @@ public class TcpService {
} }
} }
} }
}
} catch (Exception e) { } catch (Exception e) {
AssertLog.error("=====rocketmq-exception{}=====" + e.getMessage()); AssertLog.error("=====rocketmq-exception{}=====" + e.getMessage());
} }
@@ -22,6 +22,8 @@ public enum MsgEnum {
心跳上报("HEARTBEAT"), 心跳上报("HEARTBEAT"),
心跳上报应答("HEARTBEAT_RSP"),
CPU上报("CPU"), CPU上报("CPU"),
磁盘上报("DISK"), 磁盘上报("DISK"),
@@ -81,7 +81,9 @@ public class AgentEndpoint {
jsonObject.put("data", data); jsonObject.put("data", data);
MqMsg mqMsg = MqMsg.builder().topic(agentMqConfig.getAgentTopic()).content(jsonObject.toString()).build(); MqMsg mqMsg = MqMsg.builder().topic(agentMqConfig.getAgentTopic()).content(jsonObject.toString()).build();
agentProducer.asyncSend(mqMsg); agentProducer.asyncSend(mqMsg);
return null; JSONObject json = new JSONObject();
json.put("resCode",1);
return UpMsgResponse.builder().clientId(clientId).dataType(MsgEnum.心跳上报应答.getValue()).content(json.toString()).build();
} }
} }
@@ -121,8 +121,8 @@ public class AgentDecoderHandler extends ChannelInboundHandlerAdapter {
if(Objects.nonNull(agentDispatcherManager)){ if(Objects.nonNull(agentDispatcherManager)){
AgentHandler msgHandler = agentDispatcherManager.getHandler(dataType + "&" + AgentDispatcher.VersionEnum.V1.value); AgentHandler msgHandler = agentDispatcherManager.getHandler(dataType + "&" + AgentDispatcher.VersionEnum.V1.value);
if (ObjectUtil.isNotEmpty(msgHandler)) { if (ObjectUtil.isNotEmpty(msgHandler)) {
UpMsgResponse response = msgHandler.upHandle(data, clientId );
AssertLog.info("<<[up-after-handle]:clientId:{},type={},[handle-content]={}", clientId, dataType, data); AssertLog.info("<<[up-after-handle]:clientId:{},type={},[handle-content]={}", clientId, dataType, data);
UpMsgResponse response = msgHandler.upHandle(data, clientId );
if(Objects.nonNull(response)){ if(Objects.nonNull(response)){
Message agentMessage = Message.builder().build(); Message agentMessage = Message.builder().build();
agentMessage.setClientId(clientId); agentMessage.setClientId(clientId);
+7 -3
View File
@@ -1,7 +1,7 @@
server: server:
port: 9018 port: 7012
servlet: servlet:
context-path: /agent-server context-path: /tr-agent-server
# 接口文档配置 # 接口文档配置
knife4j: knife4j:
@@ -12,9 +12,13 @@ knife4j:
logging: logging:
file: file:
# path: /usr/local/tongran/logs # path: /usr/local/tongran/logs
path: /data/agent-server/logs path: /data/tr-agent-server/logs
# path: D:/job/agent-logs/logs # path: D:/job/agent-logs/logs
rocketmq:
name-server: 172.16.15.103:9876
enabled: true
netty: netty:
server: server:
# host: 172.16.15.103 # host: 172.16.15.103
+1 -1
View File
@@ -5,7 +5,7 @@ spring:
pathmatch: pathmatch:
matching-strategy: ant_path_matcher matching-strategy: ant_path_matcher
application: application:
name: agent-server name: tr-agent-server
version: 1.0 version: 1.0
web: web:
resources: resources: