Alibaba Sentinel :资源、规则
1、仓库
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-alibaba-cloud.version>0.2.0.RELEASE</spring-alibaba-cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-alibaba-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2、引入包
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-annotation-aspectj</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-simple-http</artifactId>
</dependency>
<!--<dependency>-->
<!--<groupId>com.alibaba.csp</groupId>-->
<!--<artifactId>sentinel-demo-annotation-spring-aop</artifactId>-->
<!--<version>0.1.1</version>-->
<!--</dependency>-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
3、资源
3.1 新建类1
@Configuration
public class SentinelAspectConfiguration {
@Bean
public SentinelResourceAspect sentinelResourceAspect() {
return new SentinelResourceAspect();
}
}
3.1 新建类2
public class ExceptionUtil {
public static void handleException(BlockException ex) {
System.out.println("Oops: " + ex.getClass().getCanonicalName());
}
}
3.2定义资源
@Service
public class TestService {
// 对应的 `handleException` 函数需要位于 `ExceptionUtil` 类中,并且必须为 static 函数.
@SentinelResource(value = "test", blockHandler = "handleException", blockHandlerClass = {ExceptionUtil.class})
public void test() {
System.out.println("test通过");
//
}
// 原函数
@SentinelResource(value = "hello", blockHandler = "exceptionHandler", fallback = "helloFallback")
public String hello(long s) {
return String.format("Hello通过", s);
}
// Fallback 函数,函数签名与原函数一致.
public String helloFallback(long s) {
return String.format("helloFallbackhelloFallback", s);
}
// Block 异常处理函数,参数最后多一个 BlockException,其余与原函数一致.
public static String exceptionHandler(long s, BlockException ex) {
// Do some log here.
ex.printStackTrace();
return "Oops, error occurred at " + s;
}
}
3.3新建测试接口
@RestController
@RequestMapping("/demo")
public class DemoController {
@Autowired
private TestService service;
@GetMapping({"/foo"})
public String foo() throws Exception {
this.service.test();
return this.service.hello(1);
}
}
4、启动 sentinel-dashboard.jar
下载地址:https://github.com/alibaba/Sentinel/wiki/%E6%8E%A7%E5%88%B6%E5%8F%B0
访问http://localhost:8080
5、添加配置
spring.application.name=component-sentinel
server.port=18083
spring.cloud.sentinel.transport.dashboard=localhost:8080
5、触发客户端初始化
确保客户端有访问量,Sentinel 会在客户端首次调用的时候进行初始化,开始向控制台发送心跳包。
Sentinel 的理念是开发者只需要关注资源的定义,当资源定义成功后可以动态增加各种流控降级规则。
Sentinel 提供两种方式修改规则:
- 通过 API 直接修改 (
loadRules
) - 通过
DataSource
适配不同数据源修改
https://github.com/alibaba/Sentinel/wiki/%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%99%E6%89%A9%E5%B1%95
访问测试接口。添加规则,只存在内存中,所以关闭即消失。
簇点监控
获取簇点列表
当应用启动之后,可以运行下列命令,获得当前所有簇点(ClusterNode
)的列表(JSON 格式):
curl http://localhost:8719/clusterNode
结果示例:
[
{"avgRt":0.0, //平均响应时间
"blockRequest":0, //每分钟拦截的请求个数
"blockedQps":0.0, //每秒拦截个数
"curThreadNum":0, //并发个数
"passQps":1.0, // 每秒成功通过请求
"passReqQps":1.0, //每秒到来的请求
"resourceName":"/registry/machine", 资源名称
"timeStamp":1529905824134, //时间戳
"totalQps":1.0, // 每分钟请求数
"totalRequest":193},
....
]
查询某个簇点的详细信息
可以用下面命令模糊查询该簇点的具体信息,其中 id
对应 resource name,支持模糊查询:
curl http://localhost:8719/cnode?id=xxxx
链路监控
我们可以通过命令 curl http://localhost:8719/tree
来查询链路入口的链路树形结构
https://github.com/alibaba/Sentinel/wiki/%E5%AE%9E%E6%97%B6%E7%9B%91%E6%8E%A7