2.控制台(Dashboard)基于 Spring Boot 开发,打包后可以直接运行,不需要额外的 Tomcat 等应用容器。
#!/bin/bash java -Dsentinel.dashboard.auth.username=admin \ -Dsentinel.dashboard.auth.password=admin \ -Dserver.port=8084 -Dcsp.sentinel.dashboard.server=localhost:8084 \ -Dproject.name=sentinel-dashboard \ -jar sentinel-dashboard-1.6.3.jar &用户可以通过如下参数进行配置:
-Dsentinel.dashboard.auth.username=admin 用于指定控制台的登录用户名为 admin; -Dsentinel.dashboard.auth.password=admin 用于指定控制台的登录密码为 admin;如果省略这两个参数,默认用户和密码均为 sentinel; -Dserver.servlet.session.timeout=7200 用于指定 Spring Boot 服务端 session 的过期时间,如 7200 表示 7200 秒;60m 表示 60 分钟,默认为 30 分钟;
<!-- https://www.duidaima.com --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.5.RELEASE</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> </dependencies> <dependencyManagement> <!--注意跟 SpringBoot 保持一致 2.1.x for Spring Boot 2.1.x--> <dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.1.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>配置文件:
# 应用名称 https://www.duidaima.com spring.application.name=blog spring.cloud.sentinel.transport.port=8720 # 测试请替换为自己的地址 spring.cloud.sentinel.transport.dashboard=116.190.247.112:8084这里的 spring.cloud.sentinel.transport.port 端口配置会在应用对应的机器上启动一个 Http Server,该 Server 会与 Sentinel 控制台做交互。比如 Sentinel 控制台添加了1个限流规则,会把规则数据 push 给这个 Http Server 接收,Http Server 再将规则注册到 Sentinel 中。
/** * 博文 https://www.duidaima.com */ @RequestMapping("{id}.shtml") @SentinelResource("blogView") public String page(@PathVariable("id") Long id, ModelMap model) { try{ Blog blog = blogService.getById(id); String key = "blog_"+id; Long views = redisUtil.size(key); blog.setViews(views+blog.getViews()); model.addAttribute("blog",blog); } catch (Throwable e) { return "error/404"; } return "article"; }@SentinelResource 注解用来标识资源是否被限流、降级。上述例子上该注解的属性 'blogView' 表示资源名。
Caused by: com.alibaba.csp.sentinel.slots.block.flow.FlowException: null当然,Sentinel 流程功能不仅仅这么简单,还支持集群模式,在终极版十万博文中,我们可以为集群中的节点,设置单机均分,也可以设置一个总体的阈值。