SpringCloudGateway之限流集成篇
SpringCloudGateway与Sentinel组件集成
添加依赖
首先确保项目包含Spring Cloud Gateway和Sentinel相关依赖。
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-adapter-spring-cloud-gateway-2.x</artifactId>
<version>{sentinel-version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
<version>{sentinel-version}</version>
</dependency>
</dependencies>
配置Sentinel
spring:
cloud:
sentinel:
transport:
dashboard: localhost:8080
port: 8719
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();
}
}
配置限流规则