BarathArivazhagan / spring-cloud-gateway-routing

This project demonstrates the usage of API gateway between microservices using spring cloud gateway

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

spring-cloud-gateway-routing

This project demonstrates the usage of API gateway between microservices using spring cloud gateway.

what is spring cloud gateway ?

Spring Cloud Gateway is an intelligent and programmable router based on Project Reactor.

spring cloud gateway

Compatability Matrix

choose the branch based on below maintained versions.

Branch/Version Spring Boot Spring Cloud
master 2.1.5.RELEASE Greenwich.SR1
v2.1.3 2.1.3.RELEASE Greenwich.RELEASE

Projects

Name Port Description
spring-cloud-gateway 9500 spring cloud gateway router
jio-store-service 9501 jio microservice
airtel-store-service 9502 airtel microservice
vodaphone-store-service 9503 vodaphone microservice
config-server 8888 spring cloud config server
eureka-server 8761 eureka-server

Eureka Server Registration (Optional)

By default eureka is disabled.

eureka:
  client:
    enabled: false

To enable eureka, set below property in all the microservices and restart.

spring:
  profiles:
    active: eureka

How to build and run ?

  • Download/Clone the repository :

    $ git clone https://github.com/BarathArivazhagan/spring-cloud-gateway-routing.git
    $ cd spring-cloud-gateway-routing
    $ ./mvnw clean install
    
  • To run the application :

    $ docker-compose up
    

How to test the application ?

Use gateway routes to route to respective microservices.

spring cloud gateway route definition :

spring:
  cloud:
    gateway:
      routes:
        - id: jio-service
          uri: http://localhost:9501
          predicates:
          - Path= /jio/*
          filters:
          - StripPrefix=1 # required to strip the prefix made to the request . Ex /jio/customers request will go to jio service as /customers 
        - id: airtel-service
          uri: http://localhost:9502
          predicates:
          - Path= /airtel/*
          filters:
          - StripPrefix=1
        - id: vodaphone-service
          uri: http://localhost:9503
          predicates:
          - Path= /vodaphone/*
          filters:
          - StripPrefix=1
$ curl http://localhost:9500/jio/customers

[
    {
        "customerName": "barath-jio",
        "customerAge": 25,
        "customerGender": "MALE"
    }
]

$ curl http://localhost:9500/airtel/customers

[
    {
        "customerName": "barath-airtel",
        "customerAge": 25,
        "customerGender": "MALE"
    }
]

$ curl http://localhost:9500/vodaphone/customers

[
    {
        "customerName": "barath-vodaphone",
        "customerAge": 25,
        "customerGender": "MALE"
    }
]

Header based routing strategy

spring cloud gateway

Enable SPRING_PROFILES_ACTIVE=header to test header based routing strategy

Query param based routing strategy

spring cloud gateway

Enable SPRING_PROFILES_ACTIVE=query to test query param based routing strategy

Eureka Registration View

eureka_registration_view

How to rebuild the project after the changes?

  docker-compose build

References

About

This project demonstrates the usage of API gateway between microservices using spring cloud gateway


Languages

Language:Java 96.6%Language:Dockerfile 3.4%