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:
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
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-
The advantages of this approach are as follows-
领英推荐
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-
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-
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-
demo-service ==> ClusterIP ==> 10.111.111.219 ==> 8080
hello-one-service ==> ClusterIP ==> 10.111.37.178 ==> 8081
api-gateway-deployment ==> LoadBalancer ==> localhost ==> 83
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.