Window Kafka 简单使用

Window Kafka 简单使用

文章目录

1.kafka下载

下载地址

下载二进制的,如图:

2、官网下载kafka【解压之后如图所示】

3、修改zookeeper、Kafka的配置文件

config文件夹下zookeeper.properties文件

bash 复制代码
dataDir=D:/zookeeper/data

config文件夹下server.properties文件

bash 复制代码
log.dirs=D:/kafka-logs

4、启动zookeeper

cd D:\kafka_2.12-3.7.0\bin\windows

bash 复制代码
在此目录下打开cmd,执行命令
zookeeper-server-start.bat ..\..\config\zookeeper.properties

5、启动kafka

cd D:\kafka_2.12-3.7.0\bin\windows

bash 复制代码
在此目录下打开cmd,执行命令
kafka-server-start.bat ..\..\config\server.properties

6、Confluent.Kafka在.Net中使用

生产者:

csharp 复制代码
        /// <summary>
        /// 消息生产者
        /// </summary>
        public static void Producer()
        {
            var conf = new ProducerConfig {

                BootstrapServers = "localhost:9092",
                //CompressionType=CompressionType.Gzip//压缩方式
            };

            Action<DeliveryReport<string, string>> handler = r =>
            {
                if (!r.Error.IsError)
                {
                    Console.WriteLine($"Delivered message to {r.TopicPartitionOffset}");
                }
                else
                {
                    //发送邮件,记录错误消息
                    Console.WriteLine($"Delivery Error: {r.Error.Reason}");
                }
              
            };


            using (var p = new ProducerBuilder<string, string>(conf).Build())
            {

                for (int i = 0; i < 5; ++i)
                {
                    //批消息:消息集合
                    //虽然是单条数据发送 ,但是是批量发送的
                    //先放在内存中缓存起来,一起发送的
                    //磁盘IO:顺序读写(固态硬盘,机械硬盘寻址时间比较长)
                    var vin = $"LDP95E966NE02292{i}";
                    p.Produce("test", new Message<string , string> {Key= vin, Value = i.ToString() }, handler);
                }

                // wait for up to 10 seconds for any inflight messages to be delivered.
                p.Flush(TimeSpan.FromSeconds(10));
            }

        }

消费者:

csharp 复制代码
        /// <summary>
        /// 消费者
        /// 从文件找到数据,读到内存,通过网络发给客户端
        /// </summary>
        public static void Consumer()
        {
            var conf = new ConsumerConfig
            {
                GroupId = "test-consumer-group",
                BootstrapServers = "localhost:9092",
                // Note: The AutoOffsetReset property determines the start offset in the event
                // there are not yet any committed offsets for the consumer group for the
                // topic/partitions of interest. By default, offsets are committed
                // automatically, so in this example, consumption will only start from the
                // earliest message in the topic 'my-topic' the first time you run the program.
                AutoOffsetReset = AutoOffsetReset.Earliest,
                EnableAutoCommit=true,//关闭自动提交位移
                PartitionAssignmentStrategy=PartitionAssignmentStrategy.Range,//分区分配策略  轮询 、粘滞
                
            };

            using (var c = new ConsumerBuilder<Ignore, string>(conf).Build())
            {
                c.Subscribe("test");

                CancellationTokenSource cts = new CancellationTokenSource();
                Console.CancelKeyPress += (_, e) =>
                {
                    e.Cancel = true; // prevent the process from terminating.
                    cts.Cancel();
                };

                try
                {
                    while (true)
                    {
                        try
                        {
                            var cr = c.Consume(cts.Token);
                            Console.WriteLine($"Consumed message '{cr.Value}' at: '{cr.TopicPartitionOffset}'.");
                        }
                        catch (ConsumeException e)
                        {
                            Console.WriteLine($"Error occured: {e.Error.Reason}");
                        }
                    }
                }
                catch (OperationCanceledException)
                {
                    // Ensure the consumer leaves the group cleanly and final offsets are committed.
                    c.Close();
                }
            }
        }
相关推荐
黄尚圈圈19 分钟前
快速理解mQ(三)——RabbitMQ 各种交换机的区别与应用
分布式·rabbitmq
ON.LIN1 小时前
Hadoop大数据入门——Hive-SQL语法大全
大数据·数据库·hive·hadoop·分布式·sql
励志成为美貌才华为一体的女子2 小时前
《大规模语言模型从理论到实践》第一轮学习--第四章分布式训练
人工智能·分布式·语言模型
青云交2 小时前
大数据新视界 --大数据大厂之 Kafka 性能优化的进阶之道:应对海量数据的高效传输
大数据·数据库·人工智能·性能优化·kafka·数据压缩·分区策略·磁盘 i/o
-$_$-2 小时前
【黑马点评】 使用RabbitMQ实现消息队列——1.Docker与RabbitMQ环境安装
分布式·docker·rabbitmq
weixin_453965003 小时前
master节点k8s部署]33.ceph分布式存储(四)
分布式·ceph·kubernetes
奔跑吧邓邓子10 小时前
大数据利器Hadoop:从基础到实战,一篇文章掌握大数据处理精髓!
大数据·hadoop·分布式
weixin_4539650015 小时前
[单master节点k8s部署]30.ceph分布式存储(一)
分布式·ceph·kubernetes
weixin_4539650015 小时前
[单master节点k8s部署]32.ceph分布式存储(三)
分布式·ceph·kubernetes
Dylanioucn17 小时前
【分布式微服务云原生】掌握分布式缓存:Redis与Memcached的深入解析与实战指南
分布式·缓存·云原生