跳到主要内容

Spring Cloud Alibaba 之 Gateway 集成Sentinel;SPI机制(十一)

一、Gateway 集成Sentinel

Gateway网关集成Sentinel是为了流控、熔断、降级

1、 添加sentinel-spring-cloud-gateway-adapter依赖;

<!-- sentinel-spring-cloud-gateway-adapter
Gateway集成Sentinel
-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
<version>1.8.0</version>
</dependency>

2、 添加sentinel控制台配置;

#sentinel dashboard管理后台
spring:
cloud:
sentinel:
eager: true
transport:
dashboard: 192.168.172.128:8080

3、 在配置类spring容器中配置一个Sentinel的全局过滤器;

@Configuration
public class GatewayConfiguration {

//省略号代表其它配置
......

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

......
}

4、 启动测试;

(1)启动Nacos(如果使用MySQL数据持久化,则要先启动mysql服务)

cd /opt/software/nacos/bin/

sh startup.sh -m standalone

启动MySQL服务(如果docker启动,先启动docker并挂载mysql,防止数据丢失)