This repository contains the source code of the practical use case described in the book Learn Microservices with Spring Boot (2nd Edition).
If you want to know more details about the book and its extra chapters, make sure to visit this page.
The book follows a pragmatic approach to building a Microservice Architecture, using a reference implementation that evolves from a small monolith to a full microservice system.
This extra chapter brings the project dependencies to the Spring Boot 2.7.1 version.
All these changes are described in detail in a blog post at The Practical Developer's website. Visit https://thepracticaldeveloper.com/book-update-2.7.1/
The Spring Boot 2.7 release has upgraded H2 to a new major version, 2.x. Many changes are backwards incompatible and two of them break our code.
These two features can't be used together anymore. See the blog post for more details.
Solved by removing the DB_CLOSE_ON_EXIT
parameter from the connection URL.
More detailed error:
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "create table [*]user (id bigint not null, alias varchar(255), primary key (id))"; expected "identifier"; SQL statement: [...]
Reason: user
is now a keyword, so it conflicts with the table name.
Fix (one of the possibilities): Add NON_KEYWORDS=USER;
to the connection URL for the multiplication microservice's database.
See the blog post for a complete explanation.
In addition to the mandatory upgrades to make the code work with 2.7, I also switched to the new configuration approach introduced by Spring Boot 2.4.x.
Required changes to move to the new configuration approach in 2.4:
- Remove the legacy bootstrap starter (if previously added, in my case it was)
- Remove the
bootstrap.properties
orbootstrap.yml
and move all those properties to theapplication.properties
orapplication.yml
. - For Spring Cloud Consul to work fine, add a
spring.config.import=consul:
property (or the YAML equivalent) to the projects using Consul. - We can remove all the
test.properties
files and the@TestPropertySource
annotation pointing to it that we needed to add for backwards compatibility with the bootstrap legacy mode.
All the details here
This version also includes an H2 server Docker image so the microservices can use H2 as an isolated database server instead of using the shared database files offered by the AUTO_SERVER
mode.
Check the blog post for the instructions.
To remove this error for good, I added the native DNS dependency for my local setup. This applies to the gateway
service's pom.xml
:
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-resolver-dns-native-macos</artifactId>
<!-- Change this if your Mac is not M1, see e.g. https://stackoverflow.com/questions/65954571/spring-boot-2-4-2-dns-resolution-problem-at-start-on-apple-m1 -->
<classifier>osx-aarch_64</classifier>
<version>4.1.78.Final</version>
</dependency>
Some additional references from the official documentation:
You can use Docker to start the complete system, and you can also run the services one by one on your computer. See the book's last chapter repository for the detailed instructions.
- Do you have questions about how to make this application work?
- Did you get the book and have questions about any concept explained within this chapter?
- Have you found issues using updated dependencies?
Don't hesitate to create an issue in this repository and post your question/problem there.
Are you interested in building a microservice architecture from scratch? You'll face all the challenges of designing and implementing a distributed system one by one, and will be able to evaluate if it's the best choice for your project.
Buy the book at Amazon, or visit https://tpd.io/book-extra for all the details.
You can buy the book online from these stores:
- Chapter 3. A professional 3-tier 3-layer Spring Boot app
- Chapter 4. Building a basic frontend in React (backender-friendly)
- Chapter 5. The Data Layer Concepts and Spring Data JPA
- Chapter 6. Starting with Microservices - Synchronous
- Chapter 7. Event-Driven Architectures - Making our system asynchronous
- Chapter 8 (I). The Gateway Pattern in Microservice Architectures (Spring Cloud Gateway)
- Chapter 8 (II). Service Discovery and Load Balancing for Spring Boot Microservices (Consul / Spring Cloud Load Balancer)
- Chapter 8 (III). Centralized Configuration with Consul KV
- Chapter 8 (IV). Centralized Logs, Distributed Tracing, and Containerization with Docker (Buildpacks) and Docker Compose
Extra chapters: