use of org.apache.shenyu.common.dto.convert.rule.SentinelHandle in project incubator-shenyu by apache.
the class SentinelPluginTest method buildRuleLocalDataList.
private static List<RuleLocalData> buildRuleLocalDataList(final String fallbackUri) {
final RuleLocalData ruleLocalData = new RuleLocalData();
SentinelHandle sentinelHandle = new SentinelHandle();
sentinelHandle.setDegradeRuleCount(1);
sentinelHandle.setDegradeRuleEnable(1);
sentinelHandle.setFlowRuleEnable(1);
sentinelHandle.setFlowRuleCount(1);
sentinelHandle.setFlowRuleGrade(1);
sentinelHandle.setFlowRuleControlBehavior(0);
sentinelHandle.setDegradeRuleGrade(1);
sentinelHandle.setDegradeRuleTimeWindow(1);
sentinelHandle.setDegradeRuleMinRequestAmount(1);
sentinelHandle.setDegradeRuleStatIntervals(1);
sentinelHandle.setDegradeRuleSlowRatioThreshold(0.5d);
sentinelHandle.setFallbackUri(fallbackUri);
ruleLocalData.setRuleHandler(JsonUtils.toJson(sentinelHandle));
ConditionData conditionData = new ConditionData();
conditionData.setParamType(ParamTypeEnum.URI.getName());
conditionData.setOperator(OperatorEnum.EQ.getAlias());
conditionData.setParamValue(TEST_SENTINEL_PATH);
ruleLocalData.setConditionDataList(Collections.singletonList(conditionData));
return Lists.newArrayList(ruleLocalData);
}
use of org.apache.shenyu.common.dto.convert.rule.SentinelHandle in project incubator-shenyu by apache.
the class SentinelPlugin method doExecute.
@Override
protected Mono<Void> doExecute(final ServerWebExchange exchange, final ShenyuPluginChain chain, final SelectorData selector, final RuleData rule) {
final ShenyuContext shenyuContext = exchange.getAttribute(Constants.CONTEXT);
assert shenyuContext != null;
String resourceName = CacheKeyUtils.INST.getKey(rule);
SentinelHandle sentinelHandle = GsonUtils.getInstance().fromJson(rule.getHandle(), SentinelHandle.class);
sentinelHandle.checkData(sentinelHandle);
return chain.execute(exchange).doOnSuccess(v -> {
HttpStatus status = exchange.getResponse().getStatusCode();
if (status == null || !status.is2xxSuccessful()) {
exchange.getResponse().setStatusCode(null);
throw new SentinelFallbackException(status == null ? HttpStatus.INTERNAL_SERVER_ERROR : status);
}
}).transform(new SentinelReactorTransformer<>(resourceName)).onErrorResume(throwable -> fallbackHandler.fallback(exchange, UriUtils.createUri(sentinelHandle.getFallbackUri()), throwable));
}
use of org.apache.shenyu.common.dto.convert.rule.SentinelHandle in project incubator-shenyu by apache.
the class SentinelPluginTest method testSentinelPluginFlowException.
/**
* Test Sentinel Flow.
*/
@Test
public void testSentinelPluginFlowException() {
RuleData data = new RuleData();
data.setSelectorId("sentinel");
data.setName("testSentinelPluginFlowException");
SentinelHandle sentinelHandle = new SentinelHandle();
sentinelHandle.setFlowRuleEnable(1);
sentinelHandle.setFlowRuleCount(0);
sentinelHandle.setFlowRuleGrade(1);
sentinelHandle.setFlowRuleControlBehavior(0);
sentinelHandle.setDegradeRuleEnable(0);
sentinelHandle.setDegradeRuleCount(1);
sentinelHandle.setDegradeRuleGrade(1);
sentinelHandle.setDegradeRuleTimeWindow(10);
sentinelHandle.setDegradeRuleMinRequestAmount(5);
sentinelHandle.setDegradeRuleStatIntervals(10);
sentinelHandle.setDegradeRuleSlowRatioThreshold(0.5d);
data.setHandle(GsonUtils.getGson().toJson(sentinelHandle));
when(chain.execute(exchange)).thenReturn(Mono.empty());
sentinelRuleHandle.handlerRule(data);
StepVerifier.create(sentinelPlugin.doExecute(exchange, chain, selectorData, data)).expectSubscription().verifyComplete();
// remove rule
sentinelRuleHandle.removeRule(data);
}
use of org.apache.shenyu.common.dto.convert.rule.SentinelHandle in project incubator-shenyu by apache.
the class SentinelPluginTest method testSentinelPluginNotHttpStatusOK.
/**
* Test chain.execute doOnSuccess return other status.
*/
@Test
public void testSentinelPluginNotHttpStatusOK() {
RuleData data = new RuleData();
data.setSelectorId("sentinel");
data.setName("testSentinelPluginNullPointException");
SentinelHandle sentinelHandle = new SentinelHandle();
sentinelHandle.setFlowRuleEnable(1);
sentinelHandle.setFlowRuleCount(10);
sentinelHandle.setFlowRuleGrade(0);
sentinelHandle.setFlowRuleControlBehavior(0);
sentinelHandle.setDegradeRuleCount(2);
sentinelHandle.setDegradeRuleGrade(2);
sentinelHandle.setDegradeRuleTimeWindow(5);
sentinelHandle.setDegradeRuleMinRequestAmount(5);
sentinelHandle.setDegradeRuleStatIntervals(10);
sentinelHandle.setDegradeRuleSlowRatioThreshold(0.5d);
data.setHandle(GsonUtils.getGson().toJson(sentinelHandle));
sentinelRuleHandle.handlerRule(data);
Mono mono = Mono.empty().doOnSuccess(v -> exchange.getResponse().setStatusCode(HttpStatus.TOO_MANY_REQUESTS));
when(chain.execute(exchange)).thenReturn(mono);
StepVerifier.create(sentinelPlugin.doExecute(exchange, chain, selectorData, data)).expectError(HttpStatusCodeException.class).verify();
// remove rule
sentinelRuleHandle.removeRule(data);
}
use of org.apache.shenyu.common.dto.convert.rule.SentinelHandle in project incubator-shenyu by apache.
the class SentinelPluginTest method testSentinelPluginDegradeException.
/**
* Test Sentinel Degrade.
*/
@Test
public void testSentinelPluginDegradeException() {
RuleData data = new RuleData();
data.setSelectorId("sentinel");
data.setName("testSentinelPluginDegradeException");
SentinelHandle sentinelHandle = new SentinelHandle();
sentinelHandle.setFlowRuleEnable(0);
sentinelHandle.setFlowRuleCount(10);
sentinelHandle.setFlowRuleGrade(1);
sentinelHandle.setFlowRuleControlBehavior(0);
sentinelHandle.setDegradeRuleEnable(1);
sentinelHandle.setDegradeRuleCount(1);
sentinelHandle.setDegradeRuleGrade(2);
sentinelHandle.setDegradeRuleTimeWindow(10);
sentinelHandle.setDegradeRuleMinRequestAmount(5);
sentinelHandle.setDegradeRuleStatIntervals(10);
sentinelHandle.setDegradeRuleSlowRatioThreshold(0.5d);
data.setHandle(GsonUtils.getGson().toJson(sentinelHandle));
sentinelRuleHandle.handlerRule(data);
Mono mono = Mono.error(RuntimeException::new);
when(chain.execute(exchange)).thenReturn(mono);
for (int i = 0; i < 5; i++) {
StepVerifier.create(sentinelPlugin.doExecute(exchange, chain, selectorData, data)).expectError(RuntimeException.class).verify();
}
StepVerifier.create(sentinelPlugin.doExecute(exchange, chain, selectorData, data)).expectSubscription().verifyComplete();
// remove rule
sentinelRuleHandle.removeRule(data);
}
Aggregations