我们如何在Spring Cloud Stream Kafka生产者,使用者和KStreams中为架构配置value.subject.name.strategy?

时间:2019-04-24 09:23:23

标签: apache-kafka avro apache-kafka-streams spring-cloud-stream confluent-schema-registry

我想在Spring Cloud Stream生产者,使用者和KStreams中自定义Avro模式主题的命名策略。

这将在Kafka中通过属性key.subject.name.strategyvalue.subject.name.strategy-> https://docs.confluent.io/current/schema-registry/serializer-formatter.html#subject-name-strategy

完成

在本地Kafka Producer中,此方法有效:


private val producer: KafkaProducer<Int, Customer>

    init {
        val props = Properties()
        ...
        props[AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG] = "http://localhost:8081"
        props[AbstractKafkaAvroSerDeConfig.VALUE_SUBJECT_NAME_STRATEGY] = TopicRecordNameStrategy::class.java.name
        producer = KafkaProducer(props)
    }

    fun sendCustomerEvent(customer: Customer) {
        val record: ProducerRecord<Int, Customer> = ProducerRecord("customer", customer.id, customer)
        producer.send(record)
    }

但是我无法在Spring Cloud Stream中找到如何做到这一点。到目前为止,我已经在制作人中尝试过此操作:

spring:
  application:
    name: spring-boot-customer-service
  cloud:
    stream:
      kafka:
        bindings:
          output:
            producer:
              configuration:
                key:
                  serializer: org.apache.kafka.common.serialization.IntegerSerializer
                value:
                  subject:
                    name:
                      strategy: io.confluent.kafka.serializers.subject.TopicRecordNameStrategy

显然,Spring Cloud通过接口org.springframework.cloud.stream.schema.avro.SubjectNamingStrategy和仅一个子类DefaultSubjectNamingStrategy使用它自己的主题命名策略。

是使用声明性的方式配置value.subject.name.strategy还是我们希望提供自己的org.springframework.cloud.stream.schema.avro.SubjectNamingStrategy实现和属性spring.cloud.stream.schema.avro.subject-naming-strategy

2 个答案:

答案 0 :(得分:1)

您可以在属性中将其声明为

spring.cloud.stream.schema.avro.subjectNamingStrategy=MyStrategy

其中MyStrategy是接口的实现。例如

object MyStrategy: SubjectNamingStrategy {
   override fun toSubject(schema: Schema): String = schema.fullName
}

答案 1 :(得分:0)

在另一个答案中指出,有一个dedicated propertyspring.cloud.stream.schema.avro.subjectNamingStrategy,可以为卡夫卡生产者设置不同的命名策略

我贡献了org.springframework.cloud.stream.schema.avro.QualifiedSubjectNamingStrategy,提供了开箱即用的功能。

对于 Kafka Streams 和本机序列化/反序列化(Spring Cloud Streams 3.0.0+中的默认行为),您必须使用Confluent的实现(io.confluent.kafka.serializers.subject.RecordNameStrategy)和本机属性:

spring:
  application:
    name: shipping-service
  cloud:
    stream:
      ...
      kafka:
        streams:
          binder:
            configuration:
              application:
                id: shipping-service
              ...
              value:
                subject:
                  name:
                    strategy: io.confluent.kafka.serializers.subject.RecordNameStrategy