阅读时间: 3 分钟
春天云网关
Spring Cloud Gateway(SCG)提供了一个用于在Spring和Java之上构建API网关的库。
它的目的是提供一种简单而有效的方式来路由到API,并为它们提供交叉关注,如:安全、监控/指标和弹性。
SCG是一个非阻塞式的API。当使用非阻塞式API时,一个线程总是可以用来处理传入的请求。这些请求在后台被异步处理,一旦完成就会返回响应。因此,在使用SCG时,没有传入的请求会被阻断。
春天云网关架构
它由以下构件组成:- 1.
**1.路由。**路由是网关的基本构建模块。它由以下部分组成。
- ID
- 目的地URL
- 谓词的集合和过滤器的集合
2.谓词(Predicate)。 这类似于Java 8中的Predicate函数。
使用这个功能,我们可以匹配HTTP请求,如头文件,url,cookies或参数。
3.过滤器。 这些是Spring框架网关过滤器的实例。
使用它,我们可以根据需求修改请求或响应。


当客户端向SCG发出请求时,Gateway Handler Mapping首先检查请求是否与路由匹配。这种匹配是使用谓词完成的。如果它与谓词匹配,那么请求就会被发送到过滤器。
实现Spring云网关
使用SCG,我们可以通过以下两种方式创建路由。
- 使用基于java的配置,以编程方式创建路由。
- 使用基于属性的配置(即application.properties或application.yml)来创建路由。
在这里,我们将使用基于属性的配置来实现SCG。我们将实现SCG应用程序,根据url模式将请求路由到其他两个微服务。

实现第一个微服务。
首先,在pom.xml中添加以下依赖项。
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
</dependencies>
定义application.yml如下。
spring:
application:
name: first-service
server:
port: 8081
创建一个控制器类,暴露GET REST服务。
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/employee")
public class FirstController {
@GetMapping("/message")
public String test() {
return "Hello in First Service";
}
}
用@SpringBootApplication注解创建bootstrap类。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class FirstApplication {
public static void main(String[] args) {
SpringApplication.run(FirstApplication.class, args);
}
}
在这里,我们将做同样的事情,另一个微服务。
实现第二个微服务。
首先,在pom.xml中添加以下依赖项。
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
</dependencies>
定义application.yml如下。
spring:
application:
name: second-service
server:
port: 8082
创建一个控制器类,暴露GET REST服务。
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/consumer")
public class SecondController {
@GetMapping("/message")
public String test() {
return "Hello in Second Service";
}
}
用@SpringBootApplication注解创建bootstrap类。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SecondApplication {
public static void main(String[] args) {
SpringApplication.run(SecondApplication.class, args);
}
}
使用基于属性的配置实现Spring Cloud Gateway。
首先,在pom.xml中添加以下依赖项。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
定义application.yml如下。
server:
port: 8080
spring:
cloud:
gateway:
routes:
- id: employeeModule
uri: http://localhost:8081/
predicates:
- Path=/employee/**
- id: consumerModule
uri: http://localhost:8082/
predicates:
- Path=/consumer/**
用@SpringBootApplication注解创建bootstrap类。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class APIGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(APIGatewayApplication.class, args);
}
}
现在,做完这些后,启动我们刚刚开发的三个微服务。
转到url - localhost:8080/employee/message。

转到url - localhost:8080/consumer/message

总结。
在这篇博客中,我们了解了SCG的基础知识,以及如何使用Spring Cloud Gateway实现API Gateway。现在你可以轻松地将SCG用于你的微服务。