Skip to content

Commit

Permalink
order sada demo
Browse files Browse the repository at this point in the history
  • Loading branch information
aludwiko committed May 17, 2023
1 parent c6932bd commit 9f18787
Show file tree
Hide file tree
Showing 35 changed files with 1,349 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target
.idea
86 changes: 85 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,85 @@
# kalix-order-saga
# Kalix Order Saga

## Designing

To understand the Kalix concepts that are the basis for this example, see [Designing services](https://docs.kalix.io/java/development-process.html) in the documentation.

## Developing

This project demonstrates the use of Event Sourced Entities, Subscriptions and Actions components.
To understand more about these components, see [Developing services](https://docs.kalix.io/services/)
and in particular the [Java section](https://docs.kalix.io/java/)

## Building

Use Maven to build your project:

```shell
mvn compile
```

## Running Locally

When running a Kalix application locally, at least two applications are required. The current Kalix application and its companion Kalix Proxy.

To start the applications locally, call the following command:

```shell
mvn kalix:runAll
```

This command will start your Kalix application and a Kalix Proxy using the included [docker-compose.yml](.
/docker-compose.yml) file.

By default, an `orchestration` Spring profile is enabled, to demonstrate
orchestration-based Saga with the [Workflow Entity](https://docs.kalix.io/java/workflow-entities.html). Switching to
choreography based Saga is just a matter of changing the profile configuration `-Dspring.profiles.active` to
`choreography` in `pom.xml` file.

## Exercising the service

- Add product to the inventory

```shell
curl -XPATCH -H "Content-Type: application/json" \
--data '{"productId": "p1", "quantity": 50}' \
localhost:9000/inventory/global-inventory/add
```

- Place an order

```shell
curl -XPOST -H "Content-Type: application/json" \
--data '{"userId": "u1", "productId": "p1", "quantity": 10, "price": 10}' \
localhost:9000/order/123
```

- Check order status

```shell
curl localhost:9000/order/123
```

- Check remaining stocks

```shell
curl localhost:9000/inventory/global-inventory/product/p1
```

- Place an order with payments failure. Payment will fail for an order with an `id` set to `42`.

```shell
curl -XPOST -H "Content-Type: application/json" \
--data '{"userId": "u1", "productId": "p1", "quantity": 10, "price": 10}' \
localhost:9000/order/42
```

- Check order status

```shell
curl localhost:9000/order/42
```

## Testing

There are two integration tests: `OrderWorkflowIntegrationTest` and `OrderEntityIntegrationTest` to validate orchestration and choreography-based Sagas.
25 changes: 25 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# If you're looking to use eventing with Google PubSub, to get an emulator running:
# - add property "-Dkalix.proxy.eventing.support=google-pubsub-emulator" to the JAVA_TOOL_OPTIONS environment map under the kalix-proxy service
# - uncomment the env var PUBSUB_EMULATOR_HOST and the section below for gcloud-pubsub-emulator service
version: "3"
services:
kalix-proxy:
image: gcr.io/kalix-public/kalix-proxy:1.1.8
ports:
- "9000:9000"
extra_hosts:
- "host.docker.internal:host-gateway"
environment:
JAVA_TOOL_OPTIONS: >
-Dconfig.resource=dev-mode.conf
-Dlogback.configurationFile=logback-dev-mode.xml
USER_FUNCTION_HOST: ${USER_FUNCTION_HOST:-host.docker.internal}
USER_FUNCTION_PORT: ${USER_FUNCTION_PORT:-8080}
# Comment to enable ACL check in dev-mode (see https://docs.kalix.io/services/using-acls.html#_local_development_with_acls)
ACL_ENABLED: "false"
#PUBSUB_EMULATOR_HOST: gcloud-pubsub-emulator
#gcloud-pubsub-emulator:
# image: gcr.io/google.com/cloudsdktool/cloud-sdk:341.0.0
# command: gcloud beta emulators pubsub start --project=test --host-port=0.0.0.0:8085
# ports:
# - 8085:8085
280 changes: 280 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,280 @@
<?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 http://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>3.0.4</version>
<relativePath/>
</parent>

<groupId>com.example</groupId>
<artifactId>java-spring-valueentity-counter</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>java-spring-valueentity-counter</name>
<properties>

<!-- TODO Update to your own Docker repository or Docker Hub scope -->
<dockerImage>my-docker-repo/${project.artifactId}</dockerImage>
<dockerTag>${project.version}-${build.timestamp}</dockerTag>
<maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format>
<mainClass>com.example.Main</mainClass>

<jdk.target>17</jdk.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<kalix-sdk.version>1.2.1-M4-26-f7d44aab-SNAPSHOT</kalix-sdk.version>
</properties>

<build>
<resources>
<!-- Add the generated protobuf descriptor to the classpath, so that source mapping works -->
<resource>
<directory>${project.build.directory}/generated-resources</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>

<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.7.0</version>
</extension>
</extensions>

<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers combine.children="append">
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>reference.conf</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${jdk.target}</source>
<target>${jdk.target}</target>
<compilerArgs>
<arg>-Xlint:deprecation</arg>
<arg>-parameters</arg>
<arg>--enable-preview</arg>
</compilerArgs>
</configuration>
</plugin>

<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.39.1</version>
<configuration>
<images>
<image>
<name>${dockerImage}:%l</name>
<build>
<!-- Base Docker image which contains jre-->
<from>docker.io/library/eclipse-temurin:${jdk.target}-alpine</from>
<createImageOptions>
<platform>linux/amd64</platform>
</createImageOptions>
<tags>
<!-- tag for generated image -->
<tag>${dockerTag}</tag>
</tags>
<ports>
<!-- expose port in Docker container -->
<port>8080</port>
</ports>
<assembly>
<!-- NOTE: (optional) switch to "artifact-with-dependencies" to show dependencies library-->
<descriptorRef>artifact</descriptorRef>
</assembly>
<entryPoint>
<arg>java</arg>
<arg>-jar</arg>
<arg>/maven/${project.build.finalName}.jar</arg>
</entryPoint>
</build>
</image>
</images>
</configuration>
<executions>
<execution>
<id>build-docker-image</id>
<phase>install</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
<execution>
<id>push-docker-image</id>
<phase>deploy</phase>
<goals>
<goal>push</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<!-- configure src/it/java and src/it/resources -->
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-integration-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/it/java</source>
</sources>
</configuration>
</execution>
<execution>
<id>add-integration-test-resource</id>
<phase>generate-test-resources</phase>
<goals>
<goal>add-test-resource</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/it/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>--enable-preview</argLine>
<excludes>
<!-- ignore integration test classes -->
<exclude>**/*IntegrationTest</exclude>
</excludes>
</configuration>
</plugin>

<plugin>
<groupId>io.kalix</groupId>
<artifactId>kalix-maven-plugin</artifactId>
<version>${kalix-sdk.version}</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
<goal>deploy</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmArgs>
<arg>--enable-preview</arg>
<arg>-Dspring.profiles.active=orchestration</arg>
</jvmArgs>
<dockerImage>${dockerImage}:${dockerTag}</dockerImage>
<mainClass>${mainClass}</mainClass>
<integrationTestSourceDirectory>src/it/java</integrationTestSourceDirectory>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.0.0-M1</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>

</plugins>
</build>

<profiles>
<profile>
<!-- run Integration Tests in src/it/java with `mvn verify -Pit`-->
<id>it</id>
<build>
<plugins>
<plugin>
<!-- run *IntegrationTest with failsafe -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.22.2</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<argLine>--enable-preview</argLine>
<includes>
<include>**/*IntegrationTest</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

<dependencies>
<dependency>
<groupId>io.kalix</groupId>
<artifactId>kalix-spring-boot-starter</artifactId>
<version>${kalix-sdk.version}</version>
</dependency>
<dependency>
<groupId>io.kalix</groupId>
<artifactId>kalix-spring-boot-starter-test</artifactId>
<version>${kalix-sdk.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>

</dependencies>
</project>
Loading

0 comments on commit 9f18787

Please sign in to comment.