gateway路由网关,zuul的替代品
zuul1 low, zuul2 还没长大呢,不敢用。
gateway 基于netty, spring flux, reactor 异步非阻塞, 快呀。
与spring良好整合, spring社区的呢。官方推荐。
https://spring.io/projects/spring-cloud-gateway
https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.1.RELEASE/reference/html/
gateway
- 特性
- 网关的三大核心对象
- 入门配置
- 断言配置
- 支持自定义过滤器
特性
Spring Cloud Gateway 使用的Webflux中的reactor-netty响应式编程组件,底层使用了Netty通讯框架.
1. 基于spring5.0 reactor, boot2.x
2. 能够转发所有请求的路由 反向代理
3. 路由支持断言和过滤器
4. 熔断
5. 服务发现 客户端
6. 简单断言和过滤器
7. 能限制请求
8. 根据路径转发日志监控.. 流量控制.. 鉴权..
网关在微服务的哪个地方, nginx后面就是。
网关的三大核心对象
路由Route
路由是构建网关的基本模块,它由ID,目标URI,一系列的断言和过滤器组成,如果断言为true则匹配该路由。
route --> 根据规则将请求转发到对应的微服务。
断言Predicate
参考的是java8的java.util.function.Predicate开发人员可以匹配HTTP请求中的所有内容(例如请求头或请求参数),如果请求与断言相匹配则进行路由。
可以设置对应的规则来设置断言。符合规则转发请求, 不符合可以做出对应处理。
过滤 filter
指的是Spring框架中GatewayFilter的实例,使用过滤器,可以在请求被路由前或者之后对请求进行修改。
java web 中的过滤器, 过滤器链, 懂吧。
入门配置
pom
<!--新增gateway 需要排除web和actuator 2.2.1.RELEASE --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency>
routes 下面可以配置多个路由路径。‘
使用yaml配置路由
# gateway --> 路由 断言 过滤器
server:port: 9527# 网关配置
spring:application:name: cloud-gatewaycloud:gateway:discovery:locator:enabled: true # 开启从注册中心获取动态路由的功能。利用微服务名进行路由。routes:- id: payment_routh1 # 路由的id 没有固定规则但是要求唯一 配合服务名称# uri: http://localhost:8001 # 匹配后提供服务的路由地址uri: lb://cloud-payment-service # 根据服务名进行路由predicates:- Path=/payment/get/** # 路径像匹配的进行断言~
使用bean的方式配置路由
package top.bitqian.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;/*** 网关规则bean配置* @author echo lovely* @date 2020/12/12 16:53*/@Configuration
public class GatewayConfig {// 可以配置多个bean, 根据规则跳转到不同的url@Beanpublic RouteLocator customerRouteLocator(RouteLocatorBuilder routeBuilder) {RouteLocatorBuilder.Builder routes = routeBuilder.routes();routes.route("adorable1",r -> r.path("/team"). // localhost:9527/teamuri("https://github.com/team")). // to teambuild();return routes.build();}@Beanpublic RouteLocator customerRouteLocator1(RouteLocatorBuilder routeBuilder) {RouteLocatorBuilder.Builder routes = routeBuilder.routes();routes.route("adorable2",r -> r.path("/explore"). // localhost:9527/exploreuri("https://github.com/explore")). // to explorebuild();return routes.build();}}
断言配置
https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.1.RELEASE/reference/html/#gateway-request-predicates-factories
支持cookie, 请求头, 请求方法,请求参数, 请求时间…
可以本地使用curl来测试, 像linux curl localhost:8080
# gateway --> 路由 断言 过滤器
server:port: 9527# 网关配置
spring:application:name: cloud-gatewaycloud:gateway:discovery:locator:enabled: true # 开启从注册中心获取动态路由的功能。利用微服务名进行路由。routes:- id: payment_routh1 # 路由的id 没有固定规则但是要求唯一 配合服务名称# uri: http://localhost:8001 # 匹配后提供服务的路由地址uri: lb://cloud-payment-service # 根据服务名进行路由predicates:- Path=/payment/get/** # 路径像匹配的进行断言~- id: payment_routh2 # 路由的id 没有固定规则但是要求唯一 配合服务名称# uri: http://localhost:8001 # 匹配后提供服务的路由地址uri: lb://cloud-payment-servicepredicates:- Path=/payment/lb/** # 路径像匹配的进行断言~# 新增断言配置# https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.1.RELEASE/reference/html/#gateway-request-predicates-factories- After=2020-12-13T11:37:03.340+08:00[Asia/Shanghai] # 必须在这个时间之后# - Cookie=username, adorable # 测试 curl --cookie "username=adorable" localhost:9527/payment/lb# - Header=X-Request-Id, \d+ # 属性必须是整数 curl --header "X-Request-Id:99" localhost:9527/payment/lb# - Query=age # curl localhost:9527/payment/lb?age=19# - Host=**.adorable.cn,**.adorable.top # curl localhost:9527/payment/lb -H "host:bitqian.lovely.adorable.top"# gateway 入驻eureka~
eureka:instance:hostname: cloud-gateway-serviceclient:service-url:register-with-eureka: truefetch-registry: truedefaultZone: http://eureka7001.com:7001/eureka
支持自定义过滤器
package top.bitqian.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;/*** 网关自定义过滤器配置* 官方有两种过滤器, 单一的和全局的。* @author echo lovely* @date 2020/12/13 14:37*/@Component
@Slf4j
public class MyGatewayFilter implements GlobalFilter, Ordered { // global filter...@Overridepublic Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {log.info("=======================gateway进来了======" + new Date());// 获取到请求中的参数String username = exchange.getRequest().getQueryParams().getFirst("username");if (username == null) {log.info("/(ㄒoㄒ)/~~ 用户名为空了,非法的用户名称......");// 406 不被服务器接受的exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);// 返回monoreturn exchange.getResponse().setComplete();}// 放行return chain.filter(exchange);}@Overridepublic int getOrder() {// 数字越小, 越优先return 0;}
}
总之网关作为分布式,微服务架构 在安全,限流,日志记录方面具有强大的功能。
gateway路由网关,zuul的替代品相关推荐
- 第九章:路由网关(Zuul)的使用
2019独角兽企业重金招聘Python工程师标准>>> 第九章:路由网关(Zuul)的使用 前言 介绍完分布式配置中心,结合前面的文章.我们已经有了一个微服务的框架了,可以对外提供a ...
- Gateway 路由网关
Gateway 路由网关 1-Gateway 概述 1.1-是什么 1.2-能干嘛 1.3-Why Gateway 2-Gateway 核心 2.1-核心概念 2.2-工作流程 3-Gateway 配 ...
- 史上最简单的SpringCloud教程 | 第五篇: 路由网关(zuul)
转:https://blog.csdn.net/forezp/article/details/69939114 最新版本: 史上最简单的SpringCloud教程 | 第五篇: 路由网关(zuul)( ...
- SpringCloud之 Gateway路由网关
文章目录 Gateway 路由网关 一.部署网关
- Spring Gateway路由网关
文章目录 简介 routes 路由服务 配置文件方式 Java DSL方式 predicates 路由断言 filters 过滤功能 自定义过滤器 简介 https://mp.weixin.qq.co ...
- springcloud 之 路由网关 zuul
话不多说,先上图(图是丑陋了点,大家别介意哈),这幅图是我根据公司现有的业务设计出来的一个简单的基于springcloud微服务架构图. 在springcloud微服务体系中,我们一般不对外直接暴露服 ...
- 【微服务架构】SpringCloud之路由网关(zuul)
什么是zuul zuul 是netflix开源的一个API Gateway 服务器, 本质上是一个web servlet应用. Zuul 在云平台上提供动态路由,监控,弹性,安全等边缘服务的框架.Zu ...
- GateWay路由网关的概述与入门详解
一.概述简介 官网:https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.1.RELEASE/reference/h ...
- 企业分布式微服务云SpringCloud SpringBoot mybatis (五)路由网关(zuul)
在微服务架构中,需要几个基础的服务治理组件,包括服务注册与发现.服务消费.负载均衡.断路器.智能路由.配置管理等,由这几个基础组件相互协作,共同组建了一个简单的微服务系统.一个简答的微服务系统如下图: ...
最新文章
- 浮动布局会受父框滚动条影响
- 领域驱动设计,为何死灰复燃?
- 基于飞桨PaddlePaddle的语义角色标注任务全解析
- 工信部:筹建全国首个区块链和分布式记账标准化技术委员会
- cf1555C Coin Rows
- 通信调制体制设计之64QAM性能分析MATLAB仿真及代码
- python网络爬虫与信息提取视频_Python网络爬虫与信息提取入门5
- HTML5新特性基础学习笔记上
- python像素处理_用python处理图片实现图像中的像素访问
- 14-Arco初次见到毛球
- 7-3 高空坠球 (20 分)
- 【Flutter】Dart中的Mixins混入你知道是什么吗?
- 设计趋势:网页之粗粝设计风格
- Navicat Premium 12注册码与破解解决方案
- 多元逻辑回归 - 小技巧(一)
- 计算机启动到安全模式,电脑正常开机会进入安全模式怎么办
- GitHub使用官网指南之Hello World
- 【python】使用py3-bencode打开torrent文件
- 名侦探柯南之零的执行人
- 基于asp.net028住院部病人管理系统
热门文章
- shiro学习(5):ini文件和自定义realm
- spring学习(7):加入log4g日志系统
- java学习(87):Interage包装类进制转换
- [机器学习笔记]Note5--归一化
- a标签点击事件_DOM事件机制
- saltstack mysql_saltstack学习五:return及入库_MySQL
- 微服务跨数据库联合查询_MySQL数据库联合查询
- cacti安装配置详解_MySQL实战001:8.0免安装版服务配置详解
- php用ajax方式实现四级联动
- 如何进行正确的SQL性能优化