did/did-server/src/main/java/cn/ceres/did/ServerStarter.java

50 lines
1.8 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package cn.ceres.did;
import cn.ceres.did.common.Constants;
import cn.ceres.did.core.SnowFlake;
import cn.ceres.did.server.http.HttpServer;
import cn.ceres.did.server.sdk.SdkServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 两个服务器进程最好用同一个 SnowFlake 实例部署在分布式环境时SnowFlake 的 datacenterId 和 machineId 作为联合键必须全局唯一,否则多个节点的服务可能产生相同的 ID
*
* @author ehlxr
*/
public class ServerStarter {
private static final Logger logger = LoggerFactory.getLogger(ServerStarter.class);
public static void main(String[] args) {
long datacenterId = Constants.DATACENTER_ID;
long machineId = Constants.MACHINES_ID;
if (args != null && args.length == 2) {
datacenterId = Long.parseLong(args[0]);
machineId = Long.parseLong(args[1]);
}
datacenterId = "".equals(Constants.getEnv("DATACENTER_ID")) ? datacenterId : Long.parseLong(Constants.getEnv("DATACENTER_ID"));
machineId = "".equals(Constants.getEnv("MACHINES_ID")) ? machineId : Long.parseLong(Constants.getEnv("MACHINES_ID"));
logger.info("SnowFlake datacenterId is: {}, machineId is: {}", datacenterId, machineId);
final SnowFlake snowFlake = new SnowFlake(datacenterId, machineId);
// 启动 Http 服务器
final HttpServer httpServer = new HttpServer(snowFlake);
httpServer.init();
httpServer.start();
// 启动 Sdk 服务器
final SdkServer sdkServer = new SdkServer(snowFlake);
sdkServer.init();
sdkServer.start();
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
httpServer.shutdown();
sdkServer.shutdown();
System.exit(0);
}));
}
}