irpas技术客

微服务之GateWay_随心变化_微服务gateway

网络 4411

GateWay 1 基本知识1.1 诞生原因1.2 特征1.3 Spring Cloud Gateway 与 Zuul的区别1.4 微服务架构 2 GateWay构建步骤2.1 新建Module2.2 pom.xml2.3 application.yml2.4 主启动类2.5 业务类(无)2.6 9527网关如何映射2.7 yml新增网关配置2.8 测试 3 Gateway配置动态路由3.1 修改application.yml3.2 测试 4 常用的Predicate4.1 After4.2 Cookie4.3 Header


1 基本知识 1.1 诞生原因 基于Spring 5.0+Spring Boot 2.0和 Project Reactor等技术开发的网关,它旨在为 微服务架构提供一种简单有效的统一的API路由管理方式。SpringCloud Gateway 作为Spring Cloud 生态系统中的网关,目标是替代Zuul,在Spring Cloud 2.0以上版本中,没有对新版本的Zuul 2.0以上 最新高性能版本进行集成,仍然还是使用的Zuul 1.x非Reactor模式的老版本。而为了提升网关的性能,SpringCloud Gateway是基于WebFlux框架实现的,而WebFlux框架底层则使用了高性能的Reactor模式通信框架Netty。Spring Cloud Gateway的目标提供统一的路由方式且基于Filter 链的方式提供了网关基本的功能,例如:安全、监控、指标和限流。 1.2 特征 动态路由:能够匹配任何请求属性可以对路由指定Predicate(断言)和Filter(过滤器) ? web请求,通过一些匹配条件,定位到真正的服务节点。predicate就是我们的匹配条件;而filter,就可以理解为一个无所不能的拦截器。有了这两个元素,再加上目标uri,就可以实现一个具体的路由了集成Hystrix的断路器功能集成SpringCloud服务发现功能易于编写的Predicate(断言)和Filter(过滤器)请求限流功能支持路径重写 1.3 Spring Cloud Gateway 与 Zuul的区别

在SpringCloud Finchley正式版之前,Spring Cloud推荐的网关是Netflix提供的Zuul 1、Zuul 1.x,是一个基于阻塞 I/ 0 的 API Gateway 2、Zuul 1.x基于Servlet 2. 5使用阻塞架构它不支持任何长连接(如WebSocket) Zuul的设计模式和Nginx较像,每次I/ 0操作都是从工作线程中选择T执行,请求线程被阻塞到工作线程完成,但是差别是Nginx用C++实现,Zuul用Java实现,而JVM本身会有第一次加载较慢的情况,使得Zuul的性能相对较差。 3、Zuul 2.xl里念更先进,想基于Netty非阻塞和支持长连接,但SpringCloud目前还没有整合。Zuul 2.x的性能较Zuul 1.x有较大提升 o在性能方面,根据官方提供的基准测试,Spring Cloud Gateway的RPS (每秒请求数)是Zuul的1.6倍。 4、Spring Cloud Gateway 建立在 Spring Framework 5、Project Reactor 和 Spring Boot 2 之上,使用非阻塞 API。 5、Spring Cloud Gateway还支持WebSocket,并且与Spring紧密集成拥有更好的开发体验

1.4 微服务架构

2 GateWay构建步骤 2.1 新建Module

新建一个cloud-gateway的子模块,端口号为9527

2.2 pom.xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> 2.3 application.yml

注册进Eureka服务中心

server: port: 9527 spring: application: name: cloud-gateway datasource: type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型 #mysql5.x的没有cj driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: root123 eureka: instance: instance-id: gateway9527 #修改名称 prefer-ip-address: true #默认显示地址 client: #表示是否将自己注册进Eurekaserver默认为true。 register-with-eureka: true #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡 fetchRegistry: true service-url: defaultZone: http://localhost:7001/eureka #Eureka服务端的地址(单机版) #defaultZone: http://eureka-7010.com:7010/eureka, http://eureka-7011.com:7011/eureka #Eureka服务端的地址(集群版) 2.4 主启动类 @SpringBootApplication public class GatewayMain { public static void main(String[] args) { SpringApplication.run(GatewayMain.class); } } 2.5 业务类(无) 2.6 9527网关如何映射 cloud-provider-payment8089看看controller的访问地址我们目前不想暴露8089端口,希望在8089外面套一层9527 2.7 yml新增网关配置 cloud: gateway: routes: - id: payment_route1 #路由ID,没有固定规则但要求唯一,建议配合服务名 uri: http://localhost:8089 #匹配后提供服务的路由地址 predicates: - Path=/payment/get/** #断言,路径匹配的进行路由 - id: payment_route2 #路由ID,没有固定规则但要求唯一,建议配合服务名 uri: http://localhost:8089 #匹配后提供服务的路由地址 predicates: - Path=/payment/discovery #断言,路径匹配的进行路由

2.8 测试 启动cloud-eureka-server,端口号为7001启动cloud-payment-provider,端口号为8089启动cloud-gateway,端口号为9527 3 Gateway配置动态路由 3.1 修改application.yml

将网关gateway9527注册到Eureka7010和Eureka7011服务中心

cloud: gateway: discovery: locator: enabled: true # 开启从配置中心动态创建路由功能,利用微服务进行路由 routes: - id: payment_route1 #路由ID,没有固定规则但要求唯一,建议配合服务名 # uri: http://localhost:8089 #匹配后提供服务的路由地址 uri: lb://CLOUD-PAYMENT-SERVICE #匹配提供服务的路由地址 predicates: - Path=/payment/get/** #断言,路径匹配的进行路由 - id: payment_route2 #路由ID,没有固定规则但要求唯一,建议配合服务名 # uri: http://localhost:8089 #匹配后提供服务的路由地址 uri: lb://CLOUD-PAYMENT-SERVICE #匹配提供服务的路由地址 predicates: - Path=/payment/timeout #断言,路径匹配的进行路由 3.2 测试 启动cloud-eureka-server7010,端口号为7010启动cloud-eureka-server7011,端口号为7011启动cloud-payment-provider8090,端口号为8090启动cloud-payment-provider8091,端口号为8091启动cloud-gateway,端口号为9527 4 常用的Predicate 4.1 After predicates: - Path=/payment/timeout #断言,路径匹配的进行路由 - After=2022-04-25T14:25:09.342+08:00[Asia/Shanghai] #这条路由所有请求在2022年4月25日14:17(北京)之后可以访问。

如果在2022-04-25 14:25之前访问会报错如下

Mon Apr 25 14:23:35 CST 2022 [68cafdf] There was an unexpected error (type=Not Found, status=404). org.springframework.web.server.ResponseStatusException: 404 NOT_FOUND at org.springframework.web.reactive.resource.ResourceWebHandler.lambda$handle$1(ResourceWebHandler.java:408) Suppressed: The stacktrace has been enhanced by Reactor, refer to additional information below: Error has been observed at the following site(s): *__checkpoint ? org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter [DefaultWebFilterChain] *__checkpoint ? org.springframework.boot.actuate.metrics.web.reactive.server.MetricsWebFilter [DefaultWebFilterChain] *__checkpoint ? HTTP GET "/payment/discovery" [ExceptionHandlingWebHandler] 4.2 Cookie predicates: - Path=/payment/timeout #断言,路径匹配的进行路由 - Cookie=usr,password

4.3 Header

如果请求有一个名为X-Request-Id的报头,其值与\d+正则表达式匹配(也就是说,它的值为一个或多个数字),则此路由就匹配。

predicates: - Path=/payment/timeout #断言,路径匹配的进行路由 - Header=X-Request-Id, \d+


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #微服务gateway #Gateway1 #基本知识11 #诞生原因12 #特征13 #Spring #Cloud #gateway