设为首页 加入收藏

TOP

微服务网关 —— SpringCloud Gateway(一)
2023-09-09 10:25:56 】 浏览:63
Tags:SpringCloud Gateway

Gateway 简介

Spring Cloud Gateway 基于 Spring 5、Spring Boot 2 和 Project Reactor 等技术,是在 Spring 生态系统之上构建的 API 网关服务,Gateway 旨在提供一种简单而有效的方式来对 API 进行路由以及提供一些强大的过滤器功能,例如熔断、限流、重试等

Spring Cloud Gateway 具有如下特性:

  • 基于 Spring Framework 5、Project Reactor 以及 Spring Boot 2.0 进行构建
  • 能够匹配任何请求属性
  • 可以对路由指定 Predicate(断言)和 Filter(过滤器)
  • 集成 Hystrix 的断路器功能
  • 集成 Spring Cloud 服务发现功能
  • 易于编写的 Predicate 和 Filter
  • 请求限流功能
  • 路径重写

Gateway 快速入门

创建项目,引入依赖

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

在配置文件 application.yml 添加如下配置

server:
  port: 9201 # 指定运行端口

spring:
  application:
    name: gateway-service # 指定服务名称
  cloud:
    gateway:
      routes:
        - id: path_route  # 路由ID
          uri: http://localhost:8201/user/getUser  # 匹配后路由地址
          predicates: # 断言,路径相匹配的路由
            - Path=/user/getUser

也可以按如下配置

@Configuration
public class GatewayConfig {

    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
        return builder.routes()
                .route("path_route2", r -> r.path("/user/getUserInfo")
                        .uri("http://localhost:8201/user/getUserInfo"))
                .build();
    }
}

Gateway 路由工厂

Spring Cloud Gateway 包括许多内置的路由断言工厂,所有这些断言都与 HTTP 请求的不同属性匹配,多个路由断言工厂可以进行组合

1. After Route Predicate Factory

在指定时间之后的请求会匹配该路由

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

2. Before Route Predicate Factory

在指定时间之前的请求会匹配该路由

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

3. Between Route Predicate Factory

在指定时间区间内的请求会匹配该路由

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

带有指定 Cookie 的请求会匹配该路由

spring:
  cloud:
    gateway:
      routes:
        - id: after_route
          uri: http://example.org
          predicates:
            - Cookie=milk, yili # cookie为milk=yili

5. Header Route Predicate Factory

带有指定请求头的请求会匹配该路由

spring:
  cloud:
    gateway:
      routes:
        - id: after_route
          uri: http://example.org
          predicates:
            - Header=X-Request-Id, 1	# 请求头为X-Request-Id=1

6. Host Route Predicate Factory

带有指定 Host 的请求会匹配该路由

spring:
  cloud:
    gateway:
      routes:
        - id: after_route
          uri: http://example.org
          predicates:
            - Host=**.somehost.org	# 请求头为Host:www.somehost.org的请求可以匹配该路由

7. Method Route Predicate Factory

发送指定方法的请求会匹配该路由

spring:
  cloud:
    gateway:
      routes:
        - id: after_route
          uri: http://example.org
          predicates:
            - Method=GET,POST

8. Path Route Predicate Factory

发送指定路径的请求会匹配该路由

spring:
  cloud:
    gateway:
      routes:
        - id: after_route
          uri: http://example.org
          predicates:
            - Path=/red/(segment],/blue/(segment) # /red/1或/blue/1路径请求可以匹配该路由

9. Query Route Predicate Factory

带指定查询参数的请求可以匹配该路由

spring:
  cloud:
    gateway:
      routes:
        - id: after_route
          uri: http://example.org
          predicates:
            - Query=green # 带green=l查询参数的请求可以匹配该路由

10. RemoteAddr Route Predicate Factory

从指定远程地址发起的请求可以匹配该路由

spring:
  cloud:
    gateway:
      routes:
        - id: after_route
          uri: http://example.org
          predicates:
            - RemoteAddr=192.168.1.1/24 # 从192.168.1.1发起请求可以匹配该路由

11. Weight Route Predicate Fa

首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇浅谈eureka的保护机制 下一篇RocketMQ 入门实战(2)--安装

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目