Optimized code
This commit is contained in:
parent
ed67a45915
commit
eaeeb56e93
@ -2,14 +2,15 @@ package cn.ceres.did;
|
|||||||
|
|
||||||
import cn.ceres.did.common.Constants;
|
import cn.ceres.did.common.Constants;
|
||||||
import cn.ceres.did.core.SnowFlake;
|
import cn.ceres.did.core.SnowFlake;
|
||||||
|
import cn.ceres.did.server.Server;
|
||||||
import cn.ceres.did.server.http.HttpServer;
|
import cn.ceres.did.server.http.HttpServer;
|
||||||
import cn.ceres.did.server.sdk.SdkServer;
|
import cn.ceres.did.server.sdk.SdkServer;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 两个服务器进程最好用同一个 SnowFlake 实例,部署在分布式环境时,SnowFlake 的 datacenterId 和 machineId 作为联合键必须全局唯一,否则多个节点的服务可能产生相同的 ID
|
|
||||||
*
|
|
||||||
* @author ehlxr
|
* @author ehlxr
|
||||||
*/
|
*/
|
||||||
public class ServerStarter {
|
public class ServerStarter {
|
||||||
@ -40,10 +41,8 @@ public class ServerStarter {
|
|||||||
sdkServer.init();
|
sdkServer.init();
|
||||||
sdkServer.start();
|
sdkServer.start();
|
||||||
|
|
||||||
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
// 并行 shutdown server
|
||||||
httpServer.shutdown();
|
Runtime.getRuntime().addShutdownHook(new Thread(() ->
|
||||||
sdkServer.shutdown();
|
Arrays.stream(new Server[]{httpServer, sdkServer}).parallel().forEach(Server::shutdown)));
|
||||||
System.exit(0);
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ public abstract class BaseServer implements Server {
|
|||||||
protected int port;
|
protected int port;
|
||||||
|
|
||||||
public void init() {
|
public void init() {
|
||||||
defLoopGroup = new DefaultEventLoopGroup(8, new ThreadFactory() {
|
defLoopGroup = new DefaultEventLoopGroup(Runtime.getRuntime().availableProcessors(), new ThreadFactory() {
|
||||||
private final AtomicInteger index = new AtomicInteger(0);
|
private final AtomicInteger index = new AtomicInteger(0);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -32,7 +32,7 @@ public abstract class BaseServer implements Server {
|
|||||||
return new Thread(r, "DEFAULTEVENTLOOPGROUP_" + index.incrementAndGet());
|
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);
|
private final AtomicInteger index = new AtomicInteger(0);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -40,7 +40,7 @@ public abstract class BaseServer implements Server {
|
|||||||
return new Thread(r, "BOSS_" + index.incrementAndGet());
|
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);
|
private final AtomicInteger index = new AtomicInteger(0);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -54,12 +54,15 @@ public abstract class BaseServer implements Server {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void shutdown() {
|
public void shutdown() {
|
||||||
|
try {
|
||||||
|
// 同步阻塞 shutdownGracefully 完成
|
||||||
if (defLoopGroup != null) {
|
if (defLoopGroup != null) {
|
||||||
defLoopGroup.shutdownGracefully();
|
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");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -66,4 +66,10 @@ public class HttpServer extends BaseServer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void shutdown() {
|
||||||
|
logger.info("HttpServer shutdowning......");
|
||||||
|
super.shutdown();
|
||||||
|
logger.info("HttpServer shutdown finish!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -56,4 +56,11 @@ public class SdkServer extends BaseServer {
|
|||||||
logger.error("SdkServer start fail,", e);
|
logger.error("SdkServer start fail,", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void shutdown() {
|
||||||
|
logger.info("SdkServer shutdowning......");
|
||||||
|
super.shutdown();
|
||||||
|
logger.info("SdkServer shutdown finish!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user