diff --git a/src/META-INF/application-template.xml b/src/META-INF/application-template.xml index f373f3946..9bb347876 100644 --- a/src/META-INF/application-template.xml +++ b/src/META-INF/application-template.xml @@ -19,10 +19,9 @@ serviceid1_name1 serviceid1_name2 serviceid2_name1 serviceid2_name2 --> diff --git a/src/org/redkale/boot/Application.java b/src/org/redkale/boot/Application.java index b72a7cf65..5483ff817 100644 --- a/src/org/redkale/boot/Application.java +++ b/src/org/redkale/boot/Application.java @@ -79,7 +79,7 @@ public final class Application { public static final String RESNAME_APP_NODEID = "APP_NODEID"; /** - * 当前进程节点的IP地址, 类型:InetAddress、String + * 当前进程节点的IP地址, 类型:InetSocketAddress、InetAddress、String */ public static final String RESNAME_APP_ADDR = "APP_ADDR"; @@ -112,7 +112,7 @@ public final class Application { final int nodeid; //本地IP地址 - final InetAddress localAddress; + final InetSocketAddress localAddress; //CacheSource 资源 final List cacheSources = new CopyOnWriteArrayList<>(); @@ -208,9 +208,12 @@ public final class Application { throw new RuntimeException(e); } String localaddr = config.getValue("address", "").trim(); - this.localAddress = localaddr.isEmpty() ? Utility.localInetAddress() : new InetSocketAddress(localaddr, config.getIntValue("port")).getAddress(); - this.resourceFactory.register(RESNAME_APP_ADDR, this.localAddress.getHostAddress()); - this.resourceFactory.register(RESNAME_APP_ADDR, InetAddress.class, this.localAddress); + InetAddress addr = localaddr.isEmpty() ? Utility.localInetAddress() : new InetSocketAddress(localaddr, config.getIntValue("port")).getAddress(); + this.localAddress = new InetSocketAddress(addr, config.getIntValue("port")); + this.resourceFactory.register(RESNAME_APP_ADDR, addr.getHostAddress()); + this.resourceFactory.register(RESNAME_APP_ADDR, InetAddress.class, addr); + this.resourceFactory.register(RESNAME_APP_ADDR, InetSocketAddress.class, this.localAddress); + { int nid = config.getIntValue("nodeid", 0); this.nodeid = nid; @@ -541,7 +544,7 @@ public final class Application { pidstr = "APP_PID = " + pid + "\r\n"; } catch (Throwable t) { } - logger.log(Level.INFO, pidstr + "APP_JAVA = " + System.getProperty("java.version") + "\r\n" + RESNAME_APP_NODEID + " = " + this.nodeid + "\r\n" + RESNAME_APP_ADDR + " = " + this.localAddress.getHostAddress() + "\r\n" + RESNAME_APP_HOME + " = " + homepath + "\r\n" + RESNAME_APP_CONF + " = " + confpath); + logger.log(Level.INFO, pidstr + "APP_JAVA = " + System.getProperty("java.version") + "\r\n" + RESNAME_APP_NODEID + " = " + this.nodeid + "\r\n" + RESNAME_APP_ADDR + " = " + this.localAddress.getHostString() + ":" + this.localAddress.getPort() + "\r\n" + RESNAME_APP_HOME + " = " + homepath + "\r\n" + RESNAME_APP_CONF + " = " + confpath); String lib = config.getValue("lib", "${APP_HOME}/libs/*").trim().replace("${APP_HOME}", homepath); lib = lib.isEmpty() ? confpath : (lib + ";" + confpath); Server.loadLib(classLoader, logger, lib); @@ -802,6 +805,9 @@ public final class Application { } public void start() throws Exception { + if (!singletonrun && this.clusterAgent != null) { + this.clusterAgent.register(this); + } final AnyValue[] entrys = config.getAnyValues("server"); CountDownLatch timecd = new CountDownLatch(entrys.length); final List sncps = new ArrayList<>(); @@ -1081,6 +1087,7 @@ public final class Application { } }); if (clusterAgent != null) { + clusterAgent.deregister(this); clusterAgent.destroy(clusterAgent.getConfig()); } if (this.messageAgents != null) { diff --git a/src/org/redkale/boot/NodeServer.java b/src/org/redkale/boot/NodeServer.java index 41bfe199f..e68599ed9 100644 --- a/src/org/redkale/boot/NodeServer.java +++ b/src/org/redkale/boot/NodeServer.java @@ -123,7 +123,7 @@ public abstract class NodeServer { this.serverConf = config == null ? AnyValue.create() : config; if (isSNCP()) { // SNCP协议 String host = this.serverConf.getValue("host", isWATCH() ? "127.0.0.1" : "0.0.0.0").replace("0.0.0.0", ""); - this.sncpAddress = new InetSocketAddress(host.isEmpty() ? application.localAddress.getHostAddress() : host, this.serverConf.getIntValue("port")); + this.sncpAddress = new InetSocketAddress(host.isEmpty() ? application.localAddress.getAddress().getHostAddress() : host, this.serverConf.getIntValue("port")); this.sncpGroup = application.sncpTransportFactory.findGroupName(this.sncpAddress); //单向SNCP服务不需要对等group //if (this.sncpGroup == null) throw new RuntimeException("Server (" + String.valueOf(config).replaceAll("\\s+", " ") + ") not found info"); diff --git a/src/org/redkale/cluster/ClusterAgent.java b/src/org/redkale/cluster/ClusterAgent.java index debea59c7..9227cb482 100644 --- a/src/org/redkale/cluster/ClusterAgent.java +++ b/src/org/redkale/cluster/ClusterAgent.java @@ -10,7 +10,7 @@ import java.net.InetSocketAddress; import java.util.*; import java.util.concurrent.*; import java.util.logging.Logger; -import org.redkale.boot.NodeServer; +import org.redkale.boot.*; import org.redkale.convert.json.JsonConvert; import org.redkale.net.*; import org.redkale.net.http.WebSocketNode; @@ -87,6 +87,10 @@ public abstract class ClusterAgent { return Utility.contains(ports, port); } + public abstract void register(Application application); + + public abstract void deregister(Application application); + //注册服务, 在NodeService调用Service.init方法之前调用 public void register(NodeServer ns, String protocol, Set localServices, Set remoteServices) { if (localServices.isEmpty()) return; @@ -161,6 +165,22 @@ public abstract class ClusterAgent { Sncp.updateTransport(service, transportFactory, Sncp.getResourceType(service).getName() + "-" + Sncp.getResourceName(service), entry.netprotocol, entry.address, null, addrs); } + protected String generateApplicationServiceName() { + return "application.node." + this.nodeid; + } + + protected String generateApplicationServiceId() { //与servicename相同 + return "application.node." + this.nodeid; + } + + protected String generateApplicationCheckName() { + return "check-" + generateApplicationServiceName(); + } + + protected String generateApplicationCheckId() { + return "check-" + generateApplicationServiceId(); + } + //格式: protocol:classtype-resourcename protected String generateServiceName(NodeServer ns, String protocol, Service service) { if (!Sncp.isSncpDyn(service)) return protocol.toLowerCase() + ":" + service.getClass().getName();