com.alibaba.csp.sentinel.adapter.gateway.common.api.GatewayApiDefinitionManager Java Examples

The following examples show how to use com.alibaba.csp.sentinel.adapter.gateway.common.api.GatewayApiDefinitionManager. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GatewayConfiguration.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
private void initCustomizedApis() {
    Set<ApiDefinition> definitions = new HashSet<>();
    ApiDefinition api1 = new ApiDefinition("some_customized_api")
        .setPredicateItems(new HashSet<ApiPredicateItem>() {{
            add(new ApiPathPredicateItem().setPattern("/ahas"));
            add(new ApiPathPredicateItem().setPattern("/product/**")
                .setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
        }});
    ApiDefinition api2 = new ApiDefinition("another_customized_api")
        .setPredicateItems(new HashSet<ApiPredicateItem>() {{
            add(new ApiPathPredicateItem().setPattern("/**")
                .setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
        }});
    definitions.add(api1);
    definitions.add(api2);
    GatewayApiDefinitionManager.loadApiDefinitions(definitions);
}
 
Example #2
Source File: UpdateGatewayApiDefinitionGroupCommandHandler.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Override
public CommandResponse<String> handle(CommandRequest request) {
    String data = request.getParam("data");
    if (StringUtil.isBlank(data)) {
        return CommandResponse.ofFailure(new IllegalArgumentException("Bad data"));
    }
    try {
        data = URLDecoder.decode(data, "utf-8");
    } catch (Exception e) {
        RecordLog.info("Decode gateway API definition data error", e);
        return CommandResponse.ofFailure(e, "decode gateway API definition data error");
    }

    RecordLog.info("[API Server] Receiving data change (type: gateway API definition): {}", data);

    String result = SUCCESS_MSG;

    Set<ApiDefinition> apiDefinitions = parseJson(data);
    GatewayApiDefinitionManager.loadApiDefinitions(apiDefinitions);
    if (!writeToDataSource(apiDefinitionWds, apiDefinitions)) {
        result = WRITE_DS_FAILURE_MSG;
    }
    return CommandResponse.ofSuccess(result);
}
 
Example #3
Source File: GatewayConfiguration.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
private void initCustomizedApis() {
    Set<ApiDefinition> definitions = new HashSet<>();
    ApiDefinition api1 = new ApiDefinition("some_customized_api")
        .setPredicateItems(new HashSet<ApiPredicateItem>() {{
            add(new ApiPathPredicateItem().setPattern("/ahas"));
            add(new ApiPathPredicateItem().setPattern("/product/**")
                .setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
        }});
    ApiDefinition api2 = new ApiDefinition("another_customized_api")
        .setPredicateItems(new HashSet<ApiPredicateItem>() {{
            add(new ApiPathPredicateItem().setPattern("/**")
                .setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
        }});
    definitions.add(api1);
    definitions.add(api2);
    GatewayApiDefinitionManager.loadApiDefinitions(definitions);
}
 
Example #4
Source File: GatewayRuleConfig.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
private void initCustomizedApis() {
    Set<ApiDefinition> definitions = new HashSet<>();
    ApiDefinition api1 = new ApiDefinition("some_customized_api")
        .setPredicateItems(new HashSet<ApiPredicateItem>() {{
            add(new ApiPathPredicateItem().setPattern("/images"));
            add(new ApiPathPredicateItem().setPattern("/comments")
                .setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
        }});
    ApiDefinition api2 = new ApiDefinition("another_customized_api")
        .setPredicateItems(new HashSet<ApiPredicateItem>() {{
            add(new ApiPathPredicateItem().setPattern("/**")
                .setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
        }});
    definitions.add(api1);
    definitions.add(api2);
    GatewayApiDefinitionManager.loadApiDefinitions(definitions);
}
 
Example #5
Source File: GatewayRuleConfig.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
private void initCustomizedApis() {
    Set<ApiDefinition> definitions = new HashSet<>();
    ApiDefinition api1 = new ApiDefinition("some_customized_api")
        .setPredicateItems(new HashSet<ApiPredicateItem>() {{
            add(new ApiPathPredicateItem().setPattern("/ahas"));
            add(new ApiPathPredicateItem().setPattern("/aliyun_product/**")
                .setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
        }});
    ApiDefinition api2 = new ApiDefinition("another_customized_api")
        .setPredicateItems(new HashSet<ApiPredicateItem>() {{
            add(new ApiPathPredicateItem().setPattern("/**")
                .setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
        }});
    definitions.add(api1);
    definitions.add(api2);
    GatewayApiDefinitionManager.loadApiDefinitions(definitions);
}
 
Example #6
Source File: UpdateGatewayApiDefinitionGroupCommandHandler.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
@Override
public CommandResponse<String> handle(CommandRequest request) {
    String data = request.getParam("data");
    if (StringUtil.isBlank(data)) {
        return CommandResponse.ofFailure(new IllegalArgumentException("Bad data"));
    }
    try {
        data = URLDecoder.decode(data, "utf-8");
    } catch (Exception e) {
        RecordLog.info("Decode gateway API definition data error", e);
        return CommandResponse.ofFailure(e, "decode gateway API definition data error");
    }

    RecordLog.info("[API Server] Receiving data change (type: gateway API definition): {0}", data);

    String result = SUCCESS_MSG;

    Set<ApiDefinition> apiDefinitions = parseJson(data);
    GatewayApiDefinitionManager.loadApiDefinitions(apiDefinitions);

    return CommandResponse.ofSuccess(result);
}
 
Example #7
Source File: GatewayRuleConfig.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
private void initCustomizedApis() {
    Set<ApiDefinition> definitions = new HashSet<>();
    ApiDefinition api1 = new ApiDefinition("some_customized_api")
        .setPredicateItems(new HashSet<ApiPredicateItem>() {{
            add(new ApiPathPredicateItem().setPattern("/ahas"));
            add(new ApiPathPredicateItem().setPattern("/aliyun_product/**")
                .setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
        }});
    ApiDefinition api2 = new ApiDefinition("another_customized_api")
        .setPredicateItems(new HashSet<ApiPredicateItem>() {{
            add(new ApiPathPredicateItem().setPattern("/**")
                .setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
        }});
    definitions.add(api1);
    definitions.add(api2);
    GatewayApiDefinitionManager.loadApiDefinitions(definitions);
}
 
Example #8
Source File: AbstractDataSourceProperties.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
public void postRegister(AbstractDataSource dataSource) {
	switch (this.getRuleType()) {
	case FLOW:
		FlowRuleManager.register2Property(dataSource.getProperty());
		break;
	case DEGRADE:
		DegradeRuleManager.register2Property(dataSource.getProperty());
		break;
	case PARAM_FLOW:
		ParamFlowRuleManager.register2Property(dataSource.getProperty());
		break;
	case SYSTEM:
		SystemRuleManager.register2Property(dataSource.getProperty());
		break;
	case AUTHORITY:
		AuthorityRuleManager.register2Property(dataSource.getProperty());
		break;
	case GW_FLOW:
		GatewayRuleManager.register2Property(dataSource.getProperty());
		break;
	case GW_API_GROUP:
		GatewayApiDefinitionManager.register2Property(dataSource.getProperty());
		break;
	default:
		break;
	}
}
 
Example #9
Source File: SentinelGatewayFilterTest.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
@Test
public void testPickMatchingApiDefinitions() {
    // Mock a request.
    ServerWebExchange exchange = mock(ServerWebExchange.class);
    ServerHttpRequest request = mock(ServerHttpRequest.class);
    when(exchange.getRequest()).thenReturn(request);
    RequestPath requestPath = mock(RequestPath.class);
    when(request.getPath()).thenReturn(requestPath);

    // Prepare API definitions.
    Set<ApiDefinition> apiDefinitions = new HashSet<>();
    String apiName1 = "some_customized_api";
    ApiDefinition api1 = new ApiDefinition(apiName1)
        .setPredicateItems(Collections.singleton(
            new ApiPathPredicateItem().setPattern("/product/**")
                .setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX)
        ));
    String apiName2 = "another_customized_api";
    ApiDefinition api2 = new ApiDefinition(apiName2)
        .setPredicateItems(new HashSet<ApiPredicateItem>() {{
            add(new ApiPathPredicateItem().setPattern("/something"));
            add(new ApiPathPredicateItem().setPattern("/other/**")
                .setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
        }});
    apiDefinitions.add(api1);
    apiDefinitions.add(api2);
    GatewayApiDefinitionManager.loadApiDefinitions(apiDefinitions);
    SentinelGatewayFilter filter = new SentinelGatewayFilter();

    when(requestPath.value()).thenReturn("/product/123");
    Set<String> matchingApis = filter.pickMatchingApiDefinitions(exchange);
    assertThat(matchingApis.size()).isEqualTo(1);
    assertThat(matchingApis.contains(apiName1)).isTrue();

    when(requestPath.value()).thenReturn("/products");
    assertThat(filter.pickMatchingApiDefinitions(exchange).size()).isZero();

    when(requestPath.value()).thenReturn("/something");
    matchingApis = filter.pickMatchingApiDefinitions(exchange);
    assertThat(matchingApis.size()).isEqualTo(1);
    assertThat(matchingApis.contains(apiName2)).isTrue();

    when(requestPath.value()).thenReturn("/other/foo/3");
    matchingApis = filter.pickMatchingApiDefinitions(exchange);
    assertThat(matchingApis.size()).isEqualTo(1);
    assertThat(matchingApis.contains(apiName2)).isTrue();
}
 
Example #10
Source File: RulesController.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
@GetMapping("/api")
public Set<ApiDefinition> apiRules() {
	return GatewayApiDefinitionManager.getApiDefinitions();
}
 
Example #11
Source File: RulesWebFluxController.java    From spring-cloud-alibaba with Apache License 2.0 4 votes vote down vote up
@GetMapping("/api")
public Mono<Set<ApiDefinition>> apiRules() {
	return Mono.just(GatewayApiDefinitionManager.getApiDefinitions());
}
 
Example #12
Source File: GatewayParamParserTest.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
@After
public void tearDown() {
    GatewayApiDefinitionManager.loadApiDefinitions(new HashSet<ApiDefinition>());
    GatewayRuleManager.loadRules(new HashSet<GatewayFlowRule>());
    GatewayRegexCache.clear();
}
 
Example #13
Source File: GatewayParamParserTest.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    GatewayApiDefinitionManager.loadApiDefinitions(new HashSet<ApiDefinition>());
    GatewayRuleManager.loadRules(new HashSet<GatewayFlowRule>());
    GatewayRegexCache.clear();
}
 
Example #14
Source File: GetGatewayApiDefinitionGroupCommandHandler.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
@Override
public CommandResponse<String> handle(CommandRequest request) {
    return CommandResponse.ofSuccess(JSON.toJSONString(GatewayApiDefinitionManager.getApiDefinitions()));
}
 
Example #15
Source File: SentinelGatewayFilterTest.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
@After
public void tearDown() {
    GatewayApiDefinitionManager.loadApiDefinitions(new HashSet<>());
    GatewayRuleManager.loadRules(new HashSet<>());
}
 
Example #16
Source File: SentinelGatewayFilterTest.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    GatewayApiDefinitionManager.loadApiDefinitions(new HashSet<>());
    GatewayRuleManager.loadRules(new HashSet<>());
}
 
Example #17
Source File: SentinelGatewayFilterTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
@After
public void tearDown() {
    GatewayApiDefinitionManager.loadApiDefinitions(new HashSet<>());
    GatewayRuleManager.loadRules(new HashSet<>());
}
 
Example #18
Source File: SpringCloudGatewayParamParserTest.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
@After
public void tearDown() {
    GatewayApiDefinitionManager.loadApiDefinitions(new HashSet<>());
    GatewayRuleManager.loadRules(new HashSet<>());
}
 
Example #19
Source File: SpringCloudGatewayParamParserTest.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    GatewayApiDefinitionManager.loadApiDefinitions(new HashSet<>());
    GatewayRuleManager.loadRules(new HashSet<>());
}
 
Example #20
Source File: SpringCloudGatewayParamParserTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    GatewayApiDefinitionManager.loadApiDefinitions(new HashSet<>());
    GatewayRuleManager.loadRules(new HashSet<>());
}
 
Example #21
Source File: SpringCloudGatewayParamParserTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
@After
public void tearDown() {
    GatewayApiDefinitionManager.loadApiDefinitions(new HashSet<>());
    GatewayRuleManager.loadRules(new HashSet<>());
}
 
Example #22
Source File: SentinelGatewayFilterTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
@Test
public void testPickMatchingApiDefinitions() {
    // Mock a request.
    ServerWebExchange exchange = mock(ServerWebExchange.class);
    ServerHttpRequest request = mock(ServerHttpRequest.class);
    when(exchange.getRequest()).thenReturn(request);
    RequestPath requestPath = mock(RequestPath.class);
    when(request.getPath()).thenReturn(requestPath);

    // Prepare API definitions.
    Set<ApiDefinition> apiDefinitions = new HashSet<>();
    String apiName1 = "some_customized_api";
    ApiDefinition api1 = new ApiDefinition(apiName1)
        .setPredicateItems(Collections.singleton(
            new ApiPathPredicateItem().setPattern("/product/**")
                .setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX)
        ));
    String apiName2 = "another_customized_api";
    ApiDefinition api2 = new ApiDefinition(apiName2)
        .setPredicateItems(new HashSet<ApiPredicateItem>() {{
            add(new ApiPathPredicateItem().setPattern("/something"));
            add(new ApiPathPredicateItem().setPattern("/other/**")
                .setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
        }});
    apiDefinitions.add(api1);
    apiDefinitions.add(api2);
    GatewayApiDefinitionManager.loadApiDefinitions(apiDefinitions);
    SentinelGatewayFilter filter = new SentinelGatewayFilter();

    when(requestPath.value()).thenReturn("/product/123");
    Set<String> matchingApis = filter.pickMatchingApiDefinitions(exchange);
    assertThat(matchingApis.size()).isEqualTo(1);
    assertThat(matchingApis.contains(apiName1)).isTrue();

    when(requestPath.value()).thenReturn("/products");
    assertThat(filter.pickMatchingApiDefinitions(exchange).size()).isZero();

    when(requestPath.value()).thenReturn("/something");
    matchingApis = filter.pickMatchingApiDefinitions(exchange);
    assertThat(matchingApis.size()).isEqualTo(1);
    assertThat(matchingApis.contains(apiName2)).isTrue();

    when(requestPath.value()).thenReturn("/other/foo/3");
    matchingApis = filter.pickMatchingApiDefinitions(exchange);
    assertThat(matchingApis.size()).isEqualTo(1);
    assertThat(matchingApis.contains(apiName2)).isTrue();
}
 
Example #23
Source File: GatewayParamParserTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
@After
public void tearDown() {
    GatewayApiDefinitionManager.loadApiDefinitions(new HashSet<ApiDefinition>());
    GatewayRuleManager.loadRules(new HashSet<GatewayFlowRule>());
    GatewayRegexCache.clear();
}
 
Example #24
Source File: GatewayParamParserTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    GatewayApiDefinitionManager.loadApiDefinitions(new HashSet<ApiDefinition>());
    GatewayRuleManager.loadRules(new HashSet<GatewayFlowRule>());
    GatewayRegexCache.clear();
}
 
Example #25
Source File: GetGatewayApiDefinitionGroupCommandHandler.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
@Override
public CommandResponse<String> handle(CommandRequest request) {
    return CommandResponse.ofSuccess(JSON.toJSONString(GatewayApiDefinitionManager.getApiDefinitions()));
}
 
Example #26
Source File: SentinelGatewayFilterTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    GatewayApiDefinitionManager.loadApiDefinitions(new HashSet<>());
    GatewayRuleManager.loadRules(new HashSet<>());
}