add protobuf serializer, spi extension
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2021-02-09 17:36:11 +08:00
parent 9d72433199
commit 5db4dbeba5
37 changed files with 1123 additions and 343 deletions

View File

@@ -16,7 +16,7 @@
<dependencies>
<dependency>
<groupId>io.github.ehlxr</groupId>
<artifactId>did-common</artifactId>
<artifactId>did-core</artifactId>
</dependency>
</dependencies>

View File

@@ -1,7 +1,6 @@
package io.github.ehlxr.did;
import io.github.ehlxr.did.common.Constants;
import io.github.ehlxr.did.core.SnowFlake;
import io.github.ehlxr.did.server.Server;
import io.github.ehlxr.did.server.http.HttpServer;
import io.github.ehlxr.did.server.sdk.SdkServer;

View File

@@ -1,4 +1,4 @@
package io.github.ehlxr.did.core;
package io.github.ehlxr.did;
/**
* twitter snowflake 算法 -- java 实现

View File

@@ -1,7 +1,7 @@
package io.github.ehlxr.did.server;
import io.github.ehlxr.did.SnowFlake;
import io.github.ehlxr.did.common.Try;
import io.github.ehlxr.did.core.SnowFlake;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;

View File

@@ -1,8 +1,8 @@
package io.github.ehlxr.did.server.http;
import io.github.ehlxr.did.SnowFlake;
import io.github.ehlxr.did.common.Constants;
import io.github.ehlxr.did.common.Try;
import io.github.ehlxr.did.core.SnowFlake;
import io.github.ehlxr.did.server.BaseServer;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;

View File

@@ -1,10 +1,10 @@
package io.github.ehlxr.did.server.http;
import io.github.ehlxr.did.SdkProto;
import io.github.ehlxr.did.SnowFlake;
import io.github.ehlxr.did.common.Constants;
import io.github.ehlxr.did.common.NettyUtil;
import io.github.ehlxr.did.common.Result;
import io.github.ehlxr.did.common.SdkProto;
import io.github.ehlxr.did.core.SnowFlake;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;

View File

@@ -1,10 +1,10 @@
package io.github.ehlxr.did.server.sdk;
import io.github.ehlxr.did.SnowFlake;
import io.github.ehlxr.did.adapter.MessageDecoder;
import io.github.ehlxr.did.adapter.MessageEncoder;
import io.github.ehlxr.did.common.Constants;
import io.github.ehlxr.did.common.Try;
import io.github.ehlxr.did.core.SnowFlake;
import io.github.ehlxr.did.netty.MyProtocolDecoder;
import io.github.ehlxr.did.netty.MyProtocolEncoder;
import io.github.ehlxr.did.server.BaseServer;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
@@ -51,11 +51,8 @@ public class SdkServer extends BaseServer {
@Override
protected void initChannel(SocketChannel ch) {
ch.pipeline().addLast(defLoopGroup,
// new SdkServerDecoder(Constants.DECODER_FRAMELENGTH),// 如果长度不够会等待
// new SdkServerEncoder(),
new MyProtocolEncoder(),
new MyProtocolDecoder(Constants.MAX_FRAME_LENGTH, Constants.LENGTH_FIELD_OFFSET,
Constants.LENGTH_FIELD_LENGTH, Constants.LENGTH_ADJUSTMENT, Constants.INITIAL_BYTES_TO_STRIP, false),
new MessageEncoder(),
new MessageDecoder(),
new SdkServerHandler(snowFlake)
);
}

View File

@@ -1,41 +0,0 @@
package io.github.ehlxr.did.server.sdk;
import io.github.ehlxr.did.common.NettyUtil;
import io.github.ehlxr.did.common.Try;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.FixedLengthFrameDecoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author ehlxr
*/
public class SdkServerDecoder extends FixedLengthFrameDecoder {
private static final Logger logger = LoggerFactory.getLogger(SdkServerDecoder.class);
SdkServerDecoder(int frameLength) {
super(frameLength);
}
@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) {
return Try.of(() -> {
ByteBuf buf = (ByteBuf) super.decode(ctx, in);
byte[] bytes = new byte[buf.readableBytes()];
buf.readBytes(bytes);
buf.release();
return NettyUtil.toObject(bytes);
}).trap(e -> logger.error("decode error", e)).get();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
Channel channel = ctx.channel();
logger.error("SdkServerDecoder channel [{}] error and will be closed", NettyUtil.parseRemoteAddr(channel), cause);
NettyUtil.closeChannel(channel);
}
}

View File

@@ -1,33 +0,0 @@
package io.github.ehlxr.did.server.sdk;
import io.github.ehlxr.did.common.NettyUtil;
import io.github.ehlxr.did.common.SdkProto;
import io.github.ehlxr.did.common.Try;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author ehlxr
*/
public class SdkServerEncoder extends MessageToByteEncoder<SdkProto> {
private static final Logger logger = LoggerFactory.getLogger(SdkServerEncoder.class);
@Override
protected void encode(ChannelHandlerContext channelHandlerContext, SdkProto sdkProto, ByteBuf out) {
Try.of(() -> {
out.writeBytes(NettyUtil.toBytes(sdkProto));
}).trap(e -> logger.error("encode error", e)).run();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
Channel channel = ctx.channel();
logger.error("SdkServerEncoder channel [{}] error and will be closed", NettyUtil.parseRemoteAddr(channel), cause);
NettyUtil.closeChannel(channel);
}
}

View File

@@ -1,10 +1,10 @@
package io.github.ehlxr.did.server.sdk;
import io.github.ehlxr.did.SdkProto;
import io.github.ehlxr.did.SnowFlake;
import io.github.ehlxr.did.adapter.Message;
import io.github.ehlxr.did.common.Constants;
import io.github.ehlxr.did.common.NettyUtil;
import io.github.ehlxr.did.common.SdkProto;
import io.github.ehlxr.did.core.SnowFlake;
import io.github.ehlxr.did.netty.MyProtocolBean;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
@@ -20,7 +20,7 @@ import java.util.concurrent.TimeUnit;
*
* @author ehlxr
*/
public class SdkServerHandler extends SimpleChannelInboundHandler<MyProtocolBean> {
public class SdkServerHandler extends SimpleChannelInboundHandler<Message<SdkProto>> {
private static final Logger logger = LoggerFactory.getLogger(SdkServerHandler.class);
/**
* 通过信号量来控制流量
@@ -33,23 +33,23 @@ public class SdkServerHandler extends SimpleChannelInboundHandler<MyProtocolBean
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, MyProtocolBean protocolBean) throws Exception {
logger.debug("sdk server handler receive MyProtocolBean {}", protocolBean);
protected void channelRead0(ChannelHandlerContext ctx, Message<SdkProto> message) throws Exception {
logger.debug("sdk server handler receive message {}", message);
if (semaphore.tryAcquire(Constants.ACQUIRE_TIMEOUTMILLIS, TimeUnit.MILLISECONDS)) {
SdkProto sdkProto = (SdkProto) NettyUtil.toObject(protocolBean.getContent());
SdkProto sdkProto = message.content(SdkProto.class);
sdkProto.setDid(snowFlake.nextId());
protocolBean.setContent(NettyUtil.toBytes(sdkProto));
message.setContent(sdkProto);
semaphore.release();
} else {
logger.error("tryAcquire timeout after {}ms, {} threads waiting to acquire, {} permits available in this semaphore",
Constants.ACQUIRE_TIMEOUTMILLIS, this.semaphore.getQueueLength(), this.semaphore.availablePermits());
}
logger.debug("sdk server handler write protocolBean {} to channel", protocolBean);
logger.debug("sdk server handler write message {} to channel", message);
ctx.channel().
writeAndFlush(protocolBean).
writeAndFlush(message).
addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
}