0% found this document useful (0 votes)
68 views6 pages

API Gateway Simple Notes

An API Gateway serves as a single entry point for clients to access microservices, handling routing, authentication, and other cross-cutting concerns. It simplifies client interaction, reduces latency, and improves performance through centralized management and caching. The document also provides a step-by-step guide to setting up an API Gateway with two microservices using Spring Boot.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views6 pages

API Gateway Simple Notes

An API Gateway serves as a single entry point for clients to access microservices, handling routing, authentication, and other cross-cutting concerns. It simplifies client interaction, reduces latency, and improves performance through centralized management and caching. The document also provides a step-by-step guide to setting up an API Gateway with two microservices using Spring Boot.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

API Gateway

Definition:An API Gateway is a server that acts as a single entry point for clients to
access/ requests the microservices in a system. It handles requests by routing them to the
appropriate service and consolidating responses.

o A reverse proxy that sits in front of your microservices.


o Handles routing, authentication, authorization, rate limiting, and other cross-
cutting concerns.

Key Responsibilities:

1. Routing: Directs requests to the correct microservice.


2. Authentication & Authorization: Verifies client identity and grants
[Link] security (e.g., JWT tokens).
3. Request/Response Transformation: Modifies requests or responses as needed
for different microservices. (e.g., aggregating multiple responses).
4. Rate Limiting: Controls the number of requests a client can make. This prevents
abuse and overload
5. Load Balancing: Distributes incoming requests evenly across microservices.
6. Logging & Monitoring: Tracks requests, errors, and performance for debugging
and analysis.
7. Caching: Stores frequently accessed responses for faster delivery.
Benefits:

1. Simplifies Client Interaction: Clients interact with a single entry point, not multiple
microservices.
2. Reduces Latency: Aggregates responses from multiple services into one, reducing the number of
calls.
3. Centralized Management: All cross-cutting concerns like logging, authentication, and caching
are managed centrally.
4. Performance: Improves response times through caching and optimized routing.

5. Decoupling: Clients are decoupled from the internal structure of your microservices.
6. Scalability: Can handle a large number of requests.

Examples:

 Apigee
 Amazon API Gateway
 Azure API Management

simple step-by-step demo of how to set up an API Gateway with two microservices, where
MS1 (Microservice 1) calls MS2 (Microservice 2) using RestTemplate.

Step 1: Set up the Microservices

We’ll need two Spring Boot applications for the microservices and one Spring Boot application
for the API Gateway.

Microservice 1 (MS1)

1. Create a Spring Boot Project for MS1:


o Group: [Link]
o Artifact: ms1
o Dependencies: Spring Web
2. Add Controller in MS1:
In MS1, create a simple REST endpoint that will call MS2.

@RestController
@RequestMapping("/ms1")
public class MS1Controller {

@Autowired
private RestTemplate restTemplate;

@GetMapping("/call-ms2")
public String callMS2() {
String ms2Url = "[Link] // MS2 URL
return [Link](ms2Url, [Link]);
}

@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}

3. Run MS1:
Run the MS1 application on port 8081.

Microservice 2 (MS2)

1. Create a Spring Boot Project for MS2:


o Group: [Link]
o Artifact: ms2
o Dependencies: Spring Web
2. Add Controller in MS2:
In MS2, create a simple REST endpoint that returns a response.

@RestController
@RequestMapping("/ms2")
public class MS2Controller {

@GetMapping("/hello")
public String hello() {
return "Hello from MS2!";
}
}

3. Run MS2:
Run the MS2 application on port 8082.
Step 2: Set up the API Gateway

1. Create a Spring Boot Project for the API Gateway:


o Group: [Link]
o Artifact: api-gateway
o Dependencies: Spring Cloud Gateway, Spring Web
2. Configure API Gateway ([Link]):
In [Link], configure the routing for the API Gateway.

spring:
cloud:
gateway:
routes:
- id: ms1_route
uri: [Link]
predicates:
- Path=/ms1/**

- id: ms2_route
uri: [Link]
predicates:
- Path=/ms2/**

[Link]=api-gateway
[Link]=8080

[Link][0].id=ms1_route
[Link][0].uri=[Link]
[Link][0].predicates[0]=Path=/ms1/**

[Link][1].id=ms2_route
[Link][1].uri=[Link]
[Link][1].predicates[0]=Path=/ms2/**

3. Add Main Application Class ([Link]):


In the API Gateway project, create the ApiGatewayApplication class.

@SpringBootApplication
@EnableDiscoveryClient
public class ApiGatewayApplication {

public static void main(String[] args) {


[Link]([Link], args);
}
}
4. Run the API Gateway:
Run the ApiGatewayApplication on port 8080.

Step 3: Testing the Flow

1. Start all applications:


o MS1 will run on [Link]
o MS2 will run on [Link]
o The API Gateway will run on [Link]
2. Test the API Gateway:
o Open a browser or use a tool like Postman to send a request to the API Gateway:
[Link]

The API Gateway will forward the request to MS1, which will call MS2 using
RestTemplate and return the response from MS2, which should be "Hello from MS2!".

Summary of Flow:

 The client hits the API Gateway (/ms1/call-ms2).


 The Gateway forwards the request to MS1.
 MS1 calls MS2 via RestTemplate and gets a response.
 The response from MS2 is sent back to the API Gateway, which then returns it to the
client.

You might also like