随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。
详细文档 请移步 Wiki,查阅详细的文档、示例以及使用说明。
Please refer to README for README in English。
与Sentinel 相关的生态(包括社区用户实现的扩展、整合、示例以及文章)可以参见Awesome Sentinel,欢迎补充!
如果您正在使用Sentinel,欢迎在这里留言告诉我们您的使用场景,以便我们更好地去改进。
Quick Start 下面的例子将展示应用如何三步接入Sentinel。同时,Sentinel 也提供一个所见即所得的控制台,可以实时监控资源以及管理规则。
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
<version>x.y.z</version>
</dependency>
如果您未使用依赖管理工具,请到Maven Center Repository 直接下载JAR 包。
我们将System.out.println(“hello wolrd”); 作为资源,用API 包围起来。参考 代码如下:
public static void main(String[] args) {
initFlowRules();
while (true) {
Entry entry = null;
try {
entry = SphU.entry("HelloWorld");
System.out.println("hello world");
} catch (BlockException e1) {
System.out.println("block!");
} finally {
if (entry != null) {
entry.exit();
}
}
}
}
完成以上两步后,代码端的改造就完成了。
private static void initFlowRules(){
List<FlowRule> rules = new ArrayList<FlowRule>();
FlowRule rule = new FlowRule();
rule.setResource("HelloWorld");
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
// Set limit QPS to 20.
rule.setCount(20);
rules.add(rule);
FlowRuleManager.loadRules(rules);
}
完成上面3 步,Sentinel 就能够正常工作了。
|--timestamp-|------date time----|--resource-|p |block|s |e|rt
1529998904000|2018-06-26 15:41:44|hello world|20|0 |20|0|0
1529998905000|2018-06-26 15:41:45|hello world|20|5579 |20|0|728
1529998906000|2018-06-26 15:41:46|hello world|20|15698|20|0|0
1529998907000|2018-06-26 15:41:47|hello world|20|19262|20|0|0
1529998908000|2018-06-26 15:41:48|hello world|20|19502|20|0|0
1529998909000|2018-06-26 15:41:49|hello world|20|18386|20|0|0
其中p 代表通过的请求, block 代表被阻止的请求, s 代表成功通过Sentinel 的请求个数, e 代表用户自定义的异常, rt 代表平均响应时长。
可以看到,这个程序每秒稳定输出"hello world" 20 次,和规则中预先设定的阈值是一样的。