首页 > 解决方案 > How to set connection friendly name RabbitMQ --Python

问题描述

Hello i am playing around with RabbitMq and i have a question. I have many parallel nodes running and are connected on rabbitmq-server. The thing is that in conncetions are stated like ip:port Also under ip:port there is a ?. Is there a way to set up a friendly name for python 3.6 ?

enter image description here

P.S I have tried something like this without success

                    parameters = pika.ConnectionParameters(
                        host='localhost',
                        virtual_host='/',
                        client_properties={
                            'connection_name': 'random name',
                        },
                    )
                    connection = pika.BlockingConnection(parameters)

P.S Also i found a related question about golang tho. Here How to set connection friendly name

Versions

pika = '1.1.0'
RabbitMq = '3.8.0'
Erlang = '22.1.1'
Python = ' 3.6.8'

Any ideas? :)

标签: python-3.xrabbitmqrabbitmqctl

解决方案


此代码设置连接名称,如您在此处看到的:

在此处输入图像描述

import functools
import logging
import pika

LOG_FORMAT = ('%(levelname) -10s %(asctime)s %(name) -30s %(funcName) '
              '-35s %(lineno) -5d: %(message)s')
LOGGER = logging.getLogger(__name__)

logging.basicConfig(level=logging.DEBUG, format=LOG_FORMAT)


def on_message(chan, method_frame, _header_frame, body, userdata=None):
    """Called when a message is received. Log message and ack it."""
    LOGGER.info('Userdata: %s Message body: %s', userdata, body)
    chan.basic_ack(delivery_tag=method_frame.delivery_tag)


def main():
    """Main method."""
    credentials = pika.PlainCredentials('guest', 'guest')
    props = { 'connection_name' : 'https://stackoverflow.com/q/58275505/1466825' }
    parameters = pika.ConnectionParameters('localhost', credentials=credentials, client_properties=props)
    connection = pika.BlockingConnection(parameters)

    channel = connection.channel()
    channel.exchange_declare(
        exchange='test_exchange',
        exchange_type='direct',
        passive=False,
        durable=True,
        auto_delete=False)
    channel.queue_declare(queue='standard', auto_delete=True)
    channel.queue_bind(
        queue='standard', exchange='test_exchange', routing_key='standard_key')
    channel.basic_qos(prefetch_count=1)

    on_message_callback = functools.partial(
        on_message, userdata='on_message_userdata')
    channel.basic_consume('standard', on_message_callback)

    try:
        channel.start_consuming()
    except KeyboardInterrupt:
        channel.stop_consuming()

    connection.close()


if __name__ == '__main__':
    main()

注意: RabbitMQ 团队会监控rabbitmq-users 邮件列表,有时只会在 StackOverflow 上回答问题。


推荐阅读