如何使用Mosquitto MQTT客户端连接到Amazon MQ Broker

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

代码:

//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。我发现我需要为客户端设置证书。

如何使用Mosquitto MQTT客户端连接到Amazon MQ Broker

但是在Java中它如何在没有任何证书集的情况下起作用?

songweixu 回答:如何使用Mosquitto MQTT客户端连接到Amazon MQ Broker

由于要通过mosquitto_sub启用SSL支持,您必须传递--cafile--capath。没有它们,该应用程序甚至不会尝试创建安全连接。

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

本文链接:https://www.f2er.com/3083657.html

大家都在问