Spring Cloud Alibaba tutorial series (3) Sentinel construction and use

First pull two sentences, do not know digging friends have found, in the digging community all god, and interestingly, through observing the head of each god, I was surprised to find that now in the community lack of master, two brothers, sand brothers. Ask for a team to upgrade to fight monsters. Haha !!!!!!

Declaration: I am not a god, but also occupied big brother really ashamed. I am the small white of technology circle, hope can become technology great god, dream still must have, in case realized that, ha ha ha!!


preface

So let’s talk a little bit about what Sentinel is and what it does. Sentinel website.

Sentinel is the traffic defense of distributed systems.

Sentinel takes flow as the entry point to protect the stability of service from multiple dimensions such as flow control, fusing downgrading and system load protection.

Sentinel is the equivalent of Hystrix in spring Cloud.

Sentinel is divided into two parts:

  • Core library (Java client) : does not depend on any framework/library, can run in all Java running environment, and Dubbo, Spring Cloud and other frameworks have good support.
  • Console (Dashboard) : Developed based on Spring Boot, packaged can run directly, no additional application containers such as Tomcat.

Download and Install

download

Sentinel 1.7.2.

The sentinel download is the same as nacOS in that it is a direct-run JAR package based on Spring Boot.

The installation

Note: Sentinel uses port 8080 by default. If you want to use another port, you can use the following command:

  • Java-dserver. port=8081 -jar sentinel-dashboard-1.7.2.jar to specify the port.

Run and start sentinel, then type http://loalhost:8080 I am using the default port here, you will see the screenshot below, the user name and password are snetienl.

image-20200607163008125

After successful login, the following message will be displayed:

image-20200607163048598

At this point sentinel has been successfully installed. Now let’s set up a project to use Sentinel.

Note: nothing on the page, also shows that this page is real-time data collection.

The new module

This article directly new module, easy to demonstrate, but also to distinguish from other demo. If it is a real project, it can be directly transformed in the project. Creating a Module will not be mentioned, if you can not already see my previous tutorial.

Modified pom

Modify the pom. XML file and add the following configuration:

<dependency>
  <groupId>com.alibaba.cloud</groupId>
  <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

<dependency>  <groupId>com.alibaba.cloud</groupId>  <artifactId>spring-cloud-alibaba-sentinel</artifactId> </dependency> Copy the code

Change a yaml configuration

Modify the application.yml configuration file as follows:

server:
  port: 8801

spring:
 cloud:
 nacos:  discovery:  server-addr: 127.0. 01.: 8848   sentinel:  transport:  dashboard: 127.0. 01.: 8080 # sentinel address  port: 8179 If 8179 is occupied, it will automatically increase 1 and start scanning until unregistered ports are found  application:  name: sentinel-server  management:  endpoints:  web:  exposure:  include: The '*' Copy the code

Modifying the startup class

The startup class adds the @enableDiscoveryClient annotation

Add business test classes

Create the business test class TestController. Write two random test methods.

/ * * * @author fujie.feng
 * @Date 2020-05-25
* /
@RestController
public class TestController {   @GetMapping("/test")  public String test(a) {  return "success";  }   @GetMapping("/app")  public String testApp(a) {  return "app";  }  } Copy the code

The validation test

Start the newly created Module if nacOS and Sentinel are started first.

Access to the new business class test address, http://localhost:8801/test and http://localhost:8801/app, then we can see the following screenshots refresh sentinel page,

image-20200607223350643

At this point sentinel is complete.

Using Sentinel

Flow control rules

First, a brief explanation of the indecency control rules:

Resource name: unique name, default request path.

For source: Sentinel can limit the flow for the caller and fill in the microservice name, default (regardless of source).

Threshold Type/Single-node threshold:

  • QPS: The number of requests per second. When the NUMBER of QPS calling the API reaches the threshold, traffic limiting is performed
  • Number of threads: When the number of threads calling the API reaches the threshold, traffic limiting is performed.

Cluster or not: No cluster is required

Flow control mode:

  • Direct: When the API meets the traffic limiting condition, the API directly limits traffic.
  • Association: When the associated resource reaches the threshold, traffic limits itself
  • Link: Records traffic limiting only on the specified link (the traffic of a specified resource from an incoming resource reaches a threshold, traffic limiting is performed) (API-level source)

Flow control effect:

  • Fast failure: Fail directly and throw an exception.
  • Warm Up: according to the codeFactor (cold loading factor, default 3) value, from the threshold /codeFactor, after the warm-up time, to reach the set QPS threshold.

Create a flow control rule

There are two ways to add flow control rules. One is to directly edit the list information listed in the cluster point connection, and the other is to add flow control rules directly. Here, the second way is to add flow control rules directly.

image-20200607230032029

You can perform operations in the sequence shown in the screenshot.

Here, THE flow control is /test request, and the threshold type is QPS. The single-machine threshold is 1.

image-20200607230348662

The flow control rules are as follows: /test A request can be made only once at a time

The validation rules

At this moment, we can visit http://localhost:8801/test, the result is shown in the following figure

Then we can quickly refresh the page to see a flow control effect, as shown below:

The page returns Blocked by Sentinel (flow Limiting), which means that the request is Blocked by Sentinel. It indicates that the flow control rules have taken effect.

This time we can also visit http://localhost:8801/app (not set flow control rules), and then refresh the page a few times more, will see every request is successful.

Here is no more than a demonstration of other functions, the operation is relatively simple, small partners can operate by themselves.

Compare the Hystrix

Here’s a quick comparison for Hystrix:

Hystrix

  1. We need to manually build our own monitoring platform
  2. There is no web interface that allows us to fine-tune flow, rate control, service fuses, service degradation, etc.

Sentinel

  1. Individual components that can be isolated.
  2. Direct interface fine – grained unified configuration.

conclusion

This article briefly introduced the use and setup process of Sentinel, which is generally very simple.

The source address

  • github
  • gitee


Spring Cloud Alibaba small White series construction tutorial:

  • Spring Cloud Alibaba tutorial series part 1 – Registering and discovering Nacos services
  • Spring Cloud Alibaba Tutorial series part 2 -Nacos Configuration Center