From: Kai Moritz Date: Fri, 11 Jun 2021 15:22:32 +0000 (+0200) Subject: WIP X-Git-Tag: wip-initialer-commit~13 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=22df9d17a8b5568b7685b4df4d13a5986fb5af31;p=demos%2Fkafka%2Fdemos-kafka-payment-system-setup WIP --- diff --git a/docker-compose.yml b/docker-compose.yml index dde06c8..6ac7dd9 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -27,8 +27,3 @@ services: depends_on: - zookeeper - kafka - -networks: - default: - external: - name: juplo diff --git a/transfer/pom.xml b/transfer/pom.xml index daea31b..f471b10 100644 --- a/transfer/pom.xml +++ b/transfer/pom.xml @@ -1,74 +1,74 @@ - 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 2.5.1 - - - de.juplo.kafka.payment - transfer - 1.0-SNAPSHOT - Transfer Service - An MVP for the Transfer Service - - 11 - 6.2.0 - 2.8.0 - - - - org.springframework.boot - spring-boot-starter-actuator - - - org.springframework.boot - spring-boot-starter-web - + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.5.1 + + + de.juplo.kafka.payment + transfer + 1.0-SNAPSHOT + Transfer Service + An MVP for the Transfer Service + + 11 + 6.2.0 + 2.8.0 + + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-starter-web + - - org.apache.kafka - kafka-clients - - - org.springframework.boot - spring-boot-devtools - runtime - true - - - jakarta.validation - jakarta.validation-api - - - org.projectlombok - lombok - true - - - org.springframework.boot - spring-boot-starter-test - test - - + + org.apache.kafka + kafka-clients + + + org.springframework.boot + spring-boot-devtools + runtime + true + + + jakarta.validation + jakarta.validation-api + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + - - - - org.springframework.boot - spring-boot-maven-plugin - - - - org.projectlombok - lombok - - - - - - + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + diff --git a/transfer/src/main/java/de/juplo/kafka/payment/transfer/Transfer.java b/transfer/src/main/java/de/juplo/kafka/payment/transfer/Transfer.java deleted file mode 100644 index 38673d1..0000000 --- a/transfer/src/main/java/de/juplo/kafka/payment/transfer/Transfer.java +++ /dev/null @@ -1,25 +0,0 @@ -package de.juplo.kafka.payment.transfer; - -import lombok.Builder; -import lombok.Data; - -import javax.validation.constraints.NotNull; -import java.util.UUID; - - -/** - * Simple DTO used by the REST interface - */ -@Data -@Builder -public class Transfer -{ - @NotNull(message = "Cannot be null") - private UUID id; - @NotNull(message = "Cannot be null") - private long payer; - @NotNull(message = "Cannot be null") - private long payee; - @NotNull(message = "Cannot be null") - private int amount; -} diff --git a/transfer/src/main/java/de/juplo/kafka/payment/transfer/TransferService.java b/transfer/src/main/java/de/juplo/kafka/payment/transfer/TransferService.java deleted file mode 100644 index 9e72af1..0000000 --- a/transfer/src/main/java/de/juplo/kafka/payment/transfer/TransferService.java +++ /dev/null @@ -1,80 +0,0 @@ -package de.juplo.kafka.payment.transfer; - - -import com.fasterxml.jackson.databind.ObjectMapper; -import org.apache.kafka.clients.producer.KafkaProducer; -import org.apache.kafka.clients.producer.ProducerRecord; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.http.MediaType; -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.RestController; -import org.springframework.web.context.request.async.DeferredResult; - -import javax.validation.Valid; -import java.net.URI; -import java.util.UUID; - - -@RestController -public class TransferService -{ - private final static Logger LOG = LoggerFactory.getLogger(TransferService.class); - - private final KafkaProducer producer; - private final ObjectMapper mapper; - private final String topic; - private final String path; - - - TransferService( - final KafkaProducer producer, - final ObjectMapper mapper, - final TransferServiceProperties properties) - { - this.producer = producer; - this.mapper = mapper; - this.topic = properties.getTopic(); - this.path = properties.getPath(); - } - - - @PostMapping( - path = "/transfer", - consumes = MediaType.APPLICATION_JSON_VALUE, - produces = MediaType.TEXT_PLAIN_VALUE) - public DeferredResult> transfer(@Valid @RequestBody Transfer transfer) - { - DeferredResult> result = new DeferredResult<>(); - - try - { - ProducerRecord record = - new ProducerRecord<>( - topic, - transfer.getId(), - mapper.writeValueAsString(transfer)); - - producer.send(record, (metadata, exception) -> - { - if (exception != null) - { - LOG.error("Could not place order {}: {}", transfer, exception.toString()); - result.setErrorResult(exception); - return; - } - - result.setResult(ResponseEntity.created(URI.create(path + transfer.getId())).build()); - }); - } - catch (Exception e) - { - LOG.error("Unexpected exception!", e); - result.setErrorResult(e); - } - - return result; - } -} diff --git a/transfer/src/main/java/de/juplo/kafka/payment/transfer/TransferServiceApplication.java b/transfer/src/main/java/de/juplo/kafka/payment/transfer/TransferServiceApplication.java index 484d9c2..49e57a3 100644 --- a/transfer/src/main/java/de/juplo/kafka/payment/transfer/TransferServiceApplication.java +++ b/transfer/src/main/java/de/juplo/kafka/payment/transfer/TransferServiceApplication.java @@ -1,6 +1,8 @@ package de.juplo.kafka.payment.transfer; +import com.fasterxml.jackson.databind.ObjectMapper; +import de.juplo.kafka.payment.transfer.impl.TransferServiceImpl; import lombok.extern.slf4j.Slf4j; import org.apache.kafka.clients.producer.KafkaProducer; import org.apache.kafka.clients.producer.ProducerConfig; @@ -31,6 +33,16 @@ public class TransferServiceApplication return new KafkaProducer<>(props); } + @Bean + TransferServiceImpl transferService( + KafkaProducer producer, + ObjectMapper mapper, + TransferServiceProperties properties) + { + return new TransferServiceImpl(producer, mapper, properties.topic); + } + + public static void main(String[] args) { SpringApplication.run(TransferServiceApplication.class, args); diff --git a/transfer/src/main/java/de/juplo/kafka/payment/transfer/TransferServiceProperties.java b/transfer/src/main/java/de/juplo/kafka/payment/transfer/TransferServiceProperties.java index bea701f..ccd22a3 100644 --- a/transfer/src/main/java/de/juplo/kafka/payment/transfer/TransferServiceProperties.java +++ b/transfer/src/main/java/de/juplo/kafka/payment/transfer/TransferServiceProperties.java @@ -13,5 +13,4 @@ public class TransferServiceProperties { String bootstrapServers = "localhost:9092"; String topic = "transfers"; - String path = "http://details:8092/transfers/"; }