SpringCloudAliBaba篇之gateway:手把手教你搭建服务网关(上)

简介: SpringCloudAliBaba篇之gateway:手把手教你搭建服务网关

1、什么是Spring Cloud Gateway

  • 网关作为流量的入口,常用的功能包括路由转发,权限校验,限流等
  • Spring Cloud Gateway是Spring Cloud 官方提出的第二代网关框架,定位于取代Netflix Zuul1.0,相比Zuul来说,Spring Cloud Gateway提供更优秀的性能,更强大的功能。
  • Spring Cloud Gateway 是由WebFlux + Netty + Reactor 实现的响应式的API网关,它不能在传统的servlet容器中工作,也不能构建war包
  • Spring Cloud Gateway 旨在为微服务架构提供一种简单且有效的API路由的管理方式,并基于Filter的方式提供网关的基本功能,例如说安全认证、监控、限流等等。

1.2、Spring Cloud Gateway功能特征

  • 基于Spring Framework 5,Project Reactor和Spring Boot 2.0进行构建;
  • 动态路由: 能够匹配任何请求属性;
  • 支持路径重写
  • 集成Spring Cloud服务发现功能(Nacos,Eruka)
  • 可集成流控降级功能(Sentinel、Hystrix);
  • 可以对路由指定易于编写的Predicate(断言)和Filter(过滤器)

1.3、核心概念

  • 路由(route)

路由是网关中最基础的部分,路由信息包括一个ID,一个目的URL、一组断言工厂、一组Filter组成,如果断言为真,则说明请求的URL和配置的路由匹配

  • 断言(predicates)

Java8中的断言函数,SpringCloud Gateway中的断言函数类型就是Spring5 框架中的ServerWebExchange.断言函数允许开发者去定义匹配Http request中的任何信息,比如请求头和参数等,

  • 过滤器(Filter)

SpringCloud Gateway中的filter分为Gateway Filter和Global Filter,Filter可以对请求和响应进行处理。

2、Gateway初体验

2.1、Gateway快速开始

1、引入依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

2、application.yml

server:
  port: 8889
spring:
  application:
    name: api-gateway
  cloud:
    gateway:
      routes:
        - id: order_route     #路由的唯一标识,路由到order
          uri: http://localhost:8004 # 需要转发的地址
          # 断言规则 用于路由规则的匹配
          predicates:
            - Path=/order-server/**
          # 转发之前去掉第一层路径
          filters:
            - StripPrefix=1

浏览器进行访问http://localhost:8889/order-server/order/orderList即可转发到你的http://localhost:8004/order/orderList

3、Gateway整合nacos

增加一个依赖

<!--nacos依赖-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

修改yml文件

server:
  port: 8889
spring:
  application:
    name: api-gateway
  cloud:
    gateway:
      routes:
        - id: order_route    
          uri: lb://order-server #lb表示使用nacos本地的负载均衡策略
          predicates:
            - Path=/order-server/**
          filters:
            - StripPrefix=1
    #配置nacos        
    nacos:
      discovery:
        server-addr: 127.0.0.1:8847
        username: nacos
        password: nacos

技巧:断言和过滤路由简写方式

yml编写方式

server:
  port: 8889
spring:
  application:
    name: api-gateway
  cloud:
    nacos:
      discovery:
        server-addr: 120.0.0.1:8847
        username: nacos
        password: nacos
    gateway:
      discovery:
        locator:
          enabled: true #自动识别nacos上的服务

运行即可,发现我们服务可以正常访问。

4、内置断言工厂

官网地址:

https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#the-addrequestheader-gatewayfilter-factory

4.1、基于Datetime类型

此类型断言根据时间做判断,主要由三个:

AfterRoutePredicateFactory:接受一个日期参数,判断请求日期是否晚于指定日期

BeforeRoutePredicateFactory:接受一个日期参数,判断请求日期是否早于指定日期

BetweenRoutePredicateFactory:接受一个日期参数,判断请求日期是否在指定时间段内

案例一(晚于指定日期)

spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: https://example.org
        predicates:
        - After=2017-01-20T17:42:47.789-07:00[America/Denver]

案例二(早于指定日期)

spring:
  cloud:
    gateway:
      routes:
      - id: before_route
        uri: https://example.org
        predicates:
        - Before=2017-01-20T17:42:47.789-07:00[America/Denver]

案例三(指定日期之间)

spring:
  cloud:
    gateway:
      routes:
      - id: between_route
        uri: https://example.org
        predicates:
        - Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]

4.2、基于远程地址

RemoteAddrRoutePredicateFactory: 接收一个ip地址段,判断请求主机是否在地址端中

- RemoteAddr=192.168.1.1/24

4.3、基于Cookie

CookieRoutePredicateFactory: 接收两个参数,cookie名字和一个正则表达式,判断请求cookie是否具有给定名称且与正则表达式匹配

- Cookie=chocolate, ch.p

4.4、基于Header

HeaderRoutePredicateFactory: 接收两个参数,标题名称和一个正则表达式,判断请求Header是否具有给定名称且与正则表达式匹配。

- Header=X-Request-Id, \d+

4.5、基于Host

HostRoutePredicateFactory: 接收一个参数,主机名模式。判断请求的Host是否匹配规则

- Host=**.somehost.org,**.anotherhost.org

4.6、基于Method请求方法

MethodRoutePredicateFactory: 接收一个参数,判断请求类型是否跟指定的类型匹配

- Method=GET,POST

4.7、基于Path匹配请求路径

{segment}:需要携带参数

- Path=/red/{segment},/blue/{segment}

例: /red/1 or /red/aaa or /blue/red

4.8、基于weight权重

spring:
  cloud:
    gateway:
      routes:
      - id: weight_high
        uri: https://weighthigh.org
        predicates:
        - Weight=group1, 8
      - id: weight_low
        uri: https://weightlow.org
        predicates:
        - Weight=group1, 2

4.9、基于请求参数

- Query=green

例入: ?green=121

- Query=red, gree.

例入:?red=gree,值需要匹配后面的正则

目录
相关文章
|
5天前
|
SpringCloudAlibaba 负载均衡 Java
【微服务 SpringCloudAlibaba】实用篇 · Gateway服务网关
【微服务 SpringCloudAlibaba】实用篇 · Gateway服务网关
15 0
|
5天前
|
JSON SpringCloudAlibaba Cloud Native
SpringCloudAlibaba:4.3云原生网关higress的JWT 认证
SpringCloudAlibaba:4.3云原生网关higress的JWT 认证
15 1
|
5天前
|
SpringCloudAlibaba Cloud Native 安全
SpringCloudAlibaba:4.2云原生网关higress的基本使用
SpringCloudAlibaba:4.2云原生网关higress的基本使用
14 0
|
5天前
|
SpringCloudAlibaba Cloud Native Docker
SpringCloudAlibaba:4.1云原生网关higress的搭建
SpringCloudAlibaba:4.1云原生网关higress的搭建
27 1
|
5天前
|
负载均衡 Java API
构建高效微服务架构:API网关与服务熔断策略
【5月更文挑战第2天】 在微服务架构中,确保系统的高可用性与灵活性是至关重要的。本文将深入探讨如何通过实施有效的API网关和设计合理的服务熔断机制来提升分布式系统的鲁棒性。我们将分析API网关的核心职责,包括请求路由、负载均衡、认证授权以及限流控制,并讨论如何利用熔断器模式防止故障传播,维护系统的整体稳定性。文章还将介绍一些实用的技术和工具,如Netflix Zuul、Spring Cloud Gateway以及Hystrix,以帮助开发者构建一个可靠且高效的微服务环境。
|
5天前
|
前端开发 Java 应用服务中间件
Springboot解决跨域问题方案总结(包括Nginx,Gateway网关等)
Springboot解决跨域问题方案总结(包括Nginx,Gateway网关等)
|
5天前
|
算法 NoSQL API
SpringCloud&Gateway网关限流
SpringCloud&Gateway网关限流
42 7
|
5天前
|
负载均衡 Nacos 数据安全/隐私保护
SpringCloud GateWay 使用
SpringCloud GateWay 使用
23 0
|
5天前
|
缓存
SpringCloud Gateway 网关的请求体body的读取和修改
SpringCloud Gateway 框架中,为了处理请求体body,实现多次读取与修改,创建了一个名为`RequestParamGlobalFilter`的全局过滤器。这个过滤器使用`@Component`和`@Slf4j`注解,实现了`GlobalFilter`和`Ordered`接口,设置最高优先级以首先读取body。它通过缓存请求体并创建装饰过的`ServerHttpRequest`来实现body的动态获取。
85 4
|
5天前
|
缓存 Java API
【云原生】Spring Cloud Gateway的底层原理与实践方法探究
【云原生】Spring Cloud Gateway的底层原理与实践方法探究