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