qweeze / rstream

A Python asyncio-based client for RabbitMQ Streams
MIT License
82 stars 13 forks source link

publish raise socket.gaierror: [Errno -2] Name or service not known #14

Closed xuqinghan closed 2 years ago

xuqinghan commented 2 years ago

ubuntu 20.04 + python 3.10 + rabbitMQ 3.9.13 from docker image dockerfile:

FROM rabbitmq:3-management
RUN rabbitmq-plugins enable rabbitmq_web_stomp rabbitmq_stream

docker-compose.yml

version: '3.8'

services:
  rabbitmq-webstomp:
    restart: always
    image: rabbitmq-webstomp
    build: ./
    command: rabbitmq-server
    environment:
      RABBITMQ_ERLANG_COOKIE: SWQOKODSQALRPCLNMEQG
      RABBITMQ_DEFAULT_USER: guest
      RABBITMQ_DEFAULT_PASS: guest
    ports:
      - "15672:15672" 
      - "5672:5672" 
      - "15674:15674" 
      - "61613:61613" 
      - "5552:5552" 

run readme demo:

import asyncio
from rstream import Producer, AMQPMessage

async def publish():
    async with Producer('localhost', username='guest', password='guest') as producer:
        await producer.create_stream('mystream', exists_ok=True)

        for i in range(100):
            amqp_message = AMQPMessage(
                body='hello: {}'.format(i),
            )
            #msg = f'hello: {i}'
            await producer.publish('mystream', amqp_message)

asyncio.run(publish())

the queue "mystream" is created , http://127.0.0.1:15672/#/queues/%2F/mystream, but "await producer.publish('mystream', amqp_message)" failed

Traceback (most recent call last): File "/home/xuqinghan/dev/test-cljs-rabbitmq/test/test_rstream.py", line 19, in asyncio.run(publish()) File "/usr/local/lib/python3.10/asyncio/runners.py", line 44, in run return loop.run_until_complete(main) File "/usr/local/lib/python3.10/asyncio/base_events.py", line 641, in run_until_complete return future.result() File "/home/xuqinghan/dev/test-cljs-rabbitmq/test/test_rstream.py", line 17, in publish await producer.publish('mystream', amqp_message) File "/home/xuqinghan/.local/lib/python3.10/site-packages/rstream/producer.py", line 196, in publish publishing_ids = await self.publish_batch( File "/home/xuqinghan/.local/lib/python3.10/site-packages/rstream/producer.py", line 158, in publish_batch publisher = await self._get_or_create_publisher(stream, publisher_name) File "/home/xuqinghan/.local/lib/python3.10/site-packages/rstream/producer.py", line 112, in _get_or_create_publisher client = await self._get_or_create_client(stream) File "/home/xuqinghan/.local/lib/python3.10/site-packages/rstream/producer.py", line 96, in _get_or_create_client self._clients[stream] = await self._pool.get((leader.host, leader.port)) File "/home/xuqinghan/.local/lib/python3.10/site-packages/rstream/client.py", line 468, in get self._clients[addr] = await self.new(addr) File "/home/xuqinghan/.local/lib/python3.10/site-packages/rstream/client.py", line 482, in new await client.start() File "/home/xuqinghan/.local/lib/python3.10/site-packages/rstream/client.py", line 136, in start await self._conn.open() File "/home/xuqinghan/.local/lib/python3.10/site-packages/rstream/connection.py", line 33, in open self._reader, self._writer = await asyncio.wait_for( File "/usr/local/lib/python3.10/asyncio/tasks.py", line 445, in wait_for return fut.result() File "/usr/local/lib/python3.10/asyncio/streams.py", line 47, in openconnection transport, = await loop.create_connection( File "/usr/local/lib/python3.10/asyncio/base_events.py", line 1016, in create_connection infos = await self._ensure_resolved( File "/usr/local/lib/python3.10/asyncio/base_events.py", line 1395, in _ensure_resolved return await loop.getaddrinfo(host, port, family=family, type=type, File "/usr/local/lib/python3.10/asyncio/base_events.py", line 855, in getaddrinfo return await self.run_in_executor( File "/usr/local/lib/python3.10/concurrent/futures/thread.py", line 58, in run result = self.fn(*self.args, **self.kwargs) File "/usr/local/lib/python3.10/socket.py", line 955, in getaddrinfo for res in _socket.getaddrinfo(host, port, family, type, proto, flags): socket.gaierror: [Errno -2] Name or service not known

qweeze commented 2 years ago

You should specify advertised_host in stream plugin's configuration.

The reason for that is when publishing messages, we need to make sure that the client is connected to a writable broker (leader)

xuqinghan commented 2 years ago

You should specify advertised_host in stream plugin's configuration.

The reason for that is when publishing messages, we need to make sure that the client is connected to a writable broker (leader)

Thanks. I read https://blog.rabbitmq.com/posts/2021/07/connecting-to-streams/ https://www.rabbitmq.com/stream.html

finally follow this https://blog.rabbitmq.com/posts/2021/07/rabbitmq-streams-first-application

docker-compose.yml

version: '3.8'

services:
  rabbitmq-stream:
    restart: always
    #image: rabbitmq-webstomp
    build: ./ 
    command: rabbitmq-server
    environment:
      RABBITMQ_ERLANG_COOKIE: SWQOKODSQALRPCLNMEQG
      RABBITMQ_DEFAULT_USER: guest
      RABBITMQ_DEFAULT_PASS: guest
      RABBITMQ_SERVER_ADDITIONAL_ERL_ARGS: -rabbitmq_stream advertised_host localhost

    ports:
      - "15672:15672" # mangerment webpage
      - "5672:5672" #rabbitmq
      - "15674:15674" #web stomp
      - "61613:61613" #stomp
      - "5552:5552" # stream

add "RABBITMQ_SERVER_ADDITIONAL_ERL_ARGS: -rabbitmq_stream advertised_host localhost".

it works.

Thanks!