栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 其他

三、kafka中事务的使用

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

三、kafka中事务的使用

本来以为我用过幂等性了,再使用事务应该很简单,不知道幂等性使用的可以去查看上一篇。

首先肯定是照着文档配置生产者,配置如下:

Properties props = new Properties();
        props.put("bootstrap.servers", "localhost:9092,localhost:9093,localhost:9094");
        props.put("acks", "all");
        props.put("retries", 3);
        props.put("batch.size", 163840);
        props.put("linger.ms", 10);
        props.put("buffer.memory", 33554432);
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("enable.idempotence",true);
        props.put("max.in.flight.requests.per.connection",5);
        props.put("transactional.id","aaa");
        Producer producer = new KafkaProducer<>(props);

就比幂等性多配个transactional.id,这样配好之后启动生产者一看,竟然报错了:

Exception in thread "main" java.lang.IllegalStateException: Cannot perform a 'send' before completing a call to initTransactions when transactions are enabled.
	at org.apache.kafka.clients.producer.internals.TransactionManager.failIfNotReadyForSend(TransactionManager.java:422)
	at org.apache.kafka.clients.producer.KafkaProducer.doSend(KafkaProducer.java:925)
	at org.apache.kafka.clients.producer.KafkaProducer.send(KafkaProducer.java:862)
	at org.apache.kafka.clients.producer.KafkaProducer.send(KafkaProducer.java:750)
	at org.apache.kafka.clients.producer.MyProducer.main(MyProducer.java:24)

错误所在的源码如下:

if (isTransactional()) {
            if (!hasProducerId())
                throw new IllegalStateException("Cannot perform a 'send' before completing a call to initTransactions " +
                        "when transactions are enabled.");

            if (currentState != State.IN_TRANSACTION)
                throw new IllegalStateException("Cannot call send in state " + currentState);
        }

源码大体意思是说,如果事务id不为空,而producerId未初始化,则抛错,看到这里我就疑惑了,怎么样才能初始化producerId呢,原来我用幂等性不也好好的吗,后来一查才知道,事务一定要有这一步producer.initTransactions();具体生产者代码如下:

Properties props = new Properties();
        props.put("bootstrap.servers", "localhost:9092,localhost:9093,localhost:9094");
        props.put("acks", "all");
        props.put("retries", 3);
        props.put("batch.size", 163840);
        props.put("linger.ms", 10);
        props.put("buffer.memory", 33554432);
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("enable.idempotence",true);
        props.put("max.in.flight.requests.per.connection",5);
        props.put("transactional.id","aaa");
        Producer producer = new KafkaProducer<>(props);
        producer.initTransactions();
        producer.beginTransaction();
        for(int i = 0; i < 5; i++){
            byte[] log = new byte[2];
            String slog = new String(log);
            producer.send(new ProducerRecord("te_transaction",0, Integer.toString(i),  slog));
        }
        producer.commitTransaction();
        producer.close();

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/279701.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号