Implementing Spring GateWay for Microservices using spring boot.
Microservice - mplementing Spring GateWay for Microservices using spring boot. | techijournal.com

Implementing Spring GateWay for Microservices using spring boot.

In previous post MicroServices – GateWay Pattern we discussed about gateway pattern. Lets implement it using Spring Boot & Spring gateway. Here we will create a basic Order Management system with following services

  • Order Service 
  • Customer Service 
  • Kitchen Service

In this tutorial main motive is to demonstrate how to implement gateway for our services. Gateway is interface that will route our request to required services. If you want to know more about Gateway then feel free to visit MicroServices – GateWay Pattern
Gateway Pattern

As we know that each spring boot application is a microservice there fore for creating our services we will be using Spring Boot.


Note: You can get entire code at this link

Steps to be followed

  1. Create Spring Boot Application for OrderService.
  2. Create Spring Boot Application for CustomerService.
  3. Create Spring Boot Application for Gateway.

Step 1: Create a Spring Boot Application for OrderService

Our orderservice project structure will be as follows.


Generate a spring boot application named orderservice packaged as war with following maven dependencies.


In project we will have our controller with “/order/listorder”


Create a application.properties


Create a Order.java

Now run the application as spring boot application. you will get the following result  at 8081 port.



Step 2: Create Spring Boot Application for CustomerService

Our customerservice project structure will look something like this.
Generate a spring boot application named customerservice packaged as war with following maven dependencies.

In project we will have our controller with “/customer/getcustomer
Create a application.properties



Create a Customer.java

Now run the application as spring boot application. you will get the following result  at 8082 port.

Step 3: Create Spring Boot Application for Gateway

Create a omgateway Spring Boot application packaged as JAR . With following dependencies

Note : Spring Cloud Gateway requires the Netty runtime provided by Spring Boot and Spring Webflux. It does not work in a traditional Servlet Container or built as a WAR.

Create a application.properties 

Create a application.yml

Here we have in this application.yml  “routes” is the array of the urls you want to give for routing and “Path” will match the incoming request and proxy it to given url in “uri“. Like in our case any uri with “/order/**” i.e path starting with “/order”  will be redirected to “http://localhost:8081/”.

Now run the omgateway application as springboot application on port :8000
and try to visit “localhost:/8000/order/listorder” you should be getting the result of “localhost:8081/order/listorder” And visit “localhost:/8000/customer/getcustomer” you would get result which you get by visiting “localhost:/8082/customer/getcustomer”

This is because your gateway is routing the request to 8082 and 8081 respectively.


Note: You can get entire code at this link

Durgesh Kumar

He is the Founder of TechiJournal.com. And have 4+ years of experience as full-stack Java developer. He worked with many reputed product companies and would like to share his experience and knowledge through this blog. He works very hard to provide you with quality content. But as no one is perfect, If you feel that some improvement can be made then feel free to add it in the comment section. We look forward to it.

Leave a Reply