-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f7e8ef8
commit cbac995
Showing
13 changed files
with
103 additions
and
60 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -15,4 +15,7 @@ $ ./mvnw spring-boot:run -Dspring-boot.run.profiles=local | |
|
||
### Useful Links | ||
* Swagger UI: http://localhost:8080/swagger-ui.html | ||
* Actuator Endpoint: http://localhost:8080/actuator | ||
* Actuator Endpoint: http://localhost:8080/actuator | ||
* Kafdrop UI: http://localhost:9000 | ||
* PGAdmin UI: http://localhost:5050 ([email protected]/admin) | ||
|
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 |
---|---|---|
|
@@ -27,11 +27,34 @@ services: | |
KAFKA_CONTROLLER_QUORUM_MODE: 'kraft' | ||
CLUSTER_ID: '4L6g3nShT-eMCtK--X86sw' | ||
|
||
kafdrop: | ||
image: obsidiandynamics/kafdrop | ||
restart: "no" | ||
ports: | ||
- "9000:9000" | ||
environment: | ||
KAFKA_BROKERCONNECT: "broker:29092" | ||
depends_on: | ||
- broker | ||
|
||
postgresqldb: | ||
image: postgres:15.3-alpine | ||
environment: | ||
- POSTGRES_USER=appuser | ||
- POSTGRES_PASSWORD=secret | ||
- POSTGRES_DB=appdb | ||
ports: | ||
- "5432:5432" | ||
- "5432:5432" | ||
|
||
pgadmin4: | ||
container_name: pgadmin4 | ||
image: dpage/pgadmin4 | ||
extra_hosts: [ 'host.docker.internal:host-gateway' ] | ||
environment: | ||
- [email protected] | ||
- PGADMIN_DEFAULT_PASSWORD=admin | ||
ports: | ||
- "5050:80" | ||
depends_on: | ||
- postgresqldb | ||
restart: unless-stopped |
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
42 changes: 7 additions & 35 deletions
42
...r-producer/src/main/java/com/example/boot/kafka/reactor/controller/MessageController.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,56 +1,28 @@ | ||
package com.example.boot.kafka.reactor.controller; | ||
|
||
import com.example.boot.kafka.reactor.dto.MessageDTO; | ||
import com.example.boot.kafka.reactor.repository.MessageRepository; | ||
import com.example.boot.kafka.reactor.util.AppConstants; | ||
import java.net.URI; | ||
import java.security.SecureRandom; | ||
import java.time.LocalDateTime; | ||
import com.example.boot.kafka.reactor.entity.MessageDTO; | ||
import com.example.boot.kafka.reactor.service.MessageService; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.kafka.clients.producer.ProducerRecord; | ||
import org.apache.kafka.clients.producer.RecordMetadata; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
import reactor.kafka.sender.KafkaSender; | ||
import reactor.kafka.sender.SenderRecord; | ||
|
||
@RestController | ||
@RequestMapping("/messages") | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
class MessageController { | ||
private final KafkaSender<Integer, MessageDTO> sender; | ||
private final MessageRepository messageRepository; | ||
|
||
private final MessageService messageService; | ||
|
||
@PostMapping | ||
public Mono<ResponseEntity<Object>> sendMessage(@RequestBody MessageDTO messageDTO) { | ||
public Mono<ResponseEntity<Object>> sendMessage(@RequestBody @Valid MessageDTO messageDTO) { | ||
log.debug("sending messageDTO: {}", messageDTO); | ||
Integer key = new SecureRandom().nextInt(Integer.MAX_VALUE); | ||
return messageRepository | ||
.save(messageDTO) | ||
.doOnSuccess(it -> { | ||
this.sender | ||
.send(Flux.just( | ||
SenderRecord.create(new ProducerRecord<>(AppConstants.HELLO_TOPIC, key, it), key))) | ||
.doOnError(e -> log.error("Send failed", e)) | ||
.subscribe(r -> { | ||
RecordMetadata metadata = r.recordMetadata(); | ||
log.debug( | ||
"Message {} sent successfully, topic-partition={}-{} offset={} timestamp={}", | ||
r.correlationMetadata(), | ||
metadata.topic(), | ||
metadata.partition(), | ||
metadata.offset(), | ||
LocalDateTime.now()); | ||
}); | ||
}) | ||
.map(it -> ResponseEntity.created(URI.create("/messages/" + it.id())) | ||
.build()); | ||
return messageService.sendMessage(messageDTO); | ||
} | ||
} |
7 changes: 0 additions & 7 deletions
7
...t-kafka-reactor-producer/src/main/java/com/example/boot/kafka/reactor/dto/MessageDTO.java
This file was deleted.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
...afka-reactor-producer/src/main/java/com/example/boot/kafka/reactor/entity/MessageDTO.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,8 @@ | ||
package com.example.boot.kafka.reactor.entity; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import java.time.LocalDateTime; | ||
import org.springframework.data.relational.core.mapping.Table; | ||
|
||
@Table(name = "messages") | ||
public record MessageDTO(Long id, @NotBlank(message = "Text Value Cant be Blank") String text, LocalDateTime sentAt) {} |
2 changes: 1 addition & 1 deletion
2
...r-producer/src/main/java/com/example/boot/kafka/reactor/repository/MessageRepository.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,6 +1,6 @@ | ||
package com.example.boot.kafka.reactor.repository; | ||
|
||
import com.example.boot.kafka.reactor.dto.MessageDTO; | ||
import com.example.boot.kafka.reactor.entity.MessageDTO; | ||
import org.springframework.data.r2dbc.repository.R2dbcRepository; | ||
|
||
public interface MessageRepository extends R2dbcRepository<MessageDTO, Long> {} |
52 changes: 52 additions & 0 deletions
52
...reactor-producer/src/main/java/com/example/boot/kafka/reactor/service/MessageService.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,52 @@ | ||
package com.example.boot.kafka.reactor.service; | ||
|
||
import com.example.boot.kafka.reactor.entity.MessageDTO; | ||
import com.example.boot.kafka.reactor.repository.MessageRepository; | ||
import com.example.boot.kafka.reactor.util.AppConstants; | ||
import java.net.URI; | ||
import java.security.SecureRandom; | ||
import java.time.LocalDateTime; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.kafka.clients.producer.ProducerRecord; | ||
import org.apache.kafka.clients.producer.RecordMetadata; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
import reactor.kafka.sender.KafkaSender; | ||
import reactor.kafka.sender.SenderRecord; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class MessageService { | ||
|
||
private final KafkaSender<Integer, MessageDTO> sender; | ||
private final MessageRepository messageRepository; | ||
|
||
public Mono<ResponseEntity<Object>> sendMessage(MessageDTO messageDTO) { | ||
Integer key = new SecureRandom().nextInt(Integer.MAX_VALUE); | ||
return saveMessage(messageDTO) | ||
.doOnSuccess(it -> this.sender | ||
.send(Flux.just( | ||
SenderRecord.create(new ProducerRecord<>(AppConstants.HELLO_TOPIC, key, it), key))) | ||
.doOnError(e -> log.error("Send failed", e)) | ||
.subscribe(r -> { | ||
RecordMetadata metadata = r.recordMetadata(); | ||
log.debug( | ||
"Message {} sent successfully, topic-partition={}-{} offset={} timestamp={}", | ||
r.correlationMetadata(), | ||
metadata.topic(), | ||
metadata.partition(), | ||
metadata.offset(), | ||
LocalDateTime.now()); | ||
})) | ||
.map(it -> ResponseEntity.created(URI.create("/messages/" + it.id())) | ||
.build()); | ||
} | ||
|
||
private Mono<MessageDTO> saveMessage(MessageDTO messageDTO) { | ||
return messageRepository.save(messageDTO); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
kafka-reactor/boot-kafka-reactor-producer/src/main/resources/application-local.properties
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
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