创意上海专业网站建设,阜阳市建设工程质量检测站网站,洛阳电商网站建设公司排名,懂网络维护和网站建设的专业文章目录 1.统一网关介绍2.网关开发3.predicate4.Route Predicate Factories(路由断言工厂)4.1Path 路由断言工厂4.2.Method 路由断言工厂4.3 Header 路由断言工厂4.4 Query 路由断言工厂4.5 Host 路由断言工厂4.6 After 路由断言工厂4.7 Before 路由断言工厂4.8 Between 路由断… 文章目录 1.统一网关介绍2.网关开发3.predicate4.Route Predicate Factories(路由断言工厂)4.1Path 路由断言工厂4.2.Method 路由断言工厂4.3 Header 路由断言工厂4.4 Query 路由断言工厂4.5 Host 路由断言工厂4.6 After 路由断言工厂4.7 Before 路由断言工厂4.8 Between 路由断言工厂 5.Gateway Filter Factories (网关过滤器工厂)5.1 The AddRequestHeader GatewayFilter Factory5.2 The AddRequestParameter GatewayFilter Factory5.3 The AddResponseHeader GatewayFilter Factory5.4 The DedupeResponseHeader GatewayFilter Factory5.5 Spring Cloud CircuitBreaker GatewayFilter Factory5.6 The FallbackHeaders GatewayFilter Factory5.7 自定义GatewayFilter 6.限流 - 限制流量7.GlobalFilter8.服务部署流程 大家好我是晓星航。今天为大家带来的是 统一网关-Gateway 相关的讲解
1.统一网关介绍
Spring Cloud Gateway是 Spring Cloud 的一个全新项目,基于 Spring 6.0Spring Boot 3.0和 Project Reactor 等技术开发的网关它旨在为微服务架构提供一种简单有效的统一的 API路由管理方式。 网关核心功能
1.权限控制
2.动态路由
3.负载均衡
4.限流
2.网关开发
1.创建项目 2.引入网关相关依赖 3.写启动类 4.配置文件 server:port: 10030
spring:application:name: gatewaycloud:nacos:discovery:server-addr: 110.41.51.65:10020gateway:metrics:enabled: trueroutes:- id: order-service #路由规则id, 随便起, 不重复即可uri: lb://order-service/ #目标服务地址predicates: #路由条件- Path/order/**,/feign/**- After2024-03-20T00:00:22.37085670008:00[Asia/Shanghai]filters:- AddRequestParameteruserName, bite- name: Custom #过滤器名称args:name: test_custom- id: product-serviceuri: lb://product-service/predicates:- Path/product/**default-filters:- name: Retryargs:retries: 3statuses: BAD_GATEWAY
management:endpoints:web:exposure:include: *
# endpoint:
# health:
# show-details: always
# shutdown:
# enabled: true5.测试 3.predicate
在 Spring Cloud 中Predicate 是指用于路由断言Route Predicate的一种组件。Spring Cloud 中的路由Routing功能通常用于服务网关如 Spring Cloud Gateway 或 Zuul它允许根据请求的特定条件例如路径、方法、头部信息等将请求路由到不同的目标服务。
基础写法 匿名内部类 lambda表达式 negate 非 or 判断字符串为 aa或者bb and 字符串不为空,且由数字组成,比如 “12”, “34” 4.Route Predicate Factories(路由断言工厂) 这里便是我们路由断言的作用例如上面条件为要在2024.03.20日后发送才会有相应的效果
4.1Path 路由断言工厂
根据请求路径匹配路由。
spring:cloud:gateway:routes:- id: path_routeuri: http://httpbin.orgpredicates:- Path/foo/** # 匹配所有以 /foo/ 开头的请求4.2.Method 路由断言工厂
根据 HTTP 请求方法匹配路由。
spring:cloud:gateway:routes:- id: method_routeuri: http://httpbin.orgpredicates:- MethodGET # 匹配所有 GET 请求4.3 Header 路由断言工厂
根据请求头的值匹配路由
spring:cloud:gateway:routes:- id: header_routeuri: http://httpbin.orgpredicates:- HeaderX-Request-Id, \d # 匹配请求头 X-Request-Id 存在且值为数字的请求4.4 Query 路由断言工厂
根据查询参数匹配路由
spring:cloud:gateway:routes:- id: query_routeuri: http://httpbin.orgpredicates:- QueryParamfoo, bar # 匹配查询参数 foo 的值为 bar 的请求4.5 Host 路由断言工厂
根据请求的 Host 头部信息匹配路由
spring:cloud:gateway:routes:- id: host_routeuri: http://httpbin.orgpredicates:- Host**.example.com # 匹配所有以 example.com 结尾的主机名4.6 After 路由断言工厂
根据请求时间在指定时间之后匹配路由
spring:cloud:gateway:routes:- id: after_routeuri: http://httpbin.orgpredicates:- After2023-01-20T17:42:47.789-07:00[America/Denver] # 匹配指定时间之后的请求4.7 Before 路由断言工厂
根据请求时间在指定时间之前匹配路由
spring:cloud:gateway:routes:- id: before_routeuri: http://httpbin.orgpredicates:- Before2023-01-20T17:42:47.789-07:00[America/Denver] # 匹配指定时间之前的请求4.8 Between 路由断言工厂
根据请求时间在两个时间之间匹配路由
spring:cloud:gateway:routes:- id: between_routeuri: http://httpbin.orgpredicates:- Between2023-01-20T17:42:47.789-07:00[America/Denver],2023-01-21T17:42:47.789-07:00[America/Denver] # 匹配两个时间之间的请求5.Gateway Filter Factories (网关过滤器工厂)
5.1 The AddRequestHeader GatewayFilter Factory
spring:cloud:gateway:routes:- id: add_request_header_routeuri: https://example.orgpredicates:- Path/red/{segment}filters:- AddRequestHeaderX-Request-Red, Blue-{segment}添加头信息X-Request-Redvalue为Blue-{segment}segment是路径里面带的信息
5.2 The AddRequestParameter GatewayFilter Factory
spring:cloud:gateway:routes:- id: add_request_parameter_routeuri: https://example.orgpredicates:- Host: {segment}.myhost.orgfilters:- AddRequestParameterfoo, bar-{segment}添加参数namefoo,valuebar-{segment}
5.3 The AddResponseHeader GatewayFilter Factory
spring:cloud:gateway:routes:- id: add_response_header_routeuri: https://example.orgpredicates:- Host: {segment}.myhost.orgfilters:- AddResponseHeaderfoo, bar-{segment}5.4 The DedupeResponseHeader GatewayFilter Factory
spring:cloud:gateway:routes:- id: dedupe_response_header_routeuri: https://example.orgfilters:- DedupeResponseHeaderAccess-Control-Allow-Credentials Access-Control-Allow-Origin当网关CROS和下游响应头都有Access-Control-Allow-Credentials 和 Access-Control-Allow-Origin时将删除重复的响应头
5.5 Spring Cloud CircuitBreaker GatewayFilter Factory
使⽤ Spring Cloud CircuitBreaker的API将⽹关路由包装到断路器中。Spring Cloud CircuitBreaker ⽀持多种库⽤于Spring Cloud Gateway。⽐如Resilience4J。 要启⽤Spring Cloud CircuitBreaker过滤器你需要引⼊spring-cloud-starter-circuitbreaker-reactor-resilience4j ,如下是配置示例
spring:cloud:gateway:routes:- id: circuitbreaker_routeuri: lb://backing-service:8088predicates:- Path/consumingServiceEndpointfilters:- name: CircuitBreakerargs:name: myCircuitBreakerfallbackUri: forward:/inCaseOfFailureUseThis- RewritePath/consumingServiceEndpoint, /backingServiceEndpoint5.6 The FallbackHeaders GatewayFilter Factory
spring:cloud:gateway:routes:- id: ingredientsuri: lb://ingredientspredicates:- Path//ingredients/**filters:- name: CircuitBreakerargs:name: fetchIngredientsfallbackUri: forward:/fallback- id: ingredients-fallbackuri: http://localhost:9994predicates:- Path/fallbackfilters:- name: FallbackHeadersargs:executionExceptionTypeHeaderName: Test-HeaderThe FallbackHeaders GatewayFilter Factory 在该例中在运⾏circuit breaker 发生异常后请求将被转发到 http://localhost:9994 的 /fallback 中。异常类型、消息等通过 FallbackHeaders 过滤器添加到请求头中。
5.7 自定义GatewayFilter
过滤器的代码逻辑 6.限流 - 限制流量
限流算法
1.固定窗口 2.滑动窗口 滑动窗口 也有小问题例如我们应该多久滑动一次呢是一分钟还是10秒钟呢
3.漏桶算法 请求类似于生产者
请求处理速度类似消费者
露桶类似于队列
应激流量突然出现的大量流量。
4.令牌算法 限流实现
7.GlobalFilter
GlobalFilter是Spring Cloud Gateway中的全局过滤器,它和GatewayFilter的作用是相同的.
GlobalFilter 会应用到所有的路由请求上,全局过滤器通常用于实现与安全性,性能监控和日志记录等相关的全局功能.
SpringCloud Gateway 内置的全局过滤器也有很多,比如:
Gateway Metrics Filter: 网关指标,提供监控指标Forward Routing Filter: 用于本地forword,请求不转发到下游服务器LoadBalancer Client Filter:针对下游服务,实现负载均衡…
更多过滤器参考: Global Filters
8.服务部署流程
1.确认配置
2.打包, 上传包到服务器
3.启动服务
4.开启端口号
5.测试
具体部署课看博主javaee初级篇文章 - Linux基本使用与部署
ateway中的全局过滤器,它和GatewayFilter的作用是相同的.
GlobalFilter 会应用到所有的路由请求上,全局过滤器通常用于实现与安全性,性能监控和日志记录等相关的全局功能.
SpringCloud Gateway 内置的全局过滤器也有很多,比如:
Gateway Metrics Filter: 网关指标,提供监控指标Forward Routing Filter: 用于本地forword,请求不转发到下游服务器LoadBalancer Client Filter:针对下游服务,实现负载均衡…
更多过滤器参考: Global Filters
感谢各位读者的阅读本文章有任何错误都可以在评论区发表你们的意见我会对文章进行改正的。如果本文章对你有帮助请动一动你们敏捷的小手点一点赞你的每一次鼓励都是作者创作的动力哦