Skip to content

Commit

Permalink
Add kafka consumer, create Dockerfile for it.
Browse files Browse the repository at this point in the history
  • Loading branch information
farukterzioglu committed Dec 12, 2018
0 parents commit 567cb87
Show file tree
Hide file tree
Showing 27 changed files with 2,716 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/KafkaComparer.Consumer/bin/Debug/netcoreapp2.1/KafkaComparer.Consumer.dll",
"args": [],
"cwd": "${workspaceFolder}/KafkaComparer.Consumer",
// For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window
"console": "internalConsole",
"stopAtEntry": false,
"internalConsoleOptions": "openOnSessionStart"
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
,]
}
15 changes: 15 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/KafkaComparer.Consumer/KafkaComparer.Consumer.csproj"
],
"problemMatcher": "$msCompile"
}
]
}
15 changes: 15 additions & 0 deletions KafkaComparer.Consumer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#BUILD
FROM microsoft/dotnet:2.1.302-sdk AS build
WORKDIR /src

COPY . .
RUN dotnet restore -nowarn:msb3202,nu1503
RUN dotnet build -c Release -o /app

FROM build AS publish
RUN dotnet publish -c Release -o /app

FROM microsoft/dotnet:2.1-aspnetcore-runtime AS runtime
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "KafkaComparer.Consumer.dll"]
12 changes: 12 additions & 0 deletions KafkaComparer.Consumer/KafkaComparer.Consumer.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Confluent.Kafka" Version="0.11.6" />
</ItemGroup>

</Project>
42 changes: 42 additions & 0 deletions KafkaComparer.Consumer/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Confluent.Kafka;
using Confluent.Kafka.Serialization;

namespace KafkaComparer.Consumer
{
class Program
{
static void Main(string[] args)
{
var consumergroup = Environment.GetEnvironmentVariable("CONSUMER_GROUP");
var topicName = Environment.GetEnvironmentVariable("TOPIC_NAME");
var kafkaUrl = Environment.GetEnvironmentVariable("KAFKA_URL");

var config = new Dictionary<string, object>
{
{ "group.id", consumergroup },
{ "bootstrap.servers", kafkaUrl },
{ "enable.auto.commit", "false"}
};

using (var consumer = new Consumer<Null, string>(config, null, new StringDeserializer(Encoding.UTF8)))
{
consumer.Subscribe(new string[]{ topicName });

consumer.OnMessage += (_, msg) =>
{
Console.WriteLine($"Topic: {msg.Topic} Partition: {msg.Partition} Offset: {msg.Offset} {msg.Value}");
consumer.CommitAsync(msg);
};

while (true)
{
consumer.Poll(100);
}
}
}
}
}
Loading

0 comments on commit 567cb87

Please sign in to comment.