08--gateway 集成sentinel

客官°小女子只卖身不卖艺 2022-12-23 00:56 173阅读 0赞
  1. 前面我们已经说了WebFlux集成sentinel,此集成在gateway中同样适用.
  2. gateway集成sentinel开发包


    com.alibaba.cloud
    spring-cloud-alibaba-sentinel-gateway
    1. <dependency>
    2. <groupId>com.alibaba.csp</groupId>
    3. <artifactId>sentinel-datasource-nacos</artifactId>
    4. </dependency>
  3. sentinel中可以做为 resource 的资源

    1. 网关中 的 route.id
    2. 自定义api分组
    3. 链接
  4. 接下来我们 自定义 api分组,限流规则及异常处理器

    package com.cloud.alibaba.config.sentinel.gateway;

    import com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants;
    import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition;
    import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem;
    import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem;
    import com.alibaba.csp.sentinel.adapter.gateway.common.api.GatewayApiDefinitionManager;
    import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;
    import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem;
    import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager;
    import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler;
    import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
    import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.MediaType;
    import org.springframework.web.reactive.function.BodyInserters;
    import org.springframework.web.reactive.function.server.ServerResponse;

    import javax.annotation.PostConstruct;
    import java.util.*;

    /* 限流规则配置类 @author admin */
    @Configuration
    public class GatewayConfiguration {

    1. @PostConstruct
    2. public void doInit() {
    3. // 自定义api分组
    4. initCustomizedApis();
    5. // 加载网关限流规则
    6. initGatewayRules();
    7. // 加载自定义限流异常处理器
    8. initBlockHandler();
    9. }
    10. /** * 自定义api分组 */
    11. private void initCustomizedApis() {
    12. Set<ApiDefinition> definitions = new HashSet<>();
    13. // product-api 组
    14. ApiDefinition api1 = new ApiDefinition("producer-example-api")
    15. .setPredicateItems(new HashSet<ApiPredicateItem>() { {
    16. add(new ApiPathPredicateItem().setPattern("/producer/**")
    17. /** * 网址匹配策略前缀 /producer 和 /producer/** */
    18. .setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
    19. /** * 只匹配/producer/getUser */
    20. add(new ApiPathPredicateItem().setPattern("/producer/getUser"));
    21. }});
    22. definitions.add(api1);
    23. /** * 加载限流分组 */
    24. GatewayApiDefinitionManager.loadApiDefinitions(definitions);
    25. }
    26. /** * 网关限流规则 */
    27. private void initGatewayRules() {
    28. Set<GatewayFlowRule> rules = new HashSet<>();
    29. /** * PARAM_PARSE_STRATEGY_URL_PARAM 参数提取策略 任意 URL 参数 * PARAM_MATCH_STRATEGY_CONTAINS 匹配规则 URL 参数中是否包含 FieldName。示例中是type字段 * 通俗讲就是 如果url携带type参数。将走此规则。否则不走此规则 * 示例 http://192.168.1.25:10004/producer/getUserName?type=1 走此规则 * 示例 http://192.168.1.25:10004/producer/getUserName 不走此规则 * 其余的规则 参考 https://github.com/alibaba/Sentinel/blob/master/sentinel-demo/sentinel-demo-spring-cloud-gateway/src/main/java/com/alibaba/csp/sentinel/demo/spring/sc/gateway/GatewayConfiguration.java */
    30. rules.add(new GatewayFlowRule("producer-example-api")
    31. .setResourceMode(SentinelGatewayConstants.RESOURCE_MODE_CUSTOM_API_NAME)
    32. .setGrade(1)
    33. .setCount(1)
    34. .setIntervalSec(1)
    35. .setParamItem(new GatewayParamFlowItem()
    36. .setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM)
    37. .setFieldName("type")
    38. .setMatchStrategy(SentinelGatewayConstants.PARAM_MATCH_STRATEGY_CONTAINS)
    39. )
    40. );
    41. GatewayRuleManager.loadRules(rules);
    42. }
    43. /** * 自定义限流异常处理器 */
    44. private void initBlockHandler() {
    45. BlockRequestHandler blockRequestHandler = (serverWebExchange, throwable) -> {
    46. ParamFlowException paramFlowException = (ParamFlowException) throwable;
    47. Map<String, String> result = new HashMap<>(3);
    48. result.put("state", String.valueOf(HttpStatus.TOO_MANY_REQUESTS.value()));
    49. result.put("msg", HttpStatus.TOO_MANY_REQUESTS.getReasonPhrase());
    50. result.put("route", paramFlowException.getResourceName());
    51. return ServerResponse.status(HttpStatus.TOO_MANY_REQUESTS)
    52. .contentType(MediaType.APPLICATION_JSON)
    53. .body(BodyInserters.fromObject(result));
    54. };
    55. // 加载自定义限流异常处理器
    56. GatewayCallbackManager.setBlockHandler(blockRequestHandler);
    57. }

    }

GatewayFlowRule规则配置

源码地址

发表评论

表情:
评论列表 (有 0 条评论,173人围观)

还没有评论,来说两句吧...

相关阅读