ActiveMQ Artemis Queue 不会将消息平均分发到 STOMP-Consumers。怎样才能达到偶数load-balancing?

ActiveMQ Artemis Queue does not distribute messages evenly to the STOMP-Consumers. How can we achieve an even load-balancing?

如图所示,在 queue - /queue/msgs 上发布的 n 条消息中,消费者之间的分布(STOMP-Consumer 1STOMP-Consumer 2) 是不均匀的。我可以观察到,STOMP-Consumer 2 只收到了 n 条消息中的一条消息。

两个消费者都传递了完全相同的 STOMP headers。那些如下-

STOMP 连接 Headers

踩踏订阅Headers

Broker.xml

    <?xml version="1.0"?>
    <configuration
        xmlns="urn:activemq"
        xmlns:xi="http://www.w3.org/2001/XInclude"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:activemq /schema/artemis-configuration.xsd">
        <core
            xmlns="urn:activemq:core" xsi:schemaLocation="urn:activemq:core ">
            <name>activemq-558f6696fc-2kx8q</name>
            <persistence-enabled>true</persistence-enabled>
            <journal-type>ASYNCIO</journal-type>
            <paging-directory>data/paging</paging-directory>
            <bindings-directory>data/bindings</bindings-directory>
            <journal-directory>data/journal</journal-directory>
            <large-messages-directory>data/large-messages</large-messages-directory>
            <journal-datasync>true</journal-datasync>
            <journal-min-files>2</journal-min-files>
            <journal-pool-files>10</journal-pool-files>
            <journal-device-block-size>4096</journal-device-block-size>
            <journal-file-size>10M</journal-file-size>
            <journal-buffer-timeout>24000</journal-buffer-timeout>
            <journal-max-io>4096</journal-max-io>
            <disk-scan-period>5000</disk-scan-period>
            <max-disk-usage>90</max-disk-usage>
            <critical-analyzer>true</critical-analyzer>
            <critical-analyzer-timeout>120000</critical-analyzer-timeout>
            <critical-analyzer-check-period>60000</critical-analyzer-check-period>
            <critical-analyzer-policy>HALT</critical-analyzer-policy>
            <page-sync-timeout>368000</page-sync-timeout>
            <acceptors>
                <acceptor name="artemis">tcp://0.0.0.0:61616?tcpSendBufferSize=1048576;tcpReceiveBufferSize=1048576;amqpMinLargeMessageSize=102400;protocols=CORE,AMQP,STOMP,HORNETQ,MQTT,OPENWIRE;useEpoll=true;amqpCredits=1000;amqpLowCredits=300;amqpDuplicateDetection=true</acceptor>
                <acceptor name="amqp">tcp://0.0.0.0:5672?tcpSendBufferSize=1048576;tcpReceiveBufferSize=1048576;protocols=AMQP;useEpoll=true;amqpCredits=1000;amqpLowCredits=300;amqpMinLargeMessageSize=102400;amqpDuplicateDetection=true</acceptor>
                <acceptor name="stomp">tcp://0.0.0.0:61613?tcpSendBufferSize=1048576;tcpReceiveBufferSize=1048576;protocols=STOMP;useEpoll=true</acceptor>
                <acceptor name="hornetq">tcp://0.0.0.0:5445?anycastPrefix=jms.queue.;multicastPrefix=jms.topic.;protocols=HORNETQ,STOMP;useEpoll=true</acceptor>
                <acceptor name="mqtt">tcp://0.0.0.0:1883?tcpSendBufferSize=1048576;tcpReceiveBufferSize=1048576;protocols=MQTT;useEpoll=true</acceptor>
            </acceptors>
            <security-settings>
                <security-setting match="#">
                    <permission type="createNonDurableQueue" roles="amq"/>
                    <permission type="deleteNonDurableQueue" roles="amq"/>
                    <permission type="createDurableQueue" roles="amq"/>
                    <permission type="deleteDurableQueue" roles="amq"/>
                    <permission type="createAddress" roles="amq"/>
                    <permission type="deleteAddress" roles="amq"/>
                    <permission type="consume" roles="amq"/>
                    <permission type="browse" roles="amq"/>
                    <permission type="send" roles="amq"/>
                    <!-- we need this otherwise ./artemis data imp wouldn't work -->
                    <permission type="manage" roles="amq"/>
                </security-setting>
            </security-settings>
            <message-expiry-scan-period>30000</message-expiry-scan-period>
            <connection-ttl-override>60000</connection-ttl-override>
            <address-settings>
                <address-setting match="activemq.management#">
                    <dead-letter-address>DLQ</dead-letter-address>
                    <expiry-address>ExpiryQueue</expiry-address>
                    <redelivery-delay>0</redelivery-delay>
                    <!-- with -1 only the global-max-size is in use for limiting -->
                    <max-size-bytes>-1</max-size-bytes>
                    <message-counter-history-day-limit>10</message-counter-history-day-limit>
                    <address-full-policy>PAGE</address-full-policy>
                    <auto-create-queues>true</auto-create-queues>
                    <auto-create-addresses>true</auto-create-addresses>
                    <auto-create-jms-queues>true</auto-create-jms-queues>
                    <auto-create-jms-topics>true</auto-create-jms-topics>
                </address-setting>
                <address-setting match="/queue/#">
                    <default-address-routing-type>ANYCAST</default-address-routing-type>
                    <default-queue-routing-type>ANYCAST</default-queue-routing-type>
                </address-setting>
                <address-setting match="/topic/#">
                    <default-address-routing-type>MULTICAST</default-address-routing-type>
                    <default-queue-routing-type>MULTICAST</default-queue-routing-type>
                </address-setting>
                <address-setting match="#">
                    <dead-letter-address>DLQ</dead-letter-address>
                    <expiry-address>ExpiryQueue</expiry-address>
                    <auto-delete-queues>false</auto-delete-queues>
                    <auto-delete-jms-queues>false</auto-delete-jms-queues>
                    <auto-delete-jms-topics>false</auto-delete-jms-topics>
                    <auto-delete-addresses>false</auto-delete-addresses>
                    <max-size-bytes>-1</max-size-bytes>
                    <message-counter-history-day-limit>10</message-counter-history-day-limit>
                    <address-full-policy>PAGE</address-full-policy>
                    <auto-create-queues>true</auto-create-queues>
                    <auto-create-addresses>true</auto-create-addresses>
                    <auto-create-jms-queues>true</auto-create-jms-queues>
                    <auto-create-jms-topics>true</auto-create-jms-topics>
                    <redelivery-delay-multiplier>1</redelivery-delay-multiplier>
                    <redelivery-collision-avoidance-factor>0.15</redelivery-collision-avoidance-factor>
                    <max-redelivery-delay>50000</max-redelivery-delay>
                    <default-consumer-window-size>0</default-consumer-window-size>
                </address-setting>
            </address-settings>
            <addresses>
                <address name="DLQ">
                    <anycast>
                        <queue name="DLQ"/>
                    </anycast>
                </address>
                <address name="ExpiryQueue">
                    <anycast>
                        <queue name="ExpiryQueue"/>
                    </anycast>
                </address>
            </addresses>
            <wildcard-addresses>
                <routing-enabled>true</routing-enabled>
                <delimiter>/</delimiter>
                <any-words>#</any-words>
                <single-word>*</single-word>
            </wildcard-addresses>
        </core>
    </configuration>

正在使用的acceptor是端口为61616的artemis acceptor

使用 acceptor URL 参数 stompConsumerCredits。默认值为 10240 字节,这意味着代理将向每个客户端发送 10KB 的消息。如果第一个客户端足够快地处理这些消息,那么其他消费者可能会挨饿。假设你的 STOMP 消费者正在使用 stomp acceptor 那么你可以尝试这样的事情:

<acceptor name="stomp">tcp://0.0.0.0:61613?tcpSendBufferSize=1048576;tcpReceiveBufferSize=1048576;protocols=STOMP;useEpoll=true;stompConsumerCredits=1</acceptor>

请记住,基于 stompConsumerCredits 将消息分批发送给消费者是一种性能优化,它可以防止客户端和代理之间的大量网络 round-trips 来获取消息。我建议您测试 stompConsumerCredits 的不同值以找到您的 use-case 的最佳性能,因为对 2 个消费者使用较小的值实际上可能 减少 整体消息吞吐量 vs.对 1 个消费者来说价值很大。

此外,值得注意的是,在 ActiveMQ Artemis 2.16.0(尚未发布)中,您将能够在 STOMP [=19] 上使用自定义 header(即 consumer-window-size) =] 框架来针对每个客户端进行调整,而不是将其设置为所有消费者的 acceptor 级别。