简体   繁体   English

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

[英]Scaling Mqtt connections to mosquitto broker

I am trying to establish multiple(100k) mqtt connections to mqtt broker but cannot go beyond 5000 using paho java client. 我正在尝试建立到mqtt代理的多个(100k)mqtt连接,但是使用paho java客户端不能超过5000。

I got good pointers from another post but running into error as I cross 5000 thread limit. 我从另一个帖子中获得了很好的指针,但是当我越过5000个线程限制时遇到了错误。 Error: java.lang.OutOfMemoryError: unable to create new native thread 错误:java.lang.OutOfMemoryError:无法创建新的本机线程

I have a 16gb RAM / 16VCPU ubuntu instance in cloud so resources does not look like an issue here. 我在云中有一个16gb RAM / 16VCPU ubuntu实例,因此资源在这里看起来不是问题。

Max MQTT connections 最大MQTT连接

My current ulimit is set to below. 我当前的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

Also, the post talks about adding 21 virtual addresses. 此外,该帖子还讨论了添加21个虚拟地址的问题。 On this cloud instance, I have eth0.cfg set to: 在此云实例上,我将eth0.cfg设置为:

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

How to I add virtual IP addresses here ? 如何在此处添加虚拟IP地址?

Any pointers here would help. 这里的任何指针都会有所帮助。

Thanks. 谢谢。

Edit: Here is my thread class code - public class SendMessage extends Thread { public String clientId; 编辑:这是我的线程类代码-公共类SendMessage扩展了Thread {public String clientId; public String topicId; public String topicId; public String username; 公共字符串用户名; public String password; 公共字符串密码; public String payloadType; 公共字符串有效负载类型;

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

You created way too much Threads. 您创建了太多线程。 Paho uses as far as I know 2 Threads per individual connection. 据我所知,Paho每个单独的连接使用2个线程。 You won't get too far with a "Thread per Connection" model. 使用“每个连接线程数”模型不会太过分。 I believe the only way to create such a load testing tool on your own is by using an NIO approach with Java. 我相信自己创建这样的负载测试工具的唯一方法是使用Java的NIO方法。

To get higher connections (and very bad latency), you can try to increase the JVM heap size. 要获得更高的连接(并且延迟很长),可以尝试增加JVM堆大小。

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

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

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