点击这里下载
运行
进入目录,cmd
java -jar sentinel-dashboard-1.7.2.jar
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cspgroupId>
<artifactId>sentinel-datasource-nacosartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-sentinelartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
<dependency>
<groupId>com.hry.springcloudgroupId>
<artifactId>cloud-api-commonsartifactId>
<version>${project.version}version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
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: "*"
package com.hry.springcloud.alibaba;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class SentinelMain8401 {
public static void main(String[] args) {
SpringApplication.run(SentinelMain8401.class, args);
}
}
@RestController
public class FlowLimitController {
@GetMapping(value = "/testA")
public String testA(){
return "testA";
}
@GetMapping(value = "/testB")
public String testB(){
return "testB";
}
}
略,集群改回单机 端口号改回8848
cmd启动启动nacos8848,sentinel8080,启动微服务8401
刷新网页发现啥也没有
由于sentinel采用懒加载,因此我们要先访问一次8401。
然后刷新,有了
刷新几次A和B
结论:sentinel8080正在监控8401
QPS = req/sec = 请求数/秒
设值每秒请求数为1
访问testA,1s点一次
1s内访问多次,被sentinel限流
修改controller代码
让A休眠0.8s
@GetMapping(value = "/testA")
public String testA(){
try {
TimeUnit.MILLISECONDS.sleep(800);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "testA";
}
将controller改回注释掉sleep
新建流控
现在疯狂访问B,导致A限流
@GetMapping(value = "/testC")
public String testC(){
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "testC";
}
@GetMapping(value = "/testD")
public String testD(){
int a = 1/0;
return "testD";
}
@GetMapping(value = "/testE")
public String testE(){
int a = 1/0;
return "testE";
}
@SentinelResource与@HystrixCommand相似
@GetMapping(value = "/testHotKey")
@SentinelResource(value = "testHotKey",blockHandler = "deal_testHotKey") //value任意,比如abc,唯一即可
public String testHotKey(@RequestParam(value = "p1",required = false)String p1,
@RequestParam(value = "p2",required = false)String p2){
return "testHotKey";
}
public String deal_testHotKey(String p1, String p2, BlockException exception){
//sentinel 系统默认为Blocked by sentinel(flow limiting)
return "########################deal_testHotKey";
}
注意这两个与controller方法相匹配 一个带/ 一个不带/
配置热点,参数索引是指传入的参数的下标从0开始 p1是0 p2是1
包含p1的1s超过1次访问都会flow limit而且执行了我们指定的兜底方法。如果不指定兜底方法前台会直接显示error page
比如localhost:8401/testHotKey?p1=1&p2=2
比如localhost:8401/testHotKey?p1=1
而不包含p1的就可以正常访问
比如localhost:8401/testHotKey
比如localhost:8401/testHotKey?p2=2
如果手动添加
int a = 1/0;
运行时异常,兜底方法是不能处理的,这样会前端会进入error page页面,如何处理后面会说到。
新增
测试
设置了系统规则,controller中所有请求都需要遵循规则。
比如按照如上图所示设置,QPS,每秒超过1次就会限流。