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