springcloudAlibaba_sentinel quickstart
目录
概述
Sentinel是什么
一句话解释Sentinel,就是之前介绍过的:Hystrix 实现服务降级、服务熔断、服务限流,Sentinel 后起之秀,更香
随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。
Sentinel 具有的优势
- 丰富的应用场景:Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、集群流量控制、实时熔断下游不可用应用等。
- 完备的实时监控:Sentinel 同时提供实时的监控功能。您可以在控制台中看到接入应用的单台机器秒级数据,甚至 500 台以下规模的集群的汇总运行情况。
- 广泛的开源生态:Sentinel 提供开箱即用的与其它开源框架/库的整合模块,例如与 Spring Cloud、Dubbo、gRPC 的整合。您只需要引入相应的依赖并进行简单的配置即可快速地接入 Sentinel。
- 完善的 SPI 扩展点:Sentinel 提供简单易用、完善的 SPI 扩展接口。您可以通过实现扩展接口来快速地定制逻辑。例如定制规则管理、适配动态数据源等。
Sentinel主要特性
Sentinel 的开源生态
Sentinel 分为两个部分
- 核心库(Java 客户端)不依赖任何框架/库,能够运行于所有 Java 运行时环境,同时对 Dubbo / Spring Cloud 等框架也有较好的支持。
- 控制台(Dashboard)基于 Spring Boot 开发,打包后可以直接运行,不需要额外的 Tomcat 等应用容器。
安装仪表盘
下载 sentinel-dashboard-1.8.0.jar
前提:
- java8环境ok
- 8080端口不能被占用:
lsof -i :8080
运行命令:
|
|
访问管理页面
http://localhost:8080/
用户名/密码:sentinel/sentinel
初始化演示
-
新建工程
-
pom
1 2 3 4
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency>
完整pom
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies>
-
yml,用来配置Dashboard
1 2 3 4 5 6 7 8 9 10 11 12 13
server: port: 8401 spring: application: name: cloudalibaba-sentinel-service8401 cloud: sentinel: transport: dashboard: localhost:8080 # 指定应用与sentinel控制台交互的端口,应用本地会起一个该端口占用的httpserver # 默认8719,假如被占用了会自动从8719开始依次+1扫描,直至找到未被占用的端口 port: 8719
-
主启动
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
package com.eh.cloudalibaba.sentinel; import com.alibaba.csp.sentinel.annotation.SentinelResource; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication public class ServiceMain8401 { public static void main(String[] args) { SpringApplication.run(ServiceMain8401.class, args); } @RestController public class TestController { @GetMapping(value = "/hello") @SentinelResource("hello") public String hello() { return "Hello Sentinel"; } } }
-
启动主程序,访问http://localhost:8401/hello,查看Dashboard变化
注意:Sentinel采用懒加载机制,需要先访问服务,才能在Dashboard上看到反应,如下: