04-17.eri-test 从Quarkus访问Apache Kafka

This article describes how to develop microservices with Quarkus which use Apache Kafka running in a Kubernetes cluster.

Quarkus supports MicroProfile Reactive Messaging to interact with Apache Kafka. There is a nice guide Using Apache Kafka with reactive Messaging which explains how to send and receive messages to and from Kafka.

该指南包含如何使用docker-compose通过Docker在本地运行Kafka的说明。 虽然这可能是最简单的入门方法,但微服务开发人员的下一步通常是弄清楚如何在Kubernetes环境或托管的Kafka服务中访问Apache Kafka。

选项1:在Kubernetes中运行的Apache Kafka

The open source project Strimzi provides container images and operators for running Apache Kafka on Kubernetes and Red Hat OpenShift. There is a nice blog series on Red Hat Developer that describes how to use Strimzi. 为了从应用程序访问它,有几个不同的选项,例如NodePorts,OpenShift路由,负载均衡器和Ingress.

有时,这些选项对于开发人员来说可能是不知所措的,尤其是当您只需要一个简单的开发环境来编写一些反应式hello world应用程序以入门时,尤其如此。 就我而言,我想在我的Minikube集群中安装一个简单的Kafka服务器。

There is a quickstart guide how to deploy Strimzi to Minikube. 不幸的是,它没有解释如何从应用程序访问它. The second part of the blog series Accessing Apache Kafka in Strimzi: Part 2 – Node ports explains this.

基于这两篇文章,我编写了一个简单的脚本,可在不到5分钟的时间内将Kafka部署到Minikube. The script is part of the cloud-native-starter project. 运行这些命令尝试一下:

$ git clone https://github.com/IBM/cloud-native-starter.git
 $ cd cloud-native-starter / reactive
 $ sh scripts / start-minikube.sh
 $ sh scripts / deploy-kafka.sh
 $ sh scripts / show-urls.sh

最后一条命令的输出将打印出您下一步需要的Kafka引导程序服务器的URL。 您可以在“ kafka”命名空间中找到所有资源。

In order to access Kafka from Quarkus, the Kafka connector has to be configured. 在与Kafka相同的Kubernetes集群中运行Quarkus应用程序时,请在“ application.属性. “ my-cluster-kafka-external-bootstrap”是服务名称,“ kafka”是名称空间,“ 9094”是端口.

kafka.bootstrap.servers = my-cluster-kafka-external-bootstrap.kafka:9094`
 mp.messaging.incoming.new-article-created.connector = smallrye-kafka`
 mp.messaging.incoming.new-article-created.value.deserializer = org.apache.kafka.common.serialization.StringDeserializer

在本地开发Quarkus应用程序时,可通过NodePort访问Minikube中的Kafka。 在这种情况下,请使用以下网址替换kafka.bootstrap.servers配置:

$ minikubeip = $(minikube ip)
 $ nodeport = $(kubectl get svc my-cluster-kafka-external-bootstrap -n kafka --ignore-not-found --output'jsonpath = {。spec.ports [*]。nodePort}')
 $ echo $ {minikubeip}:$ {nodeport}

选项2:Kafka即服务

大多数云提供商还托管托管的Kafka服务. For example Event Streams is the managed Kafka service in the IBM Cloud. There is a free lite plan which offers access to a single partition in a multi-tenant Event Streams cluster. All you need is an IBM id, which is free and you don’t need a credit card.

作为大多数生产中的Kafka服务,事件流需要安全的连接。 需要再次在“ application.properties”中定义此附加配置。

kafka.bootstrap.servers = broker-0-YOUR-ID.kafka.svc01.us-south.eventstreams.cloud.ibm.com:9093,broker-4-YOUR-ID.kafka.svc01.us-south.eventstreams。  cloud.ibm.com:9093,...更多服务器
 mp.messaging.incoming.new-article-created.connector = smallrye-kafka
 mp.messaging.incoming.new-article-created.value.deserializer = org.apache.kafka.common.serialization.StringDeserializer
 mp.messaging.incoming.new-article-created.sasl.mechanism = PLAIN
 mp.messaging.incoming.new-article-created.security.protocol = SASL_SSL
 mp.messaging.incoming.new-article-created.ssl.protocol = TLSv1.2
 mp.messaging.incoming.new-article-created.sasl.jaas.config = org.apache.kafka.common.security.plain.PlainLoginModule必需username =“ token” password =“ YOUR-PASSWORD”;

为了输入此信息,您需要1. Kafka引导服务器列表和2.您的事件流服务密码。 您可以在事件流服务的Web前端中接收此信息,也可以使用IBM Cloud CLI。

My colleague Harald Uebele has developed a script that creates the service programmatically and returns these two pieces of information.

下一步

The scripts mentioned in this article are part of the cloud-native-starter project which describes how to get develop reactive applications with Quarkus. My previous article describes the project.

Try out the code yourself.

阅读本系列的其他文章:

from: https://dev.to//nheidloff/accessing-apache-kafka-from-quarkus-30na

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值