Optimized code

This commit is contained in:
ehlxr 2021-01-19 20:28:04 +08:00
parent ed67a45915
commit eaeeb56e93
4 changed files with 31 additions and 16 deletions

View File

@ -2,14 +2,15 @@ package cn.ceres.did;
import cn.ceres.did.common.Constants;
import cn.ceres.did.core.SnowFlake;
import cn.ceres.did.server.Server;
import cn.ceres.did.server.http.HttpServer;
import cn.ceres.did.server.sdk.SdkServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Arrays;
/**
* 两个服务器进程最好用同一个 SnowFlake 实例部署在分布式环境时SnowFlake datacenterId machineId 作为联合键必须全局唯一否则多个节点的服务可能产生相同的 ID
*
* @author ehlxr
*/
public class ServerStarter {
@ -40,10 +41,8 @@ public class ServerStarter {
sdkServer.init();
sdkServer.start();
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
httpServer.shutdown();
sdkServer.shutdown();
System.exit(0);
}));
// 并行 shutdown server
Runtime.getRuntime().addShutdownHook(new Thread(() ->
Arrays.stream(new Server[]{httpServer, sdkServer}).parallel().forEach(Server::shutdown)));
}
}

View File

@ -24,7 +24,7 @@ public abstract class BaseServer implements Server {
protected int port;
public void init() {
defLoopGroup = new DefaultEventLoopGroup(8, new ThreadFactory() {
defLoopGroup = new DefaultEventLoopGroup(Runtime.getRuntime().availableProcessors(), new ThreadFactory() {
private final AtomicInteger index = new AtomicInteger(0);
@Override
@ -32,7 +32,7 @@ public abstract class BaseServer implements Server {
return new Thread(r, "DEFAULTEVENTLOOPGROUP_" + index.incrementAndGet());
}
});
bossGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors(), new ThreadFactory() {
bossGroup = new NioEventLoopGroup(1, new ThreadFactory() {
private final AtomicInteger index = new AtomicInteger(0);
@Override
@ -40,7 +40,7 @@ public abstract class BaseServer implements Server {
return new Thread(r, "BOSS_" + index.incrementAndGet());
}
});
workGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors() * 10, new ThreadFactory() {
workGroup = new NioEventLoopGroup(new ThreadFactory() {
private final AtomicInteger index = new AtomicInteger(0);
@Override
@ -54,12 +54,15 @@ public abstract class BaseServer implements Server {
@Override
public void shutdown() {
if (defLoopGroup != null) {
defLoopGroup.shutdownGracefully();
try {
// 同步阻塞 shutdownGracefully 完成
if (defLoopGroup != null) {
defLoopGroup.shutdownGracefully().sync();
}
bossGroup.shutdownGracefully().sync();
workGroup.shutdownGracefully().sync();
} catch (Exception e) {
logger.error("Server EventLoopGroup shutdown error.", e);
}
bossGroup.shutdownGracefully();
workGroup.shutdownGracefully();
logger.info("Server EventLoopGroup shutdown finish");
}
}

View File

@ -66,4 +66,10 @@ public class HttpServer extends BaseServer {
}
}
@Override
public void shutdown() {
logger.info("HttpServer shutdowning......");
super.shutdown();
logger.info("HttpServer shutdown finish!");
}
}

View File

@ -56,4 +56,11 @@ public class SdkServer extends BaseServer {
logger.error("SdkServer start fail,", e);
}
}
@Override
public void shutdown() {
logger.info("SdkServer shutdowning......");
super.shutdown();
logger.info("SdkServer shutdown finish!");
}
}