隨着微服務的流行,服務和服務之間的穩定性變得愈來愈重要。 Sentinel 以流量爲切入點,從流量控制、熔斷降級、系統負載保護等多個維度保護服務的穩定性。java
Sentinel 具備如下特徵:git
第一步:引入pomgithub
<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>
第二步:新建一個啓動類和controllerweb
@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了代碼本身編譯,代碼地址:https://github.com/alibaba/Se...,執行命令spring
mvn clean package
會獲得:sentinel-dashboard.jar,執行命令啓動dashboard:瀏覽器
java -jar sentinel-dashboard.jar
這樣默認是8080端口,在瀏覽器輸入:http://localhost:8080,默認帳號密碼:sentinel:sentinel,看到控制檯界面爲部署成功:app
第四步:引入配置:框架
server.port=8088 spring.application.name=spring-cloud-alibaba-sentinel-demo # sentinel dashboard spring.cloud.sentinel.transport.dashboard=localhost:8080
第五步:啓動spring boot 項目,繼續訪問localhost:8080,會看到以下界面spring-boot
第六步:使用Sentinel實現接口限流(在控制檯)微服務
第七步:測試
經過上面的配置,實現的是/hello接口qps最大是2,若是qps大於2,則快速失敗,配置完成,點擊保存,咱們快速刷新瀏覽器,會發現快速失敗
這篇文章只是springcloud和sentinel的入門,複雜的一些代碼配置和文件配置後期介紹
更多文章歡迎關注我的博客:https://www.zplxjj.com和公衆號