0

我正在使用使用 Java Eclipse Paho库的 MQTT 模拟器脚本。我进行了设备注册,按照Hono 入门指南中的说明使用 git bash 终端手动设置了设备 ID、租户 ID,还按照入门指南中的说明启动了示例 hono 客户端,然后运行它模拟器代码。

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

public class MqttPublishHeartRate {

    public static void main(String[] args) {

        String topic = "telemetry";
        String content;
        // int qos = 2;
        String mqttAdaptorIp = "tcp://10.103.102.133:1883";
        String tenantId = "f9ea5afe-2c11-4ab8-aa72-f2dd5df028a9";
        String deviceId = "713af6cc-f06c-4b1d-bfd2-2a2e0811a35e";
        String password = "password";
        MemoryPersistence persistence = new MemoryPersistence();

        try {
            while (true) {
                wait(10000);
                content = "{\"heartrate\":" + heartRateRandom() + "}";

                MqttClient sampleClient = new MqttClient(mqttAdaptorIp, tenantId, persistence);
                MqttConnectOptions connOpts = new MqttConnectOptions();
                connOpts.setUserName(deviceId);
                connOpts.setPassword(password.toCharArray());
                connOpts.setKeepAliveInterval(15);
                connOpts.setConnectionTimeout(30);

                System.out.println("Connecting to broker: " + mqttAdaptorIp);
                sampleClient.connect(connOpts);
                System.out.println("Connected");

                System.out.println("Publishing message: " + content);
                MqttMessage message = new MqttMessage(content.getBytes());
                // message.setQos(qos);

                sampleClient.publish(topic, message);
                System.out.println("Message published");

                sampleClient.disconnect();
                System.out.println("Disconnected");
            }
        } catch (MqttException me) {
            System.out.println("reason " + me.getReasonCode());
            System.out.println("msg " + me.getMessage());
            System.out.println("loc " + me.getLocalizedMessage());
            System.out.println("cause " + me.getCause());
            System.out.println("excep " + me);
            me.printStackTrace();
        }
    }
    
    public static void wait(int ms) {
        try {
            Thread.sleep(ms);
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
    }
    
    public static int heartRateRandom() {
        int heartRate = (int) Math.floor(Math.random() * 40) + 60;
        return heartRate;
    }
}

但我得到了这个例外:

Connecting to broker: tcp://10.103.102.133:1883
reason 32109
msg Connection lost
loc Connection lost
cause java.io.EOFException
excep Connection lost (32109) - java.io.EOFException
Connection lost (32109) - java.io.EOFException
    at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:181)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.io.EOFException
    at java.base/java.io.DataInputStream.readByte(DataInputStream.java:272)
    at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:92)
    at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:133)
    ... 6 more

我的 Hono 设置在本地 minikube 中,这是 kubectl get service 的输出。

c:\Users\HP>kubectl get service -n hono
NAME                                            TYPE           CLUSTER-IP       EXTERNAL-IP      PORT(S)                           AGE
eclipse-hono-adapter-amqp-vertx                 LoadBalancer   10.107.174.140   10.107.174.140   5672:32672/TCP,5671:32671/TCP     3d17h
eclipse-hono-adapter-amqp-vertx-headless        ClusterIP      None             <none>           <none>                            3d17h
eclipse-hono-adapter-http-vertx                 LoadBalancer   10.101.233.249   10.101.233.249   8080:30080/TCP,8443:30443/TCP     3d17h
eclipse-hono-adapter-http-vertx-headless        ClusterIP      None             <none>           <none>                            3d17h
eclipse-hono-adapter-mqtt-vertx                 LoadBalancer   10.103.102.133   10.103.102.133   1883:31883/TCP,8883:30883/TCP     3d17h
eclipse-hono-adapter-mqtt-vertx-headless        ClusterIP      None             <none>           <none>                            3d17h
eclipse-hono-artemis                            ClusterIP      10.105.136.215   <none>           5671/TCP                          3d17h
eclipse-hono-dispatch-router                    ClusterIP      10.103.15.52     <none>           5673/TCP                          3d17h
eclipse-hono-dispatch-router-ext                LoadBalancer   10.104.151.34    10.104.151.34    15671:30671/TCP,15672:30672/TCP   3d17h
eclipse-hono-grafana                            ClusterIP      10.110.248.10    <none>           3000/TCP                          3d17h
eclipse-hono-prometheus-server                  ClusterIP      10.110.243.239   <none>           9090/TCP                          3d17h
eclipse-hono-service-auth                       ClusterIP      10.105.228.68    <none>           5671/TCP                          3d17h
eclipse-hono-service-auth-headless              ClusterIP      None             <none>           <none>                            3d17h
eclipse-hono-service-device-registry            ClusterIP      10.101.64.187    <none>           5671/TCP,8443/TCP                 3d17h
eclipse-hono-service-device-registry-ext        LoadBalancer   10.105.144.218   10.105.144.218   28080:31080/TCP,28443:31443/TCP   3d17h
eclipse-hono-service-device-registry-headless   ClusterIP      None             <none>           <none>                            3d17h

请告知如何解决此问题。

4

1 回答 1

1

想通了,密码应该是auth-id@tenant的形式,例如sensor1@DEFAULT_TENANT ( https://www.eclipse.org/hono/docs/user-guide/mqtt-adapter/ ) 所以修改代码为:

String tenantId = "DEFAULT_TENANT";
String authId = "sensor1";
String username = authId+"@"+tenantId;

把它放在这里以防其他人可能面临这个问题。

于 2020-08-29T19:13:19.653 回答