Spring Cloud project shows how to create simple microservices with Spring Boot, Spring Cloud, Eureka (Discovery Server) and Zuul (API gateway, Proxy, Load balancer)
The project follows the microservices architectural style as in the next diagram
We have three independent microservices (User, Site, Organization), which register themself in service discovery (Eureka Server), and communicate with each other by declarative REST client (Open Feign). The whole system is hidden behind API gateway (Zuul).
This project is created using the following technologies:
-
Java 8
-
Spring Boot in cloud-native development:
- Spring Cloud Netflix Eureka – Registration and Discovery service for all microservices
- Spring Cloud Netflix Zuul – API Gateway service that provides dynamic routing, and load balancer
- Spring Cloud Netflix Feign – Declarative REST client
-
Spring Boot in microservices development:
- Spring Web
- Spring Data JPA
- Spring Devtools
- Spring Actuator
- H2 In-Memory database
-
Maven Dependency Management
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.
You need to install the following software:
- Java JDK 1.8+
- Maven 3.0+
The steps to be taken in order to create working microservices-based system using Spring Cloud:
Step 1. Creating service discovery with Spring Cloud Netflix Eureka
Step 2. Building the microservices using Spring Boot, Spring Cloud and communication between them with Spring Cloud Open Feign:
- User microservice
- Site microservice
- Organization microservice
Step 3. Creating API gateway using Spring Cloud Netflix Zuul
Step 4. Running and testing applications
To run this project, install it locally as follow:
-
Clone the application
git clone https://github.com/SayedBaladoh/Microservices-Sample-using-SpringBoot-SpringCloud-Eureka-and-Zuul.git
-
Change the servers' port for the applications as per you want
Each application will start at the server on the port as specified in the previous
architecture diagram
by default. If you want to change the defaultport
for the discovery service and the microservices- open
src/main/resources/application.properties
file - change
server.port
property
For the API Gateway service you can change the default
port
- open
src/main/resources/application.yml
file - change
server.port
property
- open
-
Run the service registry and discovery application
You can run the discovery service by typing the following command
cd discovery-service mvn spring-boot:run
The server will start on port
8761
by default, So once you have successfully started application you'll be able to visit the Eureka dashboard under addresshttps://localhost:8761
.If you changed the port in
src/main/resources/application.properties
file, use your custom porthttps://localhost:port
. -
Run the User microservice application
You can run the user microservice by typing the following command
cd user-service mvn spring-boot:run
The user microservice will start on port
8081
by default, So you'll be able to visit the user microservice under addresshttps://localhost:8081
.If you changed the port in
src/main/resources/application.properties
file, use your custom porthttps://localhost:port
.But we will use the address of Zuul API Gateway as a routing address for all microservice requests.
-
Run the Site microservice application
You can run the site microservice by typing the following command
cd site-service mvn spring-boot:run
The site microservice will start on port
8082
by default, So you'll be able to visit the site microservice under addresshttps://localhost:8082
.If you changed the port in
src/main/resources/application.properties
file, use your custom porthttps://localhost:port
.But we will use the address of Zuul API Gateway as a routing address for all microservice requests.
-
Run the Organization microservice application
You can run the organization microservice by typing the following command
cd organization-service mvn spring-boot:run
The organization microservice will start on port
8083
by default, So you'll be able to visit the organization microservice under addresshttps://localhost:8083
.If you changed the port in
src/main/resources/application.properties
file, use your custom porthttps://localhost:port
.But we will use the address of Zuul API Gateway as a routing address for all microservice requests.
-
Run the API gateway application
You can run the Zuul API gateway service by typing the following command
cd gateway-service mvn spring-boot:run
The server will start on port
8080
by default, So once you have successfully started application you'll be able to visit API gateway under addresshttps://localhost:8080
.If you changed the port in
src/main/resources/application.yml
file, use your custom porthttps://localhost:port
.Zuul API Gateway will forward the request to the specific microservice based on its proxy configuration. Such request will also be load balances by ribbon client.
-
Package the applications
You can also package the applications in the form of a
jar
file and then run each application like socd service_directory mvn clean package java -jar target/service_name-0.0.1-SNAPSHOT.jar
- service_directory: the directory of the service.
- service_name: the name of the service.
-
Run additional instances from microservices
You can run more instances of the microservices (User, Site, Organization):
-
Using eclipse just edit run command of your application (Run configurations…) and add VM parameter
-DPORT=…
or-Dserver.port=…
. -
Or using the
-Dserver.port=…
argument with the command:java -jar target/service_name.jar -Dserver.port=…
java -jar target/service_name-0.0.1-SNAPSHOT.jar -Dserver.port=8085
-
To access the applications use the following endpoints
-
Eureka Discovery Service
After running discovery-service, its monitoring console available on
8761
port.https://localhost:8761
Now take a look on Eureka monitoring console: We’ve got one instance of Site and Organization microservices running on default ports (8082, 8083) and two instances of user microservice on (8081, 8085) ports registered on discovery server.
-
API Gateway and Microservices
-
View info about
API Gateway
servicehttps://localhost:8080/actuator/info
-
Check Health for
API Gateway
servicehttps://localhost:8080/actuator/health
Zuul is configured to be available under its default port 8080 and it forwards requests:
- From
/api/users/
path touser-service
- From
/api/sites/
path tosite-service
- From
/api/organizations/
path toorganization-service
When URL
https://localhost:8080/api/users/1
is called several times the Zuul make load balanced between two instances of each user microservice instances. Also when we shut down one of microservice instance we can take a look that it is unregistered from Eureka server dashboard. -
CURL GET command samples for different (User, Site, Organization) microservices using Zuul API Gateway address
:
-
Display info about User microservice
curl -X GET \ 'https://localhost:8080/api/users/actuator/info' \ -H 'accept: */*' \ -H 'cache-control: no-cache'
-
Check Health for User microservice
curl -X GET \ 'https://localhost:8080/api/users/actuator/health' \ -H 'accept: */*' \ -H 'cache-control: no-cache'
-
List all users
curl -X GET \ 'https://localhost:8080/api/users' \ -H 'accept: */*' \ -H 'cache-control: no-cache'
-
Find user by Id
curl -X GET \ 'https://localhost:8080/api/users/1' \ -H 'accept: */*' \ -H 'cache-control: no-cache'
-
Filter users by SiteId
curl -X GET \ 'https://localhost:8080/api/users/site/3' \ -H 'accept: */*' \ -H 'cache-control: no-cache'
-
Filter users by OrganizationId
curl -X GET \ 'https://localhost:8080/api/users/organization/1' \ -H 'accept: */*' \ -H 'cache-control: no-cache'
-
Display info about Site microservice
curl -X GET \ 'https://localhost:8080/api/sites/actuator/info' \ -H 'accept: */*' \ -H 'cache-control: no-cache'
-
Check Health for Site microservice
curl -X GET \ 'https://localhost:8080/api/sites/actuator/health' \ -H 'accept: */*' \ -H 'cache-control: no-cache'
-
List all sites
curl -X GET \ 'https://localhost:8080/api/sites' \ -H 'accept: */*' \ -H 'cache-control: no-cache'
-
Find site by Id
curl -X GET \ 'https://localhost:8080/api/sites/1' \ -H 'accept: */*' \ -H 'cache-control: no-cache'
-
Filter sites by OrganizationId
curl -X GET \ 'https://localhost:8080/api/sites/organization/1' \ -H 'accept: */*' \ -H 'cache-control: no-cache'
-
Filter sites with users by OrganizationId
curl -X GET \ 'https://localhost:8080/api/sites/organization/1/with-users' \ -H 'accept: */*' \ -H 'cache-control: no-cache'
-
Display info about Organization microservice
curl -X GET \ 'https://localhost:8080/api/organizations/actuator/info' \ -H 'accept: */*' \ -H 'cache-control: no-cache'
-
Check Health for Organization microservice
curl -X GET \ 'https://localhost:8080/api/organizations/actuator/health' \ -H 'accept: */*' \ -H 'cache-control: no-cache'
-
List all organizations
curl -X GET \ 'https://localhost:8080/api/organizations' \ -H 'accept: */*' \ -H 'cache-control: no-cache'
-
Find organization by Id
curl -X GET \ 'https://localhost:8080/api/organizations/1' \ -H 'accept: */*' \ -H 'cache-control: no-cache'
-
Find organization with sites by Id
curl -X GET \ 'https://localhost:8080/api/organizations/1/with-sites' \ -H 'accept: */*' \ -H 'cache-control: no-cache'
-
Find organization with sites and users by Id
curl -X GET \ 'https://localhost:8080/api/organizations/1/with-sites-with-users' \ -H 'accept: */*' \ -H 'cache-control: no-cache'
-
Find organization with users by Id
curl -X GET \ 'https://localhost:8080/api/organizations/1/with-users' \ -H 'accept: */*' \ -H 'cache-control: no-cache'
I am Sayed Baladoh - Phd. Senior Software Engineer. I like software development. You can contact me via:
Any improvement or comment about the project is always welcome! As well as others shared their code publicly I want to share mine! Thanks!
Thanks for reading. Share it with someone you think it might be helpful.