RabbitMQ streams with Golang client - Failed to create producer: dial tcp: missing address

92 views Asked by At

Here is my rabbit server config with docker-compose:

services:

  rabbitmq:
    image: rabbitmq:3-management
    container_name: "vibe-rabbitmq"
    volumes:
      - rabbitmq_data:/var/lib/rabbitmq
    ports:
      - "5672:5672"
      - "15672:15672"
      - "5552:5552"
    environment:
      - RABBITMQ_PLUGINS=rabbitmq_stream,rabbitmq_stream_management
      - RABBITMQ_DEFAULT_USER=rabbitmq
      - RABBITMQ_DEFAULT_PASS=rabbitmq

Here is my golang code

package main

import (
    "fmt"
    "github.com/rabbitmq/rabbitmq-stream-go-client/pkg/amqp"
    "github.com/rabbitmq/rabbitmq-stream-go-client/pkg/stream"
    "log"
    "time"
)

func main() {

    streamName := "your_stream_name"

    env, err := stream.NewEnvironment(
        stream.NewEnvironmentOptions().
            SetHost("0.0.0.0").
            SetPort(5552). // 5552
            SetUser("rabbitmq").
            SetPassword("rabbitmq"),
    )

    if err != nil {
        log.Fatalf("Failed to create environment: %s", err)
    }

    err = env.DeclareStream(streamName, &stream.StreamOptions{})

    if err != nil {
        log.Fatalf("Failed to declare stream: %v", err)
    }

    log.Printf("Stream '%s' created successfully", streamName)

    producer, err := env.NewProducer(streamName, &stream.ProducerOptions{})
    if err != nil {
        log.Fatalf("Failed to create producer: %s", err)
    }

}

I run the above, and I get this error:

2023/12/14 18:15:17 Stream 'your_stream_name' created successfully
2023/12/14 18:15:17 Failed to create producer: dial tcp: missing address
exit status 1

what's very strange is I can create the stream using the env instance, but cannot create the producer?

1

There are 1 answers

5
Gabriele Santomaggio On

Please read the overview about stream

The client must be able to look up the node where it is connected. The reason why you can create the stream is because it uses a different connection. Called locator.

When you ask for a producer, the client queries the stream to understand where is the leader after that tries to connect to the node leader.

Possible solutions:

  1. Use localhost instead of 0.0.0.0
  2. Use the advertised_host and port

TO use Docker see the documentation

docker run -it --rm --name rabbitmq -p 5552:5552 -p 15672:15672\
    -e RABBITMQ_SERVER_ADDITIONAL_ERL_ARGS='-rabbitmq_stream advertised_host localhost -rabbit loopback_users "none"' \
    rabbitmq:3-management

then:

docker exec rabbitmq rabbitmq-plugins enable rabbitmq_stream_management