1. 程式人生 > 其它 >Spring Cloud Alibaba Sentinel入門簡單使用

Spring Cloud Alibaba Sentinel入門簡單使用

Spring Cloud Alibaba Sentinel入門簡單使用

匯入依賴

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
            <version>2.2.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-actuator</artifactId>
        </dependency>

actuator是資料採集工具 採集後交給sentinel做流量控制

編寫配置檔案

開放所有服務埠 同時告訴Sentinel nacos的服務埠

spring:
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    sentinel:
      transport:
        dashboard: localhost:8080
  application:
    name: provider
server:
  port: 8082

management:
  endpoints:
    web:
      exposure:
        include: '*'

下載sentinel jar包

在cmd視窗 輸入命令執行

javr -jar sentinel-dashboard-1.7.2.jar

啟動nacos服務 我們訪問幾次訪問介面

我們可以在sentinel介面看到訪問流量情況

實現流量控制

在簇點鏈路中 我們對/getPort介面進行流控

快速訪問被流控的介面 會發現頻繁訪問會被拒絕

其他流控規則:
預設是直接
如果是關聯 如下圖 那麼就是如果頻繁訪問/list介面時停止/getPort介面服務 即替罪羊

鏈路則是從controller層更細化了 如service層
實現比較麻煩 暫不學習