This is the example code for my book Microservice patterns.
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.
Please create a github issue and I’ll do my best to help you.
Not surprisingly, this application has a microservice architecture. There are the following services:
ftgo-consumer-service - the Consumer Service
ftgo-restaurant-service - the Restaurant Service
ftgo-order-service - the Order Service
ftgo-kitchen-service - the Kitchen Service
ftgo-accounting-service - the Accounting Service
ftgo-order-history-service - a Order History Service
, which is a CQRS view
ftgo-api-gateway - the API gateway
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
Eventuate Tram framework
- implements transactional messaging
Eventuate Tram Saga framework
- implements sagas
Eventuate Client framework
- implements event sourcing
This section maps the chapters to the code.
The services have a REST API
The services also communicate using the Apache Kafka message broker via the Eventuate Tram
framework
The ftgo-order-service uses sagas to maintain data consistency:
The services that participate in these sagas define the following command handlers:
Accounting Service
AccountingServiceCommandHandler
Consumer Service
ConsumerServiceCommandHandlers
Kitchen Service
KitchenServiceCommandHandler
Order Service
OrderCommandHandlers
All the services' business logic is implemented using Domain-Driven design aggregates.
Accounting Service
Account
aggregate in the ftgo-accounting-service
Consumer Service
Order Service
Kitchen Service
Restaurant Service
The Account
aggregate in the ftgo-accounting-service is implemented using event sourcing
ftgo-order-history-service is an example of a CQRS view
ftgo-api-gateway uses API composition to implement the REST endpoint for retrieving the order history
ftgo-api-gateway is the API gateway
Java 8+
Docker and Docker Compose
Internet access so that Gradle and Docker can download dependencies and container images
Temporary: Build the Spring Cloud Contracts using this command:
./gradlew buildContracts
Build the services using this command:
./gradlew assemble
Run the application using this command:
./gradlew :composeUp
Note: the ':'
This can take a while.
Use the services Swagger UIs to invoke the services.
Create consumer - http://localhost:8081/swagger-ui.html
Create a restaurant - http://localhost:8084/swagger-ui.html
Create an order - http://localhost:8082/swagger-ui.html
View the order - http://localhost:8082/swagger-ui.html
View the order history - http://localhost:8086/swagger-ui.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.
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
.
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.
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
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.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。