Search in sources :

Example 1 with SentinelHandle

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);
}
Also used : ConditionData(org.apache.shenyu.common.dto.ConditionData) SentinelHandle(org.apache.shenyu.common.dto.convert.rule.SentinelHandle) RuleLocalData(org.apache.shenyu.web.controller.LocalPluginController.RuleLocalData)

Example 2 with SentinelHandle

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));
}
Also used : SentinelReactorTransformer(com.alibaba.csp.sentinel.adapter.reactor.SentinelReactorTransformer) ShenyuContext(org.apache.shenyu.plugin.api.context.ShenyuContext) HttpStatus(org.springframework.http.HttpStatus) SentinelHandle(org.apache.shenyu.common.dto.convert.rule.SentinelHandle)

Example 3 with SentinelHandle

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);
}
Also used : RuleData(org.apache.shenyu.common.dto.RuleData) SentinelHandle(org.apache.shenyu.common.dto.convert.rule.SentinelHandle) Test(org.junit.jupiter.api.Test)

Example 4 with SentinelHandle

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);
}
Also used : RuleData(org.apache.shenyu.common.dto.RuleData) Mono(reactor.core.publisher.Mono) SentinelHandle(org.apache.shenyu.common.dto.convert.rule.SentinelHandle) HttpStatusCodeException(org.springframework.web.client.HttpStatusCodeException) Test(org.junit.jupiter.api.Test)

Example 5 with SentinelHandle

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);
}
Also used : RuleData(org.apache.shenyu.common.dto.RuleData) Mono(reactor.core.publisher.Mono) SentinelHandle(org.apache.shenyu.common.dto.convert.rule.SentinelHandle) Test(org.junit.jupiter.api.Test)

Aggregations

SentinelHandle (org.apache.shenyu.common.dto.convert.rule.SentinelHandle)8 RuleData (org.apache.shenyu.common.dto.RuleData)6 Test (org.junit.jupiter.api.Test)5 Mono (reactor.core.publisher.Mono)3 DegradeRule (com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule)2 FlowRule (com.alibaba.csp.sentinel.slots.block.flow.FlowRule)2 SentinelReactorTransformer (com.alibaba.csp.sentinel.adapter.reactor.SentinelReactorTransformer)1 DegradeRuleManager (com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager)1 FlowRuleManager (com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Constants (org.apache.shenyu.common.constant.Constants)1 ConditionData (org.apache.shenyu.common.dto.ConditionData)1 PluginEnum (org.apache.shenyu.common.enums.PluginEnum)1 GsonUtils (org.apache.shenyu.common.utils.GsonUtils)1 ShenyuContext (org.apache.shenyu.plugin.api.context.ShenyuContext)1 PluginDataHandler (org.apache.shenyu.plugin.base.handler.PluginDataHandler)1 CacheKeyUtils (org.apache.shenyu.plugin.base.utils.CacheKeyUtils)1 RuleLocalData (org.apache.shenyu.web.controller.LocalPluginController.RuleLocalData)1 HttpStatus (org.springframework.http.HttpStatus)1