SEARCH BY

All

  • All
  • Java8
  • Spring

Spring Boot Microservices application example with step by step guide for beginners

Post a Comment

Introduction

In this article iam trying to share a spring boot based microservices example. To be short and simple iam not going to discuss the nature of microservices architecture in deep.A simple definition of microservice application is a composed of small independent functioning modules where each independent functioning module called as a microservice, for example in online store application login, product catalogue, order, track, payment all are said to be as microservices.

Architecture of microservice application 

Here the folloing figure has some components which are describing nature of microservice architecture
Architecture of Microservice Application
Architecture of Microservice Application
In the above figure there are different components like browser, API Gateway, Naming server, Load Balancer and MicroServices.

Browser

From browser request can be trigger to application ( can not be contacted to microservices directly ) via API gateway

API Gateway

We are going to use ZUUL API gateway to interact with microservices

Naming Server

Naming Server is giving names to microservices, which maintains available microservices details, we are using Eureka Server as naming server

Load Balancer

Load Balancer is for maintaining load between multiple instance of microservices, we are going to use Ribbon as load balancer

What we are developing

We are going to develop blog posts and comments system based on spring boot microservices. Here we are going to develop two business logic microservices one is for dealing with blog post operations like getting posts, adding new post etc...another microservice is for dealing with post comments, like adding comments etc... Apart from these two microservices we need to develop Naming server and API gateway server.
Blog posts and comments application based on microservices
Blog posts and comments application based on microservices
From the above figure you can see that we need to develop four spring boot based applications which are
  1. Naming server ( also called as Registery Server ) - this will be eureka server
  2. Blog Posts Server ( Microservice for performing blog post CRUD operations ) - this will be  eureka client
  3. Post Comments Server ( Microservice for performing comments CRUD operations ) - this will be eureka client
  4. ZUUL API gateway server - this will be eureka client

Creating Eureka server

In this section we are creating a naming server called as Eureka server. For this we need to create a spring boot application with the following dependencies
<dependencies>
 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
    
 <dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
 </dependency>
    
 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-devtools</artifactId>
  <optional>true</optional>
 </dependency>
</dependencies>
After creating basic spring boot project with the above dependencies we need to enable naming server with the configuration level annotation @EnableEurekaServer
Next we need to set name for this server and other properties need be added in application.properties file
# Give a name to the eureka server
spring.application.name=eureka-server
# default port for eureka server
server.port=8761
# eureka by default will register itself as a client. So, we need to set it to false.
# What's a client server? See other microservices (blog posts, comments, etc).
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
In the above we set application name as eureka-server, and the default port number for eureka server is 8761 and by default we need to disable this server as a client to naming server so we have to unregister this server with the property register-with-eureka as false and fetch-registery as false. Run the application and open http://localhost:8761 in a browser to see eureka dashboard page where we can see registered microservices with this naming server.
Eureka server admin screen
Eureka server admin screen

Next we need to create other business logic included microservices one is for posts and another one is for comments

Creating blog post microservice (Eureka client)

This is also a regular spring boot application, create with the following dependencies where the special dependency is for eureka client
<dependency>
  <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
     
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  </dependency>
     
  <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-rest</artifactId>
 </dependency>
    
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-devtools</artifactId>
   <optional>true</optional>
  </dependency>
For simplicity for understanding basic connectivities in microservice application we are not working with databases at this movement. Now we are configuring this service within application.properties file 
# serivce name
spring.application.name=blog-post-service
# port
server.port=8090
# eureka server url
eureka.client.service-url.default-zone=http://localhost:8761/eureka
In the above , you can notice that we have given name to our microservice as blog-post-service and we have give the value to service-url with the path of eureka server. After configuring in application.properties we need to enable eureka client with the annotation @EnableEurekaClient at @SpringBootApplication annotated class of blog post application.
blog post instance in eureka server dashboard
blog post instance in eureka server dashboard
Restcontroller for sample hello message
@RestController
public class HomeController {

 @GetMapping("/go")
 public String hello() {
  
  return "Welcome meesage";
  
 }
}

Creating comments microservice (Eureka Client)

Do the creating of spring boot application for comments microservice by adding same dependencies which are added to the blog post microservice and do the configuration setting in the application.properties file of comments project
# serivce name
spring.application.name=comments-service
# port
server.port=8060
# eureka server url
eureka.client.service-url.default-zone=http://localhost:8761/eureka
We have given comments-service as a name to our application and with the port number 8060. Same as blog post micro service enable eureka client with the annotation @EnableEurekaClient at @SpringBootApplication annotated class of this comment service application.
Two microservices registered with eureka server
Two microservices registered with eureka server

Creating ZUUL API gateway service (Eureka Client)

In this section we are going to create API gateway to access microservices outside environment, that means each request will be targeted to API gateway and then ZUUL will route the request to appropriate microservice based on configurations done.
ZULL application also need to be registered as a client to Eureka server. Zull will take the details of other microservices to perform serverside load balancing if more than one instance of same microservice is available. 
ZUUL is a single entry point to microservice application , ZUUL will route the request to appropriate microservice. 
 Now we are going to create a spring boot application with the following dependencies
<dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
     </dependency>
     
     <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
 </dependency>
    
     <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
     </dependency>
     
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-devtools</artifactId>
         <optional>true</optional>
     </dependency>
Now we have to configure ZULL server by giving name to the ZUUL gateway application alogn with port number. We must register ZUUL gateway as a client with Eureka server and we have to give a mappring routes to microservices. We do all these configurations in application.properties file of ZUUL gateway application
server.port=8712
spring.application.name=zuul-server
eureka.client.service-url.default-zone=http://localhost:8761/eureka/

# A prefix that can added to beginning of all requests. 
#zuul.prefix=/api

# Disable accessing services using service name (i.e. blog-post-service).
# They should be only accessed through the path defined below.
zuul.ignored-services=*

# Map paths to services
zuul.routes.blog-post-service.path=/posts/**
zuul.routes.blog-post-service.service-id=blog-post-service
In the above you can notice that last two lines are doing mapping route between /post/** to blog-post-service microservice. All the requests to /post/** will hit the microservice named with blog-post-service. The port number of the ZULL gateway is given as 8712 in the above application.properties file, and so to access all the endpoints of blog-post-service service will be like http://localhot:8712/posts/** . After done with the configurations application must be enabled as a Eureka client and  as zuul proxy, so we do with the following annotations at configurations level.
@SpringBootApplication
@EnableEurekaClient   // It acts as a eureka client
@EnableZuulProxy  // Enable Zuul
We can see gateway also registered as a client with eureka server
gateway server along with logic functioning servers
gateway server along with logic functioning servers
after done with settings of ZUUL gateway now we are accessing post services from the url http://192.168.1.6:8762/posts/go , then you can get welcome message ( which we written for simplifying code while understanding microservice architecture)
routing to microservice
routing to microservice

How to access services

We have an edge service in our example which called as API gateway that is ZUUL. We can access our services by requesting to the ZUUL server, recall that our ZUUL server port number is 8762, so we have to point our requests to this port number. In the above we have done configurations for ZUUL in applications.proeprties file, and now we are making little changes in that properties file
#zuul.ignored-services=*

# Map paths to services
zuul.routes.blog-post-service.path=/posts/**
zuul.routes.blog-post-service.service-id=blog-post-service
In the above we commented the property zuul.ignored-services then now we can access services with their name and regular endpoints too but via ZUUL port number only.

We can access our blog posts in the following ways 

http://localhost:8762/blog-posts-service/go  ( blog-posts-service is the name given to blog posts service ) or http://localhost:8762/posts/go ( this is due to configurations was set in ZUUL )
Accessing service with different end points via zuul and by application name
Accessing service with different end points via zuul and by application name
jaya
I love software designing, coding, writing and teaching...

Share this article

Related Posts

Post a Comment

Place your code in <em> </em> tags to send in comments | Do not add hyper links in commnet

Close

Subscribe our newsletter for the latest articles directly into your email inbox