本篇开始记录一下Sentinel的基本使用,各位看到此博客的小伙伴,如有不对的地方请及时通过私信我或者评论此博客的方式指出,以免误人子弟。多谢!
环境说明: 统一改为官方推荐的版本,部分截图版本与实际不一致。
版本对应 点此查看
目录
介绍
参考官方文档 点此查看
Sentinel可用用来替代Hystrix,以下是借用一张网上博客的图片。
安装Sentinel控制台
下载
下载地址 点此下载 我下载的是1.8.1版本
安装运行
进入下载的jar报路径,cmd命令窗口运行jar包即可启动,命令如下:
java -jar sentinel-dashboard-1.8.1.jar
查看控制台
Sentinel默认端口8080,默认用户名密码都为sentinel,浏览器访问sentinel管理界面 http://localhost:8080/ 使用默认用户名密码登录即可:
管理界面如下:
如上,什么目录都没有,那是因为Sentinel采用的懒加载,只有你访问一次项目后才能在管理界面看到目录,接下来我们新建一个服务客户端接入Sentinel控制台。
客户端接入控制台
新建 alibaba-sentinel-server 服务模块。
pom文件
<dependencies>
<!--SpringCloud ailibaba nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--SpringCloud ailibaba sentinel-datasource-nacos 后续做持久化用到-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<!--SpringCloud ailibaba sentinel -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!--openfeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- SpringBoot整合Web组件+actuator -->
<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>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.6.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
yml文件
server:
port: 8401
spring:
application:
name: cloudalibaba-sentinel-service
cloud:
nacos:
discovery:
#Nacos服务注册中心地址
server-addr: localhost:8848
sentinel:
transport:
#配置Sentinel dashboard地址
dashboard: localhost:8080
#默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口
port: 8719
management:
endpoints:
web:
exposure:
include: '*'
启动类
启动类添加@EnableDiscoveryClient注解。
测试类
@RestController
public class FlowLimitController{
@GetMapping("/testA")
public String testA(){
return "------testA";
}
}
测试
启动服务,多次访问 http://localhost:8401/testA 再次查看Sentinel管理界面即可看到目录,如下:
如上说明:Sentinel 8080端口 正在监控微服务8401