Skip to main content
Version: 2.7.2

1. Overview

1.1 Plugin Name​

  • divide Plugin

1.2 Appropriate Scenario​

  • Handling http protocol requests.
  • Support traffic management, such as a/b test, grayscale test.
  • Service Load Balancing.
  • Set request timeout.

1.3 Plugin functionality​

  • Supports traffic management based on request information such as uri, header, and query.
  • Supports configurable load balancing and service warm-up. See loadBalance in the rule configuration below for supported algorithms.
  • Supports setting the maximum value of the request header, the maximum value of the request body, and the request level timeout.
  • Supports setting the timeout retry policy and the number of retries. Currently, the retry policy supports: current (retrying the server that failed before) and failover (retrying other servers).

1.4 Plugin Code​

  • Core module is shenyu-plugin-divide.
  • Core class is org.apache.shenyu.plugin.divide.DividePlugin.

2. How to use plugin

2.1 Plugin-use procedure chart​

2.2 Import pom​

  • Import maven in shenyu-bootstrap project's pom.xml file.
<dependency>
<groupId>org.apache.shenyu</groupId>
<artifactId>shenyu-spring-boot-starter-gateway</artifactId>
<version>${project.version}</version>
</dependency>

2.3 Enable plugin​

  • In shenyu-admin --> BasicConfig --> Plugin --> divide set Status enable.

2.4 Config plugin​

2.4.1 Configure access parameters in the client project configuration file​

  • Client access method and server address. The following example uses the http access method. Currently, the client supports the following access methods: http, zookeeper, etcd, nacos, consul. For detailed access configuration parameters, please refer to Client Access Configuration.
  • Client configuration, including the protocol name and the routing address of the service, please use the http protocol here, and the value of contextPath must be configured as the routing address of each service.
shenyu:
register:
registerType: http
serverLists: http://localhost:9095
props:
username: admin
password: 123456
client:
http: # http protocol
props:
contextPath: /http # routing address for each service

2.4.2 Configure upstream validity detection parameters in the shenyu-admin configuration file​

The following example uses the http access method. Currently, the client supports the following access methods: http, zookeeper, etcd, nacos, consul. For detailed access configuration parameters, please refer to Client Access Configuration.

Only http-type registries support upstream detection.

shenyu:
register:
registerType: http # Only http-type register center support upstream detection.
serverLists:
props:
checked: true # The default is true, set to false, do not detect.
zombieCheckTimes: 5 # The maximum number of zombie upstream detections. If it exceeds 5 times, its validity will no longer be detected. The default value is 5.
scheduledTime: 10 # Timing detection interval, the default is 10 seconds.
zombieRemovalTimes: 60 # How many seconds the upstream is offline to be considered as a zombie upstream, the default is 60 seconds.

2.4.3 Configure the selector and rule information of the divide plugin in shenyu-admin​

After the client is started, the selector and rule information will be automatically registered in shenyu-admin -> Plugin List -> Proxy -> Divide.

2.4.3.1 Selector configuration​

Example of divide selector. For general selector configuration, please refer to Selectors and Rules.

2.4.3.1.1 Selector handling information configuration​
  • host: fill in localhost, this field is not used currently.
  • ip:port: ip and port, fill in the ip + port of your real service here.
  • protocol: http protocol, fill in http: or https:, if not fill in, the default is: http:.
  • startupTime: Startup time in milliseconds.
  • weight: load balancing weight, the default value of service startup automatic registration is 50.
  • warmupTime: Warmup time, in milliseconds. The server during warmup will calculate the instantaneous weight, and the calculated value will be smaller than the actual configured weight to protect the server just started. The default value of service startup registration is 10. For example, the warm-up time is 100 milliseconds, the current startup is 50 milliseconds, the configured weight is 50, and the actual weight is 25.
  • status: On or off, this selector is valid only in the on state.

2.4.3.2 Processing information configuration of rules​

Example of divide rule. For general rule configuration, please refer to selectors and rules.

2.4.3.2.1 Rule processing information configuration​

The loadBalance, timeout, retry, retryStrategy and retryBackOffSpec fields below describe the released v2.7.2 implementation. requestTimeout and poolGroup are available only in 2.7.3-SNAPSHOT.

  • loadBalance: Selects an upstream for HTTP requests. In ShenYu 2.7.2, the supported values are random (default), roundRobin, hash, leastActive, p2c, and shortestResponse. The hash strategy uses consistent hashing by client IP. The leastActive strategy selects the upstream with the fewest in-flight requests and uses warm-up-adjusted weights to break ties.
  • timeout: The timeout of a single call attempt to the http client, in milliseconds, the default value is 3000. In v2.7.2, or with requestTimeout omitted in 2.7.3-SNAPSHOT, an attempt ends when the response headers arrive, so the deadline covers establishing the connection, waiting for the response and reading the headers - not the response body. In 2.7.3-SNAPSHOT, configuring requestTimeout (including 0) makes each attempt body-aware and caps it by this timeout; a positive requestTimeout additionally bounds the whole sequence.
  • retry: The number of retries after a failed call, the default value is 3. Eligibility depends on the selected retryStrategy and retryBackOffSpec: the legacy current strategy retries TimeoutException, ConnectTimeoutException, ReadTimeoutException and IllegalStateException, but excludes DataBufferLimitException; the default FAILOVER path retries every error (th -> true), while fixed and exponential retry only GET requests and their transient transport failures (plus upstream 5xx statuses when used with failover). DataBufferLimitException is therefore retried only by the default FAILOVER path.
  • headerMaxSize: The maximum value of the requested header.
  • requestMaxSize: The maximum value of the request body.
  • retryStrategy: Supported since 2.4.3, retry strategy after failure, default current to maintain compatibility with lower versions. For example, there are 3 downstream services http:localhost:1111, http:localhost:1112 and http:localhost:1113, assuming the first load balancing to http:localhost:1111 and call failed. Using the current strategy will continue to retry calling http:localhost:1111; using the failover strategy will retry calling other services such as http:localhost:1112 through the load balancing, if it fails again at this time , call to http:localhost:1113 until no service is available.
  • retryBackOffSpec: The back-off used between retries, the default is default. With retryStrategy=current, default uses a capped random back-off (20 ms initial, 20 s cap, 0.5 jitter); fixed uses a flat 2 s delay; exponential uses an exponential back-off starting at 500 ms with a 5 s cap. With retryStrategy=failover, default retries immediately with no back-off, while fixed and exponential keep their respective delays. In v2.7.2, current retries and fixed/exponential retries (including FAILOVER) are guarded by the aggregate budget (retry + 1) * timeout + retry * maximumBackoff, where maximumBackoff is 20 s, 2 s or 5 s respectively; the default FAILOVER path has neither this budget nor back-off.
  • requestTimeout: Optional whole-request deadline in milliseconds, available only in 2.7.3-SNAPSHOT. Omitted (null) selects the legacy mode: each attempt is bounded by timeout only up to the response headers, and the sequence is guarded by an implicit aggregate budget (retry + 1) * timeout + retry * maximumBackoff, except for the default FAILOVER path, where neither the budget nor back-off applies. A positive value bounds the whole retry sequence - every attempt, the back-off in between and writing the response body - and an expired deadline returns HTTP 504; the implicit aggregate budget is skipped because this deadline owns the sequence. 0 disables the whole-request timer and skips the implicit budget, but each attempt remains body-aware and is still capped by timeout.
  • poolGroup: Optional, available only in 2.7.3-SNAPSHOT. Selects a named connection pool defined in shenyu.httpclient.pool-groups (see the gateway property config page). An unknown group name returns HTTP 503 instead of falling back to the shared default pool.

In v2.7.2, the legacy current strategy maps attempt-count exhaustion to HTTP 408 ("the maximum number of retry times has been exceeded"). fixed and exponential rethrow the last failure, which surfaces as HTTP 504 for transport timeouts. failover still uses the retry count: it returns HTTP 503 when no healthy upstream remains, but an exhausted configured attempt timeout maps to HTTP 504, not 503 or 408. For the budgeted paths, expiry of the aggregate retry budget returns HTTP 504.

In 2.7.3-SNAPSHOT, when requestTimeout is configured (including 0) and the terminal failure is a TimeoutException, that failure is rethrown and maps to HTTP 504 instead of HTTP 408; other terminal failures keep the HTTP 408 mapping. When a positive requestTimeout expires first, the client returns HTTP 504.

A status can only be set while the response headers are still unwritten. Once a streaming response has been committed, a deadline that expires afterwards can only truncate the body, not replace the status the client already received.

  • An ordinary request is bounded per attempt by timeout (3000 ms by default). With the default current strategy, exhausting the retry count returns HTTP 408; if the aggregate budget expires first, the result is HTTP 504.
  • A slow streaming response (SSE, long polling) is a different case. In v2.7.2, or with requestTimeout omitted in 2.7.3-SNAPSHOT, an active stream can outlive timeout as long as the reads keep completing. In v2.7.2, size the transport timeouts for the expected stream. In 2.7.3-SNAPSHOT, set a positive requestTimeout to bound the whole sequence including the body; with requestTimeout: 0, the whole-request timer is off, but each attempt remains body-aware and is capped by timeout. Disabling shenyu.httpclient.responseTimeout alone does not remove the transport-level read deadlines (shenyu.httpclient.readTimeout and the idle thresholds).

2.5 Examples​

2.5.1 Example A/B Test​

To be added, welcome contribute.

2.5.2 Example Grayscale Test​

To be added, welcome contribute.

3. How to disable plugin

  • In shenyu-admin --> BasicConfig --> Plugin --> divide set Status disable.