問道阿里哨兵Sentinel框架_儀表盤_限流_v0.0.2

文件名稱 版本號 作者 qq 版本
問道阿里哨兵Sentinel框架_儀表盤_限流 v0.0.2 若布與宮合 8416837 SpringBoot 2.2.2
Spring-cloud-starter-alibaba-sentinel 2.2.1

介紹

查看阿里哨兵

儀表盤 控制檯

在這裏插入圖片描述

代碼

  • yaml
    將下述yaml文件寫到通信項目裏,這個項目負責與sentinel通信,一般是個SpringBoot後端。注意看註釋。
spring:
  cloud:
    sentinel:
      # 取消控制檯懶加載
      eager: true
      transport:
        # 控制檯地址 訪問地址
        dashboard: 127.0.0.1:8718
        port: 8719 # 通信端口 默認8719
      # nacos配置持久化
      datasource:
        ds1:
          nacos:
            server-addr: 192.168.59.135:8848
            dataId: sentinel-${spring.application.name}
            groupId: COMPONENT_GROUP
#            groupId: DEFAULT_GROUP
            data-type: json
            rule-type: flow

另外,在Nacos新建【數據源】配置,配置的Data Id與上文數據源dataId相同:
abc-auth、abc-system指spring.application.name,請改成你自己項目的。

[
    {
        "resource": "abc-auth",
        "count": 500,
        "grade": 1,
        "limitApp": "default",
        "strategy": 0,
        "controlBehavior": 0
    },
	{
        "resource": "abc-system",
        "count": 1000,
        "grade": 1,
        "limitApp": "default",
        "strategy": 0,
        "controlBehavior": 0
    }
]

在這裏插入圖片描述

		<!--        哨兵-->
        <!-- SpringCloud Ailibaba Sentinel -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>

        <!-- SpringCloud Ailibaba Sentinel Gateway -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
        </dependency>

        <!-- Sentinel Datasource Nacos -->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
        </dependency>
# 克隆後,使用遠程倉庫的Dashboard部分
git clone https://gitee.com/mirrors/Sentinel

啓動App或者打成Fat Jar包。
在這裏插入圖片描述

  • 啓動命令 單機試水
# 注意:8718呼應上文的控制檯地址
java -Dserver.port=8718 -Dcsp.sentinel.dashboard.server=localhost:8718 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.7.2.jar

啓動

  • 啓動成功,但還未與主機通信。賬號密碼:sentinel=sentinel
    在這裏插入圖片描述
  • 啓動通信機(被監控的應用須與通信機在同樣GROUP),再查看sentinel控制檯:
    在這裏插入圖片描述
    在這裏插入圖片描述

限流 資源模式

基礎工作

引入依賴
	<properties>
        <alibaba.cloud.version>2.2.1.RELEASE</alibaba.cloud.version>
	</properties>
		<dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
            <version>${alibaba.cloud.version}</version>
        </dependency>
代碼
  • 基本配置
package test.config;

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 org.springframework.context.annotation.Configuration;

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

/**
 * 功能:哨兵限流配置 聲明式預定義,到時候應用到資源
 *
 * @author: cc
 * @qq: 8416837
 * @date: 2020/5/5 13:39
 */
@Configuration
public class SentinelConfig {

	@PostConstruct
	public void initFlowRules() {
		List<FlowRule> rules = new ArrayList<>();
		FlowRule rule = new FlowRule();
//		HelloCC: 哨兵限流配置
		rule.setResource("HelloCC");
		rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
		// Set limit QPS to 5.
		rule.setCount(5);
		rules.add(rule);
		FlowRuleManager.loadRules(rules);
	}
}
  • 應用到下述接口:
	@GetMapping("/get")
	public ResponseEntity get() {
		while (true) {
			Entry entry = null;
			try {
				log.debug("{} === {}", "[代碼主動阻塞100毫秒,保證每秒大約10個'模擬請求']");
				Thread.sleep(100L);
				// HelloCC指上文的限流資源配置
				entry = SphU.entry("HelloCC");
				/*您的業務邏輯 - 開始*/
				log.debug("{} === {}", "[>>業務邏輯中 。。。<<]");
				/*您的業務邏輯 - 結束*/
			} catch (BlockException e1) {
				/*流控邏輯處理 - 開始*/
				log.debug("{} === {}", "[請求已被阻塞!]");
				/*流控邏輯處理 - 結束*/
			} catch (InterruptedException e) {
				e.printStackTrace();
			} finally {
				if (entry != null) {
					entry.exit();
				}
			}
		}
	}

主要就是用entry = SphU.entry("HelloCC");entry.exit();將業務代碼包起來,或者使用【註解】更優雅。

測試

觸發限流

調用上文寫的get接口

日誌

根據日誌,可以發現,單機限流成功了。qps設置5,即每秒大概允許5個請求,實際有10個請求,大概5個請求被限流了。但是還要細分析是滑動窗口限流,還是令牌桶限流;限流後的處理是服務降級還是拒絕服務從而直接返回響應提示。

2020-05-05 14:13:13.917 DEBUG 36424 --- [  XNIO-1 task-1] test.rest.TestController                 : [代碼主動阻塞100毫秒,保證每秒大約10'模擬請求'] === {}
2020-05-05 14:13:14.017 DEBUG 36424 --- [  XNIO-1 task-1] test.rest.TestController                 : [>>業務邏輯中 。。。<<] === {}
2020-05-05 14:13:14.017 DEBUG 36424 --- [  XNIO-1 task-1] test.rest.TestController                 : [代碼主動阻塞100毫秒,保證每秒大約10'模擬請求'] === {}
2020-05-05 14:13:14.118 DEBUG 36424 --- [  XNIO-1 task-1] test.rest.TestController                 : [請求已被阻塞!] === {}
2020-05-05 14:13:14.118 DEBUG 36424 --- [  XNIO-1 task-1] test.rest.TestController                 : [代碼主動阻塞100毫秒,保證每秒大約10'模擬請求'] === {}
2020-05-05 14:13:14.218 DEBUG 36424 --- [  XNIO-1 task-1] test.rest.TestController                 : [請求已被阻塞!] === {}
2020-05-05 14:13:14.219 DEBUG 36424 --- [  XNIO-1 task-1] test.rest.TestController                 : [代碼主動阻塞100毫秒,保證每秒大約10'模擬請求'] === {}
2020-05-05 14:13:14.319 DEBUG 36424 --- [  XNIO-1 task-1] test.rest.TestController                 : [請求已被阻塞!] === {}
2020-05-05 14:13:14.319 DEBUG 36424 --- [  XNIO-1 task-1] test.rest.TestController                 : [代碼主動阻塞100毫秒,保證每秒大約10'模擬請求'] === {}
2020-05-05 14:13:14.420 DEBUG 36424 --- [  XNIO-1 task-1] test.rest.TestController                 : [請求已被阻塞!] === {}
2020-05-05 14:13:14.420 DEBUG 36424 --- [  XNIO-1 task-1] test.rest.TestController                 : [代碼主動阻塞100毫秒,保證每秒大約10'模擬請求'] === {}
2020-05-05 14:13:14.521 DEBUG 36424 --- [  XNIO-1 task-1] test.rest.TestController                 : [>>業務邏輯中 。。。<<] === {}
2020-05-05 14:13:14.521 DEBUG 36424 --- [  XNIO-1 task-1] test.rest.TestController                 : [代碼主動阻塞100毫秒,保證每秒大約10'模擬請求'] === {}
2020-05-05 14:13:14.621 DEBUG 36424 --- [  XNIO-1 task-1] test.rest.TestController                 : [>>業務邏輯中 。。。<<] === {}
2020-05-05 14:13:14.621 DEBUG 36424 --- [  XNIO-1 task-1] test.rest.TestController                 : [代碼主動阻塞100毫秒,保證每秒大約10'模擬請求'] === {}
2020-05-05 14:13:14.721 DEBUG 36424 --- [  XNIO-1 task-1] test.rest.TestController                 : [>>業務邏輯中 。。。<<] === {}
2020-05-05 14:13:14.721 DEBUG 36424 --- [  XNIO-1 task-1] test.rest.TestController                 : [代碼主動阻塞100毫秒,保證每秒大約10'模擬請求'] === {}
2020-05-05 14:13:14.822 DEBUG 36424 --- [  XNIO-1 task-1] test.rest.TestController                 : [>>業務邏輯中 。。。<<] === {}
2020-05-05 14:13:14.822 DEBUG 36424 --- [  XNIO-1 task-1] test.rest.TestController                 : [代碼主動阻塞100毫秒,保證每秒大約10'模擬請求'] === {}
2020-05-05 14:13:14.922 DEBUG 36424 --- [  XNIO-1 task-1] test.rest.TestController                 : [請求已被阻塞!] === {}
2020-05-05 14:13:14.922 DEBUG 36424 --- [  XNIO-1 task-1] test.rest.TestController                 : [代碼主動阻塞100毫秒,保證每秒大約10'模擬請求'] === {}
2020-05-05 14:13:15.023 DEBUG 36424 --- [  XNIO-1 task-1] test.rest.TestController                 : [>>業務邏輯中 。。。<<] === {}

附件

jar

下載sentinel zip文件 jar

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章