Kubernetes - Spring Cloud API-Gateway

Kubernetes - Spring Cloud API-Gateway

in previous post, we created our first microservice and deployed it on kubernetes,

in this post we will create another microservice, and will learn how to use Spring API Gateway to deal with different microservices.

we will create another microservice hello-one. as below:

No alt text provided for this image

this is the FirstController.java :

package com.example.hello.one;




import org.springframework.web.bind.annotation.*;




@RestController

@RequestMapping("/employee")

public class FirstController {




	@GetMapping("/message")

	public String test() {

		return "Hello JavaInUse Called in First Service";

	}

}        

and this is the : HelloOneApplication.java



package com.example.hello.one;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class HelloOneApplication {


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


}

        

and this is the Dockerfile.


FROM openjdk:12
EXPOSE 8081
COPY target/*.jar /opt/app.jar
WORKDIR /opt
ENTRYPOINT exec java $JAVA_OPTS -jar app.jar

        

then we have to create, push and run the image, and create service ClusterIP to communicate with this pods contains this image

No alt text provided for this image

note demo-service and hello-one-service which we will deal with.

What is an API Gateway? Why do we need it?

An API Gateway acts as a single entry point for a collection of microservices. Any external client cannot access the microservices directly but can access them only through the application gateway

In a real world scenario an external client can be any one of the three-

  • Mobile Application
  • Desktop Application
  • External Services or third party Apps

No alt text provided for this image

The advantages of this approach are as follows-

  • This improves the security of the microservices as we limit the access of external calls to all our services.
  • The cross cutting concerns like authentication, monitoring/metrics, and resiliency will be needed to be implemented only in the API Gateway as all our calls will be routed through it.
  • The client does not know about the internal architecture of our microservices system. Client will not be able to determine the location of the microservice instances.
  • Simplifies client interaction as he will need to access only a single service for all the requirements.

Spring Cloud Gateway Architecture

Spring Cloud Gateway is API Gateway implementation by Spring Cloud team on top of Spring reactive ecosystem. It consists of the following building blocks-

  • Route: Route the basic building block of the gateway. It consists of
  • ID
  • destination URI
  • Collection of predicates and a collection of filters
  • A route is matched if aggregate predicate is true.
  • Predicate: This is similar to Java 8 Function Predicate. Using this functionality we can match HTTP request, such as headers , url, cookies or parameters.
  • Filter: These are instances Spring Framework GatewayFilter. Using this we can modify the request or response as per the requirement.

No alt text provided for this image

When the client makes a request to the Spring Cloud Gateway, the Gateway Handler Mapping first checks if the request matches a route. This matching is done using the predicates. If it matches the predicate then the request is sent to the filters.

Implementing Spring Cloud Gateway

The Maven project will be as follows-

No alt text provided for this image

The pom.xml will be as follows-

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"

	xsi:schemaLocation="https://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.7.0</version>

		<relativePath/> <!-- lookup parent from repository -->

	</parent>

	<groupId>com.example</groupId>

	<artifactId>spring-cloud-gateway</artifactId>

	<version>0.0.1-SNAPSHOT</version>

	<name>spring-cloud-gateway</name>

	<description>Demo project for Spring Boot</description>

	<properties>

		<java.version>11</java.version>

		<spring-cloud.version>2021.0.3</spring-cloud.version>

	</properties>

	<dependencies>

		<dependency>

			<groupId>org.springframework.boot</groupId>

			<artifactId>spring-boot-starter-webflux</artifactId>

		</dependency>

		<dependency>

			<groupId>org.springframework.cloud</groupId>

			<artifactId>spring-cloud-starter-gateway</artifactId>

		</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>        

Define the application.yml as follows-

server:

? port: 8082

??

?? ?

spring:

? cloud:

? ? gateway:

? ? ? routes:

? ? ? - id: demoModule

? ? ? ? uri: https://10.111.111.219:8080/

? ? ? ? predicates:

? ? ? ? - Path=/

? ? ? - id: helloOneModule

? ? ? ? uri: https://10.111.37.178:8081/

? ? ? ? predicates:

? ? ? ? - Path=/employee/**?

        

Create the bootstrap class with the @SpringBootApplication annotation


package com.example.gateway;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;


@SpringBootApplication
@EnableDiscoveryClient
public class SpringCloudGatewayApplication {


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


}

        

Start the three microservices we have developed-

No alt text provided for this image

demo-service ==> ClusterIP ==> 10.111.111.219 ==> 8080

hello-one-service ==> ClusterIP ==> 10.111.37.178 ==> 8081

api-gateway-deployment ==> LoadBalancer ==> localhost ==> 83

No alt text provided for this image
No alt text provided for this image
Eric van der Ven

Recent afgestudeerd in Software Development

10 个月

Thanks for the tutorial! I would however, use the names of the services in the routes instead of the cluster ips.

要查看或添加评论,请登录

Ahmed El-Sayed的更多文章

  • Open System Architecture

    Open System Architecture

    "Open System Architecture" typically refers to the design and implementation of systems using open standards, open…

  • ChatGPT: Revolutionizing Conversational AI

    ChatGPT: Revolutionizing Conversational AI

    Artificial intelligence (AI) has come a long way in recent years, and one of the most exciting developments in this…

  • Togaf 9.2 Level 1 (Part 1)

    Togaf 9.2 Level 1 (Part 1)

    efore we go in details , we have to know how can we read the open group standards document, you should download the…

    1 条评论
  • Kafka vs RabbitMQ

    Kafka vs RabbitMQ

    What’s the Difference Between a Message Broker and a Publish/Subscribe (Pub/Sub) Messaging System? Message brokers are…

  • What is the strangler pattern and how does it work?

    What is the strangler pattern and how does it work?

    What is the strangler pattern? Picture a motorcycle that works, but could stand to undergo extensive overhauls that…

  • MIGRATING FROM MONOLITH TO MICROSERVICES: STRATEGY & STEP-BY-STEP GUIDE

    MIGRATING FROM MONOLITH TO MICROSERVICES: STRATEGY & STEP-BY-STEP GUIDE

    Migrating from monolith to microservices is less costly and risky than redeveloping an entire system from scratch. But…

    1 条评论
  • Migrate a monolith application to microservices using DDD

    Migrate a monolith application to microservices using DDD

    A monolithic application is typically an application system in which all of the relevant modules are packaged together…

    1 条评论
  • Migrate From Monolithic To Microservices Using DDD Pattern

    Migrate From Monolithic To Microservices Using DDD Pattern

    The general migration approach has three steps: Stop adding functionality to the monolithic application Split the…

  • Migrate From Monolithic To Microservices Using Strangler Pattern

    Migrate From Monolithic To Microservices Using Strangler Pattern

    There are three steps to transition from a monolithic application to microservices by implementing the Strangler…

  • GraalVM

    GraalVM

    GraalVM has caused a revolution in Java development since it launched three years ago. One of the most discussed…

社区洞察

其他会员也浏览了