Skip to main content

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

Comments

Popular posts from this blog

Routing in Angular 7

What you will learn after reading this post.What is Routing in Angular 7.Basic routing of component in Angular 7.Active link in routing in Angular 7.Wild card routing in Angular 7.Child routing in Angular 7.1. What is Routing in Angular 7? Whenever in normal HTML web page we click on a link we are navigated to specific page given in that link. When we are navigated then entire view of the application is reloaded again which cost resources and this type of application is not single page application. 

Creating Simple Hello World Application with SPRING BOOT

For creating a simple running web application with spring boot we do not have to do much. We just require to follow some simple steps and we are good to go. Unlike SpringMVC framework we do not. have to take care of all heck of configuration or XML file in one go we are all set with working mvc based web application.

Now we have to follow these steps.

Generate spring boot project from this url "https://start.spring.io/" You can do the same using STS setting to.Importing the project in STS.

Adding Spring-Security to Spring-Boot App

In the previous post we have created a simple spring boot application. This application was very simple application just getting request and showing page for requested URL. Currently all urls are open to all user that is we do not have any kind of authentication on the URL. Our spring boot application is like the open pool in which any one can come and start fishing. Now we want to secure our application so that only authenticated user will be able to visit secure part of the application. For this purpose we will be using the Spring Security. 
Scope of this tutorial. In this we will learn basic integration of spring security with spring boot.
NOTE: This tutorial will just contain only basic integration of spring security. If you haven’t gone through previous tutorial and you are the newbies to this spring boot then I will request you to go through previous post to get understanding of spring boot. 
Now lets get started with the configuration.If you do not have the previous code you can …

Feature Modules in Angular 7

Feature Module Whenever we  create angular application we go on adding components to angular application. But as our application goes on increasing it become to much problematic to maintain such application. Every feature of our application is binded under single module which we call as app module. Which is root module of our Angular 7 application. While developing angular application without modularizing its features creates several problems  Application is not easily scalable.Application is tough to maintain.Application complexity makes it hard to understand. Solution Here feature modules comes for our rescue. Feature modules help angular applications to get modularised on basis of features present in our application. As our application grows we can divide our entire application in small feature using feature module. Feature modules helps in creating boundary that separates our specific functionality or say features. Feature modules communicates with other module or root module usin…

About Me

I am a self motivated full stack  developer. I have worked in many projects under several companies.
Java is one of my favourite language. Currently i am working with Tata Consultancy Services, Hyderabad. Tata Consultancy is my first company where i started my career on 24th Oct 2016.

Currently i am working on spring mvc in TCS but i also have a knowledge of Spring Boot, Angular, MongoDB. reqularly i am trying to add extra beauty to my profile. For this i believe in learn and unlearn strategy.

Reason for developing this blog is to show case my work and keep me busy during the weekends. Yahh! its true i have lot of free time on weekends as i don't have any girlfriend to waste my time with. And so i created this blog to show case my learning and work. Now, this blog gives me a goal to pursue and the way to keep me busy.

Through this blog i will try to share to all readers that i will be learning in course of time. I will try to keep this blog updated with latest evolution in develo…

Decompose Monolithic Application To Microservices

How To Decompose Application into Microservices. After reading the previous two posts you all must be excited to implement microservice architecture to your application and that's good you should be ready to implement something new that might boost the application performance,  maintenance, scalability and will make your application future proof. But have you ever thought how will you be breaking your application into micro application i.e microservices. Think about it for some minute exercise your brain for some time before reading the below tutorial.

Now i am sure that you all might  have come through excellent ideas for breaking up your application in microservices. Please feel free to mention your valuable ideas in comment so that other or even me can be benefited by your ideas.

Now before breaking your application into micro-services you should consider following points.
Points to be considered before creating microservices.Your each services should be easily testable.Your a…