Spring Cloud Gateway 是 Spring 官方基于 Spring 5.0,Spring Boot 2.0 和 Project Reactor 等技术开发的网关,Spring Cloud Gateway 旨在为微服务架构提供一种简单而有效的统一的 API 路由管理方式。Spring Cloud Gateway 作为 Spring Cloud 生态系中的网关,目标是替代 Netflix Zuul,其不仅提供统一的路由方式,并且基于 Filter 链的方式提供了网关基本的功能,例如:安全,监控,和限流等。
下图从总体上概述了Spring Cloud Gateway的工作方式:
客户端向 Spring Cloud Gateway 发出请求。然后在 Gateway Handler Mapping 中找到与请求相匹配的路由,将其发送到 Gateway Web Handler。Handler 再通过指定的过滤器链来将请求发送到我们实际的服务执行业务逻辑,然后返回。过滤器之间用虚线分开是因为过滤器可能会在发送代理请求之前(pre)或之后(post)执行业务逻辑。
下面以一个简单的例子展示Gateway的用法。完整代码看文章最后。
注意
:Spring Cloud Gateway 不使用 Web 作为服务器,而是 使用 WebFlux 作为服务器 (本质是通过netty的io多路复用通信),Gateway 项目已经依赖了 starter-webflux,所以这里不要依赖 starter-web。
org.springframework.cloud spring-cloud-starter-gateway
server:port: 7000spring:application:name: microservice-gatewaycloud:gateway:routes:- id: router1 # 路由的IDuri: http://localhost:9000/user/{id} # 匹配后路由地址predicates: # 断言, 路径相匹配的进行路由- Path=/user/{id}
网关匹配后跳转到改服务接口。
org.springframework.boot spring-boot-starter-web
server:port: 9000
@RestController
public class UserController {@GetMapping("/user/{id}")public String hello(@PathVariable String id){return "Hello, user"+ id;}
}
microservice-gateway
microservice-user
localhost:7000/user/2
https://gitee.com/indexman/microservice-learn
下一篇:blender UV基础