- I have two microservices written in Java, using Spring Boot.
- I use Kafka, through Spring Cloud Stream Kafka, to send messages between them.
- I need to send a custom header, but with no success until now.
- I have read and tried most of the things I have found on internet and Spring Cloud Stream documentation...
... still I have been unable to make it work.
Which means I never receive a message in the receiver because the header cannot be found and cannot be null. I suspect the header is never written in the message. Right now I am trying to verify this with Kafkacat.
Any help will be wellcome
Thanks in advance.
------ information --------------------
Here it is the sender code:
@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);
}
Where notifications
is a MessageChannel
Here is the related configuration for message sender.
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
I have also tried with headers: "EVENT_ID"
Here is the code for the receiver part:
@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
}
And the configuration for the receiving part:
spring:
cloud:
stream:
kafka:
bindings:
notifications:
consumer:
headerMode: headers
Versions
<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>