簡體   English   中英

無法使用 spring 雲流 kafka 發送自定義標頭

[英]Unable to send custom header using spring cloud stream kafka

  • 我有兩個用 Java 編寫的微服務,使用 Spring Boot。
  • 我使用 Kafka,通過 Spring Cloud Stream Kafka,在它們之間發送消息。
  • 我需要發送一個自定義標頭,但直到現在還沒有成功。
  • 我已經閱讀並嘗試了我在互聯網和 Spring Cloud Stream 文檔上找到的大部分內容......

......我仍然無法讓它發揮作用。

這意味着我永遠不會在接收器中收到消息,因為找不到頭並且不能為空。 我懷疑標題從未寫在消息中。 現在我正試圖用 Kafkacat 驗證這一點。

任何幫助都會受到歡迎

提前致謝。

- - - 信息 - - - - - - - - - -

這是發件人代碼:

@SendTo("notifications")
public void send(NotificationPayload payload, String eventId) {
   var headerMap = Collections.singletonMap("EVENT_ID",
                                eventId.getBytes(StandardCharsets.UTF_8));
   MessageHeaders headers = new MessageHeaders(headerMap);
   var message = MessageBuilder.createMessage(payload, headers);
   notifications.send(message);
}

notifications是一個MessageChannel

這里是消息發送者的相關配置。

spring:
  cloud:
    stream:
      defaultBinder: kafka
      bindings:
        notifications:
          binder: kafka
          destination: notifications
          contentType: application/x-java-object;type=com.types.NotificationPayload
              producer:
                partitionCount: 1
                headerMode: headers
      kafka:
        binder:
          headers: EVENT_ID

我也試過headers: "EVENT_ID"

這是接收器部分的代碼:

@StreamListener("notifications")
public void receiveNotif(@Header("EVENT_ID") byte[] eventId, 
                         @Payload NotificationPayload payload) {
var eventIdS = new String((byte[]) eventId, StandardCharsets.UTF_8);
...
// do something with the payload
}

以及接收部分的配置:

spring:
  cloud:
     stream:
       kafka:
         bindings:
           notifications:
             consumer:
               headerMode: headers

版本

    <spring-cloud-stream-dependencies.version>Horsham.SR4</spring-cloud-stream-dependencies.version>
    <spring-cloud-stream-binder-kafka.version>3.0.4.RELEASE</spring-cloud-stream-binder-kafka.version>
    <spring-cloud-schema-registry.version>1.0.4.RELEASE</spring-cloud-schema-registry.version>
    <spring-cloud-stream.version>3.0.4.RELEASE</spring-cloud-stream.version>

你用的是什么版本? 更詳細地描述“無法讓它工作”。

這工作正常...

@SpringBootApplication
@EnableBinding(Source.class)
public class So64586916Application {

    public static void main(String[] args) {
        SpringApplication.run(So64586916Application.class, args);
    }

    @InboundChannelAdapter(channel = Source.OUTPUT)
    Message<String> source() {
        return MessageBuilder.withPayload("foo")
                .setHeader("myHeader", "someValue")
                .build();
    }

    @KafkaListener(id = "in", topics = "output")
    void listen(Message<?> in) {
        System.out.println(in);
    }

}
spring.kafka.consumer.auto-offset-reset=earliest
GenericMessage [payload=byte[3], headers={myHeader=someValue, kafka_offset=0, ...
GenericMessage [payload=byte[3], headers={myHeader=someValue, kafka_offset=1, ...

編輯

我也通過直接發送到頻道進行了測試; 再次沒有問題:

@Autowired
MessageChannel output;

@Bean
public ApplicationRunner runner() {
    return args -> {
        this.output.send(MessageBuilder.withPayload("foo")
                .setHeader("myHeader", "someValue")
                .build());
    };
}

暫無
暫無

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

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