无法在SpringCloudKafka中设置每个主题/活页夹级别的serde、producer和consumer属性

x8goxv8g  于 2021-06-05  发布在  Kafka
关注(0)|答案(2)|浏览(439)

我尝试使用springcloudkafka活页夹创建一个简单的pub-sub应用程序。但是,我无法在application.yml中设置序列化程序、反序列化程序属性以及其他生产者和使用者属性。我总是得到序列化/反序列化错误。甚至kafka在springboot项目中的日志也显示生产者和消费者仍然通过realyserializer配置用户。下面是代码示例。
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();

}

克雷斯特. 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

版本
Spring Boot:2.2.4
Spring Cloud:hoxton.sr1
Spring Cloud溪Kafka活页夹:3.0.1

5lhxktic

5lhxktic1#

最终 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: "*"

mzmfm0qo

mzmfm0qo2#

Serde Kafka流活页夹使用。
MessageChannel 粘合剂,性能 value.serializer 以及 value.deserializer (和 key... ),和 key/value.deserializer .
还必须指定类的完全限定名。

相关问题