kafka使用范例

本文详细介绍了Apache Kafka的生产者使用,通过具体的代码示例展示如何创建及配置Kafka生产者,发送消息到主题,以及讨论关键配置选项和最佳实践。
摘要由CSDN通过智能技术生成

生产者

public class ProducerDemo {
	
	public static void main(String[] args) throws Exception {
		//配置文件
		Properties properties = new Properties();
		properties.put("zk.connect", "lp5:2181,lp6:2181,lp7:2181");
		properties.put("metadata.broker.list","lp5:9092,lp6:9092,lp7:9092");
		properties.put("serializer.class", "kafka.serializer.StringEncoder");
		ProducerConfig config = new ProducerConfig(properties);
		Producer<String, String> producer = new Producer<String, String>(config);
		
		//处理业务
		//读取文件 读取数据库 读取内存
		for(int i=0;i<100;i++){
			Thread.sleep(500);
			KeyedMessage<String, String> message = new KeyedMessage<String, String>("order", "" + i, "10086" + i*i);
			producer.send(message);
		}
	}
}

消费者

public class ComsumerDemo {

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值