How to implement API GATEWAY in Spring Boot Microservices


API Gateway provides a unified interface for a set of microservices so that clients no need to know about all the details of microservices internals. API Gateway centralize cross cutting concerns like security, monitoring, rate limiting etc. Spring cloud provides spring cloud gateway to create API Gateway.How to implement API Gateway in spring boot
Step 1: Create new Project For API-Gateway with the help of StartSpring and add given dependency in pom.xml file.

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>


Step 2: Go to main class and enable EnableDiscoveryClient

@EnableDiscoveryClient

Step 3: Go to Application.properties file and copy & paste given properties

spring.application.name=API-GATEWAY
eureka.client.service-url.default-zone=http://localhost:8761/eureka
management.endpoints.web.exposure.include=*

Step 4: Enable routing Path for all services through service Registry

spring.cloud.gateway.discovery.locator.enabled=true
spring.cloud.gateway.discovery.locator.lower-case-service-id=true
logging.level.org.springframework.cloud.gateway=DEBUG

Step 5: For CORS configuration use given properties

spring.cloud.gateway.globalcors.cors-configurations.[*/*].allowed-origins=*
spring.cloud.gateway.globalcors.cors-configurations.[*/*].allowed-methods=GET,POST,PIT,DELETE

For more configuration details use given links https://cloud.spring.io/spring-cloud-gateway/reference/html/appendix.html

Download Complete code Zip

That’s all.