龙空技术网

免费的Apache Kafka云服务-以及如何快速入门

闻数起舞 143

前言:

现时我们对“apache免费”大体比较注重,你们都需要剖析一些“apache免费”的相关知识。那么小编在网上搜集了一些对于“apache免费””的相关资讯,希望大家能喜欢,我们一起来学习一下吧!

最初于2020年4月27日在上发布。

上周,我在Apache Kafka上做了两次演讲。 一次是针对100多名学生的小组,一次是针对30多名同事。 在这两种情况下,我都邀请与会者参加动手实验室研讨会,以了解Apache Kafka。 我已经准备了一个基于Docker Compose的Kafka平台(在Guido Schmutz的帮助下),参与者可以在自己的笔记本电脑上本地安装。 但是,另一种不涉及任何本地安装且实际上更快且同样免费的替代方法是通过免费的Apache Kafka云服务:CloudKarafka。

在本文中,我将向您展示可以快速开始使用CloudKarafka。

CloudKarafka在云上提供托管的Kafka集群,作为其产品的一部分,他们提供了免费的计划-Developer Duck计划。

转到CloudKarafka网站并向下滚动到Developer Duck计划。 单击尝试开发者鸭子。

系统将要求您使用GitHub帐户,Google帐户或通过注册创建的新帐户登录。

指定实例的名称,例如,在线Meetup Apache Kafka(不过,Butterfly也能很好地工作)。

开发商鸭子计划(免费!)应该已经选择。

单击选择区域。

默认区域(也是最稳定的区域)是AWS上的US-East-1。 您可能还坚持使用该选项。

按下评论。

您将获得所做选择的概述。 您可以查看并决定返回修改。 但是你为什么呢? 继续,然后按创建实例。

稍后,您将收到通知,告知您已为您提供了新实例。

单击实例名称。 它是一个超链接,它将带您到一个页面,其中包含有关新实例的详细信息。

稍后开始以编程方式访问Kafka主题时,您将在此页面上需要各种详细信息。

· 用户名

· 密码

· 主题前缀

· 服务器(新集群实例中Kafka代理的端点)

您随时可以返回此页面以查找这些详细信息。

试用新的Cloud Karafka Ducky Plan实例

转到主题选项卡。 您将看到列出的一个主题:与您的Kafka实例一起创建的默认主题。 请注意,在Ducky计划下,您最多可以创建5个主题。 您也可以删除它们。

选择默认主题的名称并将其复制到剪贴板。 然后打开浏览器选项卡。

在此页面上,您可以将消息发布到主题或主题的使用者消息。 将主题名称粘贴到两个"主题"字段中:

然后单击消耗。 创建使用者,并将使用的消息推送到浏览器。

在"生产者"区域中输入一条消息,然后单击"生产"。 该消息已发布到该主题。 接下来,从主题中使用它,将其推送到浏览器并显示在页面中。 还提供了从中消费消息的分区的指示。

随意玩弄不同类型的消息。

注意:不幸的是,CloudKarafka提供的大多数高级工具虽然不是意外地不是免费计划的一部分。

通过Node应用程序与CloudKarafka的免费开发者计划进行程序化交互

Node(JS)中的Kafka客户端的问候世界-这就是本节中的目标,仅此而已。 来源可从GitHub获得:https://github.com/AMIS-Services/online-meetups-introduction-of-kafka/tree/master/lab2-programmatic-consume-and-produce。

当搜索关键字kafka时,NPM模块存储库将返回550个以上的模块。 并不是所有的库都是用来促进Node应用程序与Apache Kafka集群之间交互的库,但是有十几个库。 在本实验中,我们将使用GitHub上的node-rdkafka NPM模块,node-rdkafka来获取有关此特定库的详细信息以及API规范的参考文档。 node-rdkafka库是Apache Kafka的高性能NodeJS客户端,它包装了本机(基于C)的librdkafka库。 跨分区平衡写和管理(可能不断变化的)代理的所有复杂性都应封装在库中。

该应用程序具有足够简单的package.json文件-其中对node-rdkafka的依赖是最重要的元素。

{ "name": "nodejs-kafka-example", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "dependencies": { "node-rdkafka": "^2.2.0" } }

config.js文件包含Kafka群集的配置。 您需要根据CloudKarafka实例的连接详细信息更新此文件:

const topic = "kjynvuby-default" // set the correct topic name, especially when you are using CloudKarafka const kafkaConfig = { // Specify the endpoints of the CloudKarafka Servers for your instance found under Connection Details on the Instance Details Page // this looks like this: moped-01.srvs.cloudkafka.com:9094,moped-02.srvs.cloudkafka.com:9094,moped-03.srvs.cloudkafka.com:9094" "metadata.broker.list": "moped-01.srvs.cloudkafka.com:9094,moped-02.srvs.cloudkafka.com:9094,moped-03.srvs.cloudkafka.com:9094" , "security.protocol": "SASL_SSL", "sasl.mechanisms": "SCRAM-SHA-256", "sasl.username": "kjynvuby", "sasl.password": "D0_sMX2ICVfuOfYjpZE8VdAnMlrknXSd" }; module.exports = { kafkaConfig, topic };
生产

用于向Kafka主题生成消息的最简单的Node客户端看起来像这样-不依赖于您特定的Kafka群集(它利用config.js):

const Kafka = require("node-rdkafka"); // read the KAFKA Brokers and KAFKA_TOPIC values from the local file config.js const externalConfig = require('./config') // function to generate a message const generateMessage = i => new Buffer.from(`Generated a happy message - number ${i}`)function generateAndProduceMessages(arg) { for (var i = 0; i < messageBatchSize; i++) {   producer.produce(topic, -1, generateMessage(i), i) } console.log(`producer ${arg.name} is done producing messages to Kafka Topic ${topic}.`) } // construct a Kafka Configuration object understood by the node-rdkafka library // merge the configuration as defined in config.js with additional properties defined here const kafkaConf = {...externalConfig.kafkaConfig , ...{ "socket.keepalive.enable": true, "debug": "generic,broker,security"} }const messageBatchSize = 3 // number of messages to publish in one burst const topic = externalConfig.topic; // create a Kafka Producer - connected to the KAFKA_BROKERS defined in config.js const producer = new Kafka.Producer(kafkaConf); prepareProducer(producer) // initialize the connection of the Producer to the Kafka Cluster producer.connect()function prepareProducer(producer) { // event handler attached to the Kafka Producer to handle the ready event that is emitted when the Producer has connected sucessfully to the Kafka Cluster   producer.on("ready", function (arg) {     console.log(`Producer connection to Kafka Cluster is ready; message production starts now`)     generateAndProduceMessages(arg);     // after 10 seconds, disconnect the producer from the Kafka Cluster     setTimeout(() => producer.disconnect(), 10000);   });   producer.on("disconnected", function (arg) { process.exit(); });  producer.on('event.error', function (err) {      console.error(err);      process.exit(1);   });   // This event handler is triggered whenever the event.log event is emitted, which is quite often   producer.on('event.log', function (log) { // uncomment the next line if you want to see a log message every step of the way      //console.log(log); }); }
消费

最后,下面显示了最简单的Node消息使用者。 该消耗.js模块还依赖config.js来获取实际[CloudKarafka] Kafka群集的配置详细信息。

const Kafka = require("node-rdkafka"); // see:  const externalConfig = require('./config'); const CONSUMER_GROUP_ID = "node-consumer" // construct a Kafka Configuration object understood by the node-rdkafka library // merge the configuration as defined in config.js with additional properties defined here const kafkaConf = {...externalConfig.kafkaConfig , ...{ "group.id": CONSUMER_GROUP_ID, "socket.keepalive.enable": true, "debug": "generic,broker,security"} }; const topics = [externalConfig.topic] let stream = new Kafka.KafkaConsumer.createReadStream(kafkaConf, { "auto.offset.reset": "earliest" }, { topics: topics })stream.on('data', function (message) {   console.log(`Consumed message on Stream: ${message.value.toString()}`  // the structure of the messages is as follows:   // {   // value: Buffer.from('hi'),  // message contents as a Buffer   // size: 2, // size of the message, in bytes   // topic: 'librdtesting-01', // topic the message comes from   // offset: 1337, // offset the message was read from   // partition: 1, // partition the message was on   // key: 'someKey', // key of the message if present   // timestamp: 1510325354780 // timestamp of message creation   // } }); console.log(`Stream consumer created to consume from topic ${topics}`); stream.consumer.on("disconnected", function (arg) { console.log(`The stream consumer has been disconnected`)   process.exit(); }); // automatically disconnect the consumer after 30 seconds setTimeout(function () { stream.consumer.disconnect(); }, 30000)

运行该应用程序当然非常简单-运行npm install之后,下载NPM模块node-rdkafka和其他依赖项。 通过一个简单的节点Produce.js,您可以开始为CloudKarafka主题生成三个简单的生成的消息。

使用CloudKarafka控制台的"浏览器"选项卡中的主题消费,我们可以检查这些消息的到达。

当您运行consumption.js时,将创建一个与刚刚生成消息的卡夫卡主题相同的连接,并使用这些消息:

在这种情况下,两个Node应用程序都可能在笔记本电脑上执行。 但是,您可以轻松地在云环境中的某个地方运行生产者,而在偏远国家/地区的笔记本电脑上运行消费者。 由于Kafka群集在云中运行并且可以从任何地方访问,因此打开了许多选项。

摘要

要快速介绍Apache Kafka,Cloud Karafka的Developer Duck计划很难被超越。 在短短几分钟内,Apache Kafka集群就准备就绪。 向云中发布消息和从中使用消息可以非常迅速地开始。 讲习班,演示,小组作业都将受益于"开发鸭"。 我知道我的学生做过。

(本文翻译自Lucas Jellema的文章《A Free Apache Kafka Cloud Service — and how to quickly get started with it》,参考:)

标签: #apache免费