Example code for the book Microservice patterns
= FTGO example application
This is the example code for my book https://microservices.io/book[Microservice patterns].
image::http://microservices.io/i/Microservices-Patterns-Cover.png[width=50%]
== Please note
== Got questions?
Please create a https://github.com/microservices-patterns/ftgo-application/issues[github issue] and I’ll do my best to help you.
== Application architecture
Not surprisingly, this application has a microservice architecture.
There are the following services:
Consumer Service
Restaurant Service
Order Service
Kitchen Service
Accounting Service
Order History Service
, which is a CQRS view== Service design
Key points:
ftgo-order-service
implements the Order Service
http://.../swagger-ui.html
. See open-swagger-uis.sh
Eventuate Tram framework
] - implements transactional messagingEventuate Tram Saga framework
] - implements sagasEventuate Client framework
] - implements event sourcing== Chapter by chapter
This section maps the chapters to the code.
=== Chapter 3 Inter-process communication in a microservice architecture
Eventuate Tram
framework=== Chapter 4 Managing transactions with sagas
The link:./ftgo-order-service[ftgo-order-service] uses sagas to maintain data consistency:
The services that participate in these sagas define the following command handlers:
Accounting Service
link:./ftgo-accounting-service/src/main/java/net/chrisrichardson/ftgo/accountingservice/messaging/AccountingServiceCommandHandler.java[AccountingServiceCommandHandler]Consumer Service
link:./ftgo-consumer-service/src/main/java/net/chrisrichardson/ftgo/consumerservice/domain/ConsumerServiceCommandHandlers.java[ConsumerServiceCommandHandlers]Kitchen Service
link:./ftgo-kitchen-service/src/main/java/net/chrisrichardson/ftgo/kitchenservice/messagehandlers/KitchenServiceCommandHandler.java[KitchenServiceCommandHandler]Order Service
link:./ftgo-order-service/src/main/java/net/chrisrichardson/ftgo/orderservice/service/OrderCommandHandlers.java[OrderCommandHandlers]=== Chapter 5 Designing business logic in a microservice architecture
All the services’ business logic is implemented using Domain-Driven design aggregates.
Accounting Service
Account
] aggregate in the link:./ftgo-accounting-service[ftgo-accounting-service]Consumer Service
Order Service
Kitchen Service
Restaurant Service
=== Chapter 6 Developing business logic with event sourcing
Account
] aggregate in the link:./ftgo-accounting-service[ftgo-accounting-service] is implemented using event sourcing=== Chapter 7 Implementing queries in a microservice architecture
=== Chapter 8 External API patterns
== Building and running the application
=== Pre-requisites
=== Building
Temporary: Build the Spring Cloud Contracts using this command:
./gradlew buildContracts
Build the services using this command:
./gradlew assemble
=== Running the application
Run the application using this command:
./gradlew :composeUp
Note: the ‘:’
This can take a while.
=== Using the application
Use the services Swagger UIs to invoke the services.
http://localhost:8081/swagger-ui/index.html
http://localhost:8084/swagger-ui/index.html
http://localhost:8082/swagger-ui/index.html
http://localhost:8082/swagger-ui/index.html
http://localhost:8086/swagger-ui/index.html
You can also access the application via the API Gateway
at http://localhost:8087
.
However, currently it doesn’t have a Swagger UI so you will have to use curl
, for example.
Note: if the containers aren’t accessible via localhost
- e.g. you are using Docker Toolbox, you will have to use ${DOCKER_HOST_IP}
as described below.
=== Stopping the application
Stop the application using this command:
./gradlew :composeDown
== 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
== Setting environment variables to do development
You should not need to set any environment variables.
To run the application, you certainly do not.
Similarly, to do development (e.g. run tests), you typically do not need to set any environment variables.
That’s because Docker containers are generally accessible (e.g. Docker for Windows/Mac) on the host via localhost
.
However, if Docker is running elsewhere (e.g. you are using Docker Toolbox) you will need to set DOCKER_HOST_IP
.
=== Quick way
A quick way to set the environment variables is to run the script ./set-env.sh
.
=== Long way
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:
=== 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.