无法在 spring 云卡夫卡中按 topic/binder 级别设置 serde、生产者和消费者属性

Unable to set serde, producer and consumer properties per topic/binder level in spring cloud kafka

我正在尝试使用 spring 云 kafka 活页夹来启动简单的 pub-sub 应用程序。但是,我无法在 application.yml 中设置 Serializer、DeSerialzer 属性 和其他生产者和消费者属性。我一直收到 serialization/deserialization 错误。即使 kafka 登录 spring 引导项目显示生产者和消费者配置仍然使用 ByteArraySerializer。下面是相同的代码。

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>io.github.kprasad99.kafka</groupId>
    <artifactId>kp-kafka-streams-example</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>kp-kafka-streams-example</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
        <spring-cloud.version>Hoxton.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka-streams</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-kafka-streams</artifactId>
        </dependency> -->

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-kafka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-test-support</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Processor.java

public interface Processor {

    String INPUT="k-msg-source";
    String OUTPUT="k-msg-sink";

    @Input(INPUT)
    SubscribableChannel input();

    @Output(OUTPUT)
    MessageChannel output();

}

KRest.java

@RestController public class KRest {

@Autowired
private Processor processor;

@GetMapping("/send")
public ResponseEntity<Void> send(@RequestParam("key")String key, @RequestParam("msg") String text){
    processor.input().send(MessageBuilder.withPayload(Message.builder().text(text).build()).setHeader(KafkaHeaders.MESSAGE_KEY, key).build());
    return ResponseEntity.ok().build();
}

}

Message.java

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Message {

    private String text;
}

最后application.yml

spring:
  cloud:
    stream:
      bindings:
        k-msg-source:
          binder: kafka
          content-type: application/json
          destination: topic.kp.msg
          group: kp.msg.source
        k-msg-sink:
          binder: kafka
          content-type: application/json
          destination: topic.kp.msg
          group: kp.msg.sink
          producer:
            partition-count: 10
      binders:
        kafka:
          type: kafka
          environment:
            spring.cloud.stream.kafka.binder:
              brokers: localhost:9092
              configuration:
                value.serde: JsonSerde
                key.serde: StringSerde
              producer:
                value.serde: JsonSerde
                key.serde: StringSerde
              replication-factor: 1

版本

SerdeKafka Streams binder 使用。

对于 MessageChannel binder,属性是 value.serializervalue.deserializer(和 key...),以及 key/value.deserializer

您还必须指定 类 的完全限定名称。

决赛application.yml

spring.cloud.stream:
  bindings:
    k-msg-source:
      binder: kafka
      destination: topic.kp.msg
      content-type: application/json
      producer:
        partition-count: 10
        use-native-encoding: true
    k-msg-sink:
      binder: kafka
      destination: topic.kp.msg
      group: ne-publisher
      content-type: application/json
      consumer:
        use-native-decoding: true
  binders:
    kafka:
      type: kafka
      environment:
        spring.cloud.stream.kafka.binder:
          brokers: PLAINTEXT://localhost:19092,PLAINTEXT://localhost:29092,PLAINTEXT://localhost:39092
          configuration:
            key.serializer: org.apache.kafka.common.serialization.StringSerializer
            value.serializer: org.springframework.kafka.support.serializer.JsonSerializer
            key.deserializer: org.apache.kafka.common.serialization.StringDeserializer
            value.deserializer: org.springframework.kafka.support.serializer.JsonDeserializer
          consumer-properties:
            spring.json.trusted.packages: "*"