Spring Cloud Gateway HELLO
Spring Cloud Gateway介绍
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链的方式提供了网关基本的功能,例如:安全,监控/埋点,和限流等。
可能有同学就要问了,不是已经有Zuul了吗,为什么又搞了一个网关,这更新的节奏确实很快哈,没精力还真学习不过来。
之所以新搞了一个网关,是因为Zuul基于servlet 2.5 (works with 3.x),使用阻塞API。它不支持任何长期的连接,如websocket。
Gateway建立在Spring Framework 5,Project Reactor和Spring Boot 2上,使用非阻塞API。支持Websockets,因为它与Spring紧密集成,所以它会是一个让开发者有更好体验的框架。当然性能的提升是肯定的,不然完全没必要重新搞一个啊,只不过Zuul2出来的太迟了,自己已经搞了一个,所以不太可能会将Zuul2集成到Spring Cloud中了。
关于性能这块的比较可以参考我周兄的文章《纠错帖:Zuul & Spring Cloud Gateway & Linkerd性能对比》
工作原理
工作原理
如上图所示,客户端发送请求到Spring Cloud Gateway,Gateway Handler Mapping确定请求与路由匹配,则会将请求交给Gateway Web Handler处理。
在代理前后可以执行多个过滤器。最后代理到具体的服务。
创建项目集成Gateway
首先还是最基本的步骤,创建一个Maven项目,添加Gateway需要的依赖信息:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
</dependencies>
编写启动类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 网关启动入口
*
* @author yinjihuan
*
* @about http://cxytiandi.com/about
*
*/
@SpringBootApplication
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
简单路由示列
下面来实现一个最简单的转发功能,基于Path的匹配转发功能。
在resources下面建一个application.yml的文件, 内容如下:
server:
port: 8084
spring:
cloud:
gateway:
routes:
- id: path_route
uri: http://cxytiandi.com
predicates:
- Path=/course
当你访问http://localhost:8084/course的时候就会转发到http://cxytiandi.com/course,效果如下:
课程列表
关于路由规则什么的我们后面再做介绍,本章只是先体验下Spring Cloud Gateway的功能,能够创建一个新的项目,成功启动就可以了,一步步来。
如果你的项目中包含了spring-cloud-starter-gateway,但你不想启动网关的时候可以通过下面的配置禁用掉:
application.properties
spring.cloud.gateway.enabled=false.
application.yml
spring:
cloud:
gateway:
enabled: false
上面讲解的是基于配置的方式来实现路由,还有一种方式是通过代码的方式来进行路由,比如:
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route(r -> r.path("/course").uri("http://cxytiandi.com"))
.build();
}
示列代码:https://github.com/yinjihuan/spring-cloud/tree/master/fangjia-gateway
作者:尹吉欢
链接:https://www.jianshu.com/p/71d7a70a0c7b
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
推荐阅读
-
将Spring Boot应用程序绑定到Cloud Foundry中的服务的方法
-
Spring Cloud分布式微服务实战,养成应对复杂业务的综合技术能力
-
浅谈如何在项目中使用Spring Cloud Alibaba Sentinel组件
-
Spring Cloud OAuth2中/oauth/token的返回内容格式
-
spring cloud config和bus组件实现自动刷新功能
-
Spring Cloud Alibaba系列-第四节-创建生产者与消费者服务,注册到Nacos监控服务
-
10.Spring Cloud Alibaba Nacos配置中心
-
Spring-Cloud-Filter(过滤器)
-
gRPC在Spring Cloud中的应用
-
浅谈Spring Cloud下微服务权限方案