网站底部模板,建立网站需要多少钱?,企业网站开发总结,网站开发 英语词汇首先先贴上Spring Cloud Gateway官网地址 Spring Cloud Gateway官网 Spring Cloud Gateway 网关背景简介基本介绍网关在微服务架构中的位置#xff1a;Spring Cloud Gateway 特点#xff1a; Gateway的三大核心概念Route#xff08;路由#xff09;Predicate#xff08;断…首先先贴上Spring Cloud Gateway官网地址 Spring Cloud Gateway官网 Spring Cloud Gateway 网关背景简介基本介绍网关在微服务架构中的位置Spring Cloud Gateway 特点 Gateway的三大核心概念Route路由Predicate断言/谓词Filter过滤 GateWay 工作流程Gateway配置 网关
背景简介
Spring Cloud全家桶中一个重要的组件就是网关一代网关Zuul迟迟不更新Spring Cloud自己研发了一个网关(Spring Cloud GateWay)代替Zuul。
基本介绍
网关在微服务架构中的位置 官网关于Gateway2.2.6版本具体文档https://docs.spring.io/spring-cloud-gateway/docs/2.2.6.RELEASE/reference/html/ Spring Cloud GateWay 是基于WebFlux框架 使用Reactor模式 而WebFlux框架底层使用的Netty。
Spring Cloud Gateway 特点
Built on Spring Framework 5, Project Reactor and Spring Boot 2.0 基于 Spring Framework 5、Project Reactor 和 Spring Boot 2.0 构建Able to match routes on any request attribute. 能够匹配任何请求属性的路由Predicates and filters are specific to routes. 谓词和过滤器特定于路由Circuit Breaker integration. 断路器集成。Spring Cloud DiscoveryClient integration Spring Cloud Discovery客户端集成Easy to write Predicates and Filters 易于编写谓词和过滤器Request Rate Limiting 请求速率限制Path Rewriting 路径重写
Gateway的三大核心概念
Route路由
路由是构建网关的基本模块它有id目标url以及一系列的谓词过滤器组成。
Predicate断言/谓词
Predicate(断言)又称谓词用于条件判断只有断言结果都为真才会真正的执行路由。断言其本质就是定义路由转发的条件。 这个东西其实可以看成一个布尔表达式如果为真就啥都不干放你过去。如果为假即断言不成立就不让你过去。 Predicate 内置工厂
-Path/nacos/provider/echo/** 当返回值为true 运行过滤器 请求路径-After2021-01-28T23:59:59.78908:00[Asia/Shanghai] 请求时间-HeaderX-Request-Id, \d 请求头-Methodget 请求类型-QuerypageSize,\d 请求参数
Filter过滤
过滤器(Filter)就是在请求传递过程中对请求和响应做一个处理。Gateway的Filter从作用范围可分为两种GatewayFilter与GlobalFilter。其中
GatewayFilter应用到单个路由或者一个分组的路由上。GlobalFilter应用到所有的路由上(例如负载均衡过滤器请求转发过滤器等)。 全局过滤器 局部过滤器 任何请求进来网关都会把它们拦住。根据请求的URI把它们分配到不同的路由上路由上面会有断言(谓词)来判断请求能不能进来。进来之后会有一系列的过滤器对请求被转发前或转发后进行改动。
GateWay 工作流程 客户端向Gateway发出请求。然后Gateway Handler Mapping找到与请求相匹配的路由将其发送给Gateway Web Handler。Handler再通过指定的过滤器链来将请求发送到我们实际的服务执行业务逻辑然后返回。过滤器之间用虚线分开是因为过滤器可能会在发送代理请求之前或之后执行业务逻辑。Filter在请求之前可以做参数校验权限校验流量监控日志输出协议转换等等在请求之后可以做响应内容、响应头的修改日志输出流量监控等.
Gateway配置
在pom.xml中添加依赖 dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-gateway/artifactId/dependencydependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-discovery/artifactId/dependencyyml配置文件添加配置
server:port: 9000
spring:application:name: sca-gatewaycloud:nacos:discovery:server-addr: localhost:8848gateway: #http://localhost:9000/nacos/provider/echo/aadiscovery:locator:enabled: true #开启通过服务注册中心的serviceId创建路由routes: #路由元素此元素下可以定义多个路由- id: router01 #路由id每个路由都会有一个唯一标识
# uri: http://localhost:8081 #网关要基于路由转发的路径http://localhost:8081/nacos/provider/echo/aauri: lb://sca-provider #lb是负载均衡的意思底层负载均衡的实现还是Ribbonpredicates: #断言谓词这里定义请求转发规则- Path/nacos/provider/echo/** #当返回值为true 运行过滤器 请求路径
# - After2021-01-28T23:59:59.78908:00[Asia/Shanghai] #请求时间
# - HeaderX-Request-Id, \d #请求头
# - Methodget #请求类型
# - QuerypageSize,\d #请求参数filters: #过滤器特殊的拦截器写到这个位置的是局部过滤器 全局过滤器GlobalFilter- StripPrefix1 #去除path中的第一层目录 http://localhost:9000/provider/echo/aa#logging:
# level:
# org.springframework.cloud.gateway: debug主启动类
SpringBootApplication
public class GatewayApplication {public static void main(String[] args) {SpringApplication.run(GatewayApplication.class, args);}
}测试 通过Gateway网关 访问 8081 端口的微服务成功。
---------------------未完待续