Adding kafka communication using spring kafka
This commit is contained in:
57
spring-kafka-communication-service/pom.xml
Executable file
57
spring-kafka-communication-service/pom.xml
Executable file
@@ -0,0 +1,57 @@
|
|||||||
|
<?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.6.1</version>
|
||||||
|
<relativePath/> <!-- lookup parent from repository -->
|
||||||
|
</parent>
|
||||||
|
<groupId>com.amrut.prabhu</groupId>
|
||||||
|
<artifactId>spring-kafka-communication-service</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>Spring kafka Communication Service</name>
|
||||||
|
<properties>
|
||||||
|
<java.version>17</java.version>
|
||||||
|
</properties>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.kafka</groupId>
|
||||||
|
<artifactId>spring-kafka</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.kafka</groupId>
|
||||||
|
<artifactId>spring-kafka-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package com.amrut.prabhu.kafkacommunicationservice;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
@EnableScheduling
|
||||||
|
public class KafkaCommunicationServiceApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(KafkaCommunicationServiceApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package com.amrut.prabhu.kafkacommunicationservice;
|
||||||
|
|
||||||
|
import com.amrut.prabhu.kafkacommunicationservice.dto.Message;
|
||||||
|
import org.apache.kafka.clients.consumer.ConsumerRecord;
|
||||||
|
import org.springframework.kafka.annotation.KafkaListener;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class KafkaConsumer {
|
||||||
|
|
||||||
|
@KafkaListener(id = "my-client-application", topics = "${topic.name}")
|
||||||
|
public void consumer(ConsumerRecord<String, Message> consumerRecord) {
|
||||||
|
System.out.println("Consumed Record Details: " + consumerRecord);
|
||||||
|
Message message = consumerRecord.value();
|
||||||
|
System.out.println("Consumed Message" + message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package com.amrut.prabhu.kafkacommunicationservice;
|
||||||
|
|
||||||
|
import com.amrut.prabhu.kafkacommunicationservice.dto.Message;
|
||||||
|
import org.apache.kafka.clients.producer.ProducerRecord;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.kafka.core.KafkaTemplate;
|
||||||
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class KafkaProducer {
|
||||||
|
|
||||||
|
@Value("${topic.name}")
|
||||||
|
private String topicName;
|
||||||
|
|
||||||
|
private KafkaTemplate kafkaTemplate;
|
||||||
|
|
||||||
|
public KafkaProducer(KafkaTemplate kafkaTemplate) {
|
||||||
|
this.kafkaTemplate = kafkaTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Scheduled(cron = "*/2 * * * * *")
|
||||||
|
public void sendMessage() {
|
||||||
|
UUID key = UUID.randomUUID();
|
||||||
|
Message payload = new Message("jack");
|
||||||
|
System.out.println("Sending Data " + payload);
|
||||||
|
|
||||||
|
ProducerRecord<String, Message> record = new ProducerRecord<String, Message>(topicName,
|
||||||
|
key.toString(),
|
||||||
|
payload);
|
||||||
|
record.headers()
|
||||||
|
.add("message-id", UUID.randomUUID()
|
||||||
|
.toString()
|
||||||
|
.getBytes());
|
||||||
|
kafkaTemplate.send(record);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
package com.amrut.prabhu.kafkacommunicationservice.dto;
|
||||||
|
|
||||||
|
public record Message(String name) {
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package com.amrut.prabhu.kafkacommunicationservice.dto.converters;
|
||||||
|
|
||||||
|
import com.amrut.prabhu.kafkacommunicationservice.dto.Message;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.apache.kafka.common.errors.SerializationException;
|
||||||
|
import org.apache.kafka.common.serialization.Deserializer;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class MessageDeSerializer implements Deserializer<Message> {
|
||||||
|
|
||||||
|
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Message deserialize(String topic, byte[] data) {
|
||||||
|
try {
|
||||||
|
return objectMapper.readValue(data, Message.class);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new SerializationException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package com.amrut.prabhu.kafkacommunicationservice.dto.converters;
|
||||||
|
|
||||||
|
import com.amrut.prabhu.kafkacommunicationservice.dto.Message;
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import org.apache.kafka.common.errors.SerializationException;
|
||||||
|
import org.apache.kafka.common.serialization.Serializer;
|
||||||
|
|
||||||
|
public class MessageSerializer implements Serializer<Message> {
|
||||||
|
|
||||||
|
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] serialize(String topic, Message data) {
|
||||||
|
try {
|
||||||
|
return objectMapper.writeValueAsBytes(data);
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
throw new SerializationException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
18
spring-kafka-communication-service/src/main/resources/application.yml
Executable file
18
spring-kafka-communication-service/src/main/resources/application.yml
Executable file
@@ -0,0 +1,18 @@
|
|||||||
|
spring:
|
||||||
|
kafka:
|
||||||
|
bootstrap-servers:
|
||||||
|
- localhost:9092
|
||||||
|
consumer:
|
||||||
|
client-id: my-client-consumer
|
||||||
|
group-id: spring-application-group
|
||||||
|
key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
|
||||||
|
value-deserializer: com.amrut.prabhu.kafkacommunicationservice.dto.converters.MessageDeSerializer
|
||||||
|
producer:
|
||||||
|
client-id: my-client-application
|
||||||
|
key-serializer: org.apache.kafka.common.serialization.StringSerializer
|
||||||
|
value-serializer: com.amrut.prabhu.kafkacommunicationservice.dto.converters.MessageSerializer
|
||||||
|
|
||||||
|
topic:
|
||||||
|
name: "first-topic"
|
||||||
|
server:
|
||||||
|
port: 9191
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package com.amrut.prabhu.kafkacommunicationservice;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
class KafkaCommunicationServiceApplicationTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void contextLoads() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user