Sentinel 的工作模式
1慢逾、原始模式(非生產(chǎn)環(huán)境推薦模式):不持久化立倍,重啟微服務(wù)灭红,配置的限流規(guī)則就會丟失
2、Pull模式(非生產(chǎn)環(huán)境推薦模式):(拉模式)口注,將規(guī)則緩存到本地文件
3变擒、push模式(生產(chǎn)環(huán)境推薦模式):將規(guī)則在云端配置中心,然后將云端配置推送至sentinel dashboard寝志,在dashboard配置的規(guī)則也可以知道同步到云端配置中心
下面重點介紹第三種模式(push模式)
1.pom.xml添加依賴
<parent>
? ? ? ? <groupId>org.springframework.boot</groupId>
? ? ? ? <artifactId>spring-boot-starter-parent</artifactId>
? ? ? ? <version>2.1.6.RELEASE</version>
? ? ? ? <relativePath/>
? ? </parent>
<!--引入父依賴 -->
<dependencyManagement>
? ? ? ? <dependencies>
? ?? <!--spring-cloud-dependencies-->
????????????<dependency>
????????????????<groupId>org.springframework.cloud</groupId>
????????????????<artifactId>spring-cloud-dependencies</artifactId>
????????????????<version>Hoxton.SR9</version>
????????????????<type>pom</type>
????????????<scope>import</scope>
????????????</dependency>
? ? ? ? ? ? <!--alibaba nacos -->
? ? ? ? ? ? <dependency>
? ? ? ? ? ? ? ? <groupId>com.alibaba.cloud</groupId>
? ? ? ? ? ? ? ? <artifactId>spring-cloud-alibaba-dependencies</artifactId>
? ? ? ? ? ? ? ? <version>2.2.2.RELEASE</version>
? ? ? ? ? ? ? ? <type>pom</type>
? ? ? ? ? ? ? ? <scope>import</scope>
? ? ? ? ? ? </dependency>
</dependencies>
? ? </dependencyManagement>
<dependencies>
<!--springcloud 子項目nacos配置中心依賴 -->
? ? ? ? <dependency>
? ? ? ? ? ? <groupId>com.alibaba.cloud</groupId>
? ? ? ? ? ? <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
? ? ? ? </dependency>
<!--sentinel依賴? -->
????<dependency>
????????<groupId>com.alibaba.cloud</groupId>
????????<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
????</dependency>
<!--sentinel 持久化nacos依賴 -->
? ? ?<dependency>
? ? ? ? ? ? <groupId>com.alibaba.csp</groupId>
? ? ? ? ? ? <artifactId>sentinel-datasource-nacos</artifactId>
? ? ? ? </dependency>
2.在application.yml文件中添加配置內(nèi)容娇斑,本人使用的是yml文件格式,也可以使用properties格式
spring:
? cloud:
? ? sentinel:
? ? ? transport:
? ? ? ? dashboard: ip:端口 #鏈接sentinel dashboard
? ? ? datasource: #dashbord的數(shù)據(jù)由下來庫中獲取
? ? ? ? flow: #此名稱可以隨意取材部,該值得含義是key毫缆,不過最好見名知意,如限流用flow乐导、降級用degrade等
? ? ? ? ? nacos:
? ? ? ? ? ? serverAddr: 127.0.0.1:80
? ? ? ? ? ? dataId: ${spring.application.name}.json
? ? ? ? ? ? groupId: DEFAULT_GROUP
? ? ? ? ? ? ruleType: flow
? ? ? ? ? ? dataType: json
3.在nacos中添加規(guī)則
4.創(chuàng)建測試類
@RestControllerpublicclass Test {
? ? @GetMapping("/test")
? ? public String test() {
? ? ? ? return"test sentinel";
? ? }
}
訪問幾次接口之后苦丁,就可以在Sentinel Dashboard 中看到在nacos中配置的規(guī)則信息了,并且項目服務(wù)重啟依然存在物臂。
以上的代碼是經(jīng)過本人運行過的旺拉,希望對看到的您有所幫助!