gpt4 book ai didi

node.js - Strimzi - 连接外部客户端

转载 作者:太空宇宙 更新时间:2023-11-04 01:23:51 25 4
gpt4 key购买 nike

以下讨论 here ,我使用以下步骤启用外部客户端(基于 kafkajs )连接到 OpenShift 上的 Strimzi。这些步骤来自here .

启用外部路由

kafka-persistent-single.yaml编辑为如下所示。

apiVersion: kafka.strimzi.io/v1beta1
kind: Kafka
metadata:
name: my-cluster
spec:
kafka:
version: 2.3.0
replicas: 1
listeners:
plain: {}
tls: {}
external:
type: route
config:
offsets.topic.replication.factor: 1
transaction.state.log.replication.factor: 1
transaction.state.log.min.isr: 1
log.message.format.version: "2.3"
storage:
type: jbod
volumes:
- id: 0
type: persistent-claim
size: 5Gi
deleteClaim: false
zookeeper:
replicas: 1
storage:
type: persistent-claim
size: 5Gi
deleteClaim: false
entityOperator:
topicOperator: {}
userOperator: {}

提取证书,

要提取证书并在客户端中使用它,我运行了以下命令:

kubectl get secret my-cluster-cluster-ca-cert -o jsonpath='{.data.ca\.crt}' | base64 -D > ca.crt

请注意,我必须在 macOS 上使用 base64 -D,而不是文档中所示的 base64 -d

Kafkajs 客户端

这是根据他们的 npm 页面和他们的 documentation 改编的客户端.

const fs = require('fs')
const { Kafka } = require('kafkajs')

const kafka = new Kafka({
clientId: 'my-app',
brokers: ['my-cluster-kafka-bootstrap-messaging-os.192.168.99.100.nip.io'],
ssl : { rejectUnauthorized: false,
ca : [fs.readFileSync('ca.crt', 'utf-8')]
}
})

const producer = kafka.producer()
const consumer = kafka.consumer({ groupId: 'test-group' })

const run = async () => {
// Producing
await producer.connect()
await producer.send({
topic: 'test-topic',
messages: [
{ value: 'Hello KafkaJS user!' },
],
})

// Consuming
await consumer.connect()
await consumer.subscribe({ topic: 'test-topic', fromBeginning: true })

await consumer.run({
eachMessage: async ({ topic, partition, message }) => {
console.log({
partition,
offset: message.offset,
value: message.value.toString(),
})
},
})
}

run().catch(console.error)

问题

当我从包含 ca.crt 的文件夹运行 node example.js 时,我收到一条连接被拒绝的消息。

{"level":"ERROR","timestamp":"2019-10-05T03:22:40.491Z","logger":"kafkajs","message":"[Connection] Connection error: connect ECONNREFUSED 192.168.99.100:9094","broker":"my-cluster-kafka-bootstrap-messaging-os.192.168.99.100.nip.io:9094","clientId":"my-app","stack":"Error: connect ECONNREFUSED 192.168.99.100:9094\n    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1113:14)"}

我错过了什么?

最佳答案

我猜问题是您在代理地址上缺少正确的端口 443,因此您必须使用

经纪人:['my-cluster-kafka-bootstrap-messaging-os.192.168.99.100.nip.io:443']

否则它会尝试连接到 OpenShift 路由上的默认端口 80。

关于node.js - Strimzi - 连接外部客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58245089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com