Springcloud Alibaba-sentinel 流量防卫兵的使用

一、介绍、文档

https://github.com/alibaba/Sentinel/wiki/%E6%B5%81%E9%87%8F%E6%8E%A7%E5%88%B6

QPS(Queries-per-second):

每秒查询率QPS是对一个特定的查询服务器在规定时间内所处理流量多少的衡量标准

因特网上,经常用每秒查询率来衡量域名系统服务器的机器的性能,其即为QPS。

对应fetches/sec,即每秒的响应请求数,也即是最大吞吐能力。

原理:每天80%的访问集中在20%的时间里,这20%时间叫做峰值时间。

公式:( PV* 80% ) / ( 每天秒数 * 20% ) = 峰值时间每秒请求数(QPS)

机器:峰值时间每秒QPS / 单台机器的QPS = 需要的机器 。

每天300w PV 的在单台机器上,这台机器需要多少QPS

( 3000000 * 0.8 ) / (86400 * 0.2 ) = 139 (QPS)

一般需要达到139QPS,因为是峰值。

二、sentinel控制器下载

1.进入下载地址:https://github.com/alibaba/Sentinel/tags下载sentinel-dashboard-1.8.2.jar

2.启动:默认端口为8080

java -Dserver.port=6666 -jar sentinel-dashboard-1.8.2.jar

修改端口、账号、密码、后台启动:

nohup java -Dserver.port=6006 -Dcsp.sentinel.dashboard.server=localhost:6006 -Dproject.name=sentinel-dashboard -Dsentinel.dashboard.auth.username=sentinel -Dsentinel.dashboard.auth.password=123456 -jar sentinel-dashboard-1.8.3.jar >/usr/local/sentinel/out.log 2>&1 &

 

 3.浏览器访问http://localhost:8888/ 账号密码:sentinel   sentinel  

 

三、搭建父工程nacos-parent

导入依赖pom.xml

 <!--打包方式为pom-->
    <packaging>pom</packaging>

    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.1.16.RELEASE</version>
    </parent>

    <properties>
        <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
        <spring-cloud-alibaba.version>2.1.0.RELEASE</spring-cloud-alibaba.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.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>

四、搭建子项目nacos-sentinel

1.配置文件application.yml

server:
  port: 1234

spring:
  application:
    name: nacos-sentinel
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848

    sentinel:
      #开启sentinel 默认为true开启状态
      enabled: true
      #连接到sentinel
      transport:
        dashboard: localhost:8888
        #sentinel和dashboard通信的端口
        port: 8719

2.依赖pom.xml

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

        <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>    

3.控制器controller

@RestController
public class SentinelController {


    @RequestMapping("/gettest")
    public String getTest(){
        return "吆西!";
    }

    @RequestMapping("/gettest2")
    public String getTest2(){
        return "吆2西!";
    }
}

4.启动类SentinelApp

@SpringBootApplication
@EnableDiscoveryClient
public class SentinelApp {
    public static void main(String[] args) {
        SpringApplication.run(SentinelApp.class,args);
    }
}

五、访问接口,查看sentinel控制台

 

六、添加流控规则-QPS

 

 

 

 访问接口查看流控情况

 

 七、添加流控规则-并发线程数

 

 1.使用JMeter压力测试工具 下载地址:https://jmeter.apache.org/download_jmeter.cgi

 2.访问接口查看流控情况

 

 八、添加流控规则-关联

当关联资源gettest2访问达到阈值时gettest将被flow limiting

 

 访问接口查看流控情况

posted @ 2021-10-25 11:42  蒂雪凌星  阅读(77)  评论(0)    收藏  举报
Live2D