Optimized code

This commit is contained in:
2021-01-20 15:25:58 +08:00
parent eaeeb56e93
commit 13e80884d7
23 changed files with 341 additions and 282 deletions

View File

@@ -8,7 +8,7 @@ package cn.ceres.did.core;
* 协议格式0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000
* 协议解释0 - 41 位时间戳 - 5 位数据中心标识 - 5 位机器标识 - 12 位序列号
* <p>
* 1 位标识,由于 long 基本类型在 Java 中是带符号的,最高位是符号位,正数是 0负数是 1所以 id 一般是正数,最高位是 0
* 1 位标识,由于 Long 基本类型在 Java 中是带符号的,最高位是符号位,正数是 0负数是 1所以 id 一般是正数,最高位是 0
* <p>
* 41 位时间截毫秒级注意41 位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截)得到的值,这里的的开始时间截。
* 一般是我们的 id 生成器开始使用的时间,由我们程序来指定的(如下下面程序 START_STMP 属性。41 位的时间截,可以使用 69 年,(1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69

View File

@@ -1,5 +1,6 @@
package cn.ceres.did.server;
import cn.ceres.did.core.SnowFlake;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.DefaultEventLoopGroup;
@@ -22,6 +23,7 @@ public abstract class BaseServer implements Server {
protected ChannelFuture channelFuture;
protected ServerBootstrap serverBootstrap;
protected int port;
protected SnowFlake snowFlake;
public void init() {
defLoopGroup = new DefaultEventLoopGroup(Runtime.getRuntime().availableProcessors(), new ThreadFactory() {

View File

@@ -14,20 +14,23 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Http服务器使用Netty中的Http协议栈
* 实现中支持多条请求路径对于不存在的请求路径返回404状态码
* 如http://localhost:8099/getTime
* Http 服务器,使用 Netty 中的 Http 协议栈,
*
* @author ehlxr
*/
public class HttpServer extends BaseServer {
protected Logger logger = LoggerFactory.getLogger(HttpServer.class);
private final SnowFlake snowFlake;
public HttpServer(SnowFlake snowFlake, int port) {
this.snowFlake = snowFlake;
this.port = port;
}
public HttpServer(SnowFlake snowFlake) {
this.snowFlake = snowFlake;
this.port = "".equals(Constants.getEnv("HTTP_PORT")) ? Constants.HTTP_PORT : Integer.parseInt(Constants.getEnv("HTTP_PORT"));
this.port = "".equals(Constants.getEnv("HTTP_PORT")) ?
Constants.HTTP_PORT :
Integer.parseInt(Constants.getEnv("HTTP_PORT"));
}
@Override
@@ -57,10 +60,6 @@ public class HttpServer extends BaseServer {
try {
channelFuture = serverBootstrap.bind(port).sync();
logger.info("HttpServer start success, port is:{}", port);
// channelFuture = serverBootstrap.bind().sync();
// InetSocketAddress addr = (InetSocketAddress) channelFuture.channel().localAddress();
// logger.info("HttpServer start success, port is:{}", addr.getPort());
} catch (InterruptedException e) {
logger.error("HttpServer start fail,", e);
}

View File

@@ -15,11 +15,6 @@ import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
/**
* 自定义的处理器,目前支持三种请求:
* getTime: 获取服务器当前时间;
* clientInfo: 获取请求客户端的User-Agent信息
* 其它: 返回404状态并且提示404信息
*
* @author ehlxr
*/
public class HttpServerHandler extends SimpleChannelInboundHandler<FullHttpRequest> {

View File

@@ -1,44 +0,0 @@
package cn.ceres.did.server.sdk;
/**
* @author ehlxr
*/
public class SdkProto {
/**
* 请求的ID
*/
private int rqid;
/**
* 全局的 ID
*/
private long did;
SdkProto(int rqid, long did) {
this.rqid = rqid;
this.did = did;
}
public int getRqid() {
return rqid;
}
public void setRqid(int rqid) {
this.rqid = rqid;
}
public long getDid() {
return did;
}
public void setDid(long did) {
this.did = did;
}
@Override
public String toString() {
return "SdkProto{" +
"rqid=" + rqid +
", did=" + did +
'}';
}
}

View File

@@ -15,11 +15,17 @@ import org.slf4j.LoggerFactory;
*/
public class SdkServer extends BaseServer {
protected Logger logger = LoggerFactory.getLogger(SdkServer.class);
private final SnowFlake snowFlake;
public SdkServer(SnowFlake snowFlake) {
this.snowFlake = snowFlake;
this.port = "".equals(Constants.getEnv("SDKS_PORT")) ? Constants.SDKS_PORT : Integer.parseInt(Constants.getEnv("SDKS_PORT"));
this.port = "".equals(Constants.getEnv("SDK_PORT")) ?
Constants.SDK_PORT :
Integer.parseInt(Constants.getEnv("SDK_PORT"));
}
public SdkServer(SnowFlake snowFlake, int port) {
this.snowFlake = snowFlake;
this.port = port;
}
@Override
@@ -48,10 +54,6 @@ public class SdkServer extends BaseServer {
try {
channelFuture = serverBootstrap.bind(port).sync();
logger.info("SdkServer start success, port is:{}", port);
// channelFuture = serverBootstrap.bind().sync();
// InetSocketAddress addr = (InetSocketAddress) channelFuture.channel().localAddress();
// logger.info("SdkServer start success, port is:{}", addr.getPort());
} catch (InterruptedException e) {
logger.error("SdkServer start fail,", e);
}

View File

@@ -1,6 +1,7 @@
package cn.ceres.did.server.sdk;
import cn.ceres.did.common.NettyUtil;
import cn.ceres.did.common.SdkProto;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;

View File

@@ -1,6 +1,7 @@
package cn.ceres.did.server.sdk;
import cn.ceres.did.common.NettyUtil;
import cn.ceres.did.common.SdkProto;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;

View File

@@ -2,6 +2,7 @@ package cn.ceres.did.server.sdk;
import cn.ceres.did.common.Constants;
import cn.ceres.did.common.NettyUtil;
import cn.ceres.did.common.SdkProto;
import cn.ceres.did.core.SnowFlake;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFutureListener;
@@ -23,7 +24,7 @@ public class SdkServerHandler extends SimpleChannelInboundHandler<SdkProto> {
/**
* 通过信号量来控制流量
*/
private final Semaphore semaphore = new Semaphore(Constants.HANDLE_SDKS_TPS);
private final Semaphore semaphore = new Semaphore(Constants.HANDLE_SDK_TPS);
private final SnowFlake snowFlake;
SdkServerHandler(SnowFlake snowFlake) {