"Welcome To Ashok IT" "Spring Boot and MicroServices" Topic : Spring API Gateway Date : 03/02/2025 (Session - 117) _____________________________________________________________________________________________________________________________ API Gateway =========== * Spring Cloud Gateway provides a library for building an API Gateway on top of Spring WebFlux. * Spring Cloud Gateway aims to provide a simple, yet effective way to route to APIs and provide cross-cutting concerns to them such as security, monitoring/metrics, and resiliency. * The Spring Cloud Gateway has three important parts to it: 1) Route − These are the building blocks of the gateway which contain the URL to which the request is to be forwarded to and the predicates and filters that are applied to the incoming requests. 2) Predicate − These are the set of criteria that should match for the incoming requests to be forwarded to internal microservices. For example, a path predicate will forward the request only if the incoming URL contains that path. 3) Filters − These act as the place where you can modify the incoming requests before sending the requests to the internal microservices or before responding back to the client. Steps for Developing API Gateway Application ============================================ 1) Create the Spring Boot Application With below dependencies * API Gateway * Eureka Client 2) Register the API-Gateway Application with Eureka Server as Eureka Client package com.ashokit; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } 3) configure the Routes & Predicates & Filters in configuration file of API-Gateway Service #Server portno changing server.port=9955 #Configuring the Application Name spring.application.name=API-Gateway #Configure the Routes for Customer-Service spring.cloud.gateway.routes[0].id=Customer-Service spring.cloud.gateway.routes[0].uri=http://localhost:9977 spring.cloud.gateway.routes[0].predicates=Path=/api/customers/** #Configure the Routes for Address-Service spring.cloud.gateway.routes[1].id=Address-Service spring.cloud.gateway.routes[1].uri=lb://Address-Service spring.cloud.gateway.routes[1].predicates=Path=/api/address/** #Configure the Routes for ServiceDiscovery-Service spring.cloud.gateway.routes[2].id=Discovery-Service spring.cloud.gateway.routes[2].uri=http://localhost:5555 #http://localhost:9955/eureka/site >> This is URL For Accessing the Eureka Server From Gateway Application spring.cloud.gateway.routes[2].predicates=Path=/eureka/site #Modifying the Request From http://localhost:9955/eureka/site To http://localhost:5555/ spring.cloud.gateway.routes[2].filters[0]=SetPath=/ #Configuring routes for Accessing the Static Resources of EurekaServer spring.cloud.gateway.routes[3].id=Discovery-Service-staticresource spring.cloud.gateway.routes[3].uri=http://localhost:5555 spring.cloud.gateway.routes[3].predicates=Path=/eureka/** #Enabling logging statements logging.level.root=INFO logging.level.org.springframework.cloud.gateway.route.RouteDefinitionLocator=INFO logging.level.org.springframework.cloud.gateway=TRACE 4) Test the API's with Gateway Application URL http://localhost:9955/api/customers >>>>>>>>>>>>>>>>>> Post Request http://localhost:9955/api/custoemers >>>>>>>>>>>>>>>>> Get Request http://localhost:9955/api/customers/customerId >>>>>>> Get Request http://localhost:9955/eureka/site >>>>>>>>>>>>>>>>>>>> Eureka Server Application URL Config Server ============= * Generally We have several Microservices in project development and each microservice requires some of set of configurations such as database configuration,email configurations,security configuration,application configurations etc., * Suppose if We have 10 microservices in the project definetly we need to duplicate the above configuration for each microservice. * If we are duplicating the configuration of microservices the major problem is that whenever any configuration got changed we need to make necessary changes in all microservices and need to restart or redeployment required. * Inorder to overcome the above problem we got solution as "Config Server". * Config Server is an another simple microservice which will maintains common configurations for all microservices. * Config Server maintains external configuration means common configurations will be stored in Github/Gitlab/Bitbucket. * Inorder to Create the Config Server using spring boot project by adding starter called "config-server". Architecture ============ (Connecting) (Spring Boot) (Connecting) UserService/CustomerService >>>>>>>>>>>>>>>>>> Config Server >>>>>>>>>>>>>>>>>>>>>Github/Gitlab/Bitbucket * Generally Spring cloud config Server collect the configurations in two ways 1) External Configuration i.e., Collecting the configuration from github,gitlab,bitbucket etc., * This kind of configuration mostly will be used for production environment. 2) Native Configuration i.e., Collecting the configuration from local system * This kind of configuration mostly will be used of Development environment.