提交 bf634996 编写于 作者: G guide

[fix]clear registry

上级 5959b8ee
package github.javaguide.config;
import github.javaguide.registry.zk.util.CuratorUtils;
import github.javaguide.remoting.transport.netty.server.NettyRpcServer;
import github.javaguide.utils.concurrent.threadpool.ThreadPoolFactoryUtils;
import lombok.extern.slf4j.Slf4j;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
/**
* When the server is closed, do something such as unregister all services
*
......@@ -21,7 +26,11 @@ public class CustomShutdownHook {
public void clearAll() {
log.info("addShutdownHook for clearAll");
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
CuratorUtils.clearRegistry(CuratorUtils.getZkClient());
try {
InetSocketAddress inetSocketAddress = new InetSocketAddress(InetAddress.getLocalHost().getHostAddress(), NettyRpcServer.PORT);
CuratorUtils.clearRegistry(CuratorUtils.getZkClient(), inetSocketAddress);
} catch (UnknownHostException ignored) {
}
ThreadPoolFactoryUtils.shutDownAllThreadPool();
}));
}
......
......@@ -2,6 +2,7 @@ package github.javaguide.registry.zk.util;
import github.javaguide.enums.RpcConfigEnum;
import github.javaguide.utils.PropertiesFileUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
......@@ -12,6 +13,8 @@ import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.Map;
import java.util.Properties;
......@@ -83,10 +86,12 @@ public final class CuratorUtils {
/**
* Empty the registry of data
*/
public static void clearRegistry(CuratorFramework zkClient) {
public static void clearRegistry(CuratorFramework zkClient, InetSocketAddress inetSocketAddress) {
REGISTERED_PATH_SET.stream().parallel().forEach(p -> {
try {
zkClient.delete().forPath(p);
if (p.endsWith(inetSocketAddress.toString())) {
zkClient.delete().forPath(p);
}
} catch (Exception e) {
log.error("clear registry for path [{}] fail", p);
}
......
......@@ -30,7 +30,6 @@ public class SocketRpcServer {
public SocketRpcServer() {
threadPool = ThreadPoolFactoryUtils.createCustomThreadPoolIfAbsent("socket-server-rpc-pool");
SingletonFactory.getInstance(ServiceProviderImpl.class);
serviceProvider = SingletonFactory.getInstance(ServiceProviderImpl.class);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册