This project demonstrates the usage of API gateway between microservices using spring cloud gateway.
Spring Cloud Gateway is an intelligent and programmable router based on Project Reactor.
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 |
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 |
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
-
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
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"
}
]
Enable SPRING_PROFILES_ACTIVE=header to test header based routing strategy
Enable SPRING_PROFILES_ACTIVE=query to test query param based routing strategy
docker-compose build