-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEAT] Messaging : replace aws kafka broker with embeded kafka
- Loading branch information
1 parent
fc22f31
commit 7c3b025
Showing
9 changed files
with
112 additions
and
42 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
messaging-service/src/main/java/com/example/kafkamessaging/Config/KafkaConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.example.kafkamessaging.Config; | ||
|
||
import org.apache.kafka.clients.admin.AdminClient; | ||
import org.apache.kafka.clients.admin.AdminClientConfig; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import java.util.Properties; | ||
|
||
@Configuration | ||
public class KafkaConfig { | ||
|
||
@Value("${spring.kafka.bootstrap-servers}") | ||
private String bootstrapServers; | ||
|
||
@Bean | ||
public AdminClient adminClient() { | ||
Properties properties = new Properties(); | ||
properties.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers); | ||
return AdminClient.create(properties); | ||
} | ||
} |
34 changes: 25 additions & 9 deletions
34
messaging-service/src/main/java/com/example/kafkamessaging/Controller/KafkaController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,39 @@ | ||
package com.example.kafkamessaging.Controller; | ||
|
||
import com.example.kafkamessaging.MessageRecord; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.apache.kafka.clients.admin.AdminClient; | ||
import org.apache.kafka.clients.admin.NewTopic; | ||
import org.springframework.kafka.core.KafkaTemplate; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import java.util.Collections; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
@RestController | ||
@RequestMapping("api/kafka/send") | ||
@CrossOrigin(origins = "http://localhost:8888") | ||
@RequestMapping("api/kafka") | ||
@RequiredArgsConstructor | ||
public class KafkaController { | ||
|
||
private final KafkaTemplate<String, String> kafkaTemplate; | ||
private final AdminClient adminClient; | ||
|
||
public KafkaController(KafkaTemplate<String, String> kafkaTemplate) { | ||
this.kafkaTemplate = kafkaTemplate; | ||
@PostMapping("send/{topic}") | ||
public void publish(@PathVariable("topic") String topic, @RequestBody MessageRecord messageRecord) { | ||
kafkaTemplate.send(topic, messageRecord.message()); | ||
} | ||
|
||
@PostMapping | ||
public void publish(@RequestBody MessageRecord messageRecord) { | ||
kafkaTemplate.send("sporterz", messageRecord.message()); | ||
@PostMapping("create-topic") | ||
public ResponseEntity<String> createTopic(@RequestParam String topicName) { | ||
NewTopic newTopic = new NewTopic(topicName, 1, (short) 1); | ||
try { | ||
adminClient.createTopics(Collections.singleton(newTopic)).all().get(); | ||
return ResponseEntity.status(HttpStatus.CREATED).body("Topic created successfully: " + topicName); | ||
} catch (InterruptedException | ExecutionException e) { | ||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to create topic: " + topicName); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
server.port=8083 | ||
spring.application.name=messaging-service | ||
spring.kafka.bootstrap-servers=ec2-3-65-182-112.eu-central-1.compute.amazonaws.com:9092 | ||
#spring.kafka.bootstrap-servers=ec2-3-65-182-112.eu-central-1.compute.amazonaws.com:9092 | ||
spring.kafka.bootstrap-servers=localhost:9092 | ||
|
||
management.endpoints.web.exposure.include=prometheus |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters