天天看點

sentinel限流實戰sentinel限流實戰

sentinel限流實戰

文章目錄

  • sentinel限流實戰
    • 使用場景
    • 實戰
    • 搭建限流服務
    • 用戶端服務搭建
      • 配置檔案 bootstrap.yml
      • 啟動類
      • 服務類
      • 限流服務和用戶端服務應該注意的地方
    • 限流規則
    • 流控規則
      • 1,流控模式
        • 直接(預設)--> 快速失敗
      • 用戶端限流配置
      • 在要限流的服務方法上添加 注解@SentinelResource
      • 重新開機服務進行測試

使用場景

限流:我們通常使用TPS對流量來進行描述,限流就是限制服務被調用的并發TPS,進而對系統進行自我保護。

​ 1.項目在上線之前經過性能測試評估,例如服務在 TPS 達到 1w/s 時系統資源使用率飙升,與此同時響應時間急劇增大,那我們就要控制該服務的調用TPS,超過該 TPS 的流量就需要進行幹預,可以采取拒絕、排隊等政策,實作流量的削峰填谷。

2.開放平台,對接口進行收費,免費使用者要控制調用TPS,賬戶的等級不同,允許調用的TPS也不同

實戰

源碼參考

https://gitee.com/liuerchong/news-artical

搭建限流服務

參考這篇文章

用戶端服務搭建

pom

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>news-artical</artifactId>
        <groupId>com.liu.news</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>ap-article</artifactId>

    <dependencies>

        <dependency>
            <groupId>com.liu.news</groupId>
            <artifactId>artical-common-db</artifactId>
        </dependency>
        <dependency>
            <groupId>com.liu.news</groupId>
            <artifactId>artical-common-public</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-loadbalancer -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-web-servlet</artifactId>
        </dependency>

    </dependencies>

</project>
           

配置檔案 bootstrap.yml

server:
  port: 9070
spring:
  profiles:
    active: dev
  application:
    name: ap-artical
  main:
    allow-bean-definition-overriding: true
  cloud:
    nacos:
      discovery:
        username: nacos
        password: nacos
        server-addr: 172.17.169.81:8848
        namespace: b561281a-fb95-411e-b75b-cf36bfc90854
        group: ATICAL_CLNEWS_DEV_GROUP
      config:
        server-addr: ${spring.cloud.nacos.discovery.server-addr}
        file-extension: yml
        namespace: ${spring.cloud.nacos.discovery.namespace}
        group: ${spring.cloud.nacos.discovery.group}
    sentinel:
      transport:
        port: 8719 # spring.cloud.sentinel.transport.port 端口配置會在應用對應的機器上啟動一個 Http Server,該 Server 會與 Sentinel 控制台做互動。比如 Sentinel 控制台添加了1個限流規則,會把規則資料 push 給這個 Http Server 接收,Http Server 再将規則注冊到 Sentinel 中
        dashboard: 127.0.0.1:5003
      eager: true
      filter:
        enabled: false
      datasource:
        flow:
          nacos:
            server-addr: localhost:8848
            namespace: ${spring.cloud.nacos.discovery.namespace}
            data-id: ${spring.application.name}-flow-rules
            group-id: ${spring.cloud.nacos.discovery.group}
            data-type: json
            rule-type: flow
        degrade:
          nacos:
            server-addr: localhost:8848
            namespace: ${spring.cloud.nacos.discovery.namespace}
            data-id: ${spring.application.name}-degrade-rules
            group-id: ${spring.cloud.nacos.discovery.group}
            data-type: json
            rule-type: degrade
        system:
          nacos:
            server-addr: localhost:8848
            namespace: ${spring.cloud.nacos.discovery.namespace}
            data-id: ${spring.application.name}-system-rules
            group-id: ${spring.cloud.nacos.discovery.group}
            data-type: json
            rule-type: system
        authority:
          nacos:
            server-addr: localhost:8848
            namespace: ${spring.cloud.nacos.discovery.namespace}
            data-id: ${spring.application.name}-authority-rules
            group-id: ${spring.cloud.nacos.discovery.group}
            data-type: json
            rule-type: authority
        param-flow:
          nacos:
            server-addr: localhost:8848
            namespace: ${spring.cloud.nacos.discovery.namespace}
            data-id: ${spring.application.name}-param-flow-rules
            group-id: ${spring.cloud.nacos.discovery.group}
            data-type: json
            rule-type: param-flow
      management:
        endpoints:
          web:
            exposure:
              include: "*"
           

application.yml

spring:

  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver #資料庫驅動包
    url: jdbc:mysql://localhost:3307/artical?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
    username: root
    password: root
mybatis-plus:
  global-config:
    db-config:
      field-strategy: not_empty
      #駝峰下劃線轉換
      column-underline: true
      #邏輯删除配置
      logic-delete-value: 0
      logic-not-delete-value: 1
      db-type: mysql
    refresh: false
  configuration:
    map-underscore-to-camel-case: true
    cache-enabled: false


           

啟動類

package com.liu.news.artical;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @author lx
 * @version 1.0
 * @description: TODO
 * @date 2021/7/23 13:33
 */
@SpringBootApplication
@EnableDiscoveryClient
public class ApArticleApplication {

    public static void main(String[] args) throws InterruptedException{

        SpringApplication.run(ApArticleApplication.class, args);

    }
}
           

服務類

參考這篇文章

https://blog.csdn.net/liuerchong/article/details/119052304

文章服務搭建

限流服務和用戶端服務應該注意的地方

datasource:
  flow:
    nacos:
      server-addr: localhost:8848
      namespace: ${spring.cloud.nacos.discovery.namespace}
      data-id: ${spring.application.name}-flow-rules
      group-id: ${spring.cloud.nacos.discovery.group}
      data-type: json
      rule-type: flow
           

sentinel持久化的nacos伺服器ip 端口 命名空間,組,資料格式,規則 ,data-id命名與用戶端配置的位址如上部分必須一一對應,不然不起作用。

限流規則

流控規則

資源名:唯一名稱,預設請求路徑

針對來源:Sentine可以針對調用者進行限流,填寫微服務名,預設default (不區分來源)

門檻值類型/單機門檻值:

QPS (每秒鐘的請求數量):當調用該api的QPS達到門檻值的時候,進行限流

線程數:當調用該api的線程數達到門檻值的時候,進行限流

是否叢集:不需要叢集

流控模式:

直接:api達到限流條件時,直接限流

關聯:當關聯的資源達到門檻值時,就限流自己

鍊路:隻記錄指定鍊路上的流量(指定資源從入口資源進來的流量,如果達到門檻值,就進行限流)[api級别的針對來源]

流控效果:

快速失敗:直接失敗,抛異常

Warm Up:根據codeFactor (冷加載因子,預設3)的值,從門檻值 / codeFactor,經過預熱時長,才達到設定的QPS門檻值

排隊等待:勻速排隊,讓請求以勻速的速度通過,門檻值類型必須設定為QPS,則無效

1,流控模式

直接(預設)–> 快速失敗

打開sentinel界面,建立流控規則

sentinel限流實戰sentinel限流實戰

儲存後,持久化到nacos

sentinel限流實戰sentinel限流實戰

核對對應的伺服器位址,組,命名空間,檔案名是否與用戶端配置一一對應

限流規則建立完畢

用戶端限流配置

在要限流的服務方法上添加 注解@SentinelResource

@SentinelResource(value = "articleList",
        blockHandlerClass = ApArticleBlockHandler.class,
        blockHandler = "blockHandler",
        fallbackClass = ApArticleFallback.class,
        fallback = "fallback")
           

https://blog.csdn.net/liuerchong/article/details/119055193

重新開機服務進行測試

點選超過兩次後對接口進行了限流

sentinel限流實戰sentinel限流實戰

繼續閱讀