RabbitMQ流与Golang客户端-无法创建生产者:拨号tcp:缺少地址

gk7wooem  于 12个月前  发布在  Go
关注(0)|答案(1)|浏览(81)

以下是我的rabbit服务器配置与docker-compose:
服务项目:

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

字符串
这是我的Golang代码

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)
    }

}


我运行上面的代码,得到这个错误:

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


很奇怪的是我可以使用env示例创建流,但不能创建生产者?

d4so4syb

d4so4syb1#

请阅读概述about stream
客户端必须能够查找它所连接的节点。您可以创建流的原因是因为它使用不同的连接。称为定位器。
当您请求生产者时,客户端查询流以了解领导者在哪里,然后尝试连接到节点领导者。
可能的解决方案:
1.使用localhost而不是0.0.0.0
1.使用advertised_host和端口
要使用Docker,请参阅文档

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

字符串
然后:

docker exec rabbitmq rabbitmq-plugins enable rabbitmq_stream_management

相关问题