隨着微服務的流行,服務和服務之間的穩定性變得愈來愈重要。 Sentinel 以流量爲切入點,從流量控制、熔斷降級、系統負載保護等多個維度保護服務的穩定性。java
Sentinel 具備如下特徵:git
豐富的應用場景: Sentinel 承接了阿里巴巴近 10 年的雙十一大促流量的核心場景,例如秒殺(即突發流量控制在系統容量能夠承受的範圍)、消息削峯填谷、實時熔斷下游不可用應用等。github
完備的實時監控: Sentinel 同時提供實時的監控功能。您能夠在控制檯中看到接入應用的單臺機器秒級數據,甚至 500 臺如下規模的集羣的彙總運行狀況。web
普遍的開源生態: Sentinel 提供開箱即用的與其它開源框架/庫的整合模塊,例如與 Spring Cloud、Dubbo、gRPC 的整合。您只須要引入相應的依賴並進行簡單的配置便可快速地接入 Sentinel。spring
完善的 SPI 擴展點: Sentinel 提供簡單易用、完善的 SPI 擴展點。您能夠經過實現擴展點,快速的定製邏輯。例如定製規則管理、適配數據源等。瀏覽器
第一步:引入pomapp
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
複製代碼
第二步:新建一個啓動類和controller框架
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@RestController
public class TestController {
@GetMapping(value = "/hello")
@SentinelResource("hello")
public String hello() {
return "Hello Sentinel";
}
}
複製代碼
第三步:引入dashboard 能夠直接下載sentinel-dashboard的jar包,也能夠本身編譯,我這邊這裏clone了代碼本身編譯,代碼地址:github.com/alibaba/Sen…,執行命令spring-boot
mvn clean package
複製代碼
會獲得:sentinel-dashboard.jar,執行命令啓動dashboard:微服務
java -jar sentinel-dashboard.jar
複製代碼
這樣默認是8080端口,在瀏覽器輸入:http://localhost:8080,默認帳號密碼:sentinel:sentinel,看到控制檯界面爲部署成功:
第四步:引入配置:
server.port=8088
spring.application.name=spring-cloud-alibaba-sentinel-demo
# sentinel dashboard
spring.cloud.sentinel.transport.dashboard=localhost:8080
複製代碼
第五步:啓動spring boot 項目,繼續訪問localhost:8080,會看到以下界面
第六步:使用Sentinel實現接口限流(在控制檯)
第七步:測試 經過上面的配置,實現的是/hello接口qps最大是2,若是qps大於2,則快速失敗,配置完成,點擊保存,咱們快速刷新瀏覽器,會發現快速失敗
這篇文章只是springcloud和sentinel的入門,複雜的一些代碼配置和文件配置後期介紹
更多文章歡迎關注我的博客:www.zplxjj.com和公衆號