每个生产者在使用交易时使用不同的key.serializer

时间:2019-06-18 08:04:37

标签: spring-kafka spring-cloud-stream

我正在使用spring-cloud-stream-kafka,但我不了解在使用事务时如何更改key.serializer属性。这是我的配置:

spring:
  cloud:
    stream:
      bindings:
        accountSource:
          contentType: application/*+avro
          destination: account
      kafka:
        binder:
          brokers: ${KAFKA_BOOTSTRAP_ADDRESSES}
          transaction:
            transaction-id-prefix: tx-
            producer:
              configuration:
                retries: 1
                acks: all                
        bindings:
          accountSource:
            producer:
              configuration:
                key.serializer: org.apache.kafka.common.serialization.StringSerializer # Ignored!
      schema:
        avro:
          subjectNamingStrategy: com.example.CustomSubjectNamingStrategy
      schemaRegistryClient:
        endpoint: ${KAFKA_SCHEMA_REGISTRY_URL}

我读过here,说在绑定器级别设置事务属性时,所有其他特定的绑定生成器属性。这是否意味着应用程序中的所有生产者都应使用相同的key.serializer?对我来说似乎很严格。

1 个答案:

答案 0 :(得分:1)

问题在于必须通过使用者绑定来启动事务(因此它可以将偏移发送给事务,并在成功/失败之后进行提交或回滚)。

如果有多个生产者绑定,则消费者绑定不知道应用程序会将数据发送到哪个用户,因此我们必须使用一个全局生产者。

一种解决方案是编写一个自定义的委托序列化程序,并在输出消息中设置一个标头,以告知序列化程序要调用哪个委托序列化程序。