【问题标题】:How to connect to Amazon MQ Broker with Mosquitto MQTT Client如何使用 Mosquitto MQTT 客户端连接到 Amazon MQ 代理
【发布时间】:2019-11-18 04:10:16
【问题描述】:

我使用 Amazon MQ 创建了一个单实例代理,并且能够使用 Eclipse Paho MQTT 客户端仅使用用户名和密码订阅代理。

代码:

//sample endpoint from Amazon MQ
final String WIRE_LEVEL_ENDPOINT = "ssl://b-1234a5b6-78cd-901e-2fgh-3i45j6k178l9-1.mq.us-east-2.amazonaws.com:8883";
final String ACTIVE_MQ_USERNAME = "user";
final String ACTIVE_MQ_PASSWORD = "password";

// Specify the topic name and the message text.
final String topic = "whatever";
final String text = "Hello from Amazon MQ!";

// Create the MQTT client and specify the connection options.
final String clientId = "abc123";
final MqttClient client = new MqttClient(WIRE_LEVEL_ENDPOINT, clientId);
final MqttConnectOptions connOpts = new MqttConnectOptions();

// Pass the username and password.
connOpts.setUserName(ACTIVE_MQ_USERNAME);
connOpts.setPassword(ACTIVE_MQ_PASSWORD.toCharArray());

// Create a session and subscribe to a topic filter.
client.connect(connOpts);
client.setCallback(this);
client.subscribe(topic);

// Create a message.
final MqttMessage message = new MqttMessage(text.getBytes());

// Publish the message to a topic.
client.publish(topic, message);
System.out.println("Published message.");

// Wait for the message to be received.
Thread.sleep(3000L);

// Clean up the connection.
client.disconnect();

运行上面的代码表明我能够订阅主题并接收到我发送的消息。

然而,对 mosquitto_sub 客户端做同样的事情,它给了我协议错误:

mosquitto_sub -h host -p 8883 -u user -P password -t whatever -i abc123

错误:

错误:与服务器通信时发生网络协议错误 经纪人。

我搜索了如何使用 SSL 连接到 MQTT Broker。我发现我需要为客户端设置一个证书。

但是在没有任何证书设置的情况下,它是如何在 java 中工作的???

【问题讨论】:

  • 编辑问题以包含 WIRE_LEVEL_ENDPOINT 是什么。您可以编辑实际的主机名,但我们需要查看架构和任何端口信息。
  • 另外请不要发命令行的图片,发文字并格式化。图片中的文字无法搜索

标签: mqtt paho amazon-mq


【解决方案1】:

因为要使用 mosquitto_sub 启用 SSL 支持,您必须通过 --cafile--capath。没有它们,应用甚至不会尝试创建安全连接。

它可以在 java 中工作,因为它可以访问公共 CA 证书列表来检查代理证书。 mosquitto_sub 没有该列表,因此您需要向其传递证书以进行验证。

【讨论】:

  • 我明白了...谢谢!