SpringCloudGateway之Sentinel限流

SpringCloudGateway网关配置

<!-- FIXME ====================================================== SpringCloudGateway基于Sentinel网关限流的适配器 -->
<dependency>
	<groupId>com.alibaba.csp</groupId>
	<artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
</dependency>

路由维度限流(灵活性差,力度粗)

// 添加配置类代码
package com.springcloudalibaba.gateway.configurer;

import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager;
import com.alibaba.csp.sentinel.adapter.gateway.sc.SentinelGatewayFilter;
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.adapter.gateway.sc.exception.SentinelGatewayBlockExceptionHandler;
import com.springcloudalibaba.bclass.base.BaseResult;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.reactive.result.view.ViewResolver;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import javax.annotation.PostConstruct;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Description: TODO SpringCloudGateway基于Sentinel进行限流是通过Filter实现的。
 * 需要向容器中注入SentinelGatewayFilter过滤器和SentinelGatewayBlockExceptionHandler异常回调
 * @Author: lixing
 * @Date: 2020/11/21 15:40
 */
@Configuration
public class RequestRateLimiterForGatewaySentinelRouteConfig {
    
    
    private final List<ViewResolver> viewResolverList;
    private final ServerCodecConfigurer serverCodecConfigurer;

    public RequestRateLimiterForGatewaySentinelRouteConfig(ObjectProvider<List<ViewResolver>> viewResolverListProvider, ServerCodecConfigurer serverCodecConfigurer) {
    
    
        this.viewResolverList = viewResolverListProvider.getIfAvailable(Collections::emptyList);
        this.serverCodecConfigurer = serverCodecConfigurer;
    }

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

    // 初始化 SentinelGatewayBlockExceptionHandler 限流异常处理器
    @Bean
    @Order(Ordered.HIGHEST_PRECEDENCE)
    public SentinelGatewayBlockExceptionHandler sentinelGatewayBlockExceptionHandler() {
    
    
        return new SentinelGatewayBlockExceptionHandler(viewResolverList, serverCodecConfigurer);
    }

    // 自定义限流异常页面
    @PostConstruct
    public void initBlockHandlers() {
    
    
        BlockRequestHandler blockRequestHandler = new BlockRequestHandler() {
    
    
            @Override
            public Mono<ServerResponse> handleRequest(ServerWebExchange serverWebExchange, Throwable throwable) {
    
    
                return ServerResponse.status(HttpStatus.OK).
                        contentType(MediaType.APPLICATION_JSON).
                        body(BodyInserters.fromValue(new BaseResult<String>().fail("触发流控")));
            }
        };
        GatewayCallbackManager.setBlockHandler(blockRequestHandler);
    }

    // 配置初始化限流参数
    @PostConstruct
    public void initGatewayRules() {
    
    
        Set<GatewayFlowRule> gatewayFlowRuleSet = new HashSet<>();
        // 添加 BFF-API-AUTH-ROUTE路由 限流规则
        gatewayFlowRuleSet.add(
                new GatewayFlowRule("BFF-API-AUTH-ROUTE") // 路由ID
                        .setCount(1) // 限流阈值
                        .setIntervalSec(1)); // 统计时间窗口,默认单位秒
        // 添加 BFF-API-ADMIN-ROUTE路由 限流规则
        gatewayFlowRuleSet.add(
                new GatewayFlowRule("BFF-API-ADMIN-ROUTE") // 路由ID
                        .setCount(1) // 限流阈值
                        .setIntervalSec(1)); // 统计时间窗口,默认单位秒
        GatewayRuleManager.loadRules(gatewayFlowRuleSet);
    }
}

自定义API维度限流(对请求路径进行分组,再对分组进行限流,灵活性好,粒度细)

package com.springcloudalibaba.apiAuth.controller;

import com.springcloudalibaba.bclass.base.BaseResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @描述:测试
 * @作者:[email protected]
 * @时间:2021/9/17 14:26
 */
@RestController
@RequestMapping("demo")
public class DemoController {
    
    
    @GetMapping("test1/fun1")
    public BaseResult<String> test1Fun1() {
    
    
        return new BaseResult<String>().success("test1Fun1");
    }

    @GetMapping("test1/fun2")
    public BaseResult<String> test1Fun2() {
    
    
        return new BaseResult<String>().success("test1Fun2");
    }

    @GetMapping("test2/fun1")
    public BaseResult<String> test2Fun1() {
    
    
        return new BaseResult<String>().success("test2Fun1");
    }

    @GetMapping("test2/fun2")
    public BaseResult<String> test2Fun2() {
    
    
        return new BaseResult<String>().success("test2Fun2");
    }
}
// 添加配置类代码
package com.springcloudalibaba.gateway.configurer;

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.GatewayRuleManager;
import com.alibaba.csp.sentinel.adapter.gateway.sc.SentinelGatewayFilter;
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.adapter.gateway.sc.exception.SentinelGatewayBlockExceptionHandler;
import com.springcloudalibaba.bclass.base.BaseResult;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.reactive.result.view.ViewResolver;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import javax.annotation.PostConstruct;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Description: TODO SpringCloudGateway基于Sentinel进行限流是通过Filter实现的。需要向容器中注入SentinelGatewayFilter过滤器和SentinelGatewayBlockExceptionHandler异常回调
 * @Author: lixing
 * @Date: 2020/11/21 15:40
 */
@Configuration
public class RequestRateLimiterForGatewaySentinelApiConfig {
    
    
    private final List<ViewResolver> viewResolverList;
    private final ServerCodecConfigurer serverCodecConfigurer;

    public RequestRateLimiterForGatewaySentinelApiConfig(ObjectProvider<List<ViewResolver>> viewResolverListProvider, ServerCodecConfigurer serverCodecConfigurer) {
    
    
        this.viewResolverList = viewResolverListProvider.getIfAvailable(Collections::emptyList);
        this.serverCodecConfigurer = serverCodecConfigurer;
    }

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

    // 初始化 SentinelGatewayBlockExceptionHandler 限流异常处理器
    @Bean
    @Order(Ordered.HIGHEST_PRECEDENCE)
    public SentinelGatewayBlockExceptionHandler sentinelGatewayBlockExceptionHandler() {
    
    
        return new SentinelGatewayBlockExceptionHandler(viewResolverList, serverCodecConfigurer);
    }

    // 自定义限流异常页面
    @PostConstruct
    public void initBlockHandlers() {
    
    
        BlockRequestHandler blockRequestHandler = new BlockRequestHandler() {
    
    
            @Override
            public Mono<ServerResponse> handleRequest(ServerWebExchange serverWebExchange, Throwable throwable) {
    
    
                return ServerResponse.status(HttpStatus.OK).
                        contentType(MediaType.APPLICATION_JSON).
                        body(BodyInserters.fromValue(new BaseResult<String>().fail("触发流控")));
            }
        };
        GatewayCallbackManager.setBlockHandler(blockRequestHandler);
    }

    // 自定义API分组
    @PostConstruct
    public void initCustomizedApis() {
    
    
        Set<ApiDefinition> apiDefinitionSet = new HashSet<>();
        // 自定义 apiAuth_test1 分组 /springCloudGateway/gatewayAuth/
        ApiDefinition apiAuth_test1 = new ApiDefinition("apiAuth_test1").setPredicateItems(new HashSet<ApiPredicateItem>(){
    
    
            {
    
    
                // 添加断言,该值的内容为:配置文件spring.cloud.gateway.routes.predicates的值 + 自定义规则
                add(new ApiPathPredicateItem().setPattern("/springCloudGateway/gatewayAuth/demo/test1/**").setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));
            }
        });

        // 自定义 apiAuth_test1 分组 /springCloudGateway/gatewayAuth/
        ApiDefinition apiAuth_test2 = new ApiDefinition("apiAuth_test2").setPredicateItems(new HashSet<ApiPredicateItem>(){
    
    
            {
    
    
                // 添加断言,该值的内容为:配置文件spring.cloud.gateway.routes.predicates的值 + 自定义规则
                add(new ApiPathPredicateItem().setPattern("/springCloudGateway/gatewayAuth/demo/test2/fun1"));
                add(new ApiPathPredicateItem().setPattern("/springCloudGateway/gatewayAuth/demo/test2/fun2"));
            }
        });
        //
        apiDefinitionSet.add(apiAuth_test1);
        apiDefinitionSet.add(apiAuth_test2);
        GatewayApiDefinitionManager.loadApiDefinitions(apiDefinitionSet);
    }

    // 配置初始化限流参数
    @PostConstruct
    public void initGatewayRules() {
    
    
        Set<GatewayFlowRule> gatewayFlowRuleSet = new HashSet<>();
        // 添加 apiAuth_test1分组 限流规则
        gatewayFlowRuleSet.add(new GatewayFlowRule(("apiAuth_test1")).setCount(1).setIntervalSec(1));
        // 添加 apiAuth_test2分组 限流规则
        gatewayFlowRuleSet.add(new GatewayFlowRule(("apiAuth_test2")).setCount(1).setIntervalSec(1));
        //
        GatewayRuleManager.loadRules(gatewayFlowRuleSet);
    }

}

猜你喜欢

转载自blog.csdn.net/qq_30818545/article/details/121712632