巧路由:基于 Spring Boot 的请求限流与路由代理组件

一个Spring Boot应用内根据请求路径限流、代理、修改参数、修改请求体组件。

Branch2Tags7
FilesLast commitLast update
1 year ago
9 months ago
1 year ago
9 months ago
9 months ago
1 year ago
1 year ago
1 year ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
1 year ago
1 year ago
9 months ago

Smart Router

English | 中文

A Spring Boot component that provides request path-based rate limiting, proxying, parameter modification, and request body modification within applications.

star fork star fork
MIT JDK SpringBoot

Features

  • Rate Limiting
    • Multiple rate limiting algorithms support:
      • Google Guava Token Bucket Algorithm
      • Redisson Distributed Rate Limiting
        • Multiple Redis deployment modes support:
          • Single Node Redis
          • Redis Cluster Mode
          • Redis Sentinel Mode
  • Proxy
    • Supports weight configuration
    • SpEL Expression Parameter Modification
    • SpEL Expression Request Body Modification
  • Dynamic rate limiting and proxy configuration route modification
  • Real-time monitoring dashboard

Add JitPack Repository

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

Include the jar

<dependency>
    <groupId>com.gitee.wb04307201.smart-router</groupId>
    <artifactId>smart-router-spring-boot-starter</artifactId>
    <version>1.0.6</version>
</dependency>

Basic Configuration Structure

smart-router:
  rateLimiter:
    rateLimitingType: standalone                      # Rate limiting type: standalone, redis, redis-cluster, redis-sentinel
    rateLimitRules:                                   # Rate limiting rules list
      - endpoint: /test/hello                         # Rate limiting path
        capacity: 1                                   # Capacity/token count
        period: 10                                    # Time period
        unit: SECONDS                                 # Time unit (optional, default SECONDS)
  proxyRules:                                         # Proxy rules list
    - endpoint: /test/get                             # Proxy path
      proxies:                                        # Proxy targets list
        - targetEndpoint: /test/v1/get                # Target path
          weight: 5                                   # Weight
                                                      # Parameter mapping
          mapRule: |-
            #params.put('flagv1',#params.get('flag'))
        - targetEndpoint: /test/v2/get
          weight: 5
          mapRule: |-
            #params.put('flagv2',#params.get('flag'))
    - endpoint: /test/post
      proxies:
        - targetEndpoint: /test/v1/post
          weight: 5
                                                        # Request body mapping
          bodyRule: |-
            #params.put('value1','Hello')

Rate Limiting Type Configuration

1. Standalone Mode (Local Rate Limiting)

smart-router:
  rateLimiter:
    rateLimitingType: standalone

2. Redis Mode

smart-router:
  rateLimiter:
    rateLimitingType: redis
    attributes:
      address: localhost:6379    # Redis address
      password: your_password     # Redis password
      database: 0                # Database index (0-15)

3. Redis Cluster Mode

smart-router:
  rateLimiter:
    rateLimitingType: redis-cluster
    attributes:
      nodes:                    # Cluster node list
        - localhost:7000
        - localhost:7001
      password: your_password    # Password

4. Redis Sentinel Mode

smart-router:
  rateLimiter:
    rateLimitingType: redis-sentinel
    attributes:
      nodes:                    # Sentinel node list
        - localhost:26379
        - localhost:26380
      password: your_password    # Password
      masterName: mymaster       # Master node name

Runtime Dynamic Modification of Rate Limiting and Proxy Configuration

Get current rate limiting and proxy configuration

GET http://localhost:8080/smart/router/monitor/rules
Accept: application/json

Response

{
    "rateLimitRules": [  //Rate limiting rules
        {
            "endpoint": "/test/hello",
            "capacity": 1,
            "period": 10,
            "unit": "SECONDS"
        }
    ],
    "proxyRules": [  //Proxy rules
        {
            "endpoint": "/test/get",
            "proxies": [
                {
                    "targetEndpoint": "/test/v1/get",
                    "weight": 5,
                    "mapRule": "#params.put('flagv1',#params.get('flag'))",
                    "bodyRule": null
                },
                {
                    "targetEndpoint": "/test/v2/get",
                    "weight": 5,
                    "mapRule": "#params.put('flagv2',#params.get('flag'))",
                    "bodyRule": null
                }
            ]
        },
        {
            "endpoint": "/test/post",
            "proxies": [
                {
                    "targetEndpoint": "/test/v1/post",
                    "weight": 10,
                    "mapRule": null,
                    "bodyRule": "#params.put('value1','Hello')"
                }
            ]
        }
    ]
}

Modify rate limiting and proxy configuration

POST http://localhost:8080/smart/router/monitor/rules
Content-Type: application/json



Monitoring Dashboard

The project provides a built-in monitoring page. Access /smart/router/monitor/view to view the monitoring page img.png img_1.png

You can view and adjust rate limiting configuration and proxy configuration on the page img_2.png

Extensibility

The project design has good extensibility:

  1. New rate limiting algorithms can be added by implementing the IFactory.java and IRateLimiter.java interfaces
  2. Custom storage can be implemented by implementing the IStorage.java interface

Introduction

一个Spring Boot应用内根据请求路径限流、代理、修改参数、修改请求体组件。

Customize your domain