登录
首页 >  Golang >  Go问答

在 Go 中设置基本 Kafka 消费者和生产者时出现 security.protocol 错误?

来源:stackoverflow

时间:2024-04-10 16:27:35 100浏览 收藏

怎么入门Golang编程?需要学习哪些知识点?这是新手们刚接触编程时常见的问题;下面golang学习网就来给大家整理分享一些知识点,希望能够给初学者一些帮助。本篇文章就来介绍《在 Go 中设置基本 Kafka 消费者和生产者时出现 security.protocol 错误?》,涉及到,有需要的可以收藏一下

问题内容

我正在尝试在 go 中设置一个基本的 kafka 客户端 - 按照此处详细说明的示例 https://docs.confluence.io/clients-confluence-kafka-go/current/overview.html#go-example-code和 https://github.com/confluenceinc/confluence-kafka-go。

我按照给出的方式编写了消费者和生产者示例,就像这样

func produce() {

    p, err := kafka.newproducer(&kafka.configmap{"bootstrap.servers": "my-broker-name"})
    if err != nil {
        panic(err)
    }

    defer p.close()

    go func() {
        for e := range p.events() {
            switch ev := e.(type) {
            case *kafka.message:
                if ev.topicpartition.error != nil {
                    fmt.printf("delivery failed: %v\n", ev.topicpartition)
                } else {
                    fmt.printf("delivered message to %v\n", ev.topicpartition)
                }
            }
        }
    }()

    topic := "mytopic"
    for _, word := range []string{"welcome", "to", "the", "confluent", "kafka", "golang", "client"} {
        p.produce(&kafka.message{
            topicpartition: kafka.topicpartition{topic: &topic, partition: kafka.partitionany},
            value:          []byte(word),
        }, nil)
    }

    p.flush(15 * 1000)
}

func consume() {

    c, err := kafka.newconsumer(&kafka.configmap{
        "bootstrap.servers": "my-broker-name",
        "group.id":          "mygroup",
        "auto.offset.reset": "earliest",
    })

    if err != nil {
        panic(err)
    }

    c.subscribetopics([]string{"mytopic", "^aregex.*[tt]opic"}, nil)

    for {
        msg, err := c.readmessage(-1)
        if err == nil {
            fmt.printf("message on %s: %s\n", msg.topicpartition, string(msg.value))
        } else {
            
            fmt.printf("consumer error: %v (%v)\n", err, msg)
        }
    }
    
    c.close()
}

(my-broker-name 是我的主机名 + 端口的替代品,我不想在此处包含它)

但是,当运行 produce 函数时,它会返回一个错误:

disconnected while requesting apiversion: might be caused by incorrect security.protocol configuration (connecting to a ssl listener?) or broker version is < 0.10 (see api.version.request) (after 31ms in state apiversion_query)

当运行消耗函数时,我收到相同的错误,但也显示了一些内容

consumer error: 1/1 brokers are down ()

我确信情况并非如此。

不幸的是,我无法找到任何有关这些错误含义或如何解决这些错误的文档。如何解决该错误,以便我能够为我的代理进行生产和消费?

更新:

我获取了证书并将其转换为 .pem 文件,并将 configmap 更改为以下内容:

p, err := kafka.newproducer(&kafka.configmap{
        "bootstrap.servers": "my-broker:32500",
        "security.protocol": "ssl",
        "ssl.certificate.location": "mycert.pem",
        "ssl.ca.location": "ca-chain.pem"})
    if err != nil {
        panic(err)
    }

但是,现在又回来了

client SSL authentication might be required (see ssl.key.location and ssl.certificate.location and consult the broker logs for more information)

这是否意味着证书有问题?还是我在某个地方遗漏了某个步骤?


正确答案


您需要提供主机名和端口作为引导服务器

"bootstrap.servers": "host1:9092"

要连接到 kafka 中的安全端口,您需要提供包含 ca 文件的信任库配置,或任何用于安全连接的应用程序

https://www.google.com/amp/s/www.process-one.net/blog/using-tls-authentication-for-your-go-kafka-client/%3famp

https://github.com/FluuxIO/kafka/blob/master/examples/base-client/base-client.go#L6

kafka.configmap{
  "bootstrap.servers"̇: "..",
  "security.protocol": "ssl",
  // if you're using ssl authentication, provide the client's key here
  "ssl.key.location": "path-to-private-key.pem",
  "ssl.certificate.location": "path-to-public-key.pem",
  "ssl.key.password": "if any..",
}

对于您的新错误,请查看此处

What does "SSL_CTX_use_PrivateKey_file" "problems getting password error" indicate in Nginx error log?

这里的解决方案是我缺少 ssl.key.location。我不得不向管理员询问密钥。一旦我添加了密钥,一切就都正常了。我的最终配置如下所示:

c, err := kafka.NewConsumer(&kafka.ConfigMap{
        "bootstrap.servers": "hostname:port-number",
        "security.protocol": "SSL",
        "ssl.ca.location": "ca-chain.pem",
        "ssl.key.location": "key-location",
        "ssl.certificate.location": "mycert.pem"})

    if err != nil {
        panic(err)
    }

今天关于《在 Go 中设置基本 Kafka 消费者和生产者时出现 security.protocol 错误?》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>