Spring Cloud Alibaba [Sentinel console environment installation based on Linux, connecting applications to Sentinel, overview of flow control, direct mode of flow control mode, association mode of flow control mode] (6)

 

Table of contents

Distributed traffic protection_Sentinel console environment installation based on Linux

Distributed traffic protection_Sentinel console environment installation based on Docker 

 Distributed traffic protection_connecting applications to Sentinel

 Distributed Traffic Protection_Traffic Control Overview

Distributed traffic protection_direct mode of flow control mode

Distributed traffic protection_Association mode of flow control mode 

Distributed traffic protection_ link mode of flow control mode


 

Distributed traffic protection_Sentinel console environment installation based on Linux

Get Sentinel Console 

Download https://github.com/alibaba/Sentinel/releases

Start Sentinel Console 

linux mode

java -Dserver.port=8878 \
-
Dcsp.sentinel.dashboard.server=192.168.66.100:8878 \
-Dproject.name=sentinel-dashboard \
-jar ./sentinel-dashboard.jar

Running in background: nohup java -server -Xms64m -Xmx256m - Dserver.port=8878 - Dcsp.sentinel.dashboard.server=192.168.66.100:8878- Dproject.name=sentinel-dashboard -jar sentineldashboard-1.8.3.jar >> /opt/sentinel.log 2>& 1 &

windows mode 

java -jar ./sentinel-dashboard.jar \
--server.port=8858 \
--
csp.sentinel.dashboard.server=192.168.66.101:8858 \
--project.name=sentinel-dashboard

Note: It is strongly recommended to use JRE8 to start the Sentinel console. There may be unknown bugs in higher versions of JDK.

test access

访问http://192.168.66.101:8858 即可,登录用户名密码都是 sentinel。

 

Distributed traffic protection_Sentinel console environment installation based on Docker 

pull image 

docker pull docker.io/bladex/sentinel-dashboard

Create a boot container

docker run --name sentinel --restart=always -d -p 8888:8858 docker.io/bladex/sentinel-dashboard

view container

docker ps

test access 

访问http://192.168.66.101:8858即可,登录用户名密码都是 sentinel。

 Distributed traffic protection_connecting applications to Sentinel

Create project cloud-sentinel-payment8001 

POM introduces dependencies 

   <dependencies>
        <!--       Nacos 依赖-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloudstarter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!--       sentinel依赖-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloudstarter-alibaba-sentinel</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
         <!--借用监控开放端口-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

Write the main startup class

@SpringBootApplication
@Slf4j
public class PaymentSentinelMain8001 {
    public static void main(String[] args) {
       SpringApplication.run(PaymentSentinelMain8001.class,args);
        log.info("************PaymentSentinelMain8001 启动成功 **********");
   }
}

Write a YML configuration file

server:
 port: 8001
spring:
 application:
    #应用名字
   name: cloud-sentinel-payment
 cloud:
   nacos:
     discovery:
        # Nacos地址
       server-addr: 192.168.66.101:8848
   sentinel:
     transport:
        # Sentinel 控制台地址
       dashboard: 192.168.66.101:8858
        # sentinel api端⼝,默认8719
       port: 8719

Write a test controller

@RestController
public class FlowLimitController {
    @GetMapping("testA")
    public String testA(){
        return "------------testA";
   }
}

View Sentinel control layer

 send request

Send http://192.168.66.101:8858

 Distributed Traffic Protection_Traffic Control Overview

Monitor the QPS or the number of concurrent threads of the application traffic, and control the traffic when the specified threshold is reached, so as to avoid being overwhelmed by the instantaneous traffic peak, thereby ensuring the high availability of the application.

 

Flow Control Design Concept

Flow control has the following angles:

Resource invocation relationship: such as resource invocation link, relationship between resources and resources;

Operating indicators: such as QPS, thread pool, system load, etc.;

The effect of control: such as direct current limiting, cold start, queuing, etc.

 

flow control rules 

parameter:

1. Resource name

In fact, it is the resource path we requested

2. For the source

This is the flow control rule corresponding to those microservices for flow control management. Generally, fill in the name of the microservice of the caller, and use "," to separate multiple

3. Threshold type

Generally, there are 2 types, QPS (the maximum number of requests per second 2) and the number of threads (the number of concurrent threads)

4. Stand-alone threshold

The maximum limit value in stand-alone state

5. Whether to cluster

Choose according to the actual situation 

flow control mode 

 

Flow control effect

 

parameter:

1. Fail fast

The default flow control method, when the QPS exceeds the threshold of any rule, the new request will be rejected immediately, and the rejection method is to throw FlowException.

2、Warm Up

That is, warm-up/cold start mode. When the system has been at a low water level for a long time, when the flow rate suddenly increases, directly pulling the system to a high water level may overwhelm the system instantly. Through "cold start", let the passing flow increase slowly, gradually increase to the upper threshold within a certain period of time, give the cold system a warm-up time, and avoid the cold system from being pressurized.

3. Waiting in line

The uniform speed queuing method will strictly control the interval time for requests to pass through, that is, let the requests pass through at a uniform speed, corresponding to the leaky bucket algorithm. This method is mainly used to deal with intermittent burst traffic. 

 Real-time effect feedback

1. Sentienl's default flow control mode is _____.

A directly

B association

C link

All of the above are wrong

2. The default flow control effect of Sentienl is ____.

A fail fast

B cold start

C waiting in line

D above are all wrong

Distributed traffic protection_direct mode of flow control mode

When the QPS exceeds a certain threshold, measures are taken for flow control.

Note: If you use a flow control effect other than direct deny, the call relationship flow limit strategy (strategy) will be ignored. 

Configure QPS flow control rules 

Note: It means that if you query once within 1 second, it will be ok. If you query more than once, it will fail quickly and report a default error. 

 

Test Flow Control QPS 

Send request http://localhost:8001/testA

send request quickly 

Distributed traffic protection_Association mode of flow control mode 

what is association 

When the associated resource reaches the threshold, it will limit its own flow, and when the resource B associated with A reaches the threshold, it will limit its own flow.

Configure association rules 

expected result 

Since the current limit control of /testA adopts QPS association, direct access will not be limited, and it will be found that there will be no current limit when refreshing /testB, as shown in the figure:

 

However, if you access /testA and set the QPS direct policy to limit the flow, then frequent access to /testB will show a flow limit prompt: 

 

Real-time effect feedback

1. The following description of the Sentienl flow control mode association mode is correct _____.

A I take the blame on myself

B You take the blame yourself

C I will carry your blame

All of the above are wrong 

Distributed traffic protection_ link mode of flow control mode

The link flow control mode means that when the resources coming from a certain interface meet the flow limit condition, the flow limit is enabled. Its function is somewhat similar to the configuration item for the source, the difference is that the source is for the upper-level microservice, and the link flow control is for the upper-level interface, that is to say, its granularity is finer. 

Configuration example:

For example, there are two request links:

/test1 --> /common

/test2 --> /common 

If you only want to count requests from /test2 to /common, you can configure it like this: 

Practical case 

Requirements: There are order query and order creation services, both of which need to query commodities. For the statistics of requests from querying orders to querying products, and setting a current limit.

accomplish 

Add query product method

In the payment service, add a queryGoods method to the PaymentService class:

public void queryGoods(){
    System.err.println("查询商品");
}

When inquiring about an order, inquire about the product

@GetMapping("/query")
public String queryOrder() {
    // 查询商品
    orderService.queryGoods();
    // 查询订单
    System.out.println("查询订单");
    return "查询订单成功";
}

new order

In the PaymentController of the payment service, modify the /order/save endpoint to simulate a new order:

@GetMapping("/save")
public String saveOrder() {
    // 查询商品
    orderService.queryGoods();
    // 查询订单
    System.err.println("新增订单");
    return "新增订单成功";
}

Add a resource tag to the query product

By default, the methods in OrderService are not monitored by Sentinel, and we need to mark the methods to be monitored through annotations. Add the @SentinelResource annotation to the queryGoods method of OrderService:

@SentinelResource("goods")
public void queryGoods(){  
    System.err.println("查询商品");
}

 cluster link

In link mode, two links from different sources are monitored. However, sentinel will set the same root resource for all requests entering SpringMVC by default, which will cause the link mode to fail. We need to turn off this resource aggregation for SpringMVC and modify the application.yml file.

spring:
 application:
   name: payment-provider-sentinel
 cloud:
   nacos:
     discovery:
        # Nacos服务地址
       server-addr: 192.168.66.100:8848
   sentinel:
     transport:
        # Sentinel控制台地址
       dashboard: 192.168.66.100:8888
        # sentnel api端口号 默认 8719
       port: 8719
       # 关闭context整合  
     web-context-unify: false

 Restart the service, access query and save, and you can see that new resources have appeared in sentinel's cluster point link rules.

 

Add flow control rules

Click the flow control button behind the goods resource, and fill in the following information in the pop-up form

 Real-time effect feedback

1. The following description of the link mode of Sentienl flow control mode is _____.

A for source configuration items

B fail fast

C I will carry your blame

All of the above are wrong

 

Guess you like

Origin blog.csdn.net/m0_58719994/article/details/131859709