繁体   English   中英

扩展与蚊子经纪人的Mqtt连接

[英]Scaling Mqtt connections to mosquitto broker

我正在尝试建立到mqtt代理的多个(100k)mqtt连接,但是使用paho java客户端不能超过5000。

我从另一个帖子中获得了很好的指针,但是当我越过5000个线程限制时遇到了错误。 错误:java.lang.OutOfMemoryError:无法创建新的本机线程

我在云中有一个16gb RAM / 16VCPU ubuntu实例,因此资源在这里看起来不是问题。

最大MQTT连接

我当前的ulimit设置为以下。

ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 128311
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 30000000
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 200000
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

此外,该帖子还讨论了添加21个虚拟地址的问题。 在此云实例上,我将eth0.cfg设置为:

----------------------
# The primary network interface
auto eth0
iface eth0 inet dhcp
---------------------------

如何在此处添加虚拟IP地址?

这里的任何指针都会有所帮助。

谢谢。

编辑:这是我的线程类代码-公共类SendMessage扩展了Thread {public String clientId; public String topicId; 公共字符串用户名; 公共字符串密码; 公共字符串有效负载类型;

    SendMessage(String topicId,String clientId,String username,String password,String payloadType) {
        this.topicId = topicId;
        this.password = password;
        this.username = username;
        this.clientId = clientId;
        this.payloadType = payloadType;
    }
    SendMessage(String topicId) {
        this.topicId = topicId;
    }
    public void  run()  {


    // Perform the requested action
      try {

    ArrayList<MqttClient> sampleClient = new ArrayList<MqttClient>();
    MqttConnectOptions connOpts = new MqttConnectOptions();
    connOpts.setCleanSession(true);
    connOpts.setPassword(password.toCharArray());
    connOpts.setUserName(username);

    for (int i = 0; i < connectionCountPerThread ; i++) {
    MqttClient newClient = new MqttClient("tcp://" + url, clientId + "_" +i);
                 sampleClient.add(newClient);
                 newClient.connect(connOpts);

             }


            MqttMessage message1;
            int qos = 0;
            MockObservations mo = new MockObservations();
            for (int i = 0 ; i < msgCount ; i++) {
            for (int conn = 0; conn < connectionCountPerThread ; conn++) {

             message1 = new MqttMessage(mo.getSensorReading(payloadType).getBytes());
             message1.setQos(qos);
             sampleClient.get(conn).publish(topicId, message1);

            }

            }
            for (int conn = 0; conn < connectionCountPerThread ; conn++) {
            sampleClient.get(conn).disconnect();
            }

        } 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();
        }
    }
}

您创建了太多线程。 据我所知,Paho每个单独的连接使用2个线程。 使用“每个连接线程数”模型不会太过分。 我相信自己创建这样的负载测试工具的唯一方法是使用Java的NIO方法。

要获得更高的连接(并且延迟很长),可以尝试增加JVM堆大小。

这可能对您有帮助: https : //en.wikipedia.org/wiki/C10k_problem

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM