Sentinel is one of the options that supports flow control and circuit breaking.Sentinel supports flow control and circuit breaking functions for gateway.Core Module shenyu-plugin-sentinel.
Core Class org.apache.shenyu.plugin.sentinel.SentinelPlugin
sentinel dependency in the pom.xml file of the gateway(shenyu-bootstrap).<!-- apache shenyu sentinel plugin start--> <dependency> <groupId>org.apache.shenyu</groupId> <artifactId>shenyu-spring-boot-starter-plugin-sentinel</artifactId> <version>${project.version}</version> </dependency> <!-- apache shenyu sentinel plugin end-->
shenyu-admin--> BasicConfig --> Plugin --> sentinel set to enable.It is used to filter traffic for the first time and does not require handle fields.
For the final filtering of traffic, there is a rule handler logic.
| field | default value | field type | desc |
|---|---|---|---|
| degradeRuleCount | Doule | degrade threshold | |
| degradeRuleEnable | 1(enabled) | Integer | whether enable circuit breaking function of sentinel |
| degradeRuleGrade | 0(slow call ratio) | Integer | circuit breaker strategy, support RT of seconds level/ Error Ratio of seconds level/ Error Count of minutes level strategy |
| degradeRuleMinRequestAmount | 5 | Integer | circuit breaker min request amount |
| degradeRuleSlowRatioThreshold | 1.0d | Double | slow ratio threshold of degrading |
| degradeRuleStatIntervals | 1 | Integer | status intervals of degrade |
| degradeRuleTimeWindow | Integer | time of degrading(unit: second) | |
| flowRuleControlBehavior | 0(direact reject) | Integer | effect(reject directly/ queue/ slow start up), it do not support flow control by invocation relation. |
| flowRuleControlBehavior-direct rejection by default | |||
| flowRuleControlBehavior-warm up | |||
| flowRuleControlBehavior-constant speed queuing | |||
| flowRuleControlBehavior-preheating uniformly queued | |||
| flowRuleMaxQueueingTimeMs | 500ms | Integer | Maximum queuing time (valid in “preheating uniformly queued”, “constant speed queuing” mode) |
| flowRuleWarmUpPeriodSec | 10 | Integer | Cold start warm-up time (seconds) (valid in “preheating uniformly queued” “warm up” mode) |
| flowRuleCount | Integer | sentinel flow control count | |
| flowRuleEnable | 1(enabled) | Integer | whether enable sentinel flow control function. |
| flowRuleGrade | 1(QPS) | Integer | type of current limit threshold(QPS or Thread Count)。 |
| fallbackUri | String | degraded uri after circuit breaking. |
For more information on selectors and rules configuration, see Selector And Rule Config , only some of the fields are covered here.
shenyu-admin--> BasicConfig --> Plugin --> sentinel set to enable.just use qps flow control strategy, and qps is 10, reject strategy is directly reject.
the code is as follows:
@RestController @RequestMapping("/order") @ShenyuSpringMvcClient("/order") public class OrderController { /** * Save order dto. * * @param orderDTO the order dto * @return the order dto */ @PostMapping("/save") @ShenyuSpringMvcClient("/save") public OrderDTO save(@RequestBody final OrderDTO orderDTO) { orderDTO.setName("hello world save order"); return orderDTO; } }
Apache Jmetershenyu-admin--> BasicConfig --> Plugin --> sentinel set to enable.when degrade strategy is exception number, degradeRuleSlowRatioThreshold is not effective. When the minimum number of requests per unit of time is 5, and the request happens exception great than 3, it will trigger the circuit breaker.
when degrade strategy is slow call ratio, degradeRuleSlowRatioThreshold is effective, degradeRuleCount means RT(e.g. 200).
the code is as follows:
@RestController @RequestMapping("/order") @ShenyuSpringMvcClient("/order") public class OrderController { /** * Save order dto. * * @param orderDTO the order dto * @return the order dto */ @PostMapping("/save") @ShenyuSpringMvcClient("/save") public OrderDTO save(@RequestBody final OrderDTO orderDTO) { Random random = new Random(); int num = random.nextInt(100); if (num > 40) { throw new RuntimeException("num great than 20"); } orderDTO.setName("hello world save order"); return orderDTO; } }
Apache Jmetershenyu-admin --> BasicConfig --> Plugin --> sentinel set Status disable.