Show List

Creating API Gateway with Eureka

Here is an example of using API Gateway pattern to route requests to microservices that are registered with the Eureka Service Registry.

Eureka is a service registry component of the Netflix OSS suite, used for maintaining a list of available microservices and their instances. The API Gateway acts as a reverse proxy for these microservices, providing a single entry point for external clients and adding additional functionality such as authentication, rate limiting, and request mapping.

Spring cloud provides component to create API Gateway. We are going to use the same project we used for service discovery demo to add API gateway.

Add a new "apigateway" module in the project. In the POM file for the new module, add "gateway", "actuator" and "eureka client" dependency. Add @EnableEurekaClient annotation on the Spring Boot class to register the service as client.

pom.xml :
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>servicediscovery</artifactId>
<groupId>com.springcloud</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>apigateway</artifactId>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<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>


</dependencies>
</project>
apigatewayApplication.java :
package com.springcloud.apigateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@EnableEurekaClient
@SpringBootApplication
public class apigatewayApplication {

public static void main(String[] args) {
SpringApplication.run(apigatewayApplication.class, args);
}
}
application.yml :

The Routes properties mean:
  • All the requests with path /orderd/** will be routed to order-service
  • All the requests with path /payments/** will be routed to payment-service
server:
port: 9004
spring:
application:
name: apigateway
cloud:
gateway:
routes:
- id: order-route-id
uri: lb://order-service
predicates:
- Path=/orders/**
- id: payment-route-id
uri: lb://payment-service
predicates:
- Path=/payments/**
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
fetch-registry: true
register-with-eureka: true
instance:
hostname: localhost
Now, requests to both the order service and payment service can be sent through the API gateway "localhost:9004"
Source Code:

https://github.com/it-code-lab/spring-cloud-servicediscovery

    Leave a Comment


  • captcha text