kafka_producer接口(旧版)

文档参考:

http://www.cnblogs.com/fxjwind/p/3803878.html以及官方文档。这位大牛的博客里挺多干货。学习了。

producer发送的是kv数据 无论Producer或KeyedMessage都是<String, String>的泛型,这里是指key和value的类型

直接上代码实现:

 

import kafka.javaapi.producer.Producer;//这里要注意。官方文档里是kafka.producer.Producer。send那里是会报错的。换成这个才行
import kafka.producer.ProducerConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;
@Configuration
public class KafkaProducerConfig {
    
    @Bean
    public Producer getProducer(){
        Properties props = new Properties();
        props.put("metadata.broker.list", "localhost:9092"); //本机broker(kafka监听地址)
        props.put("serializer.class", "kafka.serializer.StringEncoder");
        //props.put("partitioner.class", "example.producer.SimplePartitioner"); //可以不设置。如果设置了必须给出实现
        props.put("request.required.acks", "1");

        ProducerConfig config = new ProducerConfig(props);
        Producer<String, String> producer = new Producer<String, String>(config);
        return  producer;
    }
}
官方文档里是kafka.producer.Producer。send那里是会报错的。换成这个才行
import kafka.producer.ProducerConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;
@Configuration
public class KafkaProducerConfig {
    
    @Bean
    public Producer getProducer(){
        Properties props = new Properties();
        props.put("metadata.broker.list", "localhost:9092"); //本机broker(kafka监听地址)
        props.put("serializer.class", "kafka.serializer.StringEncoder");
        //props.put("partitioner.class", "example.producer.SimplePartitioner"); //可以不设置。如果设置了必须给出实现
        props.put("request.required.acks", "1");

        ProducerConfig config = new ProducerConfig(props);
        Producer<String, String> producer = new Producer<String, String>(config);
        return  producer;
    }
}


然后通过controller来调用producer发送消息:

 

 

@RequestMapping(value = "/kafkaTest",  method = RequestMethod.GET)
public String test4() {
    KeyedMessage<String, String> data = new KeyedMessage<String, String>("test", "hello", "I am producer"); //指定topic,key,value
    producer.send(data);
    return "OK";
}
同时使用上一篇介绍的consumer接口来处理消息:
 
可以看到消费者获取了消息。
 
另外。kafka与其他基于push的消息队列的异同等,我觉得这篇里分析的有些道理,可以参考下。就不再赘述了。
http://www.cnblogs.com/fxjwind/archive/2013/03/19/2969655.html
 
另外:关于新旧接口,stackoverflow 上有个解释:
 

 Kafka starting 0.8.2.x exposed a new set of API's to work with kafka, older being Producer which works with KeyedMessage[K,V]

 where the new API is KafkaProducer with ProducerRecord[K,V]:

As of the 0.8.2 release we encourage all new development to use the new Java producer.

This client is production tested and generally both faster and more fully featured than the previous Scala client.

You should preferably be using the new supported version.

Which properties should I configure, take into account to achieve optimal, high heavy writes performance, for high scale application?

This is a very broad question, which depends a lot on the architecture of your software.

It varies with scale, amount of producers, amount of consumers, etc.. There are many things to be taken into account.

I would suggest going through the documentation and reading up the sections talking about Kafka's architecture and

design to get a better picture of how it works internally.

Generally speaking, from my experience you'll need to balance the replication

factor of your data, along with retention times and number of partitions each queue goes into.

If you have more specific questions down the road, you should definitely post a question.

 

I would suggest going through the documentation and reading up the sections talking about Kafka's architecture “

虽然觉得我英语还行,但是官文实在太难看了。晚些时候我会实现一下新版的接口。

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值