物联网 基于netty核心实战-握手与认证

物联网 基于netty核心实战-握手与认证

简述

MQTT PUBLISH 报文 的二进制结构(固定头、可变头、有效载荷),并基于 Netty 实现一个支持 QoS 0 的发布/订阅 Broker

源码(netty-sample-05-auth)

https://gitee.com/kcnf-iot/iot-sample/tree/master/netty/netty-sample-05

协议结构

MQTT 报文结构 Netty 中的对应 Java 类 关键字段/作用
固定报头 (Fixed Header) MqttFixedHeader 包含 messageType (如 CONNECT), qosLevel, isRetain 等标志位
可变报头 (Variable Header) MqttConnectVariableHeader 包含协议名、版本号、连接标志(如 Clean Session)、保活时间
有效载荷 (Payload) MqttConnectPayload 包含实际数据,如客户端ID、遗嘱消息、用户名和密码
完整连接请求 MqttConnectMessage 一个对象就包含了上述三部分,可以直接在处理器中使用

简易demo

服务端
复制代码
package com.jysemel.iot;

import com.jysemel.iot.handler.MqttAuthHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.mqtt.MqttDecoder;
import io.netty.handler.codec.mqtt.MqttEncoder;

public class MqttAuthServer {
    public static void main(String[] args) throws Exception {
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) {
                            ChannelPipeline p = ch.pipeline();
                            // (A) 使用 Netty 内置的 MqttDecoder
                            p.addLast("mqttDecoder", new MqttDecoder(1024));
                            // (B) 使用单例的 MqttEncoder
                            p.addLast("mqttEncoder", MqttEncoder.INSTANCE);
                            // (C) 实现我们自己的业务逻辑处理器
                            p.addLast("authHandler", new MqttAuthHandler());
                        }
                    });
            ChannelFuture f = b.bind(1883).sync();
            System.out.println("MQTT Auth Server started on port 1883");
            f.channel().closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}
客户端
复制代码
package com.jysemel.iot;

import com.jysemel.iot.handler.MqttAuthClientHandler;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.mqtt.MqttDecoder;
import io.netty.handler.codec.mqtt.MqttEncoder;

public class MqttAuthClient {
    public static void main(String[] args) throws Exception {
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            b.group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) {
                            ChannelPipeline p = ch.pipeline();
                            p.addLast("mqttDecoder", new MqttDecoder(1024));
                            p.addLast("mqttEncoder", MqttEncoder.INSTANCE);
                            p.addLast("clientHandler", new MqttAuthClientHandler());
                        }
                    });
            ChannelFuture f = b.connect("127.0.0.1", 1883).sync();
            f.channel().closeFuture().sync();
        } finally {
            group.shutdownGracefully();
        }
    }
}
相关推荐
物联通信量讯说10 小时前
2026年12月国际物联网卡服务商怎么选?从GSMA杭州论坛看2027年AIoT设备全球连接趋势
物联网·iot·企业出海·国际物联网卡·全球iot·gsma杭州论坛
程序员小杰@10 小时前
Java 统一报文转换 + 签名验签完整介绍
java·开发语言
冰美式只要糖10 小时前
Prefill 长短请求混批,为什么 128 Token 也要等 2.58 秒?
后端
小张成长计划..10 小时前
【Linux】44:五种IO模型与非阻塞IO
java·服务器·数据库
不甘平凡的小鸟10 小时前
vs code 打开spring boot项目
java·spring boot·后端
ZhengEnCi10 小时前
Java 内存泄漏怎么排查:jstat、heap dump 与 MAT 线上定位流程
java·jvm·性能优化
忆想不到的晖10 小时前
删掉 1800 行编排代码后,我用 Agent-Native + 薄 AOP 重写了内容工作台
后端·node.js·agent
冰美式只要糖11 小时前
Transformers 静态 KV Cache 更新更快,为什么 Decode 反而变慢?
后端
Penge66611 小时前
职场生存法则:识别服从性测试,建立自己的边界感
后端
想不到一个好的ID11 小时前
基于 Redis 的秒杀系统架构设计方案
后端