1

此次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,目前还未处理。


Steven
21 声望3 粉丝

对酒当歌,人生几何,美景之中,举杯畅饮,放下心中的执念,醉一回,痛快一回;一本樱花酒,醉了春风醉了酒。