0
votes

I'm trying to bring up simple pub-sub application using spring cloud kafka binder. However I'm unable to set Serializer, DeSerialzer property and other producer and consumer properties in application.yml. I consistently get serialization/deserialization error. Even kafka logs in spring boot project shows producer and consumer config still users ByteArraySerializer. Below is the code sameple.

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;
}

And finally 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

Versions

  • spring-boot: 2.2.4
  • spring-cloud: Hoxton.SR1
  • spring-cloud-stream-kafka-binder: 3.0.1
2

2 Answers

1
votes

Serdes are used by the Kafka Streams binder.

With the MessageChannel binder, the properties are value.serializer and value.deserializer (and key...), and key/value.deserializer.

You also have to specify the fully qualified named of the classes.

0
votes

Final 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: "*"