Add a throttling rule

更新时间:
复制 MD 格式

SOFAStack microservices use SOFARPC to publish and reference services. Service throttling in SOFAStack primarily targets the SOFARPC framework. Guardian is a service throttling component for microservices. You can integrate this component into your business system and configure throttling rules to protect your system from being overwhelmed by sudden traffic spikes and improve its stability.

Prerequisites

Service throttling rules depend on dynamic configuration pushes. Before you enable service throttling, you must first enable dynamic configuration. For more information, see Add a dynamic configuration.

Procedure

  1. Log on to the SOFAStack console.

  2. In the navigation pane on the left, choose Middleware > Microservice Platform > Microservice > Service Administration.

  3. Click Service Throttling, and then click Add Application.

  4. Enter an application name, and then click OK.

  5. In the application list, click Add Rule to the right of the target application, and then configure the rule parameters.

    Area

    Parameter

    Description

    Basic Information

    Rule Name

    The name of the throttling rule. Use this name to describe the rule.

    Throttling Type

    The method for the throttling rule. Valid values:

    • Interface Method: Throttles a specific RPC interface or a method of a regular bean.

      Configure the interface path name and method signature in the Throttling Object section.

    • Web Page: Throttles web requests based on Spring MVC.

      Configure the request URI in the Throttling Object section.

    Running Mode

    The running mode for the throttling rule. Valid values:

    • Block Mode: Throttling is active and blocks requests based on the rule.

    • Monitoring Mode: Only logs throttling records without actually blocking requests.

    Throttling Algorithm

    The algorithm for the throttling rule. Valid values:

    • QPS Counting Algorithm: Limits the number of requests allowed per unit of time.

    • Extended Token Bucket Algorithm: Controls the amount of data sent to the network and allows for data bursts.

    For more information about the algorithms, see Throttling algorithm description.

    Post-throttling Action

    Action After Throttling

    The action to take after a request is throttled. Valid values:

    • Empty Configuration: Takes no action and returns an empty value.

    • Throw Exception: Returns an exception message. The message is the text you enter in the input box.

      This option is available only when Throttling Type is set to Interface Method.

    • Redirect to a Specified Page: Redirects the request to a specified URL.

      This option is available only when Throttling Type is set to Web Page.

    • Page JSON Message: Returns a specified JSON string in the HTML response.

      The default response is:

      {
        "success": false,
        "error": "MAX_VISIT_LIMIT"
      }

      This option is available only when Throttling Type is set to Web Page.

    • Page XML Message: Returns a specified XML string in the HTML response.

      The default response is:

      <?xml version="1.0" encoding="GBK"?><alipay><is_success>F</is_success><error>MAX_VISIT_LIMIT</error></alipay>

      This option is available only when Throttling Type is set to Web Page.

    Throttling Threshold

    Throttling Threshold

    You can configure the traffic throttling threshold with the following configuration items:

    • Condition Model: The condition for throttling.

      Valid values:

      • Number of service accesses or web page accesses per unit of time: Throttles based on the number of requests per unit of time.

      • Heap memory usage: Throttles based on the current heap memory usage.

      • CPU load: Throttles based on the average CPU load over the past minute.

      • Number of concurrent threads: Throttles based on the number of concurrent threads on a single machine.

    • Unit of Time: The period for logging throttling events. For the access count condition, this is also the statistical period.

      The unit is milliseconds (ms). The minimum value is 1000 ms.

    • Throttling Threshold: The threshold value based on the selected Condition Model. Traffic that exceeds this threshold is throttled.

      • If you select Number of service accesses or web page accesses per unit of time: The maximum allowed queries per second (QPS).

      • If you select Heap memory usage: The maximum heap memory usage in MB.

      • If you select CPU load: The maximum CPU load. The value is 100 × CPU load percentage.

      • If you select Number of concurrent threads: The maximum number of concurrent threads.

    • Traffic Type: The type of traffic the throttling rule applies to.

      Valid values:

      • All Traffic: Throttles both normal traffic and stress testing traffic.

      • Normal Traffic: Throttles only normal traffic.

      • Stress Testing Traffic: Throttles only stress testing traffic.

    Throttling Object

    The object to throttle. The settings depend on the selected Throttling Type.

    • If you select Interface Method, configure the following parameters:

      • Interface: The name of the interface to throttle. Supports RPC service interfaces or beans with a configured Spring AOP interceptor.

      • Method: The method to throttle. Supports method signatures with or without parameters.

      • Parameter Relationship: The logical relationship between multiple parameter conditions. Valid values are AND and OR.

    • If you select Web Page, configure the following parameters:

      • URI: The request URI to throttle. Do not include the domain name and parameter parts.

      • Parameter Relationship: The logical relationship between multiple parameter conditions. Valid values are AND and OR.

    After you configure the throttling object, add parameter conditions. For more information, see Configure a throttling object method signature.

  6. After the configuration is complete, select a push method as needed:

    • Submit: Pushes the throttling rule to all machines.

    • Canary Push: Select the push targets. The throttling rule is pushed only to the specified machines.

      Important
      • Data from a canary push is not saved to the database. It is only saved in the memory of the target servers.

      • If a server restarts, the pushed rule is reverted, and the rule that was active before the canary push is restored.

  7. In the application list, click the plus sign (+) to the left of the target application, and then change the status of the target rule to Enabled.

    After the throttling rule is enabled, it is pushed to all machines or specified machines based on the push configuration. You can also enable or disable the rule on specific machines:

    • When the throttling rule status is Disabled: Click Canary Enable, and then select the machines on which to enable the rule.

    • When the throttling rule status is Enabled: Click Canary Disable, and then select the machines on which to disable the rule.