欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring中的Sentinel規(guī)則持久化解析

 更新時(shí)間:2023年09月12日 09:50:58   作者:小新要變強(qiáng)  
這篇文章主要介紹了Spring中的Sentinel規(guī)則持久化解析,具體內(nèi)容包括,Sentinel規(guī)則推送三種模式介紹,原始模式,拉模式,推模式,并對(duì)基于Nacos配置中心控制臺(tái)實(shí)現(xiàn)推送進(jìn)行詳盡介紹,需要的朋友可以參考下

前言

本文小新為大家?guī)?Sentinel規(guī)則持久化 相關(guān)知識(shí),具體內(nèi)容包括, Sentinel規(guī)則推送三種模式 介紹,包括: 原始模式 , 拉模式 , 推模式 ,并對(duì) 基于Nacos配置中心控制臺(tái)實(shí)現(xiàn)推送 進(jìn)行詳盡介紹~

Sentinel規(guī)則的推送有下面三種模式:

推送模式說明優(yōu)點(diǎn)缺點(diǎn)
原始模式API 將規(guī)則推送至客戶端并直接更新到內(nèi)存中,擴(kuò)展寫數(shù)據(jù)源(WritableDataSource)簡單,無任何依賴不保證一致性;規(guī)則保存在內(nèi)存中,重啟即消失。嚴(yán)重不建議用于生產(chǎn)環(huán)境
Pull 模式擴(kuò)展寫數(shù)據(jù)源(WritableDataSource), 客戶端主動(dòng)向某個(gè)規(guī)則管理中心定期輪詢拉取規(guī)則,這個(gè)規(guī)則中心可以是 RDBMS、文件 等簡單,無任何依賴;規(guī)則持久化不保證一致性;實(shí)時(shí)性不保證,拉取過于頻繁也可能會(huì)有性能問題。
Push 模式擴(kuò)展讀數(shù)據(jù)源(ReadableDataSource),規(guī)則中心統(tǒng)一推送,客戶端通過注冊(cè)監(jiān)聽器的方式時(shí)刻監(jiān)聽變化,比如使用 Nacos,Zookeeper 等配置中心。這種方式有更好的實(shí)時(shí)性和一致性保證。生產(chǎn)環(huán)境下一般采用 push 模式的數(shù)據(jù)源。規(guī)則持久化;一致性;快速引入第三方依賴

一、原始模式

如果不做任何修改,Dashboard 的推送規(guī)則方式是通過 API 將規(guī)則推送至客戶端并直接更 新到內(nèi)存中:

在這里插入圖片描述

這種做法的好處是簡單,無依賴;壞處是應(yīng)用重啟規(guī)則就會(huì)消失,僅用于簡單測試,不能 用于生產(chǎn)環(huán)境。

二、拉模式

pull 模式的數(shù)據(jù)源(如本地文件、RDBMS 等)一般是可寫入的。使用時(shí)需要在客戶端注冊(cè)數(shù)據(jù)源:將對(duì)應(yīng)的讀數(shù)據(jù)源注冊(cè)至對(duì)應(yīng)的 RuleManager,將寫數(shù)據(jù)源注冊(cè)至 transport 的 WritableDataSourceRegistry 中。

三、推模式

生產(chǎn)環(huán)境下一般更常用的是 push 模式的數(shù)據(jù)源。對(duì)于 push 模式的數(shù)據(jù)源,如遠(yuǎn)程配置中心 (ZooKeeper, Nacos, Apollo等等),推送的操作不應(yīng)由 Sentinel 客戶端進(jìn)行,而應(yīng)該經(jīng)控 制臺(tái)統(tǒng)一進(jìn)行管理,直接進(jìn)行推送,數(shù)據(jù)源僅負(fù)責(zé)獲取配置中心推送的配置并更新到本 地。

因此推送規(guī)則正確做法應(yīng)該是: 配置中心控制臺(tái)/Sentinel 控制臺(tái)配置中心Sentinel 數(shù)據(jù)源Sentinel

而不是經(jīng) Sentinel 數(shù)據(jù)源推送至配置中心。這樣的流程就非常清晰了。

基于Nacos配置中心控制臺(tái)實(shí)現(xiàn)推送:

官方demo: sentinel­demo­nacos­datasource

(1)引入依賴

<dependency>
  <groupId>com.alibaba.csp</groupId>
  <artifactId>sentinel‐datasource‐nacos</artifactId>
</dependency>

(2)nacos配置中心中配置流控規(guī)則

[
  {
    "resource": "TestResource",
    "controlBehavior": 0,
    "count": 10.0,
    "grade": 1,
    "limitApp": "default",
    "strategy": 0
  }
]

在這里插入圖片描述

(3)application.yml中進(jìn)行配置

spring:
  application:
    name: mall‐user‐sentinel‐demo
    cloud:
      nacos:
        discovery:
          server‐addr: 127.0.0.1:8848
      sentinel:
        transport:
          # 添加sentinel的控制臺(tái)地址
          dashboard: 127.0.0.1:8080
          # 指定應(yīng)用與Sentinel控制臺(tái)交互的端口,應(yīng)用本地會(huì)起一個(gè)該端口占用的HttpServer
          port: 8719
        datasource:
          ds1:
            nacos:
              server‐addr: 127.0.0.1:8848
              dataId: ${spring.application.name}
              groupId: DEFAULT_GROUP
              data‐type: json
              rule‐type: flow

(4)nacos配置中心中添加

[
  {
    "resource": "userinfo",
    "limitApp": "default",
    "grade": 1,
    "count": 1,
    "strategy": 0,
    "controlBehavior": 0,
    "clusterMode": false
  }
]

在這里插入圖片描述

引入依賴:

<!‐‐sentinel持久化 采用 Nacos 作為規(guī)則配置數(shù)據(jù)源‐‐>
<dependency>
  <groupId>com.alibaba.csp</groupId>
  <artifactId>sentinel‐datasource‐nacos</artifactId>
</dependency>

增加application.yml配置:

server:
  port: 8806
spring:
  application:
    name: mall‐user‐sentinel‐rule‐push‐demo #微服務(wù)名稱
  #配置nacos注冊(cè)中心地址
  cloud:
    nacos:
      discovery:
        server‐addr: 127.0.0.1:8848
    sentinel:
      transport:
        # 添加sentinel的控制臺(tái)地址
        dashboard: 127.0.0.1:8080
        # 指定應(yīng)用與Sentinel控制臺(tái)交互的端口,應(yīng)用本地會(huì)起一個(gè)該端口占用的HttpServer
        #port: 8719
        datasource:
          # ds1: #名稱自定義,唯一
          # nacos:
          # server‐addr: 127.0.0.1:8848
          # dataId: ${spring.application.name}
          # groupId: DEFAULT_GROUP
          # data‐type: json
          # rule‐type: flow
        flow‐rules:
          nacos:
            server‐addr: 127.0.0.1:8848
            dataId: ${spring.application.name}‐flow‐rules
            groupId: SENTINEL_GROUP # 注意groupId對(duì)應(yīng)Sentinel Dashboard中的定義
            data‐type: json
            rule‐type: flow
        degrade‐rules:
          nacos:
            server‐addr: 127.0.0.1:8848
            dataId: ${spring.application.name}‐degrade‐rules
            groupId: SENTINEL_GROUP
            data‐type: json
            rule‐type: degrade
        param‐flow‐rules:
          nacos:
            server‐addr: 127.0.0.1:8848
            dataId: ${spring.application.name}‐param‐flow‐rules
            groupId: SENTINEL_GROUP
            data‐type: json
            rule‐type: param‐flow
        authority‐rules:
          nacos:
            server‐addr: 127.0.0.1:8848
            dataId: ${spring.application.name}‐authority‐rules
            groupId: SENTINEL_GROUP
            data‐type: json
            rule‐type: authority
        system‐rules:
          nacos:
            server‐addr: 127.0.0.1:8848
            dataId: ${spring.application.name}‐system‐rules
            groupId: SENTINEL_GROUP
            data‐type: json
            rule‐type: system

以流控規(guī)則測試,當(dāng)在sentinel dashboard配置了流控規(guī)則,會(huì)在nacos配置中心生成對(duì)應(yīng) 的配置。

在這里插入圖片描述

到此這篇關(guān)于Spring中的Sentinel規(guī)則持久化解析的文章就介紹到這了,更多相關(guān)Sentinel規(guī)則持久化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論