第二节 Sentinel 快速入门

亮子 2022-12-25 13:49:46 17106 0 0 0

2.1 Sentinel HelloWorld实现

2.1.1 第一步:创建项目,引入依赖

为了方便后期sentinel和springcloud以及springcloud alibaba其他组件整合开发讲解,我们这边规范好springboot,springcloud,springcloud alibaba
的版本,选用稳定的组合版本;

图片alt

具体查看:
https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E

图片alt

图片alt

也可以手动添加下面的依赖:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.shenmazong</groupId>
    <artifactId>server-sentinel-hello</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>server-sentinel-hello</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>org.hibernate</groupId>-->
<!--            <artifactId>hibernate-validator</artifactId>-->
<!--            <version>6.1.3.Final</version>-->
<!--        </dependency>-->

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

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2.1.2 第二步:定义规则和使用限流规则

package com.shenmazong.hello.controller;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.ArrayList;

/**
 * @author 军哥
 * @version 1.0
 * @description: HelloSentinelController
 * @date 2022/12/25 21:11
 */

@RestController
@Slf4j
@RequestMapping(value = "/hello")
public class HelloSentinelController {

    /**
     * 定义限流规则
     * PostConstruct 构造方法执行完后执行方法定义和加载限流规则
     */
    @PostConstruct
    public void initFlowRules() {

        log.info("初始化流控规则...");

        // 定义限流规则集合
        ArrayList<FlowRule> flowRuleArrayList = new ArrayList<FlowRule>();

        // 定义限流规则
        FlowRule flowRule = new FlowRule();
        flowRule.setResource("helloWorld");
        flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        flowRule.setCount(1);

        // 加载规则集合
        flowRuleArrayList.add(flowRule);

        FlowRuleManager.loadRules(flowRuleArrayList);
    }

    /**
     * 测试熔断
     * @return
     */

    @GetMapping(value = "/helloWorld")
    public String helloWorld() {
        try(Entry entry = SphU.entry("helloWorld")) {
            // 正常流程
            long millis = System.currentTimeMillis();
            log.info("helloWorld="+millis);

            return "hello,world:" + millis;
        }
        catch (Exception exception) {
            // 系统降级
            log.error("系统繁忙,请稍后再试!");
            return "系统繁忙,请稍后再试!";
        }
    }

}

2.1.3 第三步:测试

浏览器请求:http://localhost:8080/hello/helloWorld

  • 正常请求:

图片alt

  • 当访问频率超过QPS阈值2,则sentinel降级返回异常信息;

图片alt

  • 日志输出

图片alt