WIP
authorKai Moritz <kai@juplo.de>
Fri, 11 Jun 2021 15:22:32 +0000 (17:22 +0200)
committerKai Moritz <kai@juplo.de>
Fri, 11 Jun 2021 15:22:32 +0000 (17:22 +0200)
docker-compose.yml
transfer/pom.xml
transfer/src/main/java/de/juplo/kafka/payment/transfer/Transfer.java [deleted file]
transfer/src/main/java/de/juplo/kafka/payment/transfer/TransferService.java [deleted file]
transfer/src/main/java/de/juplo/kafka/payment/transfer/TransferServiceApplication.java
transfer/src/main/java/de/juplo/kafka/payment/transfer/TransferServiceProperties.java

index dde06c8..6ac7dd9 100644 (file)
@@ -27,8 +27,3 @@ services:
     depends_on:
       - zookeeper
       - kafka
-
-networks:
-  default:
-    external:
-      name: juplo
index daea31b..f471b10 100644 (file)
@@ -1,74 +1,74 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
-       <modelVersion>4.0.0</modelVersion>
-       <parent>
-               <groupId>org.springframework.boot</groupId>
-               <artifactId>spring-boot-starter-parent</artifactId>
-               <version>2.5.1</version>
-               <relativePath/> <!-- lookup parent from repository -->
-       </parent>
-       <groupId>de.juplo.kafka.payment</groupId>
-       <artifactId>transfer</artifactId>
-       <version>1.0-SNAPSHOT</version>
-       <name>Transfer Service</name>
-       <description>An MVP for the Transfer Service</description>
-       <properties>
-               <java.version>11</java.version>
-               <confluent.version>6.2.0</confluent.version>
-               <kafka.version>2.8.0</kafka.version>
-       </properties>
-       <dependencies>
-               <dependency>
-                       <groupId>org.springframework.boot</groupId>
-                       <artifactId>spring-boot-starter-actuator</artifactId>
-               </dependency>
-               <dependency>
-                       <groupId>org.springframework.boot</groupId>
-                       <artifactId>spring-boot-starter-web</artifactId>
-               </dependency>
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <parent>
+    <groupId>org.springframework.boot</groupId>
+    <artifactId>spring-boot-starter-parent</artifactId>
+    <version>2.5.1</version>
+    <relativePath/> <!-- lookup parent from repository -->
+  </parent>
+  <groupId>de.juplo.kafka.payment</groupId>
+  <artifactId>transfer</artifactId>
+  <version>1.0-SNAPSHOT</version>
+  <name>Transfer Service</name>
+  <description>An MVP for the Transfer Service</description>
+  <properties>
+    <java.version>11</java.version>
+    <confluent.version>6.2.0</confluent.version>
+    <kafka.version>2.8.0</kafka.version>
+  </properties>
+  <dependencies>
+    <dependency>
+      <groupId>org.springframework.boot</groupId>
+      <artifactId>spring-boot-starter-actuator</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.springframework.boot</groupId>
+      <artifactId>spring-boot-starter-web</artifactId>
+    </dependency>
 
-               <dependency>
-                       <groupId>org.apache.kafka</groupId>
-                       <artifactId>kafka-clients</artifactId>
-               </dependency>
-               <dependency>
-                       <groupId>org.springframework.boot</groupId>
-                       <artifactId>spring-boot-devtools</artifactId>
-                       <scope>runtime</scope>
-                       <optional>true</optional>
-               </dependency>
-               <dependency>
-                       <groupId>jakarta.validation</groupId>
-                       <artifactId>jakarta.validation-api</artifactId>
-               </dependency>
-               <dependency>
-                       <groupId>org.projectlombok</groupId>
-                       <artifactId>lombok</artifactId>
-                       <optional>true</optional>
-               </dependency>
-               <dependency>
-                       <groupId>org.springframework.boot</groupId>
-                       <artifactId>spring-boot-starter-test</artifactId>
-                       <scope>test</scope>
-               </dependency>
-       </dependencies>
+    <dependency>
+      <groupId>org.apache.kafka</groupId>
+      <artifactId>kafka-clients</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.springframework.boot</groupId>
+      <artifactId>spring-boot-devtools</artifactId>
+      <scope>runtime</scope>
+      <optional>true</optional>
+    </dependency>
+    <dependency>
+      <groupId>jakarta.validation</groupId>
+      <artifactId>jakarta.validation-api</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.projectlombok</groupId>
+      <artifactId>lombok</artifactId>
+      <optional>true</optional>
+    </dependency>
+    <dependency>
+      <groupId>org.springframework.boot</groupId>
+      <artifactId>spring-boot-starter-test</artifactId>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
 
-       <build>
-               <plugins>
-                       <plugin>
-                               <groupId>org.springframework.boot</groupId>
-                               <artifactId>spring-boot-maven-plugin</artifactId>
-                               <configuration>
-                                       <excludes>
-                                               <exclude>
-                                                       <groupId>org.projectlombok</groupId>
-                                                       <artifactId>lombok</artifactId>
-                                               </exclude>
-                                       </excludes>
-                               </configuration>
-                       </plugin>
-               </plugins>
-       </build>
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.springframework.boot</groupId>
+        <artifactId>spring-boot-maven-plugin</artifactId>
+        <configuration>
+          <excludes>
+            <exclude>
+              <groupId>org.projectlombok</groupId>
+              <artifactId>lombok</artifactId>
+            </exclude>
+          </excludes>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
 
 </project>
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 (file)
index 38673d1..0000000
+++ /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 (file)
index 9e72af1..0000000
+++ /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<UUID, String> producer;
-  private final ObjectMapper mapper;
-  private final String topic;
-  private final String path;
-
-
-  TransferService(
-      final KafkaProducer<UUID, String> 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<ResponseEntity<?>> transfer(@Valid @RequestBody Transfer transfer)
-  {
-    DeferredResult<ResponseEntity<?>> result = new DeferredResult<>();
-
-    try
-    {
-      ProducerRecord<UUID, String> 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;
-  }
-}
index 484d9c2..49e57a3 100644 (file)
@@ -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<UUID, String> producer,
+      ObjectMapper mapper,
+      TransferServiceProperties properties)
+  {
+    return new TransferServiceImpl(producer, mapper, properties.topic);
+  }
+
+
   public static void main(String[] args)
   {
     SpringApplication.run(TransferServiceApplication.class, args);
index bea701f..ccd22a3 100644 (file)
@@ -13,5 +13,4 @@ public class TransferServiceProperties
 {
   String bootstrapServers = "localhost:9092";
   String topic = "transfers";
-  String path = "http://details:8092/transfers/";
 }