feat[provider]: add provider start event

This commit is contained in:
godotg
2024-01-20 15:43:01 +08:00
parent 63949dc53f
commit 471f0611e7
3 changed files with 46 additions and 2 deletions
@@ -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;
}
@@ -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;
}
}
@@ -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));
}
/**