优化交换机数据采集、脚本策略、agent更新优化、注册增加路由信息

This commit is contained in:
gaoyutao
2025-10-24 18:55:21 +08:00
parent 37eff51e48
commit 36362e2236
23 changed files with 263 additions and 98 deletions
@@ -209,7 +209,7 @@ public class RmResourceRegistrationController extends BaseController
@PostMapping("/bindBusinessPubulicIp")
public AjaxResult bindBusinessPubulicIp(@RequestBody RmResourceRegistration rmResourceRegistration)
{
int rows = rmResourceRegistrationService.bindBusinessPubulicIp(rmResourceRegistration);
int rows = rmResourceRegistrationService.bindBusinessPublicIp(rmResourceRegistration);
return toAjax(rows);
}
@@ -28,6 +28,8 @@ public class RmEpsTopologyManagement extends BaseEntity
/** 交换机硬件SN */
@Excel(name = "交换机硬件SN")
private String switchSn;
/** 交换机clientId */
private String clientId;
/** 接口名称 */
@Excel(name = "接口名称")
@@ -7,9 +7,11 @@ import java.time.Instant;
@Data
public class RspVo {
/** 状态码,0、失败;1、成功*/
private int resCode;
private Integer resCode;
/** 描述 */
private String resMag;
/** 路由 */
private String addRoute;
/** 时间戳 */
private long timestamp = Instant.now().getEpochSecond();
}
@@ -112,5 +112,5 @@ public interface IRmResourceRegistrationService
* @param rmResourceRegistration
* @return
*/
int bindBusinessPubulicIp(RmResourceRegistration rmResourceRegistration);
int bindBusinessPublicIp(RmResourceRegistration rmResourceRegistration);
}
@@ -9,10 +9,7 @@ import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.RemoteRocketMqService;
import com.ruoyi.system.api.domain.NetworkInfo;
import com.ruoyi.system.api.domain.RmNetworkInterfaceRemote;
import com.ruoyi.system.api.domain.RmRegisterMsgRemote;
import com.ruoyi.system.api.domain.RmResourceRegistrationRemote;
import com.ruoyi.system.api.domain.*;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.vo.MessageVo;
import com.ruoyi.system.domain.vo.ResourceVo;
@@ -25,10 +22,7 @@ import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
/**
@@ -530,6 +524,24 @@ public class RmResourceRegistrationServiceImpl implements IRmResourceRegistratio
content.append("流量网口设置为").append(networkInfo.getName());
recordAddData.setChangeContent(content.toString());
epsMethodChangeRecordMapper.insertEpsMethodChangeRecord(recordAddData);
// 设置路由信息
RouteMsg routeMsg = new RouteMsg();
routeMsg.setName(networkInfo.getName());
routeMsg.setGateway(networkInfo.getGateway());
// 构建注册应答信息
MessageVo messageVo = new MessageVo();
messageVo.setClientId(registerMsg.getClientId());
messageVo.setDataType(MsgEnum.注册应答.getValue());
RspVo rspVo = new RspVo();
rspVo.setResCode(1);
rspVo.setResMag("注册成功");
rspVo.setAddRoute(JSONObject.toJSONString(routeMsg));
messageVo.setData(JSONObject.toJSONString(rspVo));
remoteRocketMqService.sendAsyncProducerMessage(
"tr_agent_down", "", "regist_rsp", JSONObject.toJSONString(messageVo), SecurityConstants.INNER
);
// 第一次注册
return 2;
}else{
insertData.setClientId(registerMsg.getClientId());
insertData.setHardwareSn(registerMsg.getSn());
@@ -539,32 +551,8 @@ public class RmResourceRegistrationServiceImpl implements IRmResourceRegistratio
insertData.setCreateTime(createTime);
rmResourceRegistrationMapper.insertRmResourceRegistration(insertData);
}
// 构建注册应答信息
MessageVo messageVo = new MessageVo();
messageVo.setClientId(registerMsg.getClientId());
messageVo.setDataType(MsgEnum.注册应答.getValue());
RspVo rspVo = new RspVo();
rspVo.setResCode(1);
rspVo.setResMag("注册成功");
messageVo.setData(JSONObject.toJSONString(rspVo));
remoteRocketMqService.sendAsyncProducerMessage(
"tr_agent_down", "", "", JSONObject.toJSONString(messageVo), SecurityConstants.INNER
);
// 第一次注册
return 2;
}catch (Exception e){
log.error("注册服务器失败:{}",e.getMessage());
// 构建注册应答信息
MessageVo messageVo = new MessageVo();
messageVo.setClientId(registerMsg.getClientId());
messageVo.setDataType(MsgEnum.注册应答.getValue());
RspVo rspVo = new RspVo();
rspVo.setResCode(1);
rspVo.setResMag("平台错误信息:" + e.getMessage());
messageVo.setData(JSONObject.toJSONString(rspVo));
remoteRocketMqService.sendAsyncProducerMessage(
"tr_agent_down", "", "", JSONObject.toJSONString(messageVo), SecurityConstants.INNER
);
return 0;
}
}
@@ -601,34 +589,78 @@ public class RmResourceRegistrationServiceImpl implements IRmResourceRegistratio
}
@Override
public int bindBusinessPubulicIp(RmResourceRegistration rmResourceRegistration) {
List<Map> bindMsgMap = rmResourceRegistration.getBindNetworkMsg();
for (Map map : bindMsgMap) {
// id
Long id = (Long) map.get("id");
String status = map.get("status").toString();
String interfaceName = map.get("interfaceName").toString();
// 绑定ip
RmNetworkInterfaceRemote rmNetworkInterfaceRemote = new RmNetworkInterfaceRemote();
rmNetworkInterfaceRemote.setId(id);
rmNetworkInterfaceRemote.setBindIp(status);
remoteRocketMqService.bindPublicIp(rmNetworkInterfaceRemote, SecurityConstants.INNER);
// 添加业务变更记录
EpsMethodChangeRecord recordAddData = new EpsMethodChangeRecord();
recordAddData.setClientId(rmResourceRegistration.getClientId());
recordAddData.setTrafficPort(interfaceName);
recordAddData.setUpdateTime(DateUtils.getNowDate());
recordAddData.setUpdateBy(SecurityUtils.getUsername());
recordAddData.setCreatBy(SecurityUtils.getUsername());
StringBuilder content = new StringBuilder();
content.append("流量网口设置为").append(interfaceName);
recordAddData.setChangeContent(content.toString());
epsMethodChangeRecordMapper.insertEpsMethodChangeRecord(recordAddData);
public int bindBusinessPublicIp(RmResourceRegistration rmResourceRegistration) {
// 参数校验
if (rmResourceRegistration == null || StringUtils.isEmpty(rmResourceRegistration.getClientId())) {
throw new IllegalArgumentException("参数不合法");
}
// 修改多公网IP状态为已绑定
String clientId = rmResourceRegistration.getClientId();
String username = SecurityUtils.getUsername();
Date now = DateUtils.getNowDate();
// 1. 处理IP绑定和记录
rmResourceRegistration.getBindNetworkMsg().forEach(map -> {
// 绑定IP
RmNetworkInterfaceRemote network = new RmNetworkInterfaceRemote();
network.setId((Long) map.get("id"));
network.setBindIp(map.get("status").toString());
remoteRocketMqService.bindPublicIp(network, SecurityConstants.INNER);
// 添加变更记录
EpsMethodChangeRecord record = new EpsMethodChangeRecord();
record.setClientId(clientId);
record.setTrafficPort(map.get("interfaceName").toString());
record.setUpdateTime(now);
record.setUpdateBy(username);
record.setCreatBy(username);
record.setChangeContent("流量网口设置为" + map.get("interfaceName"));
epsMethodChangeRecordMapper.insertEpsMethodChangeRecord(record);
});
// 2. 更新注册状态
RmResourceRegistration updateData = new RmResourceRegistration();
updateData.setId(rmResourceRegistration.getId());
updateData.setMultiPublicIpStatus("1");
// 处理未注册情况
RmResourceRegistration isRegist = rmResourceRegistrationMapper.selectRmResourceRegistrationById(rmResourceRegistration.getId());
if (isRegist != null && "0".equals(isRegist.getRegistrationStatus())) {
RmNetworkInterfaceRemote queryParam = new RmNetworkInterfaceRemote();
queryParam.setClientId(clientId);
Optional.ofNullable(remoteRocketMqService.getNetworkInterfaceList(queryParam, SecurityConstants.INNER))
.map(R::getData)
.flatMap(list -> list.stream()
.filter(ni -> "1".equals(ni.getBindIp()) || "3".equals(ni.getBindIp()))
.findFirst())
.ifPresent(network -> {
updateData.setRegistrationStatus("1");
// 发送注册响应
RouteMsg routeMsg = new RouteMsg();
routeMsg.setName(network.getInterfaceName());
routeMsg.setGateway(network.getGateway());
MessageVo messageVo = new MessageVo();
messageVo.setClientId(clientId);
messageVo.setDataType(MsgEnum.注册应答.getValue());
RspVo rspVo = new RspVo();
rspVo.setResCode(1);
rspVo.setResMag("注册成功");
rspVo.setAddRoute(JSONObject.toJSONString(routeMsg));
messageVo.setData(JSONObject.toJSONString(rspVo));
remoteRocketMqService.sendAsyncProducerMessage(
"tr_agent_down", "", "regist_rsp", JSONObject.toJSONString(messageVo), SecurityConstants.INNER
);
// 注册成功,下发优先级为0的策略
remoteRocketMqService.issueDefaultPolicyByClientId(clientId, SecurityConstants.INNER);
});
}
rmResourceRegistrationMapper.updateRmResourceRegistration(updateData);
return 1;
}
@@ -7,6 +7,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<resultMap type="RmEpsTopologyManagement" id="RmEpsTopologyManagementResult">
<result property="id" column="id" />
<result property="switchName" column="switch_name" />
<result property="clientId" column="client_Id" />
<result property="switchSn" column="switch_sn" />
<result property="interfaceName" column="interface_name" />
<result property="connectedDeviceType" column="connected_device_type" />
@@ -26,7 +27,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</resultMap>
<sql id="selectRmEpsTopologyManagementVo">
select id, switch_name, switch_sn, interface_name, connected_device_type, server_name, server_sn, server_port, create_time, update_time, creator_id, creator_name, updater_id, updater_name, switch_ip_address, peer_switch_name, peer_switch_interface, server_client_id from rm_eps_topology_management
select id, switch_name, client_id, switch_sn, interface_name, connected_device_type, server_name, server_sn, server_port, create_time, update_time, creator_id, creator_name, updater_id, updater_name, switch_ip_address, peer_switch_name, peer_switch_interface, server_client_id from rm_eps_topology_management
</sql>
<select id="selectRmEpsTopologyManagementList" parameterType="RmEpsTopologyManagement" resultMap="RmEpsTopologyManagementResult">
@@ -48,6 +49,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="peerSwitchName != null and peerSwitchName != ''"> and peer_switch_name like concat('%', #{peerSwitchName}, '%')</if>
<if test="peerSwitchInterface != null and peerSwitchInterface != ''"> and peer_switch_interface = #{peerSwitchInterface}</if>
<if test="serverClientId != null and serverClientId != ''"> and server_client_id = #{serverClientId}</if>
<if test="clientId != null and clientId != ''"> and client_id = #{clientId}</if>
</where>
</select>
@@ -60,6 +62,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
insert into rm_eps_topology_management
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="switchName != null">switch_name,</if>
<if test="clientId != null">client_id,</if>
<if test="switchSn != null">switch_sn,</if>
<if test="interfaceName != null">interface_name,</if>
<if test="connectedDeviceType != null">connected_device_type,</if>
@@ -79,6 +82,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="switchName != null">#{switchName},</if>
<if test="clientId != null">#{clientId},</if>
<if test="switchSn != null">#{switchSn},</if>
<if test="interfaceName != null">#{interfaceName},</if>
<if test="connectedDeviceType != null">#{connectedDeviceType},</if>
@@ -102,6 +106,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
update rm_eps_topology_management
<trim prefix="SET" suffixOverrides=",">
<if test="switchName != null">switch_name = #{switchName},</if>
<if test="clientId != null">client_id = #{clientId},</if>
<if test="switchSn != null">switch_sn = #{switchSn},</if>
<if test="interfaceName != null">interface_name = #{interfaceName},</if>
<if test="connectedDeviceType != null">connected_device_type = #{connectedDeviceType},</if>