我正在尝试使用Spring Cloud Streams + Kafka Binding与Apache Kafka进行“完全一次交付”概念的一些PoC。
我安装了Apache Kafka“kafka_2.11-1.0.0”并在生产者中定义了“transactionIdPrefix”,据我所知,这是我在Spring Kafka中启用事务所需要做的唯一事情,但是当我这样做并运行时简单来源&在同一个应用程序中的接收器绑定,我看到消费者收到并打印了一些消息,有些消息会出错。
例如,收到消息#6:
[49] Received message [Payload String content=FromSource1 6][Headers={kafka_offset=1957, scst_nativeHeadersPresent=true, kafka_consumer=org.apache.kafka.clients.consumer.KafkaConsumer@6695c9a9, kafka_timestampType=CREATE_TIME, my-transaction-id=my-id-6, id=302cf3ef-a154-fd42-6b43-983778e275dc, kafka_receivedPartitionId=0, contentType=application/json, kafka_receivedTopic=test10, kafka_receivedTimestamp=1514384106395, timestamp=1514384106419}]
但是消息#7出现错误“从状态IN_TRANSACTION到状态IN_TRANSACTION尝试无效转换”:
2017-12-27 16:15:07.405 ERROR 7731 --- [ask-scheduler-4] o.s.integration.handler.LoggingHandler : org.springframework.messaging.MessageHandlingException: error occurred in message handler [org.springframework.cloud.stream.binder.kafka.KafkaMessageChannelBinder$ProducerConfigurationMessageHandler@7d3bbc0b]; nested exception is org.apache.kafka.common.KafkaException: TransactionalId my-transaction-3: Invalid transition attempted from state IN_TRANSACTION to state IN_TRANSACTION, failedMessage=GenericMessage [payload=byte[13], headers={my-transaction-id=my-id-7, id=d31656af-3286-99b0-c736-d53aa57a5e65, contentType=application/json, timestamp=1514384107399}]
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:153)
at org.springframework.cloud.stream.binder.AbstractMessageChannelBinder$SendingHandler.handleMessageInternal(AbstractMessageChannelBinder.java:575)
更新 我在项目的github上打开了一个问题,请参考那里的讨论。
无法找到如何在启用Kafka绑定+ Trasanctions的情况下使用Spring Cloud Stream的示例
要重现,需要使用spring boot version“2.0.0.M5”和“spring-cloud-stream-dependencies”版本“Elmhurst.M3”创建一个简单的maven项目,并使用此配置创建一个简单的应用程序:
server:
port: 8082
spring:
kafka:
producer:
retries: 5555
acks: "all"
cloud:
stream:
kafka:
binder:
autoAddPartitions: true
transaction:
transactionIdPrefix: my-transaction-
bindings:
output1:
destination: test10
group: test111
binder: kafka
input1:
destination: test10
group: test111
binder: kafka
consumer:
partitioned: true
我还创建了简单的Source和Sink类:
@EnableBinding(SampleSink.MultiInputSink.class)
public class SampleSink {
@StreamListener(MultiInputSink.INPUT1)
public synchronized void receive1(Message<?> message) {
System.out.println("["+Thread.currentThread().getId()+"] Received message " + message);
}
public interface MultiInputSink {
String INPUT1 = "input1";
@Input(INPUT1)
SubscribableChannel input1();
}
}
和
@EnableBinding(SampleSource.MultiOutputSource.class)
public class SampleSource {
AtomicInteger atomicInteger = new AtomicInteger(1);
@Bean
@InboundChannelAdapter(value = MultiOutputSource.OUTPUT1, poller = @Poller(fixedDelay = "1000", maxMessagesPerPoll = "1"))
public synchronized MessageSource<String> messageSource1() {
return new MessageSource<String>() {
public Message<String> receive() {
String message = "FromSource1 "+atomicInteger.getAndIncrement();
m.put("my-transaction-id","my-id-"+ UUID.randomUUID());
return new GenericMessage(message, new MessageHeaders(m));
}
};
}
public interface MultiOutputSource {
String OUTPUT1 = "output1";
@Output(OUTPUT1)
MessageChannel output1();
}
}
答案 0 :(得分:0)
我在项目的github上开了一张票。 请参考那里的答案和讨论:
https://github.com/spring-cloud/spring-cloud-stream/issues/1166
但第一个答案是:
活页夹当前不支持生产者发起的交易。
处理器(消费者启动的地方)支持交易 交易和生产者参与该交易)。
你应该可以直接使用spring-kafka来启动 当没有消费者时,生产者方面的交易。