Gateway服务网关
概念
spring cloud全家桶有个很重要的组件就是网关,在1.x版本中都是采用的Zuul网关,在2.x版本中,zuul的升级一直跳票,spring cloud最后自己研发了一个网关替代zuul,那就是spring cloud gateway。
spring cloud gateway是spring cloud的一个全新项目,基于spring 5.0+spring boot 2.0和project reactor等技术开发的网关,它旨在为微服务架构提供一种简单有效的统一的API路由管理方式。
spring cloud gateway作为spring cloud生态系统中的网关,目标是替代zuul,在spring cloud 2.0以上版本中,没有对新版本的zuul 2.0以上最新高性能版本进行集成,仍然使用的zuul 1.x非reactor模式的老版本。而为了提升网关性能,spring cloud gateway是基于webflux框架实现的,而webflux框架底层则使用了高性能的reactor模式通信框架netty。
spring cloud gateway的目标提供统一的路由方式且基于filter链的方式提供了网关基本的功能,例如:安全、监控/指标、限流。
功能:反向代理、鉴权、流量控制、熔断、日志监控。
spring cloud gateway具有如下特性:
- 基于spring framework 5,project reactor和spring boot 2.0进行构建;
- 动态路由:能够匹配任何请求属性;
- 可以对路由指定predicate(断言)和filter(过滤器);
- 集成hystrix的断路器功能;
- 集成spring cloud服务发现功能;
- 易于编写的predicate和filter;
- 请求限流功能;
- 支持路径重写。
传统的web框架,比如说:struts2,springmvc等都是基于servlet API与servlet容器基础上运行的。但是,在servlet 3.x之后有了异步非阻塞的支持。而webflux是一个典型非阻塞异步的框架,它的核心是基于reactor的相关API实现的。相对于传统的web框架来说,它可以运行在诸如netty、undertow及支持servlet 3.1的容器上。非阻塞式+函数式编程。
spring webflux是spring 5.0引入的新的响应式框架,区别于springmvc,它不需要依赖servlet API,它是完全异步非阻塞的,并且基于reactor来实现响应式流规范。
gateway核心概念
route(路由)
路由是构建网关的基本模块,它由ID,目标URI,一系列的断言和过滤器组成,如果断言为true则匹配该路由。
predicate(断言)
参考Java 8的java.util.function.Predicate,开发人员可以匹配HTTP请求中的所有内容(例如请求头或请求参数),如果请求与断言相匹配则进行路由。
filter(过滤)
指的是spring框架中GatewayFilter的实例,使用过滤器,可以在请求被路由前或者之后对请求进行修改。
web请求,通过一些匹配条件,定位到真正的服务节点。并在这个转发过程的前后,进行一些精细化控制。predicate就是匹配的条件;filter可以理解为一个拦截器,有了这两个元素,再加上目标uri,就可以实现一个具体的路由了。
搭建微服务网关
创建cloud-gateway-gateway9527模块
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>cloud2022</artifactId><groupId>com.qrxqrx.springcloud</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>cloud-gateway-gateway9527</artifactId><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>com.qrxqrx.springcloud</groupId><artifactId>cloud-api-commons</artifactId><version>${project.version}</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional><scope>runtime</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies></project>
application.yml
server:port: 9527
spring:application:name: cloud-gatewaycloud:gateway:routes:- id: payment_route # 路由的id,没有固定规则但是要求唯一,建议配合服务名uri: http://localhost:8001 # 匹配后提供服务的路由地址predicates:- Path=/payment/get/** # 断言,路径匹配进行路由- id: payment_route2 # 路由的id,没有固定规则但是要求唯一,建议配合服务名uri: http://localhost:8001 # 匹配后提供服务的路由地址predicates:- Path=/payment/lb/** # 断言,路径匹配进行路由eureka:instance:hostname: cloud-gateway-serviceclient:service-url:register-with-eureka: truefetch-registry: truedefaultZone: http://eureka7001.com:7001/eureka
GatewayMain9527
package com.qrxqrx.springcloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class GatewayMain9527 {public static void main(String[] args) {SpringApplication.run(GatewayMain9527.class,args);}}
Gateway网关路由的两种配置方式
- 在yml文件中配置(如上)
- 代码中注入RouteLocator的bean
GatewayConfig
package com.qrxqrx.springcloud.config;import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class GatewayConfig {@Beanpublic RouteLocator routes(RouteLocatorBuilder builder) {return builder.routes().route("path_route_qrx1",r -> r.path("/payment/get/**").uri("http://localhost:8001")).build();}}
配置动态路由
默认情况下,gateway会根据注册中心注册的微服务列表,以微服务名为路径创建动态路由进行转发,从而实现动态路由的功能。
server:port: 9527
spring:application:name: cloud-gatewaycloud:gateway:discovery:locator:enabled: true # 开启动态路由routes:- id: payment_route # 路由的id,没有固定规则但是要求唯一,建议配合服务名# uri: http://localhost:8001 # 匹配后提供服务的路由地址uri: lb://cloud-payment-service # 动态路由predicates:- Path=/payment/get/** # 断言,路径匹配进行路由- id: payment_route2 # 路由的id,没有固定规则但是要求唯一,建议配合服务名# uri: http://localhost:8001 # 匹配后提供服务的路由地址uri: lb://cloud-payment-service # 动态路由predicates:- Path=/payment/lb/** # 断言,路径匹配进行路由eureka:instance:hostname: cloud-gateway-serviceclient:service-url:register-with-eureka: truefetch-registry: truedefaultZone: http://eureka7001.com:7001/eureka
Predicate的使用
spring:application:name: cloud-gatewaycloud:gateway:discovery:locator:enabled: true # 开启动态路由routes:- id: payment_route # 路由的id,没有固定规则但是要求唯一,建议配合服务名# uri: http://localhost:8001 # 匹配后提供服务的路由地址uri: lb://cloud-payment-service # 动态路由predicates:- Path=/payment/get/** # 断言,路径匹配进行路由# ZonedDateTime zbj = ZonedDateTime.now();# System.out.println(zbj);- After=2022-06-30T16:17:33.876+08:00[Asia/Shanghai] # 在此时间之后访问才有效果- Cookie=name,qrx
# Cookie Route Predicate需要两个参数,一个是cookie name,一个是正则表达式。
# 路由规则会通过获取对应的cookie name值和正则表达式去匹配,如果匹配上就会执行路由,如果没有匹配上则不执行。
# 测试:curl http://localhost:9527/payment/lb --cookie "username=zzyy"- Header=X-Request-Id,\d+
# 两个参数:一个属性名称和一个正则表达式,属性值匹配则执行
# 测试:curl http://localhost:9527/payment/lb --H "X-Request-Id:123"- Host=www.qrxqrx.com
# 测试:curl http://localhost:9527/payment/lb --H "Host:www.qrxqrx.com"- Method=GET- Query=username,qrx
# 测试:curl http://localhost:9527/payment/lb?username=qrx
Filter的使用
路由过滤器可用于修改进入的HTTP请求和返回的HTTP响应,路由过滤器只能指定路由进行使用。
按生命周期分:
- pre
- post
按种类分: - GatewayFilter
- GlobalFilter
spring:application:name: cloud-gatewaycloud:gateway:discovery:locator:enabled: true # 开启动态路由routes:- id: payment_route # 路由的id,没有固定规则但是要求唯一,建议配合服务名# uri: http://localhost:8001 # 匹配后提供服务的路由地址uri: lb://cloud-payment-service # 动态路由predicates:- Path=/payment/get/** # 断言,路径匹配进行路由- id: payment_route2 # 路由的id,没有固定规则但是要求唯一,建议配合服务名# uri: http://localhost:8001 # 匹配后提供服务的路由地址uri: lb://cloud-payment-service # 动态路由predicates:- Path=/payment/lb/** # 断言,路径匹配进行路由filters:- AddRequestParameter=X-Request-Id,1024 # 过滤器工厂会在匹配的请求头加上一对请求头,名称为X-Request-Id,值为1024
自定义过滤器
package com.qrxqrx.springcloud.filter;import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;import java.util.Date;@Component
@Slf4j
public class MyLogGatewayFilter implements GlobalFilter, Ordered {@Overridepublic Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {log.info("----come in MyLogGatewayFilter----"+new Date());String uname = exchange.getRequest().getQueryParams().getFirst("uname");if (uname == null) {log.info("++++username is null! illegal!");exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);return exchange.getResponse().setComplete();}return chain.filter(exchange);}@Overridepublic int getOrder() {return 0;}
}
测试:http://localhost:9527/payment/lb?uname=xxx
Gateway服务网关相关推荐
- 【Spring Cloud Alibaba】Gateway 服务网关
[Spring Cloud Alibaba]Gateway 服务网关 1 架构图 2 Predicate 断言 3 路由 3.1 静态路由 3.2 动态路由 3.3 Nacos 配置 4 过滤器 4. ...
- 微服务01SpringCloud Eureka Ribbon Nacos Feign Gateway服务网关
微服务技术栈导学 SpringCloud01 1.认识微服务 随着互联网行业的发展,对服务的要求也越来越高,服务架构也从单体架构逐渐演变为现在流行的微服务架构.这些架构之间有怎样的差别呢? 1.0.学 ...
- Spring cloud Gateway 服务网关 实战
Spring cloud Gateway 服务网关 一.简介 优点: 特性: 总结: 二.核心概念 三.路由规则 1.Path 2.Query 3.Method 4.Datetime 5.Romote ...
- Spring Cloud入门-Gateway服务网关(Hoxton版本)
文章目录 Spring Cloud入门系列汇总 摘要 Gateway 简介 相关概念 创建 api-gateway模块 在pom.xml中添加相关依赖 两种不同的配置路由方式 使用yml配置 使用Ja ...
- 微服务笔记:第一章_微服务简介|Eureka注册中心|Nacos注册中心|Nacos配置管理|Feign|Gateway服务网关
微服务笔记:第一章_微服务简介|Eureka注册中心|Nacos注册中心|Nacos配置管理|Feign|Gateway服务网关 1. 微服务简介 1.1 服务架构演变 1.2 SpringCloud ...
- Spring Cloud —— Gateway 服务网关
导航 一.什么是服务网关 二.业界常见网关组件 三.Spring Cloud Gateway 四.Gateway 快速入门 4.1 创建 gateway 服务 4.2 添加 gateway 依赖和 n ...
- SpringCloud Gateway 服务网关,限流
SpringCloud Gateway 提供了基于Redis 和lua脚本实现的令牌桶算法进行限流,即 RequestRateLimiterGatewayFilterFactory类,通过设置过滤器实 ...
- SpringCloud Gateway 服务网关,断言
SpringCloud Gateway 是SpringCloud 生态系中的网关,不仅提供统一路由功能,通过 Filter 过滤链实现网关的基本功能,比如用户验证,鉴权,限流等功能.它使用非阻塞模式, ...
- Gateway服务网关使用教程
目录 1.为什么需要网关 2.gateway快速入门 1)创建gateway服务,引入依赖 2)编写启动类 3)编写基础配置和路由规则 4)重启测试 5)网关路由的流程图 3.断言工厂 4.过滤器工厂 ...
- 微服务架构-服务网关(Gateway)-服务网关在微服务中的应用
服务网关在微服务中的应用 我们将目光转向Spring Cloud应用的外围,讨论微服务架构下的各个模块如何对外提供服务. 1.对外服务的难题 微服务架构下的应用系统体系很庞大,光是需要独立部零的基础组 ...
最新文章
- pytorch实现BiLSTM+CRF用于NER(命名实体识别)
- 死磕单点登录的实现原理
- automake linux,Linux下automake软件编译与发布快速入门
- java 全半角转换_Java 全半角转换
- Crystal Report 2008
- Python-网站页面代码获取
- C# XmlReader
- TSC TTP-244条码打印机如何批量打印二维码
- JSONObject.fromObject(xx)方法执行后首字母大写变小写解决
- 字字珠玑,证监会84问小米如何反击?
- afp专用计算机,FRM考试只能用这些金融计算器(内含用法功能全解读)
- 原生js实现新年倒计时
- 实验室设备管理系统C语言——课程设计实习
- 电脑如何双开两个微信
- xss靶场练习(一)之xss.haozi.me
- Matlab系列教程_基础知识_绘图(一)
- POE供电 网线 电源 网络情况图
- 地表反射率影响因素_全了,高考地理答题要点归纳!
- html5手机移动端三级联动城市选择器,jquery移动端省市县(区)三级联动插件
- Win10系统双硬盘安装Ubuntu18.04心得
热门文章
- 苹果台式机怎么设置我的电脑计算机,怎么让台式电脑用苹果手机的wifi上网
- Flink报错 Could not instantiate the executor_ Make sure a planner module is on the classpath
- 解除宝塔面板强制绑定手机号教程
- 【支付宝商家中心】支付功能签约,产品签约失败;修改经营类目;系统检测到您的账户不符合国家相关法律法规或《支付宝用户服务协议》约定
- html二级页面怎么设置,网页中的二级页面和三级页面是什么啊?怎么做?
- c语言笛卡尔坐标系,CAD笛卡尔坐标系(直角坐标系),极坐标系精讲,看完秒会...
- python 经典图书排行榜_书榜 | 计算机书籍(5.4-5.10)销售排行榜
- 服务器上系统使用排行,服务器操作系统使用排行榜
- 线性布局和表格布局的嵌套使用
- 大模型落地实践:同花顺大模型技术应用及优化