Alibaba Sentinel動態規則(Nacos數據源)

關於Sentinel支持的數據源類型及相關的結束這裡就不在敘述,請查看:

Alibaba Sentinel 動態規則(動態文件數據源)

直接進入正題:

1、pom.xml 引入依賴

<code>

<

properties

>

<

java.version

>

1.8

java.version

>

<

spring-cloud.version

>

Hoxton.SR1

spring-cloud.version

>

<

spring-cloud-alibaba-dependencies.version

>

0.9.0.RELEASE

spring-cloud-alibaba-dependencies.version

>

properties

>

<

dependencies

>

<

dependency

>

<

groupId

>

org.springframework.boot

groupId

>

<

artifactId

>

spring-boot-starter-web

artifactId

>

dependency

>

<

dependency

>

<

groupId

>

org.springframework.boot

groupId

>

<

artifactId

>

spring-boot-starter

artifactId

>

dependency

>

<

dependency

>

<

groupId

>

com.alibaba.cloud

groupId

>

<

artifactId

>

spring-cloud-starter-alibaba-sentinel

artifactId

>

<

version

>

2.2.3.RELEASE

version

>

dependency

>

<

dependency

>

<

groupId

>

org.springframework.boot

groupId

>

<

artifactId

>

spring-boot-starter-actuator

artifactId

>

dependency

>

<

dependency

>

<

groupId

>

com.alibaba

groupId

>

<

artifactId

>

fastjson

artifactId

>

<

version

>

1.2.12

version

>

dependency

>

<

dependency

>

<

groupId

>

com.alibaba.csp

groupId

>

<

artifactId

>

sentinel-datasource-nacos

artifactId

>

<

version

>

1.8.0

version

>

dependency

>

dependencies

>

<

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

>

dependencies

>

dependencyManagement

>

/<code>
<code>

<

dependency

>

<

groupId

>

com.alibaba.csp

groupId

>

<

artifactId

>

sentinel-datasource-nacos

artifactId

>

<

version

>

1.8.0

version

>

dependency

>

/<code>

這個依賴是必須的

2、application.yml配置

<code>

server:

port:

9400

spring:

application:

name:

my-app

management:

endpoints:

web:

exposure:

include:

'*'

spring:

cloud:

sentinel:

eager:

true

filter:

enabled:

true

transport:

port:

8730

dashboard:

localhost:8080

heartbeatIntervalMs:

3000

clientIp:

10.100

.102

.109

spring:

cloud:

sentinel:

datasource:

ds:

nacos:

serverAddr:

localhost:8848

dataId:

${spring.application.name}.sentinel.rule

groupId:

study

namespace:

3d857485-5f81-4f1b-9a7e-86586d05261f

username:

dev

password:

123123

ruleType:

flow

/<code>

說明:

這裡的ds是 ReadableDataSource 的名字,可隨意編寫。後面的nacos 就是對應具體的數據源,它們後面的配置就是這些具體數據源各自的配置。

serverAddr:nacos地址

dataId:nacos 配置管理中配置的數據 (如下圖)

groupId: nacos 配置管理中配置的數據 (如下圖)


Alibaba Sentinel動態規則(Nacos數據源)


namespace:命名空間的ID,我們可以配置各種環境 下的配置信息(如下圖)


Alibaba Sentinel動態規則(Nacos數據源)

我這裡是用的開發環境

username:登錄nacos的用戶名 ;我這裡配置了不同的開發環境每個開發環境分配了賬戶

password:登錄nacos的密碼;

ruleType: 規則類型;(flow,degrade,authority,system, param-flow, gw-flow,gw-api-group)

flow:流控規則;degrade:降級規則;system:系統規則;authority:授權規則;

注意:一個系統中如果有相同的規則,那隻會有一個生效。比如我項目中配置了一個基於文件的,一個基於nacos的,最終生效的是nacos。


nacos配置內容:


Alibaba Sentinel動態規則(Nacos數據源)


啟動服務,在nacos中配置的信息在Sentinel中就能看見了


分享到:


相關文章: