簡體   English   中英

無法在 Spring Cloud kafka 中為每個主題/綁定器級別設置 serde、生產者和消費者屬性

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

我正在嘗試使用 spring cloud kafka binder 來啟動簡單的 pub-sub 應用程序。 但是我無法在 application.yml 中設置 Serializer、DeSerialzer 屬性和其他生產者和消費者屬性。 我一直收到序列化/反序列化錯誤。 甚至 kafka 登錄 spring boot 項目顯示生產者和消費者配置仍然使用 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>

處理器.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 公共類 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();
}

}

消息.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

版本

  • 彈簧靴:2.2.4
  • 彈簧雲:Hoxton.SR1
  • spring-cloud-stream-kafka-binder:3.0.1

Kafka Streams binder使用Serde

使用MessageChannel binder ,屬性是value.serializervalue.deserializer (和key... ),以及key/value.deserializer

您還必須指定類的完全限定名稱。

最終application.yml .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: "*"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM