【SpringCloud-Alibaba系列教程】11.gateway与sentinel组合

简介: 在之前,我们教程已经写了一部分关于sentinel限流的方式,主要是针对于某个微服务本身进行限流,后来我们引入网关的概念,现在我们结合gateway与sentinel进行限流,主要是从一下两个纬度,第一个就是路由维度,另一种就是分组维度,下面我们根据不同维度进行实战。

引入问题
在之前,我们教程已经写了一部分关于sentinel限流的方式,主要是针对于某个微服务本身进行限流,后来我们引入网关的概念,现在我们结合gateway与sentinel进行限流,主要是从一下两个纬度,第一个就是路由维度,另一种就是分组维度,下面我们根据不同维度进行实战。

我们开始吧
首先引入pom相关文件

  <!--网关路由限流-->

       <dependency>

           <groupId>com.alibaba.csp</groupId>

           <artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>

       </dependency>

然后编写相关配置类
image.png

我们是基于sentinel的所以我们需要初始化这样一个。

//初始化限流过滤器
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public GlobalFilter sentinelGatewayFilter() {
    return new SentinelGatewayFilter();
}

然后就是编写配置类了
image.png

我们多次刷新就可以看到了
image.png

另外一个就是分组限流,主要就是通过将不同的api进行分组,指定一些接口限流。
直接上代码

/**
 * 配置限流规则
 */
@PostConstruct
private void initGatewayRules() {
    //Set<GatewayFlowRule> rules = new HashSet<>();
    //rules.add(new GatewayFlowRule("product_route")//资源名称对应的路由id
    //        .setCount(1) // 限流阈值
    //        .setIntervalSec(1) // 统计时间窗口,单位是秒,默认是 1 秒
    //);
    //GatewayRuleManager.loadRules(rules);
    Set<GatewayFlowRule> rules = new HashSet<>();
    rules.add(new GatewayFlowRule("product_api1").setCount(1).setIntervalSec(1));
    rules.add(new GatewayFlowRule("product_api2").setCount(1).setIntervalSec(1));
    GatewayRuleManager.loadRules(rules);
}
//配置限流的异常处理器
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public SentinelGatewayBlockExceptionHandler sentinelGatewayBlockExceptionHandler() {
    // Register the block exception handler for Spring Cloud Gateway.
    return new SentinelGatewayBlockExceptionHandler(viewResolvers, serverCodecConfigurer);
}
//自定义限流异常页面
@PostConstruct
public void initBlockHandlers() {
    BlockRequestHandler blockRequestHandler = new BlockRequestHandler(){
        @Override
        public Mono<ServerResponse> handleRequest(ServerWebExchange serverWebExchange, Throwable throwable) {
            Map map = new HashMap();
            map.put("code",0);
            map.put("messgae","接口被限流了...");
            return  ServerResponse.status(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON_UTF8).
                    body(BodyInserters.fromObject(map));
        }
    };
    GatewayCallbackManager.setBlockHandler(blockRequestHandler);
}
//自定义API分组
@PostConstruct
private void initCustomizedApis() {
    Set<ApiDefinition> definitions = new HashSet<>();
    ApiDefinition api1 = new ApiDefinition("product_api1")
            .setPredicateItems(new HashSet<ApiPredicateItem>() {{
                ///product-serv/product/api1开头的请求
                add(new ApiPathPredicateItem().setPattern("/product-serv/product/api1/**")
                        .setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
            }});
    ApiDefinition api2 = new ApiDefinition("product_api2")
            .setPredicateItems(new HashSet<ApiPredicateItem>() {{
                ///product-serv/product/api2
                add(new ApiPathPredicateItem().setPattern("/product-serv/product/api2/demo1"));
            }});
    definitions.add(api1);
    definitions.add(api2);
    GatewayApiDefinitionManager.loadApiDefinitions(definitions);
}

主要就是这样的两种方式进行限流。
到此,我们这一章的sentinel与gateway组合就完成了
后期会在这个项目上不断添加,喜欢的请点个start~
项目源码参考一下分支220226_xgc_gatewayAndSentinel
Gitee:https://gitee.com/coderxgc/springcloud-alibaba
GitHub:https://github.com/coderxgc/springcloud-alibaba

目录
相关文章
|
2天前
|
算法 NoSQL API
SpringCloud&Gateway网关限流
SpringCloud&Gateway网关限流
42 7
|
2天前
|
负载均衡 Nacos 数据安全/隐私保护
SpringCloud GateWay 使用
SpringCloud GateWay 使用
23 0
|
2天前
|
缓存
SpringCloud Gateway 网关的请求体body的读取和修改
SpringCloud Gateway 框架中,为了处理请求体body,实现多次读取与修改,创建了一个名为`RequestParamGlobalFilter`的全局过滤器。这个过滤器使用`@Component`和`@Slf4j`注解,实现了`GlobalFilter`和`Ordered`接口,设置最高优先级以首先读取body。它通过缓存请求体并创建装饰过的`ServerHttpRequest`来实现body的动态获取。
83 4
|
2天前
|
SpringCloudAlibaba Java 网络架构
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(七)Spring Cloud Gateway服务网关
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(七)Spring Cloud Gateway服务网关
124 0
|
2天前
|
Sentinel
Sentinel实现黑白名单控制详细教程来了
Sentinel实现黑白名单控制详细教程来了
|
2天前
|
Java 数据安全/隐私保护 Sentinel
微服务学习 | Spring Cloud 中使用 Sentinel 实现服务限流
微服务学习 | Spring Cloud 中使用 Sentinel 实现服务限流
|
2天前
|
Java 微服务 Spring
SpringCloud&Gateway全局过滤器
SpringCloud&Gateway全局过滤器
11 1
|
2天前
|
Java API Nacos
第十二章 Spring Cloud Alibaba Sentinel
第十二章 Spring Cloud Alibaba Sentinel
29 0
|
2天前
|
监控 Java API
第七章 Spring Cloud 之 GateWay
第七章 Spring Cloud 之 GateWay
23 0
|
2天前
|
JSON 安全 关系型数据库
SpringCloud Gateway 实现自定义全局过滤器 + JWT权限验证
SpringCloud Gateway 实现自定义全局过滤器 + JWT权限验证