一、Sentinel
Sentinel GitHub地址:
https://github.com/alibaba/Sentinel
關於Sentinel詳細介紹:
https://github.com/alibaba/Sentinel/wiki/%E4%BB%8B%E7%BB%8D
Sentinel官方文檔:
https://sentinelguard.io/zh-cn/docs/introduction.html
這里只明確一點,Sentinel主要作用是在高流量下如何保持服務的穩定性。
二、下載運行Sentinel
1.下載源代碼
git clone https://github.com/alibaba/Sentinel.git
2.打包
cd Sentinel
mvn clean package -Dmaven.test.skip=true
特別注意:
Sentinel項目路徑不能在有中文,否則會報錯。
3.打包成功指定jar運行
java -jar sentinel-dashboard.jar
Linux部署只需執行jar即可。
平時如果需要調試,只需導入Sentinel到IDE,然后直接運行DashboardApplication.java文件即可(sentinel-dashboard模塊下)。
三、SpringCloud整合Sentinel
注意:
我的SpringCloud Version為Hoxton.SR4。
1.Maven依賴
父pom.xml
<dependencyManagement> <dependencies> <!-- SpringCloud Alibaba 微服務 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.2.1.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
子pom.xml(將其依賴放入網關模塊里的pom.xml即可):
<!-- SpringCloud Ailibaba Sentinel --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> <!-- SpringCloud Ailibaba Sentinel Gateway --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId> </dependency>
2.gateway模塊配置文件(application.yml),添加如下內容
spring: cloud: sentinel: # 取消控制台懶加載 eager: true transport: # 控制台地址 dashboard: 127.0.0.1:8718
我的詳細配置文件內容如下:
server: port: 8080 spring: cloud: sentinel: # 取消控制台懶加載 eager: true transport: # 控制台地址 dashboard: 127.0.0.1:8718 gateway: discovery: locator: lowerCaseServiceId: true enabled: true routes: # 第三方API - id: blog-api uri: lb://blog-api predicates: - Path=/api/** filters: - StripPrefix=1 ## 后台 - id: blog-admin uri: lb://blog-admin predicates: - Path=/admin/** filters: - StripPrefix=1 ## PC端 - id: blog-portal uri: lb://blog-portal predicates: - Path=/portal/** filters: - StripPrefix=1 ## 工具 - id: blog-tools uri: lb://blog-tools predicates: - Path=/blog-tools/** filters: - StripPrefix=1 application: name: blog-gateway-server eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/
3.啟動項目
啟動項目不報錯,然后訪問Sentinel,有如下效果,表示整合成功:
接着訪問網關下的blog-api的接口,Sentinel實時監控效果如下:
4.說明
Sentinel功能很強大,這里列舉僅僅是整合,如用得比較多需深入學習。