Skip to content

Commit 1aea3d1

Browse files
committed
kafka sample
1 parent d4e54e5 commit 1aea3d1

5 files changed

Lines changed: 117 additions & 0 deletions

File tree

kafka-sample/pom.xml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.fd</groupId>
8+
<artifactId>kafka-sample</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<java-version>1.8</java-version>
13+
<maven.compiler.source>1.8</maven.compiler.source>
14+
<maven.compiler.target>1.8</maven.compiler.target>
15+
</properties>
16+
17+
<parent>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-starter-parent</artifactId>
20+
<version>2.1.4.RELEASE</version>
21+
</parent>
22+
23+
<dependencies>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-web</artifactId>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>org.projectlombok</groupId>
31+
<artifactId>lombok</artifactId>
32+
<version>1.18.4</version>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>org.springframework.kafka</groupId>
37+
<artifactId>spring-kafka</artifactId>
38+
</dependency>
39+
40+
</dependencies>
41+
42+
</project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.fd.kafka;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
/**
7+
* @author fdanismaz
8+
* date: 5/1/19 2:03 PM
9+
*/
10+
@SpringBootApplication
11+
public class App {
12+
13+
public static void main(String[] args) {
14+
SpringApplication.run(App.class);
15+
}
16+
17+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.fd.kafka.controller;
2+
3+
import com.fd.kafka.service.Producer;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.http.ResponseEntity;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
/**
11+
* @author fdanismaz
12+
* date: 5/1/19 2:09 PM
13+
*/
14+
@RestController
15+
@RequestMapping("/producer")
16+
public class ProducerController {
17+
18+
@Autowired
19+
private Producer producer;
20+
21+
@GetMapping("/heartbeat")
22+
public ResponseEntity<Boolean> heartbeat() {
23+
this.producer.sendMessage("test", "heartbeat", "up");
24+
return ResponseEntity.ok(true);
25+
}
26+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.fd.kafka.service;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.kafka.core.KafkaTemplate;
5+
import org.springframework.stereotype.Service;
6+
7+
/**
8+
* @author fdanismaz
9+
* date: 5/1/19 2:14 PM
10+
*/
11+
@Service
12+
public class Producer {
13+
14+
@Autowired
15+
private KafkaTemplate<String, String> kafkaTemplate;
16+
17+
public void sendMessage(String topic, String key, String message) {
18+
this.kafkaTemplate.send(topic, key, message);
19+
}
20+
21+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#spring.datasource.driver-class-name=com.mysql.jdbc.Driver
2+
#spring.datasource.url=jdbc:mysql://localhost:3306/bookstore
3+
#spring.datasource.username=root
4+
#spring.datasource.password=123456
5+
#spring.jpa.hibernate.ddl-auto=none
6+
#spring.jpa.show-sql=true
7+
#spring.jpa.generate-ddl=true
8+
9+
spring.kafka.producer.bootstrap-servers = localhost:9092
10+
spring.kafka.producer.key-serializer = org.apache.kafka.common.serialization.StringSerializer
11+
spring.kafka.producer.value-serializer = org.apache.kafka.common.serialization.StringSerializer

0 commit comments

Comments
 (0)