【备注: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
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
{
static void Main(string[] args) { do { Produce(GetKafkaBroker(), getTopicName()); System.Threading.Thread.Sleep(3000); } while (true); } private static void Produce(string broker, string topic) { var options = new KafkaOptions(new Uri(broker)); var router = new BrokerRouter(options); var client = new Producer(router); var currentDatetime =DateTime.Now; var key = currentDatetime.Second.ToString(); var events = new[] { new Message("Hello World " + currentDatetime, key) }; client.SendMessageAsync(topic, events).Wait(1500); Console.WriteLine("Produced: Key: {0}. Message: {1}", key, events[0].Value.ToUtf8String()); using (client) { } } private static string GetKafkaBroker() { string KafkaBroker = string.Empty; const string 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; const string 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; } }
4.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; } }
5.Consumer结果: