tocrhz / mqtt-spring-boot-starter

MQTT starter for Spring Boot, easier to use.
Apache License 2.0
93 stars 47 forks source link

默认客户端使用3.1.1,导致无法使用共享订阅主题 #25

Closed QianKun123456 closed 3 months ago

QianKun123456 commented 3 months ago

如何在创建客户端时,指定MQTT协议为5.0呢? 没有在配置项中找到协议配置信息

tocrhz commented 3 months ago

共享订阅只跟服务器有关,emqx同时支持3和5,

---原始邮件--- 发件人: @.> 发送时间: 2024年6月26日(周三) 中午11:31 收件人: @.>; 抄送: @.***>; 主题: [tocrhz/mqtt-spring-boot-starter] 默认客户端使用3.1.1,导致无法使用共享订阅主题 (Issue #25)

如何在创建客户端时,指定MQTT协议为5.0呢? 没有在配置项中找到协议配置信息

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.Message ID: @.***>

QianKun123456 commented 3 months ago

image 我的服务端是5.7的,使用EMQX测试$share/group/{topic}通过了,代码中创建客户端时,从服务端监测到协议采用的3.1.1,无法识别$share这样的共享主题

tocrhz commented 3 months ago

无法识别是什么意思?emqx不可能无法识别,从标准不支持共享订阅时,emqx就用这个格式的共享订阅了。而且不识别时订阅的主题应该显示$开头的完整topic,如果显示的是不完整的topic说明识别了

---原始邮件--- 发件人: @.> 发送时间: 2024年6月26日(周三) 中午11:36 收件人: @.>; 抄送: @.**@.>; 主题: Re: [tocrhz/mqtt-spring-boot-starter] 默认客户端使用3.1.1,导致无法使用共享订阅主题 (Issue #25)

image.png (view on web) 我的服务端是5.7的,使用EMQX测试$share/group/{topic}通过了,代码中创建客户端时,从服务端监测到协议采用的3.1.1,无法识别$share这样的共享主题

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

QianKun123456 commented 3 months ago

这是一个MQ自身的解释【https://www.emqx.com/zh/blog/introduction-to-mqtt5-protocol-shared-subscription#mqtt-3-1-1-%E5%AE%A2%E6%88%B7%E7%AB%AF%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%E5%85%B1%E4%BA%AB%E8%AE%A2%E9%98%85

我这里测试采用$share/{group}/{topic}或者$queue/{topic}形式订阅主题,代码中均没有输出收到信息,但是启动的两个EMQX客户端(这两个客户端使用的5.0协议)都有接收到主题的消息。 但是我这里有个疑惑,我生产了6条消息,两个EMQX工具消费端分别收到了两条,总计四条,还有两条消息,没有在客户端输出,也没有再代码中输出

tocrhz commented 3 months ago

所以你的group是一样的吗?

---原始邮件--- 发件人: @.> 发送时间: 2024年6月26日(周三) 中午11:58 收件人: @.>; 抄送: @.**@.>; 主题: Re: [tocrhz/mqtt-spring-boot-starter] 默认客户端使用3.1.1,导致无法使用共享订阅主题 (Issue #25)

这是一个MQ自身的解释【https://www.emqx.com/zh/blog/introduction-to-mqtt5-protocol-shared-subscription#mqtt-3-1-1-%E5%AE%A2%E6%88%B7%E7%AB%AF%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%E5%85%B1%E4%BA%AB%E8%AE%A2%E9%98%85

我这里测试采用$share/{group}/{topic}或者$queue/{topic}形式订阅主题,代码中均没有输出收到信息,但是启动的两个EMQX客户端(这两个客户端使用的5.0协议)都有接收到主题的消息。 但是我这里有个疑惑,我生产了6条消息,两个EMQX工具消费端分别收到了两条,总计四条,还有两条消息,没有在客户端输出,也没有再代码中输出

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

QianKun123456 commented 3 months ago

image image image image image image

主题都是【$queue//+/+/properties/report】,我这里主题名称有个根节点/,这个时历史问题不影响订阅

tocrhz commented 3 months ago

你写错了,别直接写$queue,topic是topic,共享订阅是共享订阅,不是一个属性

---原始邮件--- 发件人: @.> 发送时间: 2024年6月26日(周三) 中午12:09 收件人: @.>; 抄送: @.**@.>; 主题: Re: [tocrhz/mqtt-spring-boot-starter] 默认客户端使用3.1.1,导致无法使用共享订阅主题 (Issue #25)

image.png (view on web) image.png (view on web) image.png (view on web) image.png (view on web) image.png (view on web) image.png (view on web)

主题都是【$queue//+/+/properties/report】,我这里主题名称有个根节点/,这个时历史问题不影响订阅

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

tocrhz commented 3 months ago

你再看下注解,写错了

---原始邮件--- 发件人: @.> 发送时间: 2024年6月26日(周三) 中午12:17 收件人: @.>; 抄送: @.**@.>; 主题: Re: [tocrhz/mqtt-spring-boot-starter] 默认客户端使用3.1.1,导致无法使用共享订阅主题 (Issue #25)

image.png (view on web) image.png (view on web) image.png (view on web) image.png (view on web) image.png (view on web) image.png (view on web)

主题都是【$queue//+/+/properties/report】,我这里主题名称有个根节点/,这个是历史问题不影响订阅

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

QianKun123456 commented 3 months ago

也就是说 我不能细化到topic上,而是需要整个客户端设置enableSharedSubscription默认开启共享订阅?

那如果系统中,有需要共享订阅实现负载均衡的topic,有需要直接订阅的topic,届时系统中都是共享订阅形式了吗?是否支持$share形式,分组共享订阅呢?

tocrhz commented 3 months ago

不是,是你写topic写错了,共享订阅是几部分组成的,$share + group名称 + 实际topic,你在注解value里只需要写实际topic就行了,group在 groups属性里,这个属性如果有值,会在订阅时自动组装成上面的格式。你说的配置只是全局开关,不改变订阅行为

---原始邮件--- 发件人: @.> 发送时间: 2024年6月26日(周三) 中午12:27 收件人: @.>; 抄送: @.**@.>; 主题: Re: [tocrhz/mqtt-spring-boot-starter] 默认客户端使用3.1.1,导致无法使用共享订阅主题 (Issue #25)

也就是说 我不能细化到topic上,而是需要整个客户端设置enableSharedSubscription默认开启共享订阅?

那如果系统中,有需要共享订阅实现负载均衡的topic,有需要直接订阅的topic,届时系统中都是共享订阅形式了吗?是否支持$share形式,分组共享订阅呢?

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

tocrhz commented 3 months ago

@.***这个注解不是四个属性,就更新一下包的版本,最新版本是2.0.2

---原始邮件--- 发件人: @.> 发送时间: 2024年6月26日(周三) 中午12:27 收件人: @.>; 抄送: @.**@.>; 主题: Re: [tocrhz/mqtt-spring-boot-starter] 默认客户端使用3.1.1,导致无法使用共享订阅主题 (Issue #25)

也就是说 我不能细化到topic上,而是需要整个客户端设置enableSharedSubscription默认开启共享订阅?

那如果系统中,有需要共享订阅实现负载均衡的topic,有需要直接订阅的topic,届时系统中都是共享订阅形式了吗?是否支持$share形式,分组共享订阅呢?

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

tocrhz commented 3 months ago

如果包版本不好修改,(在2.0.0之前)注解里还有个属性是 shared,需要设置成true,此时如果groups属性有值,订阅使用$share+group+topic格式,如果groups没有值,订阅会使用$queue+topic格式

---原始邮件--- 发件人: @.> 发送时间: 2024年6月26日(周三) 中午12:27 收件人: @.>; 抄送: @.**@.>; 主题: Re: [tocrhz/mqtt-spring-boot-starter] 默认客户端使用3.1.1,导致无法使用共享订阅主题 (Issue #25)

也就是说 我不能细化到topic上,而是需要整个客户端设置enableSharedSubscription默认开启共享订阅?

那如果系统中,有需要共享订阅实现负载均衡的topic,有需要直接订阅的topic,届时系统中都是共享订阅形式了吗?是否支持$share形式,分组共享订阅呢?

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

QianKun123456 commented 3 months ago

感谢解答,可以完成共享订阅了。还有个小想法,就是配置信息可以不配置clientid不,希望共享订阅实现负载均衡时,并不想给每个子服务取名clientid,我现在是在adapter中修改的@Autowired private MqttProperties mqttProperties; /**

tocrhz commented 3 months ago

本来就不用配置,这个clientid作用是防止一个程序里配置了多个客户端,但是订阅的topic却相同,而且需要不同的处理方法的情况。 订阅时,有clientid的方法,只会在连接对应clientid时,处理订阅,没有clientid的方法,在任意客户端连接时都会处理订阅。 消息接收时也类似,有clientid的,只会处理对应客户端收到的符合topic的消息,没有的会处理所有收到的符合topic的消息。

---原始邮件--- 发件人: @.> 发送时间: 2024年6月26日(周三) 中午1:05 收件人: @.>; 抄送: @.**@.>; 主题: Re: [tocrhz/mqtt-spring-boot-starter] 默认客户端使用3.1.1,导致无法使用共享订阅主题 (Issue #25)

@.* private MqttProperties mqttProperties; /

清除的原有客户端, 增加客户端 "client01"

*/ public void beforeCreate(MqttClientRegistry registry) { String clientId = mqttProperties.getClientId(); if(StringUtils.isEmpty(clientId)){ String uuid = UUID.randomUUID().toString(); mqttProperties.setClientId("data_service"+ uuid); } }

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

tocrhz commented 3 months ago

配置文件里的clientid,不配置也是可以的,会默认使用uuid生成一个

---原始邮件--- 发件人: @.> 发送时间: 2024年6月26日(周三) 中午1:05 收件人: @.>; 抄送: @.**@.>; 主题: Re: [tocrhz/mqtt-spring-boot-starter] 默认客户端使用3.1.1,导致无法使用共享订阅主题 (Issue #25)

@.* private MqttProperties mqttProperties; /

清除的原有客户端, 增加客户端 "client01"

*/ public void beforeCreate(MqttClientRegistry registry) { String clientId = mqttProperties.getClientId(); if(StringUtils.isEmpty(clientId)){ String uuid = UUID.randomUUID().toString(); mqttProperties.setClientId("data_service"+ uuid); } }

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

QianKun123456 commented 3 months ago

当我没有在yml中配置client-id时(MqttSubscribe注解中也没有),启动项目都没有提示SimpleMqttClient中任何成功或者错误的日志呐

QianKun123456 commented 3 months ago

我用2.0.1版本

tocrhz commented 3 months ago

可能是我记错了,不行就 mqtt.client-id=${random.uuid}就可以,spring配置文件支持这么写,abc-${random.uuid} 这些都是支持的

---原始邮件--- 发件人: @.> 发送时间: 2024年6月26日(周三) 中午1:28 收件人: @.>; 抄送: @.**@.>; 主题: Re: [tocrhz/mqtt-spring-boot-starter] 默认客户端使用3.1.1,导致无法使用共享订阅主题 (Issue #25)

当我没有在yml中配置client-id时(MqttSubscribe注解中也没有),启动项目都没有提示SimpleMqttClient中任何成功或者错误的日志呐

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>