Sentinel安裝和項目整合Sentinel
今日目標
- 安裝Sentinel
- 項目整合Sentinel
昨天我們已經介紹了Sentinel的原理,今天來了解一下Sentinel快速入門
1. Sentinel介紹和安裝
1.1.初識Sentinel
Sentinel是阿里巴巴開源的一款微服務流量控制組件。官網地址:https://sentinelguard.io/zh-cn/index.html
Sentinel 具有以下特征:
?豐富的應用場景:Sentinel 承接了阿里巴巴近 10 年的雙十一大促流量的核心場景,例如秒殺(即突發流量控制在系統容量可以承受的范圍)、消息削峰填谷、集群流量控制、實時熔斷下游不可用應用等。
?完備的實時監控:Sentinel 同時提供實時的監控功能。您可以在控制臺中看到接入應用的單臺機器秒級數據,甚至 500 臺以下規模的集群的匯總運行情況。
?廣泛的開源生態:Sentinel 提供開箱即用的與其它開源框架/庫的整合模塊,例如與 Spring Cloud、Dubbo、gRPC 的整合。您只需要引入相應的依賴并進行簡單的配置即可快速地接入 Sentinel。
?完善的 SPI 擴展點:Sentinel 提供簡單易用、完善的 SPI 擴展接口。您可以通過實現擴展接口來快速地定制邏輯。例如定制規則管理、適配動態數據源等。
1.2. Sentinel安裝
相對于于前面Nacos的下載、shell指令的啟動,Sentinel為開發者提供開箱即用的jar包,小伙伴可以從 release頁面 下載不同版本的控制臺 jar 包。我使用的是sentinel-dashboard-1.8.6.jar
圖片
1.2.1.Sentinel運行
將jar包放到任意非中文目錄,執行命令:
java -Dserver.port=8080 -jar sentinel-dashboard-1.8.6.jar
圖片
如果要修改Sentinel的默認端口、賬戶、密碼,可以通過下列配置:
配置項 | 默認值 | 說明 |
server.port | 8080 | 服務端口 |
sentinel.dashboard.auth.username | sentinel | 默認用戶名 |
sentinel.dashboard.auth.password | sentinel | 默認密碼 |
例如,修改端口:
java -Dserver.port=8888 -jar sentinel-dashboard-1.8.6.jar
如果您覺得本文不錯,歡迎關注,點贊,收藏支持,您的關注是我堅持的動力!
1.2.2.Sentinel訪問
訪問http://localhost:8080頁面,可以訪問sentinel的控制臺了:
圖片
需要輸入賬號和密碼,默認都是:sentinel
登錄后,發現一片空白,什么都沒有:
圖片
這是因為我們還沒有與微服務整合
3. 微服務整合Sentinel
Sentinel項目實際上和前面介紹的Seata項目是項目的,不過我這里是將項目名稱進行修改
【步驟一】:環境導入微服務
獲取代碼地址:
https://github.com/bangbangzhou/learn_springboot/tree/main/sentinel-study
圖片
sentinel-study:父工程,負責管理項目依賴
- sentinel-account-service:賬戶服務,用于用戶的資金管理。提供扣減余額的接口
- sentinel-order-service:庫存服務,用于管理商品庫存。提供扣減庫存的接口,創建訂單時,需要調用sentinel-account-service和sentinel-storage-service
- sentinel-storage-service:訂單服務,用于管理訂單。
【步驟二】:引入Sentinel依賴
在sentinel-order-service服務中新增sentinel的pom依賴,如下:
<!--流控降級管理-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
【步驟三】:配置控制臺Sentinel
在sentinel-order-service服務中修改application.yaml文件,添加下面內容:
spring:
cloud:
sentinel:
transport:
dashboard: localhost:8080
【步驟四】:啟動項目
啟動order、Account和Storage三個微服務
圖片
【步驟五】:訪問order-service的任意端點
打開POSTMAN,
- 請求方式:POST
- 訪問地址:http://localhost:9092/order?userId=user20230929&commodityCode=100202003032041&count=3&mnotallow=30
圖片
- 這樣才能觸發sentinel的監控。
然后再訪問sentinel的控制臺,查看效果:
圖片