免費的Apache Kafka雲服務-以及如何快速入門

最初於2020年4月27日在https://technology.amis.nl上發佈。

上週,我在Apache Kafka上做了兩次演講。 一次是針對100多名學生的小組,一次是針對30多名同事。 在這兩種情況下,我都邀請與會者參加動手實驗室研討會,以瞭解Apache Kafka。 我已經準備了一個基於Docker Compose的Kafka平臺(在Guido Schmutz的幫助下),參與者可以在自己的筆記本電腦上本地安裝。 但是,另一種不涉及任何本地安裝且實際上更快且同樣免費的替代方法是通過免費的Apache Kafka雲服務:CloudKarafka。

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

免費的Apache Kafka雲服務-以及如何快速入門

CloudKarafka在雲上提供託管的Kafka集群,作為其產品的一部分,他們提供了免費的計劃-Developer Duck計劃。

轉到CloudKarafka網站並向下滾動到Developer Duck計劃。 單擊嘗試開發者鴨子。

系統將要求您使用GitHub帳戶,Google帳戶或通過註冊創建的新帳戶登錄。

免費的Apache Kafka雲服務-以及如何快速入門

指定實例的名稱,例如,在線Meetup Apache Kafka(不過,Butterfly也能很好地工作)。

免費的Apache Kafka雲服務-以及如何快速入門

開發商鴨子計劃(免費!)應該已經選擇。

單擊選擇區域。

默認區域(也是最穩定的區域)是AWS上的US-East-1。 您可能還堅持使用該選項。

免費的Apache Kafka雲服務-以及如何快速入門

按下評論。

您將獲得所做選擇的概述。 您可以查看並決定返回修改。 但是你為什麼呢? 繼續,然後按創建實例。

免費的Apache Kafka雲服務-以及如何快速入門

稍後,您將收到通知,告知您已為您提供了新實例。

免費的Apache Kafka雲服務-以及如何快速入門

單擊實例名稱。 它是一個超鏈接,它將帶您到一個頁面,其中包含有關新實例的詳細信息。

免費的Apache Kafka雲服務-以及如何快速入門

稍後開始以編程方式訪問Kafka主題時,您將在此頁面上需要各種詳細信息。

· 用戶名

· 密碼

· 主題前綴

· 服務器(新集群實例中Kafka代理的端點)

您隨時可以返回此頁面以查找這些詳細信息。

試用新的Cloud Karafka Ducky Plan實例

轉到主題選項卡。 您將看到列出的一個主題:與您的Kafka實例一起創建的默認主題。 請注意,在Ducky計劃下,您最多可以創建5個主題。 您也可以刪除它們。

免費的Apache Kafka雲服務-以及如何快速入門

選擇默認主題的名稱並將其複製到剪貼板。 然後打開瀏覽器選項卡。

在此頁面上,您可以將消息發佈到主題或主題的使用者消息。 將主題名稱粘貼到兩個"主題"字段中:

免費的Apache Kafka雲服務-以及如何快速入門

然後單擊消耗。 創建使用者,並將使用的消息推送到瀏覽器。

在"生產者"區域中輸入一條消息,然後單擊"生產"。 該消息已發佈到該主題。 接下來,從主題中使用它,將其推送到瀏覽器並顯示在頁面中。 還提供了從中消費消息的分區的指示。

免費的Apache Kafka雲服務-以及如何快速入門

隨意玩弄不同類型的消息。

免費的Apache Kafka雲服務-以及如何快速入門

注意:不幸的是,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的依賴是最重要的元素。

<code>{ "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" 
} 
}/<code>
免費的Apache Kafka雲服務-以及如何快速入門

config.js文件包含Kafka群集的配置。 您需要根據CloudKarafka實例的連接詳細信息更新此文件:

<code>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 };/<code>

生產

用於向Kafka主題生成消息的最簡單的Node客戶端看起來像這樣-不依賴於您特定的Kafka群集(它利用config.js):

<code>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); 
}); 
}/<code>

消費

最後,下面顯示了最簡單的Node消息使用者。 該消耗.js模塊還依賴config.js來獲取實際[CloudKarafka] Kafka群集的配置詳細信息。

<code>const Kafka = require("node-rdkafka"); // see: https://github.com/blizzard/node-rdkafka 
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)/<code>

運行該應用程序當然非常簡單-運行npm install之後,下載NPM模塊node-rdkafka和其他依賴項。 通過一個簡單的節點Produce.js,您可以開始為CloudKarafka主題生成三個簡單的生成的消息。

免費的Apache Kafka雲服務-以及如何快速入門

使用CloudKarafka控制檯的"瀏覽器"選項卡中的主題消費,我們可以檢查這些消息的到達。

免費的Apache Kafka雲服務-以及如何快速入門

當您運行consumption.js時,將創建一個與剛剛生成消息的卡夫卡主題相同的連接,並使用這些消息:

免費的Apache Kafka雲服務-以及如何快速入門

在這種情況下,兩個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》,參考:https://medium.com/@lucasjellema/a-free-apache-kafka-cloud-service-and-how-to-quickly-get-started-with-it-8f14520fff35)


分享到:


相關文章: