├── src ├── main │ ├── resources │ │ └── application.properties │ └── java │ │ └── com │ │ └── javatechie │ │ └── spring │ │ └── kafka │ │ └── api │ │ ├── User.java │ │ ├── KafkaPublisherApplication.java │ │ └── config │ │ └── KafkaPublisherConfig.java └── test │ └── java │ └── com │ └── javatechie │ └── spring │ └── kafka │ └── api │ └── KafkaPublisherApplicationTests.java ├── README.md └── pom.xml /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | server.port=9091 -------------------------------------------------------------------------------- /src/main/java/com/javatechie/spring/kafka/api/User.java: -------------------------------------------------------------------------------- 1 | package com.javatechie.spring.kafka.api; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | @Data 8 | @AllArgsConstructor 9 | @NoArgsConstructor 10 | public class User { 11 | private int id; 12 | private String name; 13 | private String[] address; 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/test/java/com/javatechie/spring/kafka/api/KafkaPublisherApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.javatechie.spring.kafka.api; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class KafkaPublisherApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # kafka-publisher 2 | Apache Kafka Publisher Example using SpringBoot 3 | 4 | # start zookeeper.start bat file like below 5 | zookeeper-server-start.bat D:\DEV-SOFTWARES\kafka_2.12-1.1.0\config\zookeeper.properties 6 | 7 | # start kafka server 8 | kafka-server-start.bat D:\DEV-SOFTWARES\kafka_2.12-1.1.0\config\server.properties 9 | 10 | # Create Topic: 11 | kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 -topic javatechie 12 | 13 | # Produce a message 14 | kafka-console-producer.bat --broker-list localhost:9092 --topic javatechie 15 | 16 | # Consume a message 17 | kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic javatechie 18 | -------------------------------------------------------------------------------- /src/main/java/com/javatechie/spring/kafka/api/KafkaPublisherApplication.java: -------------------------------------------------------------------------------- 1 | package com.javatechie.spring.kafka.api; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.kafka.core.KafkaTemplate; 7 | import org.springframework.web.bind.annotation.GetMapping; 8 | import org.springframework.web.bind.annotation.PathVariable; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | @SpringBootApplication 12 | @RestController 13 | public class KafkaPublisherApplication { 14 | 15 | @Autowired 16 | private KafkaTemplate template; 17 | 18 | private String topic = "javatechie"; 19 | 20 | @GetMapping("/publish/{name}") 21 | public String publishMessage(@PathVariable String name) { 22 | template.send(topic, "Hi " + name + " Welcome to java techie"); 23 | return "Data published"; 24 | } 25 | 26 | @GetMapping("/publishJson") 27 | public String publishMessage() { 28 | User user = new User(2532, "User88", new String[] { "Bangalore", "BTM", "house 90" }); 29 | template.send(topic, user); 30 | return "Json Data published"; 31 | } 32 | 33 | public static void main(String[] args) { 34 | SpringApplication.run(KafkaPublisherApplication.class, args); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/javatechie/spring/kafka/api/config/KafkaPublisherConfig.java: -------------------------------------------------------------------------------- 1 | package com.javatechie.spring.kafka.api.config; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | import org.apache.kafka.clients.producer.ProducerConfig; 7 | import org.apache.kafka.common.serialization.StringSerializer; 8 | import org.springframework.context.annotation.Bean; 9 | import org.springframework.context.annotation.Configuration; 10 | import org.springframework.kafka.core.DefaultKafkaProducerFactory; 11 | import org.springframework.kafka.core.KafkaTemplate; 12 | import org.springframework.kafka.core.ProducerFactory; 13 | import org.springframework.kafka.support.serializer.JsonSerializer; 14 | 15 | @Configuration 16 | public class KafkaPublisherConfig { 17 | 18 | @Bean 19 | public ProducerFactory producerFactory() { 20 | Map configs = new HashMap<>(); 21 | configs.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092"); 22 | configs.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class); 23 | configs.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class); 24 | return new DefaultKafkaProducerFactory(configs); 25 | } 26 | 27 | @Bean 28 | public KafkaTemplate kafkaTemplate() { 29 | return new KafkaTemplate<>(producerFactory()); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.example 7 | kafka-publisher 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | kafka-publisher 12 | Publish message using kafka 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 2.0.2.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter-web 31 | 32 | 33 | org.springframework.kafka 34 | spring-kafka 35 | 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-devtools 40 | runtime 41 | 42 | 43 | org.projectlombok 44 | lombok 45 | true 46 | 47 | 48 | org.springframework.boot 49 | spring-boot-starter-test 50 | test 51 | 52 | 53 | 54 | 55 | 56 | 57 | org.springframework.boot 58 | spring-boot-maven-plugin 59 | 60 | 61 | 62 | 63 | 64 | 65 | --------------------------------------------------------------------------------