此次netty版本用的netty-all:4.1.36.Final。
springboot+netty集成实例网上有很多,刚添加376规约作为上报数据处理,看一下具体实现。
1、创建一个多规约handler处理类:
@Override
protected void initChannel(SocketChannel channel) throws Exception {
channel.pipeline().addLast(new IdleStateHandler(DivMultiprotocolSelection.HEARTBEAT_TIME,
0, 0, DivMultiprotocolSelection.TIME_UNIT));
channel.pipeline().addLast(new StringDecoder());
//channel.pipeline().addLast(new StringEncoder());
channel.pipeline().addLast("encoder", new ProtobufEncoder());
channel.pipeline().addLast(new DivServerHandler());
switch (DivMultiprotocolSelection.PROTOCOL_TYPES) {
case "376":
channel.pipeline().addLast(new Check376Handler());
break;
default:
channel.pipeline().addLast(new CheckAllPurposeHandler());
break;
}
2、创建376规约
public class Check376Handler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
String body = msg.toString();
String ipport = body.split("_")[0];
String data = body.split("_")[1];
FlowListener.getInstance().writeFlow(ipport, data.length() / 2);
if ("68".equals(data.substring(0, 2)) && "68".equals(data.substring(10, 12))
&& "16".equals(data.substring(data.length() - 2))) {
Channel channel = MasterSlotsPartition.getInstance().getActiveMaster();
Message mast = Message.newBuilder().setIp(ipport.split(":")[0])
.setPort(ipport.split(":")[1]).setProtocolId(DivMultiprotocolSelection.PROTOCOL_TYPES)
.setContent(data).build();
channel.writeAndFlush(mast);
}
}
}
以上是376规约解析,消息处理考虑引进socket,目前还未处理。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。