上一篇:SpringCloudAlibaba集成Gateway動態(tài)路由Nacos服務(wù)
之前我們搭建了網(wǎng)關(guān)項目gateway-web,進行api管理。
我們之前使用Sentinel對web項目的接口和Dubbo 的RPC接口分別進行了流控,那么Sentinel能對網(wǎng)關(guān)項目進行流控嗎,答案是肯定的。
Sentinel的GitHub中對適配Gateway做了介紹和demo,傳送門:https://github.com/alibaba/Sentinel/wiki/%E4%B8%BB%E6%B5%81%E6%A1%86%E6%9E%B6%E7%9A%84%E9%80%82%E9%85%8D#api-gateway-%E9%80%82%E9%85%8D
但是這個文檔已經(jīng)比較老, 可以看到wiki是2年前編寫的,由于我們現(xiàn)在用的SpringCloud和SpringBoot依賴是最新的,所以官方文檔中的使用方式并不太適用。
下面來介紹一下,最新的使用方式。
pom.xml中引入依賴
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
application.yml文件中添加配置
spring:
application:
name: gateway-web
cloud:
sentinel:
transport:
dashboard: 127.0.0.1:8888
port: 8888
啟動gateway-web項目,訪問gateway-web接口:

image.png
可以在Sentinel管理后臺發(fā)現(xiàn)gateway-web服務(wù)

image.png
對admin-1添加流控規(guī)則,設(shè)置qps為1:

image.png
瘋狂訪問gateway-web接口,出現(xiàn)錯誤信息:

image.png
打完收工。