Springboot(五十)SpringBoot3集成sentinel并连接sentinel-dashboard

614 阅读4分钟

对,你没看错,又是sentinel。我真是够了,而且,我觉得这应该不是最后一次,以后应该还会写到关于sentinel的学习记录。

 

前边我们了解了sentinel如何使用。相对来讲还是比较简单的。之后学到自定义注解的时候,还自定义了一个sentinel注解来实现限流。用着相对来讲还是很方便的。

 

但是呢,有一个小小的问题。官方推荐使用sentinel-dashboard,这玩意我一直没用明白。我得项目一直是注册不到sentinel-dashboard,我很费解。

 

看了官方文档,没有找到具体是怎么链接。很费解。

 

官方文档: https://github.com/alibaba/Sentinel/wiki/Dashboard

这个是再github上的官方文档。这个是没有问题的。

 

Github的文档有的时候是访问不到的。可以试下官方文档。

 

官方网站:sentinelguard.io/zh-cn/index…

这个是再github上的官方文档。这个也是没有问题的。

 

下边是我在项目中使用sentinel-dashboard的全过程。可跟做。

 

一:部署sentinel-dashboard

1:Docker命令:(在linux服务器上使用)

docker run --restart always -d --name sentinel -p 8858:8858 -d bladex/sentinel-dashboard:1.8.8

这里得sentinel-dashboard版本要和你项目中使用的sentinel版本一致。

 

2:本地启动sentinel-dashboard命令:

java -Dserver.port=8858 -Dcsp.sentinel.dashboard.server=localhost:8858 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.8.jar

用上边的命令启动,默认账号密码都是:sentinel

 

对于用户登录的相关配置可以在启动命令中增加下面的参数来进行配置:

1.       -Dsentinel.dashboard.auth.username=sentinel: 用于指定控制台的登录用户名为 sentinel;

2.       -Dsentinel.dashboard.auth.password=123456: 用于指定控制台的登录密码为 123456;如果省略这两个参数,默认用户和密码均为 sentinel

3.       -Dserver.servlet.session.timeout=7200: 用于指定 Spring Boot 服务端 session 的过期时间,如 7200 表示 7200 秒;60m 表示 60 分钟,默认为 30 分钟;

 

3:本地IDEA开发项目使用sentinel注册sentinel-dashboard

在IDEA中添加虚拟机选项即可。

1.png.jpg

 

4:运行jar包注册sentinel-dashboard

如果打成jar包之后无法注册sentinel,在启动jar命令加上-Dcsp.sentinel.dashboard.server=1.15.157.156:8858即可,例如:

java -Dcsp.sentinel.dashboard.server=1.15.157.156:8858 -jar entry-0.0.1-SNAPSHOT.jar --jasypt.encryptor.password=camellia

 

5:注意点

这里需要注意一个小小的问题。

Springboot项目注册到sentinel-dashboard需要引入pom依赖:

<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-transport-simple-http</artifactId>
    <version>版本号</version>
</dependency>

 

sentinel-dashboard需要和你的项目进行双向通信,所以说如果你在本地开发项目,你链接的sentinel-dashboard就需要运行在本地,这样才能进行双向通信。


如果你的sentinel-dashboard运行在服务器上,是没有办法实现双向通信的。

 

二:项目集成sentinel-dashboard

1:添加pom依赖

<!-- Sentinel 对 Spring Cloud Alibaba 的适配 -->
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-core</artifactId>
    <version>1.8.8</version>
</dependency>
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!-- SpringBoot 使用 @SentinelResource(value = "AddUser", blockHandler = "exceptionHandler") 需要的依赖 -->
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-annotation-aspectj</artifactId>
    <version>1.8.8</version>
</dependency>
<!-- sentinel客户端与dashboard通信依赖 版本与控制台保持一致即可 -->
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-transport-simple-http</artifactId>
    <version>1.8.8</version>
</dependency>

 

2:运行项目,项目自动注册到sentinel-dashboard

2.png.jpg

 

3:使用sentinel限流注解@SentinelResource

我一直在想,为啥要使用sentinel提供的 @SentinelResource注解呢?直接使用我自己定义的 @MySentinelResource限流注解不就可以了嘛。

 

换个角度考虑这个问题,使用sentinel提供的 @SentinelResource注解可以实时在sentinel-dashboard调整限流规则来应对各种可能回出现的问题,如果在规则在代码中声明,那就做不到实时修改了。还是我格局小了。

 

(1):创建限流规则msg1(名字我随便起的,不要介意)

如下图所示:

3.jpg

 

(2):在项目中使用@SentinelResource调用限流规则

代码如下所示:

@GetMapping("article/testMail")
@SentinelResource(value = "msg1"// sentinel限流注解
public Map<String, Object> testMail() throws Exception
{
    System.out.println("testmail");
    return null;
}

 

浏览器访问:http://127.0.0.1:8001/java/article/testMail

疯狂刷新几次,IDEA控制台输出:

testmail
2024-11-26T14:02:14.679+08:00 ERROR 26620 --- [blog-business-article] [nio-8001-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet]    
: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: com.alibaba.csp.sentinel.slots.block.flow.FlowException] with root cause
com.alibaba.csp.sentinel.slots.block.flow.FlowException: null

 

说明限流规则使用成功。

 

(3):sentinel-dashboard实时统计

4.png.jpg

 

我目前能用到的就是sentinel的流控规则,除此之外,sentinel还可以设置熔断、热点、系统、授权、集群流控等规则设置,这些后边用到的时候再说。

 

有好的建议,在下方输入你的评论。