| | |
| | | import com.ard.work.component.netty.handler.MessageProtocolEncoder; |
| | | import com.ard.work.domain.cache.TyTcpServerCache; |
| | | import com.ard.work.domain.entity.ArdServerInfo; |
| | | import com.ard.work.service.IArdServerService; |
| | | import com.ard.work.service.IArdServerInfoService; |
| | | import io.netty.bootstrap.Bootstrap; |
| | | import io.netty.channel.*; |
| | | import io.netty.channel.nio.NioEventLoopGroup; |
| | |
| | | private final EventLoopGroup eventLoopGroup = new NioEventLoopGroup(); |
| | | |
| | | @Resource |
| | | private IArdServerService ardServerService; // ← 从数据库读取边缘服务器配置 |
| | | private IArdServerInfoService ardServerService; // ← 从数据库读取边缘服务器配置 |
| | | |
| | | @Resource |
| | | private ClientHandler clientHandler; |
| | |
| | | return bootstrap; |
| | | } |
| | | |
| | | /** 新增或重连服务器 */ |
| | | /** |
| | | * 新增或重连服务器 |
| | | */ |
| | | public void connectServer(ArdServerInfo serverInfo) { |
| | | String serverId = serverInfo.getId(); |
| | | String host = serverInfo.getHost(); |
| | |
| | | channels.put(serverId, session); |
| | | } |
| | | |
| | | /** 断开服务器连接 */ |
| | | /** |
| | | * 断开服务器连接 |
| | | */ |
| | | public void disconnectServer(String serverId) { |
| | | TcpClientSession session = channels.remove(serverId); |
| | | if (session != null && session.getChannelFuture() != null) { |
| | |
| | | @PostConstruct |
| | | public void initConnectServers() { |
| | | try { |
| | | var serverList = ardServerService.selectArdServerManagementList(new ArdServerInfo()); // ← 从数据库查询所有服务器配置 |
| | | var serverList = ardServerService.selectArdServerInfoList(new ArdServerInfo()); // ← 从数据库查询所有服务器配置 |
| | | if (serverList == null || serverList.isEmpty()) { |
| | | log.warn("未在数据库中找到边缘服务器配置,跳过连接初始化"); |
| | | return; |
| | |
| | | public void reconnectFailedServers() { |
| | | TyTcpServerCache.getTcpServerCacheMap().forEach((key, state) -> { |
| | | if (Boolean.FALSE.equals(state)) { // 连接失败的服务器 |
| | | ArdServerInfo serverInfo = ardServerService.selectArdServerManagementById(key); |
| | | ArdServerInfo serverInfo = ardServerService.selectArdServerInfoById(key); |
| | | if (serverInfo != null) { |
| | | connectServer(serverInfo); |
| | | } |
| | |
| | | session.getServerId(), session.getHost(), session.getPort(), timestamp); |
| | | }); |
| | | } |
| | | |
| | | } |