SoFunction
Updated on 2025-04-07

Interpretation of Gateway routing matching rules

Gateway routing matching rules

In the microservice architecture, Gateway plays a crucial role as the entrance to requests.

It is not only responsible for routing and forwarding, but also has various functions such as security, monitoring, and current limiting. Among them, the routing matching rules are one of the core functions of Gateway, which determines how requests are correctly forwarded to the target service.

This article will introduce in detail the basic concepts, common attributes and practical applications of Gateway routing matching rules.

1. Basic concepts

Gateway routing matching rules are the core concept of network routing, which determines the transmission path of packets (or requests) in the network.

In the microservice architecture, Gateway forwards external requests to internal service instances through matching rules. These rules can be matched based on a variety of conditions, such as request paths, request headers, request parameters, etc.

2. Common properties

In Spring Cloud Gateway, routing matching rules are mainly defined through configuration files.

Here are some commonly used properties and their explanations:

  1. id: The unique identifier of the route, each route must have a unique id.

  2. uri: The URI of the target service can be a specific URL (such as) or it can be combined with the load balancing URI of the service registration center (such as Nacos) (such as lb://service-name).

  3. predicates: Defines a set of assertions that determine whether the request matches the route. Each assertion is a condition, and the request will be routed to the target service only when all conditions are met. Commonly used assertions include:

    • Path: Match the request path, supporting exact matching, single-level wildcard matching, multi-level wildcard matching and regular expression matching.
    • Method: Match request methods (such as GET, POST, etc.).
    • Query: Match the query parameters in the request.
    • Header: Match the request header.
    • Cookie: Match the cookies in the request.
    • Host: Match the requested host.
    • After/Before/Between: Based on point-of-time matching, used to match requests within a certain point-of-time or time interval.
  4. filters: Define a set of filters to modify the request or response before and after the request is forwarded. Commonly used filters include:

    • StripPrefix: Remove the path prefix.
    • RewritePath: Rewrite the request path.
    • AddResponseHeader: Add response header.
    • SetStatus: Set the response status code.
    • Retry: Retry the mechanism.
  5. order: Define the priority of the route. The smaller the order value, the higher the priority. By default, routes are matched in order in order, and no longer matches down as long as they match.

  6. metadata: Stores additional metadata information, which can be used in filters or other components.

III. Practical application

Here is an example of the routing configuration for Spring Cloud Gateway:

server:
  port: 8080
spring:
  application:
    name: gateway-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    gateway:
      discovery:
        locator:
          enabled: true
      routes:
        - id: auth-service
          uri: lb://auth-service
          order: -1
          predicates:
            - Path=/auth/**
            - Method=GET,POST
          filters:
            - StripPrefix=1
            - RewritePath=/auth/(?<segment>.*), /$\{segment}

In this configuration, we define a name calledauth-serviceThe route will match the path to/auth/A request that begins with the request method GET or POST.

The request is forwarded to the nameauth-serviceOn the service instance, a prefix segment in the path was removed (/auth/), and the path was rewritten at the same time.

4. Things to note

  1. Routing matching order: By default, routes match in order of configuration. If multiple routes match the same request, the first matching route will be selected. Therefore, you need to pay attention to the order when configuring routing.
  2. Performance considerations: Complex routing matching rules may affect Gateway performance. Therefore, when configuring routing, you need to weigh the complexity and performance requirements of matching rules.
  3. Security: Routing matching rules can expose some sensitive information of the service (such as service name, path, etc.). Therefore, when configuring routing, you need to pay attention to security and avoid exposing sensitive information to external users.

Summarize

Gateway routing matching rules are an indispensable part of the microservice architecture.

By rationally configuring routing matching rules, we can realize various functions such as accurate forwarding, load balancing, and security control of requests.

The above is personal experience. I hope you can give you a reference and I hope you can support me more.