diff --git a/net/src/main/java/com/zfoo/net/config/model/ProviderConfig.java b/net/src/main/java/com/zfoo/net/config/model/ProviderConfig.java index a31960cf..5326279a 100644 --- a/net/src/main/java/com/zfoo/net/config/model/ProviderConfig.java +++ b/net/src/main/java/com/zfoo/net/config/model/ProviderConfig.java @@ -45,7 +45,9 @@ public class ProviderConfig { public HostAndPort localHostAndPortOrDefault() { if (StringUtils.isBlank(address)) { - var defaultHostAndPort = HostAndPort.valueOf(NetUtils.getLocalhostStr(), NetUtils.getAvailablePort(ProviderConfig.DEFAULT_PORT)); + var host = NetUtils.getLocalhostStr(); + var availablePort = NetUtils.getAvailablePort(ProviderConfig.DEFAULT_PORT); + var defaultHostAndPort = HostAndPort.valueOf(host, availablePort); this.address = defaultHostAndPort.toHostAndPortStr(); return defaultHostAndPort; } diff --git a/net/src/main/java/com/zfoo/net/consumer/event/ProviderStartEvent.java b/net/src/main/java/com/zfoo/net/consumer/event/ProviderStartEvent.java new file mode 100644 index 00000000..630b63b8 --- /dev/null +++ b/net/src/main/java/com/zfoo/net/consumer/event/ProviderStartEvent.java @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package com.zfoo.net.consumer.event; + +import com.zfoo.event.model.IEvent; +import com.zfoo.net.core.HostAndPort; + +/** + * @author godotg + */ +public class ProviderStartEvent implements IEvent { + + private HostAndPort hostAndPort; + + public static ProviderStartEvent valueOf(HostAndPort hostAndPort) { + var event = new ProviderStartEvent(); + event.hostAndPort = hostAndPort; + return event; + } + + public HostAndPort getHostAndPort() { + return hostAndPort; + } + + public void setHostAndPort(HostAndPort hostAndPort) { + this.hostAndPort = hostAndPort; + } +} diff --git a/net/src/main/java/com/zfoo/net/consumer/registry/ZookeeperRegistry.java b/net/src/main/java/com/zfoo/net/consumer/registry/ZookeeperRegistry.java index 526ddd04..bbf5d220 100644 --- a/net/src/main/java/com/zfoo/net/consumer/registry/ZookeeperRegistry.java +++ b/net/src/main/java/com/zfoo/net/consumer/registry/ZookeeperRegistry.java @@ -16,6 +16,7 @@ package com.zfoo.net.consumer.registry; import com.zfoo.event.manager.EventBus; import com.zfoo.net.NetContext; import com.zfoo.net.consumer.event.ConsumerStartEvent; +import com.zfoo.net.consumer.event.ProviderStartEvent; import com.zfoo.net.core.HostAndPort; import com.zfoo.net.core.tcp.TcpClient; import com.zfoo.net.core.tcp.TcpServer; @@ -150,8 +151,10 @@ public class ZookeeperRegistry implements IRegistry { // 服务提供者也仅仅是一个TcpServer // 这里可以看出并没有指定接口,是找一个可用的端口 - var providerServer = new TcpServer(providerConfig.localHostAndPortOrDefault()); + var providerHostAndPort = providerConfig.localHostAndPortOrDefault(); + var providerServer = new TcpServer(providerHostAndPort); providerServer.start(); + EventBus.post(ProviderStartEvent.valueOf(providerHostAndPort)); } /**