Senior architect takes you in-depth, Sentinel
Sentinel
The traffic Sentinel of distributed system takes traffic as the starting point. Compared with the Sentinel mode of Redis, it can be concluded that Sentinel's role in micro service is to monitor and manage traffic, such as traffic control, fuse degradation, system load protection, etc.
Github: https://github.com/alibaba/Sentinel
Sentinel's main features:
Sentinel's open source ecosystem:
Sentinel is divided into two parts:
- The core library (Java client) does not rely on any framework / library and can run in all Java runtime environments. At the same time, it also has good support for frameworks such as Dubbo / Spring Cloud.
- The Dashboard is developed based on Spring Boot and can be run directly after packaging without additional application containers such as Tomcat.
Quick Start
The following example will show how the application accesses sentinel in three steps. Sentinel also provides a WYSIWYG console, which can monitor resources and manage rules in real time.
STEP 1. Sentinel Jar package is introduced into the application
If the application uses pom project, it should be in pom Add the following code to the XML file:
<dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-core</artifactId> <version>1.7.0</version> </dependency>
Note: starting from Sentinel 1.5.0, only JDK 1.7 or above is supported. Sentinel before 1.5.0 supports JDK 1.6 as a minimum.
STEP 2. Define resources
Next, we use sentinel API sphu to control the flow of code Entry ("HelloWorld") and entry Just surround it with exit(). In the following example, we will use system out. println("hello wolrd"); As a resource, it is surrounded by API. Reference codes are as follows:
public static void main(String[] args) { initFlowRules(); while (true) { Entry entry = null; try { entry = SphU.entry("HelloWorld"); /*Your business logic - start*/ System.out.println("hello world"); /*Your business logic - end*/ } catch (BlockException e1) { /*Flow control logic processing - start*/ System.out.println("block!"); /*Flow control logic processing - end*/ } finally { if (entry != null) { entry.exit(); } } } }
After completing the above two steps, the transformation of the code end is completed.
STEP 3. Define rules
Next, specify the number of requests that the resource is allowed to pass through through through rules. For example, the following code defines that the resource HelloWorld can only pass 20 requests per second.
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); }
After completing the above three steps, Sentinel can work normally.
STEP 4. Check the effect
After the Demo runs, we can log ~ / logs / CSP / ${appName} - metrics log. See the following output in 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
Where p represents the passed requests, block represents the blocked requests, s represents the number of successfully executed requests, e represents user-defined exceptions, and rt represents the average response time.
You can see that this program stably outputs "hello world" 20 times per second, which is the same as the preset threshold in the rule.
STEP 5. Launch Sentinel console
You can refer to Sentinel console documentation to launch the console, which can monitor the operation of various resources in real time, and modify the current limit rules in real time.
last
Recently, I compiled a complete set of "summary of Java core knowledge points". To be honest, as a java programmer, you should take a good look at this material whether you need an interview or not. Kwai won't lose my hand. Many of my fans got the Offer of Tencent's byte express.