Java中调用Kafka

使用的是kafka 0.11.0.0版本。

生产者:

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;

import java.util.Properties;

public class KafkaProducerFirst {

    private  final static String TOPIC = "first";

    public static void main(String[] args) {

        Properties props = new Properties();
        // 服务器ip:端口号,集群用逗号分隔
        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.122.132:9092");
        // key序列化指定类
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        // value序列化指定类
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());

        // 生产者对象
        KafkaProducer<String, String> producer = new KafkaProducer<String, String>(props);
        // 向first发送hello, kafka
        producer.send(new ProducerRecord<String, String>(TOPIC, "生产者:发送出去了"));
        System.out.println("已发送");
        producer.close();

    }
}

消费者:

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.TopicPartition;

import java.util.ArrayList;
import java.util.Properties;

public class KafkaComsumerFirst {

    private final static String TOPIC = "first";

    public static void main(String[] args) {

        Properties prop = new Properties();
        prop.put("bootstrap.servers", "192.168.122.132:9092");
        prop.put("group.id", "group-1");
        prop.put("enable.auto.commit", "true");
        prop.put("auto.commit.interval.ms", "1000");
        //auto.offset.reset=earliest 表示从头开始消费,latest消费最新的
        prop.put("auto.offset.reset", "latest");
        //prop.put("session.timeout.ms", "30000");
        //prop.put("partition.assignment.strategy", "range");
        prop.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        prop.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");

        KafkaConsumer<String, String> consumer =  new KafkaConsumer<String, String>(prop);
        TopicPartition partition = new TopicPartition(TOPIC, 0);
        ArrayList<String> list = new ArrayList<String>();
        list.add(TOPIC);
        consumer.subscribe(list);
        //list.add(partition);
        //consumer.assign(list);
        //consumer.subscribe(partition);

        while (true) {
            ConsumerRecords<String, String> records = consumer.poll(1000);
            for (ConsumerRecord<String, String> record : records) {
                System.out.println("offset:" + record.offset());
                System.out.println("value:" + record.value());
            }
        }
    }
}

出现的问题:

在linux内使用Shell可以成功发送消息创建Topic。 
但是在外部使用API无法发送消息。
具体原因:
Hostname and port the broker will advertise to producers and consumers. If not set, it uses the value for “listeners” if configured. Otherwise, it will use the value returned from java.net.InetAddress.getCanonicalHostName().
解决方法:
conf/service.properties中
#advertised.listeners=PLAINTEXT://your.host.name:9092
修改为
advertised.listeners=PLAINTEXT://192.168.84.136:9092 (为虚拟机的ip)

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值