Skip to content

Unable to create producer when connected to localhost and rabbit uses a hostname #296

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Zerpet opened this issue Sep 14, 2023 · 4 comments · Fixed by #305
Closed

Unable to create producer when connected to localhost and rabbit uses a hostname #296

Zerpet opened this issue Sep 14, 2023 · 4 comments · Fixed by #305
Assignees
Labels
bug Something isn't working

Comments

@Zerpet
Copy link
Member

Zerpet commented Sep 14, 2023

Describe the bug

From this conversation: spring-projects/spring-amqp#2522

When RabbitMQ is running in a environment where rabbit node name is not resolvable by the clients, and the client connects locally via the loopback, the client library fails to create a producer and retries infinitely.

This kind of setups are common for local development environments, where rabbit may resolve its node name as foobar, and the client connects via localhost. One example is running rabbit locally, and another, running rabbit in a container.

Reproduction steps

  1. Start RabbitMQ in a container, with a specific hostname
    docker run --hostname foobar --rm --name bunny -p 5672:5672 -p 15672:15672 -p 5552:5552 rabbitmq:3-management
    docker exec bunny rabbitmq-plugins enable rabbitmq_stream_management
    
  2. Create a StreamSystem that connects to localhost and default port 5552
  3. Create a stream
  4. Create a producer using Producer.Create()

Observe that the client does not create the producer connection, and it retries infinitely.

Expected behavior

The client library connects via localhost, or gives up connecting.

Additional context

Related rabbitmq/rabbitmq-server#9424

The issue can be workarounded using an address resolver, or changing the advertised host/port to always be localhost and the default stream port. However, there's a fair point made in the Spring Boot discussion: spring-projects/spring-amqp#2522 (comment). In summary, if there's no load balancer involved (address resolved was created for LB setups), and there's no rabbit cluster, then the localhost and default loopback user configuration should work.

This may arguably be an improvement, rather than a bug.

@Zerpet Zerpet added the bug Something isn't working label Sep 14, 2023
@Zerpet Zerpet self-assigned this Sep 14, 2023
@aeb-dev
Copy link

aeb-dev commented Sep 14, 2023

I am not sure whether this is the same issue here is my environment:

Deployed to windows wsl kind.
Accessing through nodeport.
Rabbitmq version RabbitMQ 3.13.0-beta.6

AddressResolver = new AddressResolver(new DnsEndPoint("localhost", 30552)),
Endpoints = new List<EndPoint> { new DnsEndPoint("localhost", 30552) },

If I don't provide AddressResolver creating consumer and producer fails
if I don't provide Endpoints creating streams fails

@MikhailTushev
Copy link
Contributor

you need add advertised_host localhost for rmq
example of docker compose without enabled plugin :

rabbitmq:
    container_name: rabbitmq
    image: rabbitmq:3.11-management
    ports:
      - 5552:5552
      - 5672:5672
      - 15672:15672
    volumes:
      - ~/.docker-conf/rabbitmq/data/:/var/lib/rabbitmq/
      - ~/.docker-conf/rabbitmq/log/:/var/log/rabbitmq
    environment:
      - RABBITMQ_SERVER_ADDITIONAL_ERL_ARGS=-rabbitmq_stream advertised_host localhost

@Zerpet
Copy link
Member Author

Zerpet commented Sep 19, 2023

I am not sure whether this is the same issue here is my environment:

Deployed to windows wsl kind. Accessing through nodeport. Rabbitmq version RabbitMQ 3.13.0-beta.6

AddressResolver = new AddressResolver(new DnsEndPoint("localhost", 30552)),
Endpoints = new List<EndPoint> { new DnsEndPoint("localhost", 30552) },

If I don't provide AddressResolver creating consumer and producer fails if I don't provide Endpoints creating streams fails

This is related in a slightly different setup. The Endpoints must be provided always in your case, because you are using a non-standard port (since you are exposing via NodePort). After resolving this issue, you will not need to provide the AddressResolver.

@Zerpet
Copy link
Member Author

Zerpet commented Sep 19, 2023

you need add advertised_host localhost for rmq

After resolving this issue, you won't even need to advertise the host/port 🙂

Zerpet added a commit that referenced this issue Sep 19, 2023
Related to #296

It is common to have a local instance of RabbitMQ running locally,
either via a local rabbit (e.g. installed from source), or inside a
container (e.g. Docker or local Kubernetes). In both cases, our client
may not work without providing an address resolver, or without changing
the advertised host/port in rabbit via an env variable. Specifically,
our clients won't work if the client can't resolve the hostname of the
local machine.

For exampe, a laptop may have a hostname mylaptop.some-company.com.
Rabbit won't be impacted as long as it works in a single-node
configuration. However, the client smart features to locate the stream
leaders and replicas will get "confused" because it can't resolve the
hostname.

This commit changes the smart layer, so that it won't try to locate the
stream replicas if the client is connected to localhost. Instead, it
will force the producer/consumer connections to be localhost, and it
will assume that stream replicas/leader are in localhost. This is true
for single-node rabbits in local development environments. The smart
feature to locate leader/replicas remains unchanged for non-local
connections.

Signed-off-by: Aitor Perez Cedres <[email protected]>
Zerpet added a commit that referenced this issue Sep 21, 2023
Related to #296

It is common to have a local instance of RabbitMQ running locally,
either via a local rabbit (e.g. installed from source), or inside a
container (e.g. Docker or local Kubernetes). In both cases, our client
may not work without providing an address resolver, or without changing
the advertised host/port in rabbit via an env variable. Specifically,
our clients won't work if the client can't resolve the hostname of the
local machine.

For exampe, a laptop may have a hostname mylaptop.some-company.com.
Rabbit won't be impacted as long as it works in a single-node
configuration. However, the client smart features to locate the stream
leaders and replicas will get "confused" because it can't resolve the
hostname.

This commit changes the smart layer, so that it won't try to locate the
stream replicas if the client is connected to localhost. Instead, it
will force the producer/consumer connections to be localhost, and it
will assume that stream replicas/leader are in localhost. This is true
for single-node rabbits in local development environments. The smart
feature to locate leader/replicas remains unchanged for non-local
connections.

Signed-off-by: Aitor Perez Cedres <[email protected]>
Gsantomaggio pushed a commit that referenced this issue Sep 21, 2023
* Improve local development experience

Related to #296

It is common to have a local instance of RabbitMQ running locally,
either via a local rabbit (e.g. installed from source), or inside a
container (e.g. Docker or local Kubernetes). In both cases, our client
may not work without providing an address resolver, or without changing
the advertised host/port in rabbit via an env variable. Specifically,
our clients won't work if the client can't resolve the hostname of the
local machine.

For exampe, a laptop may have a hostname mylaptop.some-company.com.
Rabbit won't be impacted as long as it works in a single-node
configuration. However, the client smart features to locate the stream
leaders and replicas will get "confused" because it can't resolve the
hostname.

This commit changes the smart layer, so that it won't try to locate the
stream replicas if the client is connected to localhost. Instead, it
will force the producer/consumer connections to be localhost, and it
will assume that stream replicas/leader are in localhost. This is true
for single-node rabbits in local development environments. The smart
feature to locate leader/replicas remains unchanged for non-local
connections.


---
Signed-off-by: Aitor Perez Cedres <[email protected]>
@Zerpet Zerpet modified the milestones: 1.7.1, 1.8.0 Sep 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants