Practical combat: Springboot integrates Sentinel to achieve flow control, circuit breaker degradation, and load protection

Article Table of Contents
    Preface
    Knowledge accumulation
        Traffic control
        Load protection
        Meltdown downgrade
        Official documentation Practical
    drill
        Deploy sentinel-dashboard
            Direct jar package deployment
            Docker-compose
        Orchestration Springboot integration Sentinel
            infrastructure Build
            Sentinel console
            Sentinel verification
    extension: System adaptive current limiting
        system rules
        principle
        configuration page
    write at the end

01

Preface

In the previous article, we learned about Hystrix and integrated it with the springboot project to implement circuit breaker, downgrade, and isolation measures for services. However, Hystrix does not control traffic very well. Semaphores alone can only limit the flow of specified interfaces. As for the protection mechanism, Hystrix can only fuse when the target is reached. So, is there a middleware that can accurately implement flow control and load protection while being compatible with circuit breaker degradation? The answer is of course, it is our protagonist today, Spring Cloud Alibaba Sentinel.

picture

02

knowledge accumulation

Sentinel is a high-availability traffic protection component for distributed service architecture. It mainly uses traffic as the entry point to help developers ensure the stability of microservices from multiple dimensions such as current limiting, traffic shaping, circuit breaker degradation, system load protection, and hotspot protection. .

flow control

Sentinel can perform process shaping control according to specified indicators for different calling relationships. Moreover, the shaping strategies include three modes: direct rejection, slow start preheating, and equalizer. Developers can select strategies according to their own needs to achieve precise control of traffic.

load protection

Sentinel can provide an accurate load protection mechanism. If the called service reaches the maximum load, the request load will be balanced to other service providers. If other services also reach the maximum load, the service protection mechanism will be activated to allow the request traffic and service load to reach balance.

circuit breaker downgrade

Sentinel also provides a basic circuit breaker and downgrade function. If the service is thrown once, it will be directly downgraded using fallback logic. If it is a configuration indicator, blockhander logic will be used. Of course, if the service cluster reaches the maximum load, it will perform a circuit breaker function to prevent service avalanches.

official document

https://github.com/alibaba/Sentinel/wiki/%E4%BB%8B%E7%BB%8D

picture

03

Practical exercises

Deploy sentinel-dashboard

Download the jar package
https://github.com/alibaba/Sentinel/releases

picture

Direct jar package deployment

nohup java -jar -Dserver.port=8109 sentinel-dashboard.jar &

docker-compose orchestration

If possible, you can use the dashboard image directly. If pull is slow, consider building the image yourself.
dockerfile

# this is sentinel-dashboard dockerfile
# version 1.0
# 基础镜像
FROM openjdk:8-jre
# 维护人
MAINTAINER senfel<[email protected]>
#jar
COPY ./sentinel-dashboard.jar /home/app.jar
# 端口
EXPOSE 8109
# 执行命令启动jar
ENTRYPOINT ["java","-jar","/home/app.jar"]

docker-compose.yml

version: '3.3'
services:
  sentinel:
    build: ./
    image: senfel/sentinel-dashboard
    container_name: sentinel-dashboard
    ports:
      - 8109:8109
    environment:
      JVM_OPTS: -server -Xmx512M -Xms512M -XX:MaxMetaspaceSize=256M -XX:CompressedClassSpaceSize=50M -XX:ReservedCodeCacheSize=240M -XX:MaxDirectMemorySize=400M
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "1"
    volumes:
      - "/home/test/demo/sentinel/logs:/root/logs"
      - "/home/test/demo/sentinel/logs:/app-logs"
    command: [
      "--server.port=8109",
      "--logging.file.path=/app-logs"
    ]

Build and start the container

docker-compose up -d --build

View sentinel container

docker ps | grep sentinel

picture

springboot integrated sentinel

Infrastructure construction

Persistent traffic control rules, other rules only need to add configuration.
Sentinel is persisted to nacos. If it is not persistent, the rules will be cleared every time it is restarted.

Add maven dependency

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    <version>2.2.9.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--sentinel持久化到nacos 不持久每次重启都会清除规则-->
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-nacos</artifactId>
    <version>1.8.0</version>
</dependency>

Add bootstrap.yml to configure nacos to build by itself

server:
  port: 9999
spring:
  application:
    name: test-demo
  profiles:
    active: uat
  cloud:
    nacos:
      config:
        server-addr: 10.10.18.16:8848,10.10.18.16:2848,10.10.18.16:5848
        username: nacos
        password: nacos
        file-extension: yaml
      discovery:
        server-addr: 10.10.18.16:8848,10.10.18.16:2848,10.10.18.16:5848
        username: nacos
        password: nacos
    sentinel:
      transport:
        dashboard: 10.10.22.174:8109
        port: 8719
      datasource:
        ds1:
          nacos:
            server-addr: 10.10.18.16:8848,10.10.18.16:2848,10.10.18.16:5848
            dataId: test-demo-sentinel.json
            data-type: json
            rule-type: flow
            username: nacos
            password: nacos
            
#开启sentinel
feign:
  sentinel:
    enabled: true
  circuitbreaker:
    enabled: true

nacos new current limiting configuration

Flow control
resource: resource name
limitApp: source application
grade: threshold type, 0 represents the number of threads, 1 represents QPS
count: click threshold
strategy: flow control mode, 0 represents direct, 1 represents association, 2 represents link
controlBehavior: flow control Effect, 0 represents fast failure, 1 represents Warm Up, 2 represents waiting in line
clusterMode: whether to cluster

-Other rules are similar and will not be repeated here.
-Nacos configuration has nothing to do with this article and will not be repeated here.

[
  {
    "resource": "testFeign",
    "limitApp": "default",
    "grade":   1,
    "count":   1,
    "strategy": 0,
    "controlBehavior": 0,
    "clusterMode": false
  }
]

The startup class opens feign and nacos

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@Slf4j
public class TestDemoApplication implements ApplicationRunner {

    public static void main(String[] args) {
        SpringApplication.run(TestDemoApplication.class, args);
    }
}

test case

/**
 * testFeign
 * fallback只负责业务异常
 * blockHandler只负责sentinel配置超标
 * 两个都配置,如果都出错,会进入blockHandler
 * @param str
 * @author senfel
 * @date 2023/7/11 14:26
 * @return java.lang.String
 */
@SentinelResource(value = "testFeign",blockHandler = "blockHandler",fallback = "handlerFallback")
@GetMapping("/testFeign")
public String testFeign(String str){
    if(str.contains("y")){
        throw new RuntimeException();
    }
    return testMQService.testFeign(str);
}

/**
 * 异常阻塞处理
 * @param str
 * @author senfel
 * @date 2023/7/11 14:44
 * @return java.lang.String
 */
private String blockHandler(String str, BlockException blockException){
    return "blockHandler"+"-"+str;
}

/**
 * 降级处理
 * @param str
 * @author senfel
 * @date 2023/7/11 14:44
 * @return java.lang.String
 */
private String handlerFallback(String str,Throwable throwable){
    return "handlerFallback"+"-"+str;
}

openfeign downgrade test case

/**
 * @author senfel
 * @version 1.0
 * @date 2023/7/03 15:04
 */
@Service
@FeignClient(value = "test-demo",fallback = FallbackService.class)
public interface TestService {

    /**
     * 测试sentinel
     */
    @GetMapping("/feign")
    String feign(@RequestParam String str);


}



/**
 * FallbackService
 * @author senfel
 * @version 1.0
 * @date 2023/7/3 15:26
 */
@Service
public class FallbackService implements TestService {

    @Override
    public String feign(String str) {
        return ">>>>>客户端服务降级>>>>>";
    }
}

sentinel console

View http://10.10.22.174:8109/ after postman request

picture

Automatically load configured traffic rules

picture

picture

sentinel verification

Verify active control traffic
127.0.0.1:9999/testFeign?str=fffffffffss

picture

Verify abnormal control traffic
127.0.0.1:9999/testFeign?str=ffffffffffyy

picture

Verify client feign downgrade
127.0.0.1:9999/testFeign?str=fffffffffss

picture

View real-time monitoring

picture

04

Extension: system adaptive current limiting

System rules

System protection rules are controlled from the application level ingress traffic, and application indicators are monitored from several dimensions such as the load of a single machine, CPU usage, average RT, ingress QPS and number of concurrent threads, so that the system can run at the maximum throughput as much as possible. While ensuring the overall stability of the system.
System protection rules apply to the overall dimension, not the resource dimension, and only take effect for ingress traffic. Ingress traffic refers to the traffic entering the application (EntryType.IN), such as requests received by web services or Dubbo servers, are all ingress traffic.
System rules support the following modes:
Load adaptive (only effective for Linux/Unix-like machines): The system's load1 is used as a heuristic indicator for adaptive system protection. System protection (BBR phase) will be triggered when the system load1 exceeds the set heuristic value and the current number of concurrent threads in the system exceeds the estimated system capacity. System capacity is estimated by maxQps * minRt of the system. The setting reference value is generally CPU cores * 2.5.
CPU usage (version 1.5.0+): When the system CPU usage exceeds the threshold, system protection will be triggered (value range 0.0-1.0), which is more sensitive.
Average RT: When the average RT of all ingress traffic on a single machine reaches the threshold, system protection is triggered, and the unit is milliseconds.
Number of concurrent threads: When the number of concurrent threads of all ingress traffic on a single machine reaches the threshold, system protection is triggered.
Ingress QPS: When the QPS of all ingress traffic on a single machine reaches the threshold, system protection is triggered.

principle

We imagine the system's request processing process as a water pipe. Incoming requests are filled with water into this water pipe. When the system processes smoothly, the request does not need to be queued and passes directly through the water pipe. The RT of this request is the shortest; conversely, When requests pile up, the time to process the requests will become: queuing time + minimum processing time.

picture

Configuration page

picture

05

pass

The actual use of Sentinel to achieve flow control, circuit breaker degradation, and load protection is relatively simple. It only needs to be integrated and configured according to the official article. Sentinel's flow control and load protection and high performance are better than hystrix, and it provides a visual console. If in the high availability and reliability and important service cluster scenarios, sentinel can be given priority.

Guess you like

Origin blog.csdn.net/qq_45635347/article/details/132515945