Creating a cloud services by spring and Micro Service

Djamel Zerrouki
7 min readMay 12, 2020

Spring Boot Micro-Services with Eureka and Zuul proxy.

One issue that we face day-to-day as developers is the speed of development. One of the coolest things to me in the ever-changing landscape of technology is how this issue is continually being addressed in an effort to makes our lives easier.

We have gone from having to write everything needed in a verbose way to being able to configure a simple REST API in a few lines of code with Spring Boot. The most amazing part of this is not only the ability to create web services but also the ability to allow these services to communicate in a smart way. Spring has given us many tools to allow easy configuration and putting together things that just work (mostly).

This post is not to be considered a full guide to which the extent of these technologies can be leveraged. In this post, we give examples of how to create cloud services by spring and MicroService and how they can be used (along with Zuul and Eureka) to create a simple discovery service.

Get started

Tools and Versions

  1. Spring Boot Version: 2.0.1
  2. Spring Cloud Version 1.2.4
  3. Java Version 1.8.0
  4. Maven Version 3.6.1
  5. You can use the eclipse STS “eclipse Spring Tool Suite” editor or IntelliJ IDEA (your choice).

We will therefore create the following microservices:

1. Main service:

Main service, which offers for example a REST API for the list of system users.

2. Config Service:

Configuration service, whose role is to centralize configuration files
different microservices in one place.

3. Proxy Service:

Gateway responsible for routing a request to one of the instances of a
service, so as to automatically manage the load distribution.

4. Discovery Service:

Service allowing the recording of instances of services with a view to being discovered by other services.

Spring Cloud and Netflix architecture by Microservices

2. Creation of Microservices

2.1. Microservice Service 1

Microservice Service 1

Each microservice will take the form of a Spring project. To create quickly
and easily a Spring project with all the necessary dependencies, Spring Boot
provides Spring Initialized, you need to access the start.spring.io site, and create a project with the following characteristics:

  1. Maven project with Java and Spring Boot version 2.0.1
  2. Group: tn.insat.tpmicro
  3. Artifact: User-service
  4. Dependencies:

Web
Rest Repositories
JPA: Java Persistence API
Mysql: database for storage
Actuator: for montoring and application management
Eureka Discovery: for integration with Discovery Service
Config Client: for integration with Config Service

Then follow these steps to create the Microservice UserService:

Open the downloaded project with your editor.
Under the src / main / java directory and in the com.example.entities package, create the User class

User class implements in java

5. This class is annotated with JPA, to then store user objects in the MySQL database using Spring Data. To do this, create the UserRepository interface in the same package

Create the UserRepository interface

6. To insert the objects into the database, we will use the Stream object. For that, we will create the class DummyDataCLR.

7. We notice here that the UserRepository will be instantiated automatically thanks to the dependency injection mechanism, used by Spring. Start the main class. A MySql database will be created and the CommandLineRunner will be responsible for injecting the data.

Attention: Take care to use JDK 8!
8. To run your application:
✅ Create a mvn package configuration by the editor
✅ Then launch the Spring Boot UserServiceApplication configuration

2.2. Microservice ConfigService

Microservice ConfigService

In a microservices architecture, several services run at the same time, on different processes, each with its own configuration and its own parameters. Spring Cloud Config provides server-side and client-side support for outsourcing configurations in a distributed system.

Thanks to the configuration service, it is possible to have a centralized place to manage the properties of each of these services

For that :

  1. Start by creating a ConfigService service in Spring Initializr, with the appropriate dependencies.
  2. Open the project in another instance of IntelliJ IDEA.
  3. To expose a configuration service, use the annotation
    @EnableConfigServer for the ConfigServiceApplication class.
ConfigServiceApplication class in java

4. To configure this configuration service, add the following values to its application.properties file:

application.properties file

This indicates that the configuration service will be launched on port 8888 and that the directory containing the configuration files is located in the src / main / resources / myConfig directory. Now just create this directory.
5. Create the myConfig directory in the src / main / resources tree.
6. Create in this directory the application.properties file in which you insert the following instruction:

global=xxxxx

This file will be shared between all microservices using this configuration service.
7. The configuration directory must be a git directory. For that:

✅ Open the terminal with IntelliJ and navigate to this directory.
✅ Initialize your directory: git init
✅ Create a root entry in the repository: git add.
✅ Commit: git commit -m “add.”

Go back to the UserService project and add it to the application.properties configuration file:

application.properties file

Restart your services. To consult the configuration service, go to

http: // localhost: 8888 / user-service / master.

You will see the JSON file

As the application.properties file contains all the shared properties of the different microservices, we will need other files for the properties specific to a microservice. For that:

8. Create a user-service.properties file in the myConfig directory for the UserService service.
9. Add the properties of your service, namely, for example:

me=Djamel.Zerrouki@jimmi.fr

Restart the configuration microservice. By consulting the URL http: // localhost: 8888 / user-service / master, we notice the addition of the new property.

We will now define a REST call to this property. For that:
10. Create the UserRestService class in the user-service project.

userRestService class in java

11. Restart the service instance, then call the service in your browser by typing: http: // localhost: 8080 / messages.

2.3. Microservice DiscoveryService

To avoid a strong coupling between microservices, it is strongly recommended to use a discovery service which makes it possible to record the properties of the different services and thus to avoid having called a service directly. Instead, the discovery service will dynamically provide the necessary information, thereby ensuring the elasticity and dynamicity inherent in a microservices architecture.

Microservice DiscoveryService

To achieve this, Netflix offers the Eureka Service Registration and Discovery service, which we will use in our application.

  1. Return to Spring Initializr and create a new Spring Boot project called discovery-service with the dependencies Eureka Server and Config Client.
  2. Start the project with your editor.
  3. In the DiscoveryServiceApplication class, add the @EnableEurekaServer annotation.
EurekaServer in java

4. Add the following properties to its application.properties file.

application.properties file

5. In the config-service project, create a discovery-service.properties file under the myConfig directory.

6. Add the following properties to (1) set the default port for the discovery service and (2) prevent self-registration of the Eureka service.

discovery-service.properties file

To consult the Eureka service, go to http: // localhost: 8761.

2.4. Microservice ProxyService

Microservice ProxyService

The microservice architecture, by providing a set of independent and weakly coupled services, is faced with the challenge of providing a unified interface for consumers, so that they do not see the low granularity breakdown of your services. This is why the use of a proxy service, responsible for routing requests and load balancing, is important.

Netflix offers the Zuul service to achieve this. To create your Proxy microservice:

  1. Go to Spring Initializr.
  2. Create the proxy-service project with the following dependencies: (Zuul, Web, HATEOAS, Actuator, Config Client and Eureka Discovery).
  3. Open the service with your IDEA.
  4. Add to the ProxyServiceApplication class the annotation @EnableZuulProxy, as well as @EnableDiscoveryClient so that the proxy is also registered in the service for this discovery.
Microservice ProxyService ProxyServiceApplication

5. Add the properties spring.application.name and spring.cloud.config.uri in the application.properties file of the proxy service.

6. Create the file proxy-service.properties in the myConfig directory of the configuration service, in which you will set the port of the proxy service to 9999.

By launching the Proxy service, you will notice that it is added in Eureka.

Consult the services via the proxy

The sequence diagram of the figure follow expresses the consultation of the services via the proxy

  1. An HTTP client sends the HTTP request to the proxy that contains the microservice name.
  2. The proxy service knows the name of the microservice, it will ask the registration service for the location of the machine where this microservice is located.
  3. The registration service will return the IP address and port if it has one instance or a list if it has multiple instances.
  4. The proxy service will drop an instance and send the request.
  5. The requested service will do the processing then sends the result to the proxy which will transmit it to the client.
Sequence diagram represented how to Consult the services via
the proxy

Description of the architecture of our system

Our system architecture on the Spring cloud environment and
micro-service

Thanks for reading!

--

--

Djamel Zerrouki

Software engineer 💡, and full stack java developer 📱☁💻.