Spring Boot Microservices — Part4 — APIGW

In this tutorial I will implement the APIGW using Spring Cloud API Gateway.

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.6.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>api-gateway-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>api-gateway-service</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
<spring-cloud.version>2021.0.2</spring-cloud.version>
</properties>
<dependencies>
<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>
</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.properties

spring.application.name=api-gateway
spring.cloud.gateway.discovery.locator.enabled
=true
spring.cloud.gateway.discovery.locator.lower-case-service-id
=true

eureka.instance.instance-id
=${spring.application.name}-${random.uuid}
spring.cloud.loadbalancer.ribbon.enabled
=false

spring.cloud.gateway.routes[0].id
=product-service
spring.cloud.gateway.routes[0].uri
=lb://product-service
spring.cloud.gateway.routes[0].predicates[0]
=Path=/api/product

MainApp.java

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class ApiGatewayServiceApplication {

public static void main(String[] args) {
SpringApplication.run(ApiGatewayServiceApplication.class, args);
}

}

Start the api-gateway-service

POST — http://localhost:8080/api/product

GET — http://localhost:8080/api/product

It shows APIGW works fine!

--

--

--

Java Developer and enthusiast

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Staff Engineer Jamie Riedesel teaches us how to connect with coworkers and write a book from home

Nested virtualization and PCI passthrough with KVM and OpenNebula

Using Docker APIs to Perform Health Checks

Using Docker APIs to Perform Health Checks

How I Sped Up a Data Refactor By a Factor of 15 Using Kinesis Streams & Lambda

Setting up a Lab

Probably not this kind of dewing

CVE-2020–29607: Remote Code Execution Via File Upload Restriction Bypass In Pluck CMS ≤ 4.7.13

CI Test Automation Strategy

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
PA

PA

Java Developer and enthusiast

More from Medium

Spring Boot Microservices — Part6— OpenFeign and Circuit Breaker Resilient4j

Exception handling — Java

QUEUE in java:

Spring Boot and Weblogic JMS