【RuoYi-Cloud-Plus】Spring Cloud Gateway

foreword

About  Spring Cloud, there are a lot of knowledge points that need to be learned. This article is  a summary of some knowledge points during  learning Spring Cloud Gateway (hereinafter referred to as  ) and mainly  revolves around configuration files.Gatewayruoyi-gateway.yml

reference list

document

  • Spring Cloud Gateway Official Documentation
  • According to the documentation - service gateway

books

  • Spring Cloud Microservices and Distributed Systems Actual Combat
    Chapter 9 New Gateway——Spring Cloud Gateway
  • Spring Microservices in Practice (Second Edition)
    Chapter 8 Using Spring Cloud Gateway for Service Routing
  • In-depth understanding of Spring Cloud and actual combat
    Chapter 9 Gateway
  • Advanced Spring Cloud Microservice Architecture
    Chapter 8 API Gateway: Spring Cloud Gateway

1. Gateway framework configuration

ruoyi-gateway.yml

2. Gateway execution process

The official documentation states:

3. Gateway execution principle

"Spring Cloud Microservices and Distributed System Combat" explains:

 

4. Arranging and analyzing framework configuration parameters

Take the first routing configuration as an example to illustrate:

spring:
  cloud:
    # 网关配置
    gateway:
      # 打印请求日志(自定义)
      requestLog: true
      discovery:
        # 定位器
        locator:  
          # 断言和过滤器使用小写服务id
          lowerCaseServiceId: true
          # 开启从注册中心动态创建路由的功能
          enabled: true
      routes:
        # 认证中心
        # 路由的ID,没有固定规则但要求唯一,建议配合服务名
        - id: ruoyi-auth
          # 匹配后的目标服务地址,供服务的路由地址
          # 需要注意的是 uri 的协议为 lb,表示启用 Gateway 的负载均衡功能。
          # lb://serviceName 是 spring cloud gateway 在微服务中自动为我们创建的负载均衡 uri
          uri: lb://ruoyi-auth
          # 断言
          predicates:
            # 路径相匹配的进行路由
            - Path=/auth/**
          filters:
            # 验证码处理
            - ValidateCodeFilter
            # StripPrefixGatewayFilterFactory
            # 剥去前缀,此过滤器在将请求发送到下游之前从请求中删除路径的第一部分(称为前缀)。
            - StripPrefix=1

4.1. Open the registry service discovery integration

spring.cloud.gateway.discovery.locator.enabled=true

Article source address https://www.yii666.com/blog/358122.html

4.2. Lowercase service ID

spring.cloud.gateway.discovery.locator.lowerCaseServiceId=true

​​​​​

4.3. Load balancing lb

spring.cloud.gateway.routes[0].uri=lb://ruoyi-auth

uri Parameters can directly fill in the specific path, such as: , http://localhost:8080/ruoyi-authused  lbhere  means to enable the load balancing function :

4.4. Path routing assertion

spring.cloud.gateway.routes[0].predicates=[Path=/auth/**]

Regarding assertions, there are a total of 12 assertion factories:

Path routing assertion configuration:

Path routing assertion is relatively simple, only need to match the relevant path. https://www.yii666.com/ Article source address https://www.yii666.com/blog/358122.html

PathRoutePredicateFactory#apply

4.5. Prefix gateway filter

spring.cloud.gateway.routes[0].filters=[StripPrefix=1]

There are many types of filters, and there are 34 by default:

 

Article source site https://www.yii666.com/

Remove the first prefix:

StripPrefixGatewayFilterFactory#apply

Reposted from: [RuoYi-Cloud-Plus] Study Notes 05 - Spring Cloud Gateway (1) About Configuration File Parameters 

Guess you like

Origin blog.csdn.net/fuhanghang/article/details/131080401