[RabbitMQ #99] Server Connection 방법

2022. 2. 20. 18:172018년 이전 관심사/통신프로토콜

반응형

pika를 이용해 RabbitMQ Server Connection 방법을 정리 한다.

 

ConnectionParameters

  • ConnectionParameter를 이용하여 접속할 Server 정보를 입력하는 방법
  • 장점 : host, port 및 RabbitMQ의 virtual_host 설정도 가능
import pika

# Set the connection parameters to connect to rabbit-server1 on port 5672
# on the / virtual host using the username "guest" and password "guest"
credentials = pika.PlainCredentials('guest', 'guest')
parameters = pika.ConnectionParameters('rabbit-server1',
                                       5672,
                                       '/',
                                       credentials)
                                       
pika.BlockingConnection(parameters)
  • Parameters
 def __init__( # pylint: disable=R0913,R0914
            self,
            host=_DEFAULT,
            port=_DEFAULT,
            virtual_host=_DEFAULT,
            credentials=_DEFAULT,
            channel_max=_DEFAULT,
            frame_max=_DEFAULT,
            heartbeat=_DEFAULT,
            ssl_options=_DEFAULT,
            connection_attempts=_DEFAULT,
            retry_delay=_DEFAULT,
            socket_timeout=_DEFAULT,
            stack_timeout=_DEFAULT,
            locale=_DEFAULT,
            blocked_connection_timeout=_DEFAULT,
            client_properties=_DEFAULT,
            tcp_options=_DEFAULT,
            **kwargs):

 

URLParameters

  • URL 문자열을 이용하여 RabbitMQ 서버에 연결
brokerUserName = "admin"
brokerUserPassword = "admin"
brokerUrl = "192.168.0.40"
brokerPort = 5672

connectionUrl = "amqp://{0}:{1}@{2}:{3}/".format(brokerUserName, brokerUserPassword,
                                                 brokerUrl, brokerPort)
parameters = pika.URLParameters(connectionUrl)
pika.BlockingConnection(parameters)

 

반응형