Spring Cloud Gateway集成Sentinel 1.8.6
时间: 2024-01-10 19:02:40 浏览: 229
spring cloud gateway整合sentinel实现网关限流
3星 · 编辑精心推荐
Spring Cloud Gateway集成Sentinel 1.8.6的步骤如下:
1. 引入依赖
在pom.xml文件中引入Spring Cloud Gateway和Sentinel的依赖:
```xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
```
2. 配置Sentinel
在application.yml文件中配置Sentinel的相关信息:
```yaml
spring:
cloud:
sentinel:
transport:
dashboard: localhost:8080
filter:
enabled: true
```
其中,transport.dashboard配置了Sentinel Dashboard的地址,filter.enabled配置了是否开启Sentinel的过滤器。
3. 配置Gateway
在Gateway的配置类中添加Sentinel的过滤器:
```java
@Configuration
public class GatewayConfig {
@Bean
public SentinelGatewayFilterFactory sentinelGatewayFilterFactory() {
return new SentinelGatewayFilterFactory();
}
@Bean
public GatewayFilterChain gatewayFilterChain(RouteLocator routeLocator,
List<GatewayFilterFactory> gatewayFilterFactories) {
DefaultGatewayFilterChain chain = new DefaultGatewayFilterChain(routeLocator.getRoutes(), gatewayFilterFactories);
return chain;
}
}
```
4. 配置限流规则
在Sentinel Dashboard中配置限流规则,即可对Gateway进行限流。
完成上述步骤后,就可以在Spring Cloud Gateway中集成Sentinel 1.8.6了。
阅读全文