既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上物联网嵌入式知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、电子书籍、讲解视频,并且后续会持续更新
=================================================================
服务绑定的IP地址
#bind_address
服务绑定的端口号
#port 1883
允许的最大连接数,-1表示没有限制
#max_connections -1
cafile:CA证书文件
capath:CA证书目录
certfile:PEM证书文件
keyfile:PEM密钥文件
#cafile
#capath
#certfile
#keyfile
必须提供证书以保证数据安全性
#require_certificate false
若require_certificate值为true,use_identity_as_username也必须为true
#use_identity_as_username false
启用PSK(Pre-shared-key)支持
#psk_hint
SSL/TSL加密算法,可以使用“openssl ciphers”命令获取
as the output of that command.
#ciphers
=================================================================
Persistence
=================================================================
消息自动保存的间隔时间
#autosave_interval 1800
消息自动保存功能的开关
#autosave_on_changes false
持久化功能的开关
persistence true
持久化DB文件
persistence_file mosquitto.db
持久化DB文件目录
persistence_location /var/lib/mosquitto/
=================================================================
Logging
=================================================================
4种日志模式:stdout、stderr、syslog、topic
none 则表示不记日志,此配置可以提升些许性能
log_dest none
选择日志的级别(可设置多项)
#log_type error
#log_type warning
#log_type notice
#log_type information
是否记录客户端连接信息
#connection_messages true
是否记录日志时间
#log_timestamp true
=================================================================
Security
=================================================================
客户端ID的前缀限制,可用于保证安全性
#clientid_prefixes
允许匿名用户
#allow_anonymous true
用户/密码文件,默认格式:username:password
#password_file
PSK格式密码文件,默认格式:identity:key
#psk_file
pattern write sensor/%u/data
ACL权限配置,常用语法如下:
用户限制:user
话题限制:topic [read|write]
正则限制:pattern write sensor/%u/data
#acl_file
=================================================================
Bridges
=================================================================
允许服务之间使用“桥接”模式(可用于分布式部署)
#connection
#address [:]
#topic [[[out | in | both] qos-level] local-prefix remote-prefix]
设置桥接的客户端ID
#clientid
桥接断开时,是否清除远程服务器中的消息
#cleansession false
是否发布桥接的状态信息
#notifications true
设置桥接模式下,消息将会发布到的话题地址
$SYS/broker/connection//state
#notification_topic
设置桥接的keepalive数值
#keepalive_interval 60
桥接模式,目前有三种:automatic、lazy、once
#start_type automatic
桥接模式automatic的超时时间
#restart_timeout 30
桥接模式lazy的超时时间
#idle_timeout 60
桥接客户端的用户名
#username
桥接客户端的密码
#password
bridge_cafile:桥接客户端的CA证书文件
bridge_capath:桥接客户端的CA证书目录
bridge_certfile:桥接客户端的PEM证书文件
bridge_keyfile:桥接客户端的PEM密钥文件
#bridge_cafile
#bridge_capath
#bridge_certfile
#bridge_keyfile
关于详细配置可参考:http://mosquitto.org/man/mosquitto-conf-5.html
3.1.3 设置用户名和密码
将配置文件中 #allow_anonymous true
去掉注释,设置为 false
,#password_file
去掉注释并添加密码文件保存的位置:
allow_anonymous false
password_file /etc/mosquitto/pwfile.example
mosquitto_passwd -c /etc/mosquitto/pwfile.example 用户名
之后需输入两次密码
注意如果想添加用户
mosquitto_passwd -b /etc/mosquitto/pwfile.example 用户名 密码
同样连续会提示连续输入两次密码。注意第二次创建用户时不用加 -c 如果加 -c 会把第一次创建的用户覆盖。
3.1.4 启动 mosquitto
mosquitto -c /etc/mosquitto/mosquitto.conf -d
成功将启动并监听 1883 端口
3.2 测试
新建两个 shell 窗口 A/B
A 订阅主题:
mosquitto_sub -t 主题名 -h 主机IP -u 用户名 -P 密码
例如:mosquitto_sub -t topic-riemann -h localhost -u mosquitto -P mosquitto
B 推送消息:
mosquitto_pub -t 主题名 -h 主机IP -m “消息内容” -u 用户名 -P 密码
例如:mosquitto_pub -t topic-riemann -h localhost -m “hello,mqtt” -u mosquitto -P mosquitto
3.3 可能遇到的问题
如果你出现这个错误:
mosquitto_sub: error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory
解决方法:
编译完 mosquitto
之后,进入到 lib
目录下,将编译之后的 libmosquitto.so.1
拷贝到目录 /usr/local/lib
下,执行如下命令:
cp libmosquitto.so.1 /usr/local/lib
然后再执行命令:
sudo ln -s /usr/local/lib/libmosquitto.so.1 /usr/lib/libmosquitto.so.1
ldconfig
3.4 测试结果
4.1 项目结构图
4.2 添加 pom.xml
org.springframework.boot
spring-boot-starter-parent
2.1.6.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-devtools
org.projectlombok
lombok
true
org.springframework.integration
spring-integration-mqtt
5.2.5.RELEASE
4.3 application.yml
mqtt:
host: tcp://服务器IP:1883
clientId: client_${random.value}
topic: test/system/module/biz
qoslevel: 1
username: mosquitto
password: mosquitto
timeout: 10000
keepalive: 20
server:
port: 8888
4.4 MqttConfig
/**
- @author: 微信公众号【老周聊架构】
*/
@Slf4j
@Configuration
@IntegrationComponentScan
public class MqttConfig {
@Value(“${mqtt.username}”)
private String username;
@Value(“${mqtt.password}”)
private String password;
@Value(“${mqtt.host}”)
private String hostUrl;
@Value(“${mqtt.clientId}”)
private String clientId;
@Value(“${mqtt.topic}”)
private String defaultTopic;
// 连接超时
@Value(“${mqtt.timeout}”)
private int completionTimeout;
@Bean
public MqttConnectOptions getMqttConnectOptions() {
MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
mqttConnectOptions.setCleanSession(true);
mqttConnectOptions.setConnectionTimeout(10);
mqttConnectOptions.setKeepAliveInterval(90);
mqttConnectOptions.setAutomaticReconnect(true);
mqttConnectOptions.setUserName(username);
mqttConnectOptions.setPassword(password.toCharArray());
mqttConnectOptions.setServerURIs(new String[]{hostUrl});
mqttConnectOptions.setKeepAliveInterval(2);
return mqttConnectOptions;
}
@Bean
public MqttPahoClientFactory mqttClientFactory() {
DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
factory.setConnectionOptions(getMqttConnectOptions());
return factory;
}
@Bean
@ServiceActivator(inputChannel = “mqttOutboundChannel”)
public MessageHandler mqttOutbound() {
MqttPahoMessageHandler messageHandler = new MqttPahoMessageHandler(clientId, mqttClientFactory());
messageHandler.setAsync(true);
messageHandler.setDefaultTopic(defaultTopic);
return messageHandler;
}
@Bean
public MessageChannel mqttOutboundChannel() {
DirectChannel directChannel = new DirectChannel();
return directChannel;
}
// 接收通道
@Bean
public MessageChannel mqttInputChannel() {
DirectChannel directChannel = new DirectChannel();
return directChannel;
}
// 配置client,监听的topic
@Bean
public MessageProducer inbound() {
MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter(clientId + “_inbound”,
mqttClientFactory(), “test/#”);
adapter.setCompletionTimeout(completionTimeout);
adapter.setConverter(new DefaultPahoMessageConverter());
adapter.setQos(1);
adapter.setOutputChannel(mqttInputChannel());
return adapter;
}
// 通过通道获取数据
@Bean
@ServiceActivator(inputChannel = “mqttInputChannel”)
public MessageHandler handler() {
return message -> {
String topic = (String)message.getHeaders().get(“mqtt_receivedTopic”);
log.info(“主题:{},消息接收到的数据:{}”, topic, message.getPayload());
};
}
}
4.5 MqttGateWay
/**
- @author: 微信公众号【老周聊架构】
*/
@MessagingGateway(defaultRequestChannel = “mqttOutboundChannel”)
public interface MqttGateWay {
// 定义重载方法,用于消息发送
void sendToMqtt(String payload);
// 指定topic进行消息发送
void sendToMqtt(@Header(MqttHeaders.TOPIC) String topic, String payload);
void sendToMqtt(@Header(MqttHeaders.TOPIC) String topic, @Header(MqttHeaders.QOS) int qos, String payload);
}
4.6 MqttController 控制类
收集整理了一份《2024年最新物联网嵌入式全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升的朋友。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人
都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
/
@MessagingGateway(defaultRequestChannel = “mqttOutboundChannel”)
public interface MqttGateWay {
// 定义重载方法,用于消息发送
void sendToMqtt(String payload);
// 指定topic进行消息发送
void sendToMqtt(@Header(MqttHeaders.TOPIC) String topic, String payload);
void sendToMqtt(@Header(MqttHeaders.TOPIC) String topic, @Header(MqttHeaders.QOS) int qos, String payload);
}
4.6 MqttController 控制类
收集整理了一份《2024年最新物联网嵌入式全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升的朋友。
[外链图片转存中…(img-HmAcHQHG-1715635197225)]
[外链图片转存中…(img-1xW8KhBE-1715635197226)]
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人
都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!