Hi geeks! as per our complete microservices architecture, we have developed two microservices with a service registry. With this implementation, users are calling APIs of these microservices directly. Then there are different entry points for these APIs and to set a single entry point we have to set up an API Gateway service. We can define even this as a microservice. So we will see how can we implement an API gateway and how our existing microservices can be integrated with this with practical examples.
API Gateway
Step 4 - API Gateway
Let's take a look at the project initialization with https://start.spring.io/
Initialization details
- Project build tool: Maven
- Language: Java
- Spring boot: 2.4.2
- Project Metadata
- Group: com.agnasarp
- Artifact: agnasarp-cloud-gateway
- Name: agnasarp-cloud-gateway
- Description: Agnasarp Cloud Gateway
- Package name: com.agnasarp.cloudgateway
- Packaging: Jar
- Java version: 8
- Dependencies:
- Eureka Discovery Client: A REST based service for locating services for the purpose of load balancing and failover of middle-tier servers.
- Gateway: Provides a simple, yet effective way to route to APIs and provide cross cutting concerns to them such as security, monitoring/metrics, and resiliency.
- Spring Boot Actuator: Supports built in (or custom) endpoints that let you monitor and manage your application - such as application health, metrics, sessions, etc.
Then, we will import the project into IntelliJ Idea.
Project structure
Project structure of API Gateway in IntelliJ Idea
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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.2</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.agnasarp</groupId> <artifactId>agnasarp-cloud-gateway</artifactId> <version>0.0.1-SNAPSHOT</version> <name>agnasarp-cloud-gateway</name> <description>Agnasarp Cloud Gateway</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>2020.0.1</spring-cloud.version> </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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.yml
server: port: 9191 spring: application: name: API-GATEWAY cloud: gateway: routes: - id: DEPARTMENT-SERVICE uri: lb://DEPARTMENT-SERVICE predicates: - Path=/departments/** - id: USER-SERVICE uri: lb://USER-SERVICE predicates: - Path=/users/** eureka: client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://localhost:8761/eureka/ instance: hostname: localhost
AgnasarpServiceRegistryApplication.java
package com.agnasarp.cloudgateway; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class AgnasarpCloudGatewayApplication { public static void main(String[] args) { SpringApplication.run(AgnasarpCloudGatewayApplication.class, args); } }
Now we can see Department Service, User Service, and API Gateway Service are up and running when you refresh the Service Registry. Instead of using ports of services themselves, we can use the port of API Gateway to contact each service configured in it. Any service can be called via port 9191 already configured but as an example, we will call the get-user-service as below.
Get-User-Service
That is all for today's post. Hope you got the concept of API Gateway in the microservices architecture. If you need the source code of these services, please click the below buttons to get them from GitHub. We will meet again with this kind of tech topic soon. Until then bye bye!
Post a Comment