feat: support non-LAN communication(server side)

This commit is contained in:
tjq 2023-07-15 22:22:38 +08:00
parent 7318fed73a
commit d3140d0501
2 changed files with 18 additions and 10 deletions

View File

@ -60,9 +60,11 @@ public class ServerElectionService {
final String currentServer = request.getCurrentServer(); final String currentServer = request.getCurrentServer();
// 如果是本机就不需要查数据库那么复杂的操作了直接返回成功 // 如果是本机就不需要查数据库那么复杂的操作了直接返回成功
Optional<ProtocolInfo> localProtocolInfoOpt = Optional.ofNullable(transportService.allProtocols().get(request.getProtocol())); Optional<ProtocolInfo> localProtocolInfoOpt = Optional.ofNullable(transportService.allProtocols().get(request.getProtocol()));
if (localProtocolInfoOpt.isPresent() && localProtocolInfoOpt.get().getAddress().equals(currentServer)) { if (localProtocolInfoOpt.isPresent()) {
log.debug("[ServerElectionService] this server[{}] is worker's current server, skip check", currentServer); if (localProtocolInfoOpt.get().getExternalAddress().equals(currentServer) || localProtocolInfoOpt.get().getAddress().equals(currentServer)) {
return currentServer; log.info("[ServerElection] this server[{}] is worker[appId={}]'s current server, skip check", currentServer, request.getAppId());
return currentServer;
}
} }
} }
return getServer0(request); return getServer0(request);
@ -110,13 +112,13 @@ public class ServerElectionService {
// 篡位如果本机存在协议则作为Server调度该 worker // 篡位如果本机存在协议则作为Server调度该 worker
final ProtocolInfo targetProtocolInfo = transportService.allProtocols().get(protocol); final ProtocolInfo targetProtocolInfo = transportService.allProtocols().get(protocol);
if (targetProtocolInfo != null) { if (targetProtocolInfo != null) {
// 注意写入 AppInfoDO#currentServer 的永远是 default 地址仅在返回的时候特殊处理为协议地址 // 注意写入 AppInfoDO#currentServer 的永远是 default 绑定地址仅在返回的时候特殊处理为协议地址
appInfo.setCurrentServer(transportService.defaultProtocol().getAddress()); appInfo.setCurrentServer(transportService.defaultProtocol().getAddress());
appInfo.setGmtModified(new Date()); appInfo.setGmtModified(new Date());
appInfoRepository.saveAndFlush(appInfo); appInfoRepository.saveAndFlush(appInfo);
log.info("[ServerElection] this server({}) become the new server for app(appId={}).", appInfo.getCurrentServer(), appId); log.info("[ServerElection] this server({}) become the new server for app(appId={}).", appInfo.getCurrentServer(), appId);
return targetProtocolInfo.getAddress(); return targetProtocolInfo.getExternalAddress();
} }
}catch (Exception e) { }catch (Exception e) {
log.error("[ServerElection] write new server to db failed for app {}.", appName, e); log.error("[ServerElection] write new server to db failed for app {}.", appName, e);
@ -129,10 +131,10 @@ public class ServerElectionService {
/** /**
* 判断指定server是否存活 * 判断指定server是否存活
* @param serverAddress 需要检测的server地址 * @param serverAddress 需要检测的server地址绑定的内网地址
* @param downServerCache 缓存防止多次发送PING这个QPS其实还蛮爆表的... * @param downServerCache 缓存防止多次发送PING这个QPS其实还蛮爆表的...
* @param protocol 协议用于返回指定的地址 * @param protocol 协议用于返回指定的地址
* @return null or address * @return null or address外部地址
*/ */
private String activeAddress(String serverAddress, Set<String> downServerCache, String protocol) { private String activeAddress(String serverAddress, Set<String> downServerCache, String protocol) {
@ -156,9 +158,9 @@ public class ServerElectionService {
final JSONObject protocolInfo = JsonUtils.parseObject(response.getData(), JSONObject.class).getJSONObject(protocol); final JSONObject protocolInfo = JsonUtils.parseObject(response.getData(), JSONObject.class).getJSONObject(protocol);
if (protocolInfo != null) { if (protocolInfo != null) {
downServerCache.remove(serverAddress); downServerCache.remove(serverAddress);
final String protocolAddress = protocolInfo.toJavaObject(ProtocolInfo.class).getAddress(); ProtocolInfo remoteProtocol = protocolInfo.toJavaObject(ProtocolInfo.class);
log.info("[ServerElection] server[{}] is active, it will be the master, final protocol address={}", serverAddress, protocolAddress); log.info("[ServerElection] server[{}] is active, it will be the master, final protocol={}", serverAddress, remoteProtocol);
return protocolAddress; return remoteProtocol.getExternalAddress();
} else { } else {
log.warn("[ServerElection] server[{}] is active but don't have target protocol", serverAddress); log.warn("[ServerElection] server[{}] is active but don't have target protocol", serverAddress);
} }

View File

@ -30,6 +30,12 @@ public class ProtocolInfo {
private transient Transporter transporter; private transient Transporter transporter;
/**
* 序列化需要必须存在无参构造方法严禁删除
*/
public ProtocolInfo() {
}
public ProtocolInfo(String protocol, String host, int port, Transporter transporter) { public ProtocolInfo(String protocol, String host, int port, Transporter transporter) {
this.protocol = protocol; this.protocol = protocol;
this.transporter = transporter; this.transporter = transporter;