alibaba sentinel简单实践

alibaba sentinel是一套流控组件,有限流、熔断、降级等功能。由一个管理端dashboard和放入需要使用的应用的拦截组件组成。前者是个jar包、启起来就行了,后者可以通过starter方便的集成到springboot应用里。dashboard调用应用开的一个端口(8720)发送限流配置给拦截组件,拦截组件通过dashboard的端口上报应用的流量情况。是双向的一个通信。
1、从官网下载sentinel-dashboard-1.8.2.jar,然后启动dashboard管理端:java -jar -Dserver.port=9100 sentinel-dashboard-1.8.2.jar
2、用idea创建一个springboot项目并集成sentinel真是方便,配置好创建向导start.aliyun.com,勾选spring web和alibaba sentinel两个依赖就行了。不用自己费心去搞pom.xml文件和各种依赖的搭配。
pom.xml

<?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>
    <groupId>com.wangan</groupId>
    <artifactId>springbootone</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springbootone</name>
    <description>springbootone</description>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.1.17.RELEASE</spring-boot.version>
        <spring-cloud-alibaba.version>2.1.2.RELEASE</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-sentinel</artifactId>
        </dependency>

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <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>
                <version>2.1.17.RELEASE</version>
                <configuration>
                    <mainClass>com.wangan.springbootone.SpringbootoneApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

连application.properties文件都给自动生成了一份:

# 应用名称
spring.application.name=springbootone
# Sentinel 控制台地址
spring.cloud.sentinel.transport.dashboard=localhost:9100
# 取消Sentinel控制台懒加载
# 默认情况下 Sentinel 会在客户端首次调用的时候进行初始化,开始向控制台发送心跳包
# 配置 sentinel.eager=true 时,取消Sentinel控制台懒加载功能
spring.cloud.sentinel.eager=true
# 如果有多套网络,又无法正确获取本机IP,则需要使用下面的参数设置当前机器可被外部访问的IP地址,供admin控制台使用
# spring.cloud.sentinel.transport.client-ip=
# 应用服务 WEB 访问端口
server.port=8080

写一个Controller测试一下:

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("limit")
public class LimitController {

    @RequestMapping(value = "test", method = RequestMethod.GET)
    public String test(){
        log.info("LimitController test接口");
        return "success";
    }
}

3、到dashboard上配置一下流控规则,比如设置上面的接口GET limit/test的单机阈值QPS=1,这样当接口超过这个QPS会返回状态http 429 Too Many Requests response body: "Blocked by Sentinel (flow limiting)"
还能看到比较直观的实时监控的可视化流控图。dashboard

上面的过程属于快速上手的实践,有个问题在于springboot应用重启之后,流控规则就没了,看来流控规则在dashboard没有持久化保存,拦截组件侧也是只放在内存里的,所以我们的架构中应该还少个配置持久化放在哪里的问题,配合阿里家的另一款开源组件————配置中心nacos即可解决。

posted on 2021-10-30 22:41  肥兔子爱豆畜子  阅读(156)  评论(0编辑  收藏  举报

导航