我使用 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 中工作???