Example code for the book Microservice patterns
Clone or download
Latest commit f8ad5af Oct 6, 2018
Permalink
Failed to load latest commit information.
.circleci Upgrade Docker Compose Oct 6, 2018
buildSrc Simplified environment variables, Upgraded Gradle Oct 6, 2018
common-swagger Work in progress - upgrade to Spring Boot 2 Mar 4, 2018
deployment/kubernetes Upgraded Docker base image, misc other changes Jul 10, 2018
dynamodblocal-init misc changes Aug 20, 2018
dynamodblocal Reduced heap sizes Dec 11, 2017
ftgo-accounting-service-api Initial version of code Oct 23, 2017
ftgo-accounting-service-contracts Cleaned up building of contracts Mar 26, 2018
ftgo-accounting-service Upgraded Docker base image, misc other changes Jul 10, 2018
ftgo-api-gateway-graphql Support for Java 9+ and Misc cleanups Sep 20, 2018
ftgo-api-gateway Upgraded some dependencies Jul 15, 2018
ftgo-common-jpa Added examples tests for Chapter 9 - testing Feb 2, 2018
ftgo-common Simplified environment variables, Upgraded Gradle Oct 6, 2018
ftgo-consumer-service-api Added examples tests for Chapter 9 - testing Feb 2, 2018
ftgo-consumer-service-contracts Cleaned up building of contracts Mar 26, 2018
ftgo-consumer-service misc changes Aug 20, 2018
ftgo-end-to-end-tests Added gRPC example Jul 14, 2018
ftgo-kitchen-service-api Renamed RestaurantOrder -> Ticket, RestaurantOrderService -> KitchenS… Jun 9, 2018
ftgo-kitchen-service-contracts Renamed RestaurantOrder -> Ticket, RestaurantOrderService -> KitchenS… Jun 9, 2018
ftgo-kitchen-service Simplified environment variables, Upgraded Gradle Oct 6, 2018
ftgo-order-history-service Simplified environment variables, Upgraded Gradle Oct 6, 2018
ftgo-order-service-api Added restaurantName to OrderCreatedEvent and changed to Order Histor… Jun 10, 2018
ftgo-order-service-contracts Added restaurantName to OrderCreatedEvent and changed to Order Histor… Jun 10, 2018
ftgo-order-service Simplified environment variables, Upgraded Gradle Oct 6, 2018
ftgo-restaurant-service-api Merge branch 'master' into public-wip-chapter-10-kubernetes Feb 19, 2018
ftgo-restaurant-service-aws-lambda Simplified environment variables, Upgraded Gradle Oct 6, 2018
ftgo-restaurant-service Moved testing frameworks to their respective Eventuate projects Aug 23, 2018
ftgo-test-util Work in progress - upgrade to Spring Boot 2 Mar 4, 2018
gradle/wrapper Simplified environment variables, Upgraded Gradle Oct 6, 2018
mariadb Added polling support for MariaDB Aug 26, 2018
mysql Upgraded dependency version Sep 5, 2018
.gitignore Switching to the new cdc service. Fixing tests. Jul 31, 2018
LICENSE.md Initial version of code Oct 23, 2017
README.adoc Simplified environment variables, Upgraded Gradle Oct 6, 2018
TODO.txt Renamed RestaurantOrder -> Ticket, RestaurantOrderService -> KitchenS… Jun 9, 2018
build-and-restart-service.sh Upgraded various Spring Cloud dependencies Jun 8, 2018
build-and-run.sh Upgraded various Spring Cloud dependencies Jun 8, 2018
build-and-test-all-mariadb.sh Added polling support for MariaDB Aug 26, 2018
build-and-test-all.sh Support for Java 9+ and Misc cleanups Sep 20, 2018
build-contracts.sh Simplified build-contracts Jun 11, 2018
build.gradle Support for Java 9+ and Misc cleanups Sep 20, 2018
docker-compose-mariadb.yml Upgraded libraries, Switched to binlog CDC for MariaDB Oct 3, 2018
docker-compose.yml Simplified environment variables, Upgraded Gradle Oct 6, 2018
gradle.properties Upgraded libraries, Switched to binlog CDC for MariaDB Oct 3, 2018
gradlew Initial version of code Oct 23, 2017
gradlew.bat Initial version of code Oct 23, 2017
initialize-dynamodb.sh Implemented GraphQL-based API gateway Jul 13, 2018
mysql-cli.sh misc changes Aug 20, 2018
open-swagger-uis.sh Initial version of code Oct 23, 2017
publish-docker-images.sh Renamed RestaurantOrder -> Ticket, RestaurantOrderService -> KitchenS… Jun 9, 2018
run-end-to-end-tests.sh Switching to the new cdc service. Fixing tests. Jul 31, 2018
run-graphql-api-gateway-tests.sh Fixed table names, run GraphQL unit tests Jul 14, 2018
set-env.sh Simplified environment variables, Upgraded Gradle Oct 6, 2018
setenv-circle-ci.sh Upgrade Docker Compose Oct 6, 2018
settings.gradle Moved testing frameworks to their respective Eventuate projects Aug 23, 2018
show-swagger-ui-urls.sh Upgraded various Spring Cloud dependencies Jun 8, 2018
start-infrastructure-services.sh Moved testing frameworks to their respective Eventuate projects Aug 23, 2018
start-services.sh Enhanced start-services.sh Oct 24, 2017
truncate-table.sh Initial version of code Oct 23, 2017
wait-for-mysql.sh Added start-services.sh Oct 24, 2017
wait-for-services.sh Renamed docker containers to make them more readable Nov 7, 2017

README.adoc

FTGO example application

This is the example code for my book Microservice patterns.

Microservices Patterns Cover

Please note

  • The code is still work in progress

  • It primarily illustrates the technical aspects of the microservice architecture and so the business logic is minimal

  • The documentation is sparse/non-existent and you will need to look in the book

  • The application consists of many services and so requires a lot of memory. It runs well, for example, on a 16GB Macbook pro.

  • The application’s services and the infrastructure services, such as MySQL and Apache Kafka, are deployed using Docker containers using either Docker Compose or Kubernetes.

Got questions

Please post a message to the book’s discussion forum or create a github issue. I’ll do my best to help you.

Application architecture

Not surprisingly, this application has a microservice architecture. There are the following services:

Service design

Key points:

  • A service consists of a single Gradle module. For example, ftgo-order-service implements the Order Service

  • A service is a Spring Boot application

  • A service has a Swagger UI http://…​/swagger-ui.html. See open-swagger-uis.sh

  • A service typically consists of the following packages:

    • domain - domain logic including aggregates

    • messaging - messaging adapters

    • web - Spring MVC controllers (HTTP adapters)

    • main - the main application

  • The services use the following other frameworks

Chapter by chapter

This section maps the chapters to the code.

Chapter 3 Inter-process communication in a microservice architecture

  • The services have a REST API

  • The services also communicate using the Apache Kafka message broker via the Eventuate Tram framework

Chapter 4 Managing transactions with sagas

The ftgo-order-service uses sagas to maintain data consistency:

The services that participate in these sagas define the following command handlers:

Chapter 5 Designing business logic in a microservice architecture

All the services' business logic is implemented using Domain-Driven design aggregates.

Chapter 6 Developing business logic with event sourcing

Chapter 7 Implementing queries in a microservice architecture

Chapter 8 External API patterns

Building and running the application

Pre-requisites

  • Java 8+

  • Docker and Docker Compose

  • Internet access so that Gradle and Docker can download dependencies and container images

Building

Temporary: Build the Spring Cloud Contracts using this command:

./build-contracts.sh

Build the services using this command:

./gradlew assemble

Setting environment variables

Quick way

A quick way to set the environment variables is to run the script ./set-env.sh

Long way

To run the application you must set the DOCKER_HOST_IP environment variable to the IP address of where the Docker containers are running:

  • Docker toolbox/Virtual machine - IP address of the virtual machine

  • Docker for Windows/Mac/Linux - IP address of your laptop/desktop

The value of DOCKER_HOST_IP must be meaningful to both Java services/tests running on your desktop/laptop and to Docker containers. Please do NOT set it to the unresolvable hostname of your machine, localhost or 127.0.0.1 since the Docker containers will probably not work correctly.

Verifying that DOCKER_HOST_IP is set correctly

You can verify that DOCKER_HOST_IP is set correctly by running this command:

docker run -p 8889:8888 -e DOCKER_DIAGNOSTICS_PORT=8889 -e DOCKER_HOST_IP \
     --rm eventuateio/eventuateio-docker-networking-diagnostics:0.2.0.RELEASE

Setting the environment variable in your IDE

If you want to run Java services/tests within your IDE on your desktop/laptop AND the Docker containers are not accessible via localhost THEN you will need to set DOCKER_HOST_IP within your IDE. How to do this depends on your operating system and IDE. For example, I find it convenient to launch my IDE from the command line and after setting this environment variable.

Running the application

Run the application using this command:

docker-compose up -d

This can take a while.

Using the application

Use the services Swagger UIs to invoke the services.

You can also access the application via the API Gateway at http://${DOCKER_HOST_IP?}:8087. However, currently it doesn’t have a Swagger UI so you will have to use curl, for example.

Stopping the application

Stop the application using this command:

docker-compose down -v

Deploying the application on Kubernetes

You can find Kubernetes YAML files in the following directories: deployment/kubernetes and */src/deployment/kubernetes. There are also some helpful shell scripts.

Deploying services

You can run this command

./deployment/kubernetes/scripts/kubernetes-deploy-all.sh

Undeploying the services

You can run the script to undeploy the services:

./deployment/kubernetes/scripts/kubernetes-delete-all.sh

If you want to delete the persistent volumes for Apache Kafka, Zookeeper and MySQL please run the command:

./deployment/kubernetes/scripts/kubernetes-delete-volumes.sh