Adding 100-2 App, Functional Programming Intro for SCS

This commit is contained in:
Jay Ehsaniara
2021-06-28 02:37:11 -07:00
parent bea8f7c211
commit fab59e5298
8 changed files with 187 additions and 30 deletions

58
scs-100-2/README.md Normal file
View File

@@ -0,0 +1,58 @@
# SCS-100-2
## Functional Programming (For Alternative of SCS-100 Project)
A simple Example of an Event Driven Flow by the help of **SPRING CLOUD STREAM KAFKA**
##### properties
* java.version: `11`
* spring-cloud.version: `2020.0.3`
* spring-boot.version: `2.5.2`
![General Flow Diagram](material/kafka-events-intro-100.svg)
The Docker-compose file contains: single kafka and zookeeper. just simply run the following command
```shell
docker-compose up -d
```
_Note: I assume you already have docker setup in your machine._
### Make the project
run the following command line to create you jar file in `target` directory
```shell
mvn clean package
```
Then run the generated jar file in `target` folder, (so make sure you are in the same directory when you run the jar file
or give the full path)
```shell
java -jar scs-100-0.0.1-SNAPSHOT.jar
```
the application starts to listen on port 8080. make sure that port not being occupied by any other app already, if is try
to pass the following parameter before `-jar` by adding `-Dserver.port=8081` as:
```shell
java -Dserver.port=8081 -jar scs-100-0.0.1-SNAPSHOT.jar
```
_Note: you can also modify the application.yml and set the same properties based on your app profile_
At this point you should have already seen the information about your topics.
### Check Application
#### Create Order or Place your Order
you should now be able to place your order by calling the following `curl` command
```shell
# assuming your app is listening on 8080
ORDER_UUID=$(curl --silent -H 'Content-Type: application/json' -d "{\"itemName\":\"book\"}" http://localhost:8080/order | jq -r '.orderUuid') && for i in `seq 1 15`; do echo $(curl --silent "http://localhost:8080/order/status/"$ORDER_UUID); sleep 1; done;
```
Note: make sure you have already installed the `jq`

View File

@@ -0,0 +1,19 @@
version: '3'
services:
kafka:
image: wurstmeister/kafka
container_name: kafka-mc
ports:
- "9092:9092"
environment:
- KAFKA_ADVERTISED_HOST_NAME=127.0.0.1
- KAFKA_ADVERTISED_PORT=9092
- KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181
depends_on:
- zookeeper
zookeeper:
image: wurstmeister/zookeeper
ports:
- "2181:2181"
environment:
- KAFKA_ADVERTISED_HOST_NAME=zookeeper

View File

@@ -6,39 +6,37 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<version>2.5.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>scs-101</artifactId>
<groupId>com.ehsaniara.scs_kafka_intro</groupId>
<artifactId>scs-100-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>scs-101</name>
<packaging>pom</packaging>
<name>scs-100-2</name>
<description>Demo project for Spring Boot</description>
<modules>
<module>scs-101-commons</module>
<module>scs-101-producer</module>
<module>scs-101-consumer</module>
</modules>
<properties>
<java.version>11</java.version>
<spring-cloud.version>2020.0.2</spring-cloud.version>
<spring-cloud.version>2020.0.3</spring-cloud.version>
<spring-boot.version>${project.parent.version}</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream</artifactId>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-kafka-streams</artifactId>
</dependency>
<dependency>
@@ -47,19 +45,6 @@
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream</artifactId>
<scope>test</scope>
<classifier>test-binder</classifier>
<type>test-jar</type>
</dependency>
</dependencies>
<dependencyManagement>
@@ -74,4 +59,23 @@
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,24 @@
package com.ehsaniara.scs_kafka_intro.scs1002;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.kafka.common.serialization.Serde;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.kafka.support.serializer.JsonSerde;
@SpringBootApplication
public class Application {
final static String STATE_STORE_NAME = "scs-100-2-order-events";
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public Serde<Order> orderJsonSerde() {
return new JsonSerde<>(Order.class, new ObjectMapper());
}
}

View File

@@ -0,0 +1,23 @@
package com.ehsaniara.scs_kafka_intro.scs1002;
import lombok.*;
import javax.validation.constraints.NotBlank;
import java.io.Serializable;
import java.util.UUID;
@ToString
@Builder
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class Order implements Serializable {
private UUID orderUuid;
@NotBlank
private String itemName;
private OrderStatus orderStatus;
}

View File

@@ -0,0 +1,11 @@
package com.ehsaniara.scs_kafka_intro.scs1002;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class OrderNotFoundException extends RuntimeException {
public OrderNotFoundException(String msg) {
super(msg);
}
}

View File

@@ -0,0 +1,18 @@
package com.ehsaniara.scs_kafka_intro.scs1002;
import lombok.AllArgsConstructor;
@AllArgsConstructor
public enum OrderStatus {
PENDING("PENDING"),
INVENTORY_CHECKING("INVENTORY_CHECKING"),
OUT_OF_STOCK("OUT_OF_STOCK"),
SHIPPED("SHIPPED"),
CANCELED("CANCELED");
private final String name;
public String toString() {
return this.name;
}
}