0
点赞
收藏
分享

微信扫一扫

微前端框架 qiankun 配置使用【基于 vue/react脚手架创建项目 】

SpringCloudGateway之限流集成篇

SpringCloudGateway与Sentinel组件集成

添加依赖

首先确保项目包含Spring Cloud Gateway和Sentinel相关依赖。
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>
    <!-- Sentinel Spring Cloud Gateway Adapter -->
    <dependency>
        <groupId>com.alibaba.csp</groupId>
        <artifactId>sentinel-adapter-spring-cloud-gateway-2.x</artifactId>
        <version>{sentinel-version}</version>
    </dependency>
    <!-- Sentinel Core -->
    <dependency>
        <groupId>com.alibaba.csp</groupId>
        <artifactId>sentinel-core</artifactId>
        <version>{sentinel-version}</version>
    </dependency>
</dependencies>

配置Sentinel

spring:
  cloud:
    sentinel:
      transport:
        dashboard: localhost:8080 # Sentinel控制台地址
        port: 8719 # Sentinel与控制台之间的通讯端口
      filter:
        url-patterns: "/api/**" # 需要进行限流处理的路由路径

启用Sentinel Gateway适配器

import com.alibaba.csp.sentinel.adapter.gateway.sc.SentinelGatewayFilter;
import com.alibaba.csp.sentinel.adapter.gateway.sc.config.SentinelGatewayConfig;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SentinelGatewayConfiguration {

    @Bean
    public GlobalFilter sentinelGatewayFilter() {
        return new SentinelGatewayFilter();
    }

    @Bean
    public SentinelGatewayConfig sentinelGatewayConfig() {
        return new SentinelGatewayConfig();
    }
}

配置限流规则

举报

相关推荐

0 条评论