.net windows Kafka 安装与使用入门(入门笔记)
【备注:kafka相关的入门,使用demo资料来源于互联网!本文是根据实际实践成功安装使用经验的总结,:)】
完整解决方案请参考:
Setting Up and Running Apache Kafka on Windows OS
在环境搭建过程中遇到两个问题,在这里先列出来,以方便查询:
搭建环境
1. 安装JDK
1.1 安装文件:http://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.html下载Server JRE.
1.2 安装完成后需要添加以下的环境变量(右键点击“我的电脑” -> “高级系统设置” -> “环境变量” ):
-
- JAVA_HOME: C:\Program Files (x86)\Java\jre1.8.0_60(这个是默认安装路径,如果安装过程中更改了安装目录,把更改后的路径填上就行了)
-
- PATH: 在现有的值后面添加”; %JAVA_HOME%\bin”
1.3 打开cmd运行 “java -version” 查看当前系统Java的版本:
2. 安装Zookeeper
Kafka的运行依赖于Zookeeper,所以在运行Kafka之前我们需要安装并运行Zookeeper
-
- ZOOKEEPER_HOME: G:\zookeeper-3.4.8
- Path: 在现有的值后面添加 “;%ZOOKEEPER_HOME%\bin;”
2.7 运行Zookeeper: 打开cmd然后执行
zkserver
3. 安装并运行Kafka
.\bin\windows\kafka-server-start.bat .\config\server.properties
4. 创建topics
kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
5. 打开一个Producer:
cd /d G:\kafka_2.11-0.10.0.1\bin\windows<br/> kafka-console-producer.bat --broker-list localhost:9092 --topic test
6. 打开一个Consumer:
cd /d G:\kafka_2.11-0.10.0.1\bin\windows
kafka-console-consumer.bat --zookeeper localhost:2181 --topic test
至此,Kafka运行环境的搭建就完成了:-)
web管理方面(正式运维部署):
网上有人说 kafka manage
.net sdk方面:
有人建议:https://github.com/Microsoft/CSharpClient-for-Kafka (微软提供的sdk,看了源码个人感觉有些复杂)
还有
kafka-net 这个感觉会简单很多(看了源码感觉一些Task写法并不喜欢)
推荐:kafka-net 简单易用些。
地址: https://github.com/Jroland/kafka-net
以上sdk 可以搜索nuget 查看开源地址和最新release包,当然.net sdk 还有很多其他选择。
个人开发工具(windows支持),windows 开发环境使用
kafkaTool
下载地址: http://www.kafkatool.com/download.html
kafka-net sdk使用demo
参考来源:http://www.cnblogs.com/Wulex/p/5619425.html
详细请查看官方开源地址的demo: https://github.com/Jroland/kafka-net
KafkaProducer程序:
class Program<br/> {<br/> static void Main(string[] args)<br/> {<br/> do<br/> {<br/> Produce(GetKafkaBroker(), getTopicName());<br/> System.Threading.Thread.Sleep(3000);<br/> } while (true);<br/> } private static void Produce(string broker, string topic)<br/> {<br/> var options = new KafkaOptions(new Uri(broker));<br/> var router = new BrokerRouter(options);<br/> var client = new Producer(router); var currentDatetime =DateTime.Now;<br/> var key = currentDatetime.Second.ToString();<br/> var events = new[] { new Message("Hello World " + currentDatetime, key) };<br/> client.SendMessageAsync(topic, events).Wait(1500);<br/> Console.WriteLine("Produced: Key: {0}. Message: {1}", key, events[0].Value.ToUtf8String()); using (client) { }<br/> } private static string GetKafkaBroker()<br/> {<br/> string KafkaBroker = string.Empty;<br/> const string kafkaBrokerKeyName = "KafkaBroker"; if (!ConfigurationManager.AppSettings.AllKeys.Contains(kafkaBrokerKeyName))<br/> {<br/> KafkaBroker = "http://localhost:9092";<br/> }<br/> else<br/> {<br/> KafkaBroker = ConfigurationManager.AppSettings[kafkaBrokerKeyName];<br/> }<br/> return KafkaBroker;<br/> }<br/> private static string getTopicName()<br/> {<br/> string TopicName = string.Empty;<br/> const string topicNameKeyName = "Topic"; if (!ConfigurationManager.AppSettings.AllKeys.Contains(topicNameKeyName))<br/> {<br/> throw new Exception("Key \"" + topicNameKeyName + "\" not found in Config file -> configuration/AppSettings");<br/> }<br/> else<br/> {<br/> TopicName = ConfigurationManager.AppSettings[topicNameKeyName];<br/> }<br/> return TopicName;<br/> }<br/> }
4.KafkaConsumer程序:
class Program<br/> {<br/> static void Main(string[] args)<br/> {<br/> Consume(getKafkaBroker(), getTopicName()); } private static void Consume(string broker, string topic)<br/> {<br/> var options = new KafkaOptions(new Uri(broker));<br/> var router = new BrokerRouter(options);<br/> var consumer = new Consumer(new ConsumerOptions(topic, router)); //Consume returns a blocking IEnumerable (ie: never ending stream)<br/> foreach (var message in consumer.Consume())<br/> {<br/> Console.WriteLine("Response: Partition {0},Offset {1} : {2}",<br/> message.Meta.PartitionId, message.Meta.Offset, message.Value.ToUtf8String());<br/> }<br/> } private static string getKafkaBroker()<br/> {<br/> string KafkaBroker = string.Empty;<br/> var KafkaBrokerKeyName = "KafkaBroker"; if (!ConfigurationManager.AppSettings.AllKeys.Contains(KafkaBrokerKeyName))<br/> {<br/> KafkaBroker = "http://localhost:9092";<br/> }<br/> else<br/> {<br/> KafkaBroker = ConfigurationManager.AppSettings[KafkaBrokerKeyName];<br/> }<br/> return KafkaBroker;<br/> } private static string getTopicName()<br/> {<br/> string TopicName = string.Empty;<br/> var TopicNameKeyName = "Topic"; if (!ConfigurationManager.AppSettings.AllKeys.Contains(TopicNameKeyName))<br/> {<br/> throw new Exception("Key \"" + TopicNameKeyName + "\" not found in Config file -> configuration/AppSettings");<br/> }<br/> else<br/> {<br/> TopicName = ConfigurationManager.AppSettings[TopicNameKeyName];<br/> }<br/> return TopicName;<br/> }<br/> }
5.Consumer结果:
转发申明:
本文转自互联网,由小站整理并发布,在于分享相关技术和知识。版权归原作者所有,如有侵权,请联系本站 top8488@163.com,将在24小时内删除。谢谢