码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-07-08 19:29:05      阅读:502      评论:0      收藏:0      [点我收藏+]

标签: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

<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>2.3.0</version>
</dependency>

特别注意的需要设置: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<String, String> kafkaProducer = new KafkaProducer<String,String>(p);

        try {
            while (true) {
                String msg = "Hello," + new Random().nextInt(100);
                ProducerRecord<String, String> record = new ProducerRecord<String, String>(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<String, String> kafkaConsumer = new KafkaConsumer<String, String>(p);
        kafkaConsumer.subscribe(Collections.singletonList(Producer.topic));// 订阅消息

        while (true) {
            ConsumerRecords<String, String> records = kafkaConsumer.poll(100);
            for (ConsumerRecord<String, String> 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<Message> msgArr = new List<Message>();
            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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!