HelloWorld in Kafka with .Net Core From Scratch
By: Ameer Thasthahir
Tags: Apache Kafka Tutorial, Net Core, Apache Kafka, Kafka, Console Application

Step by step explanation for how to implement apache Kafka pub/sub in .Net Core Console Application.

Did you exhaust by searching to know how to implement a simple Hello World Kafka in .Net Core? To create momentum in Kafka.

Couldn’t find one! Don’t worry, This blog is for you…

I understand, you know what is Kafka (At at least what it does)?.

Of course, You will, otherwise, you will never come to the implementation part of Kafa with .Net Core. right? :P The benefit of beginners,

Kafka does many more fancy things, Explaining a core system in layman's terms. It is a publish/subscribe system. We can publish a message on a specific topic. The subscribers of the topic get the message. It’s wildly used for Event-driven architectures

Step1: Download Kafka,

To get started, download Kafka from the below link.

https://kafka.apache.org/downloads

Download Kafka Scala 2.12

Step2: Next Unzip the downloaded link to the particular location

(Don’t worry if the file is “.tgz” format. Still, you can unzip it. Facing trouble to unzip try “ZipGenius” software )

Step3: Open the command prompt on your file location and copy-paste the below command

As you can see in the above screenshot. My unzipped Kafka folder located below file location. C:\kafka_2.12–0.10.2.1

Change your file location accordingly. and Update it in the below command.

<YourFileLocation/> .\bin\windows\zookeeper-server-start.bat .\config\zookeeper.properties

So, It comes below for me since my folder location in C:\kafka_2.12–0.10.2.1.>

C:\kafka_2.12-0.10.2.1>.\bin\windows\zookeeper-server-start.bat .\config\zookeeper.properties

The above comment for run zookeeper.

Step4: Open a new command prompt on the same location run the below command.

Change your file location accordingly. Instead of C:\kafka_2.12–0.10.2.1>

C:\kafka_2.12-0.10.2.1>.\bin\windows\kafka-server-start.bat .\config\server.properties

The above comment for run Apache Kafka.

Step5: Open a new command prompt on the same location and run the below command.

Change your file location accordingly. Instead of C:\kafka_2.12–0.10.2.1>

C:\kafka_2.12-0.10.2.1>.\bin\windows\kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic testtopic

The above comment for creating a topic “testtopic”

Step6: Create a console application for Publisher and Install Confluent.Kafka v0.9.5 Nuget package

I hope the reader knows how to create a console application in .Net Core and How to install Nuget Packages. If no, please refer to the below site.

How to create a Basic Console Application

Then, Install below Kafka Nuget package v0.9.5. on created Console application.

Implement the below code to your Progam.cs file of the created publisher console application.

Code for implement publisher for Kafka

Step7: Create a console application for the subscriber and Install the Kafka Nuget package

Follow the step6 process to create a console application and install Kafka NuGet package v0.9.5

Implement the below code.

Code for Implementing Subscriber for Kafka

Final Step: Time for Victory!! Run the two console application (Publisher and Subscriber)

Recommend: First Run Subscriber application, then run publisher application

As below result shows Publisher Console Application published 10 messages.

publisher output

As the below result shows, the Subscriber Console application received published 10 messages simultaneously.

Subscriber output

Hurray!! We have done our HelloWorld Kafka implementation in the .Net Console application. Will meet you in the next blog ;)