下载:
https://github.com/alibaba/Sentinel/ 本次下载1.7.0版本
启动:
H:\softwar>java -jar sentinel-dashboard-1.7.0.jar
INFO: log output type is: file
INFO: log charset is: utf-8
INFO: log base dir is: C:\Users\smy\logs\csp\
INFO: log name use pid is: false
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.5.RELEASE)
访问:http://127.0.0.1:8080 sentinel/sentinel登录
监控本地服务:
新建服务pom:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
yml配置文件:
server:
port: 8401
spring:
application:
name: cloud-alibaba-sentinel-service
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
sentinel:
transport:
dashboard: 127.0.0.1:8080
port: 8719
management:
endpoints:
web:
exposure:
exclude: '*'
主类:
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentSentinelMain8401 {
public static void main(String[] args) {
SpringApplication.run(PaymentSentinelMain8401.class,args);
}
@RestController
public class FlowLimitController{
@GetMapping("/testA")
public String testA(){
return "testA";
}
}
}
启动,然后打开http://127.0.0.1:8080/#/dashboard/home 发现没服务,因为采用的是懒加载,因此先请求一下8041服务,再打开sentinel
如下:
流控直接:狂点 http://127.0.0.1:8401/testA 抛出
Blocked by Sentinel (flow limiting)
@GetMapping("/testA")
public String testA() throws Exception {
return "testA";
}
流控关联:testA关联testB快速访问testB然后迅速切回访问testA会导致testA失败
流控降级:
热点规则:配置完访问 http://127.0.0.1:8401/testC?p1=p1 当达到阈值抛出自定义的错误
@GetMapping("/testC")
@SentinelResource(value = "testC",blockHandler = "testC_fallbacl")
public String testC(@RequestParam(value = "p1",required = false)String p1
,@RequestParam(value = "p1",required = false)String p2) throws InterruptedException{
System.out.println("-----");
return "testC";
}
public String testC_fallbacl(String p1, String p2, BlockException exception){
return "testC_fallbacl";
}
热点规则参数例外项:
系统规则:全部拦截整个系统级别的
@SentinelResource注解
@GetMapping("/testD")
@SentinelResource(value = "testD",blockHandlerClass = MyBlockHandler.class
,blockHandler = "testD")
public String testD(){
return "testD";
}
配置对应的blockHandlerClass 下面的形参BlockException 必须给,否则无法实现。
public class MyBlockHandler {
public static String testD(BlockException b){
return "MyBlockHandler----testD";
}
}