资源简介
c# kafka 发送端与接收 完整demo 生成 消费
代码片段和文件信息
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using KafkaNet;
using KafkaNet.Common;
using KafkaNet.Model;
using KafkaNet.Protocol;
using System.Configuration;
namespace KafkaConsumer
{
class Program
{
static void Main(string[] args)
{
Consume(getKafkaBroker() getTopicName());
}
private static void Consume(string broker string topic)
{
var options = new KafkaOptions(new Uri(broker));
var router = new BrokerRouter(options);
var consumer = new Consumer(new ConsumerOptions(topic router));
//Consume returns a blocking IEnumerable (ie: never ending stream)
foreach (var message in consumer.Consume())
{
Console.WriteLine(“Response: Partition {0}Offset {1} : {2}“
message.meta.PartitionId message.meta.Offset message.Value.ToUtf8String());
}
}
private static string getKafkaBroker()
{
string KafkaBroker = string.Empty;
var KafkaBrokerKeyName = “KafkaBroker“;
if (!ConfigurationManager.AppSettings.AllKeys.Contains(KafkaBrokerKeyName))
{
KafkaBroker = “http://localhost:9092“;
}
else
{
KafkaBroker = ConfigurationManager.AppSettings[KafkaBrokerKeyName];
}
return KafkaBroker;
}
private static string getTopicName()
{
string TopicName = string.Empty;
var TopicNameKeyName = “Topic“;
if (!ConfigurationManager.AppSettings.AllKeys.Contains(TopicNameKeyName))
{
throw new Exception(“Key \““ + TopicNameKeyName + “\“ not found in Config file -> configuration/AppSettings“);
}
else
{
TopicName = ConfigurationManager.AppSettings[TopicNameKeyName];
}
return TopicName;
}
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 30208 2017-01-20 18:23 kafka-net-demo\comsumer.v11.suo
文件 1979 2017-12-05 19:52 kafka-net-demo\Kafka.sln
文件 161 2017-12-05 19:52 kafka-net-demo\KafkaConsumer\app.config
文件 154112 2017-01-20 18:23 kafka-net-demo\KafkaConsumer\bin\Debug\kafka-net.dll
文件 6656 2017-12-05 19:58 kafka-net-demo\KafkaConsumer\bin\Debug\KafkaConsumer.exe
文件 161 2017-12-05 19:52 kafka-net-demo\KafkaConsumer\bin\Debug\KafkaConsumer.exe.config
文件 13824 2017-12-05 19:58 kafka-net-demo\KafkaConsumer\bin\Debug\KafkaConsumer.pdb
文件 2836 2017-12-05 19:56 kafka-net-demo\KafkaConsumer\KafkaConsumer.csproj
文件 6904 2017-12-05 19:56 kafka-net-demo\KafkaConsumer\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
文件 735 2017-12-05 19:58 kafka-net-demo\KafkaConsumer\obj\Debug\KafkaConsumer.csproj.FileListAbsolute.txt
文件 6656 2017-12-05 19:58 kafka-net-demo\KafkaConsumer\obj\Debug\KafkaConsumer.exe
文件 13824 2017-12-05 19:58 kafka-net-demo\KafkaConsumer\obj\Debug\KafkaConsumer.pdb
文件 0 2017-12-05 19:52 kafka-net-demo\KafkaConsumer\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
文件 0 2017-12-05 19:52 kafka-net-demo\KafkaConsumer\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
文件 0 2017-12-05 19:52 kafka-net-demo\KafkaConsumer\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
文件 2144 2017-12-05 19:51 kafka-net-demo\KafkaConsumer\Program.cs
文件 1350 2017-12-05 19:51 kafka-net-demo\KafkaConsumer\Properties\AssemblyInfo.cs
文件 161 2017-12-05 19:52 kafka-net-demo\KafkaProducer\app.config
文件 154112 2017-01-20 18:23 kafka-net-demo\KafkaProducer\bin\Debug\kafka-net.dll
文件 6656 2017-12-05 19:58 kafka-net-demo\KafkaProducer\bin\Debug\KafkaProducer.exe
文件 161 2017-12-05 19:52 kafka-net-demo\KafkaProducer\bin\Debug\KafkaProducer.exe.config
文件 13824 2017-12-05 19:58 kafka-net-demo\KafkaProducer\bin\Debug\KafkaProducer.pdb
文件 2836 2017-12-05 19:56 kafka-net-demo\KafkaProducer\KafkaProducer.csproj
文件 6904 2017-12-05 19:56 kafka-net-demo\KafkaProducer\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
文件 735 2017-12-05 19:58 kafka-net-demo\KafkaProducer\obj\Debug\KafkaProducer.csproj.FileListAbsolute.txt
文件 6656 2017-12-05 19:58 kafka-net-demo\KafkaProducer\obj\Debug\KafkaProducer.exe
文件 13824 2017-12-05 19:58 kafka-net-demo\KafkaProducer\obj\Debug\KafkaProducer.pdb
文件 0 2017-12-05 19:52 kafka-net-demo\KafkaProducer\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
文件 0 2017-12-05 19:52 kafka-net-demo\KafkaProducer\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
文件 0 2017-12-05 19:52 kafka-net-demo\KafkaProducer\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
............此处省略20个文件信息
评论
共有 条评论