Senior Architect takes you deep understanding, Sentinel

Sentinel

Sentry distributed system flow at a flow rate as a starting point, comparison can be drawn pattern Redis sentinel role in the micro Sentinel service is traffic monitoring and management, such as flow control, fuse downgrade, system load protection.

Github:https://github.com/alibaba/Sentinel

Sentinel's main features:

Sentinel Open Source Ecology:

Sentinel divided into two parts:

  • Core libraries (Java client) does not depend on any framework / library, it can run on any Java runtime environment, while Dubbo / Spring Cloud and other frameworks and has a good support.

  • Console (Dashboard) Spring Boot developers, after pack can be run directly, no additional applications such as container based on Tomcat.

Quick Start

The following example shows how to apply the three-step access Sentinel. At the same time, Sentinel provides WYSIWYG console, real-time monitoring and resource management rules.

STEP 1. Sentinel Jar incorporated in the application package

If the application uses pom project, then add the following code to the pom.xml file:

<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-core</artifactId>
    <version>1.7.0</version>
</dependency>

Note: Starting Sentinel 1.5.0 only supports JDK 1.7 or above. Sentinel before the minimum version 1.5.0 supports JDK 1.6.

STEP 2. define the resource

Next, we need to control the flow of code in  Sentinel API SphU.entry("HelloWorld") and  entry.exit() surrounded can be. In the following example, we will  System.out.println("hello wolrd"); as a resource, surrounded by API. Reference code is as follows:

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();
            }
        }
    }
}

After the above two steps, the transformation code side is completed.

STEP 3. define rules

Next, the number of rules specified by the resource request to allow, for example, the following code is defined by a maximum of 20 requests per second resource HelloWorld.

private static void initFlowRules(){
    List<FlowRule> rules = new ArrayList<>();
    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);
}

Complete 3 steps above, Sentinel will be able to work properly.

STEP 4. Check results

Demo 运行之后,我们可以在日志 ~/logs/csp/${appName}-metrics.log.xxx 里看到下面的输出:

|--timestamp-|------date time----|-resource-|p |block|s |e|rt
1529998904000|2018-06-26 15:41:44|HelloWorld|20|0    |20|0|0
1529998905000|2018-06-26 15:41:45|HelloWorld|20|5579 |20|0|728
1529998906000|2018-06-26 15:41:46|HelloWorld|20|15698|20|0|0
1529998907000|2018-06-26 15:41:47|HelloWorld|20|19262|20|0|0
1529998908000|2018-06-26 15:41:48|HelloWorld|20|19502|20|0|0
1529998909000|2018-06-26 15:41:49|HelloWorld|20|18386|20|0|0

其中 p 代表通过的请求,block 代表被阻止的请求,s 代表成功执行完成的请求个数,e 代表用户自定义的异常,rt 代表平均响应时长。

可以看到,这个程序每秒稳定输出 "hello world" 20 次,和规则中预先设定的阈值是一样的。

STEP 5. 启动 Sentinel 控制台

您可以参考 Sentinel 控制台文档 启动控制台,可以实时监控各个资源的运行情况,并且可以实时地修改限流规则。

发布了111 篇原创文章 · 获赞 66 · 访问量 5万+

Guess you like

Origin blog.csdn.net/qq_39662660/article/details/103648921