kafka 创建topic 并使用java/.net 连接

2020-12-13 05:51

阅读:434

标签:depend   git   end   val   lis   linq   ext   prot   消息   

创建 topic javatest

bin/kafka-topics.sh --bootstrap-server localhost:9092 --create --topic javatest --partitions 3 --replication-factor 1

技术图片

java 连接:https://www.cnblogs.com/duanjt/p/10132116.html

pom.xml


org.apache.kafka
kafka-clients
2.3.0

特别注意的需要设置:advertised.listeners

技术图片

 

然后附上连接的 Producer 和Consumer 的代码

import java.util.Properties;
import java.util.Random;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;
public class Producer {

    public static String topic = "javatest";//定义主题

    public static void main(String[] args) throws InterruptedException {
        Properties p = new Properties();
        p.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "www.top1novel.com:9092");//kafka地址,多个地址用逗号分割
        p.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        p.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
        KafkaProducer kafkaProducer = new KafkaProducer(p);

        try {
            while (true) {
                String msg = "Hello," + new Random().nextInt(100);
                ProducerRecord record = new ProducerRecord(topic, msg);
                kafkaProducer.send(record);
                System.out.println("消息发送成功:" + msg);
                Thread.sleep(500);
            }
        } finally {
            kafkaProducer.close();
        }

    }






}

  

import java.util.Collections;
import java.util.Properties;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;
public class Consumer {

    public static void main(String[] args) {
        Properties p = new Properties();
        p.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "www.top1novel.com:9092");
        p.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        p.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        p.put(ConsumerConfig.GROUP_ID_CONFIG, "test-consumer-group");

        KafkaConsumer kafkaConsumer = new KafkaConsumer(p);
        kafkaConsumer.subscribe(Collections.singletonList(Producer.topic));// 订阅消息

        while (true) {
            ConsumerRecords records = kafkaConsumer.poll(100);
            for (ConsumerRecord record : records) {
                System.out.println(String.format("topic:%s,offset:%d,消息:%s", //
                        record.topic(), record.offset(), record.value()));
            }
        }
    }
}

  

使用kafka-net 也可以连接。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using KafkaNet;
using KafkaNet.Model;
using KafkaNet.Protocol;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("this ");


            var options = new KafkaOptions(new Uri("http://www.top1novel.com:9092"));
            
            var router = new BrokerRouter(options);
           
            var producer = new Producer(router);
            List msgArr = new List();
            msgArr.Add(new Message("你好"));
            producer.SendMessageAsync("javatest", msgArr.ToArray()).Wait(3000);
            Console.ReadKey();
        }
    }
}

  

 另外java 的全部源代码地址: https://github.com/fmyuan2000/kafka01

 

kafka 创建topic 并使用java/.net 连接

标签:depend   git   end   val   lis   linq   ext   prot   消息   

原文地址:https://www.cnblogs.com/wenwenxiaoxing/p/11152214.html

上一篇:windows dll

下一篇:windows常用环境变量


评论


亲,登录后才可以留言!