I have problem with producer of Kafka with Spring cloud with Spring boot. When I try to create configuration in application.yml, It allways send messages to wrong topic. I use placeholder for my topic "kafka_demo_topic_out_0" and it sends message not in right destination kafka_demo_topic but in kafka_demo_topic_out_0. Here is my code for producer :
package org.heller.kafka.demo.producer;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicLong;
import org.heller.kafka.demo.producer.pojo.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.databind.ObjectMapper;
@Component
@EnableScheduling
public class KafkaProducer {
private AtomicLong idGenerator = new AtomicLong();
@Autowired
StreamBridge streamBridge;
@Scheduled(fixedDelay = 1000)
public void scheduleFixedRateTask() throws Exception {
Message message = constructMessage();
System.out.println("sending message" + message);
streamBridge.send("kafka_demo_topic_out_0", new ObjectMapper().writeValueAsString(message));
}
private Message constructMessage() {
Message message = new Message();
message.setId(idGenerator.getAndIncrement());
message.setUuid( UUID.randomUUID().toString());
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
message.setDate(now.format(formatter));
return message;
}
}
Here is my application.yml :
spring:
cloud:
stream:
kafka:
binder:
autoAddPartitions: true
brokers: localhost:9092
auto-create-topics: false
bindings:
kafka_demo_topic_out_0:
producer:
headerMode: raw
destination: kafka_demo_topic
content-type: application/json
binder: kafka
I use Spring starter Kafka :
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-kafka</artifactId>
<version>4.1.0</version>
</dependency>
Thank you for any help.
Update: Spring cloud stream bridge somehow ignore configuration in application.yml. I try this configuration, but It still create new topic named kafkaDemoTopic.
spring:
cloud:
stream:
function:
definition: kafkaDemoTopic
kafka:
binder:
autoAddPartitions: true
brokers: localhost:9092
auto-create-topics: false
bindings:
kafkaDemoTopic-out-0:
headerMode: raw
destination: kafka_demo_topic
content-type: application/json
binder: kafka
I find solution, my application.yml :
My Kafka producer:
My pom.xml