docker布署應(yīng)用案例:ftp://www.cjlly.com:4001/(賬號(hào)/密碼:test/test株婴,點(diǎn)擊后請(qǐng)去掉前面的http:// , 然后復(fù)制粘貼)
寫在前面的話:10.25出現(xiàn)的問(wèn)題邮屁,是因?yàn)槲遗渲昧薾acos集群彩郊,同時(shí)集群中的一臺(tái)服務(wù)又down掉矫废,導(dǎo)致配置服務(wù)出現(xiàn)了故障岂膳,所致!
一纳猫、微服務(wù)規(guī)則獲取流程:
1. 正常流程:
微服務(wù)啟動(dòng)婆咸,監(jiān)聽8719端口,sentinel server內(nèi)部規(guī)則變化芜辕,將通知微服務(wù)擅耽,微服務(wù)應(yīng)用這些規(guī)則創(chuàng)造調(diào)用鏈路上的攔截器。但此方式的缺點(diǎn)是物遇,微服務(wù)重新啟動(dòng)后乖仇,規(guī)則將丟失。
2. 使用持久化流程:
微服務(wù)啟動(dòng)询兴,相根據(jù)sentinel.datasource
的配置乃沙,讀取相應(yīng)的規(guī)則腳本,然后應(yīng)用些腳本诗舰,創(chuàng)造鏈路上的攔截器警儒。
而sentinel.datasource可以是,zookeeper,consoul,redis及nacos等。此方式可以直接使用預(yù)先編寫好的規(guī)則眶根,同時(shí)完了的持久化任務(wù)蜀铲。最關(guān)鍵的是:此方式并不需要有sentinel server的參與。
3. 對(duì)SentinelServer的重新認(rèn)識(shí)定位:
(1)流量監(jiān)控器
(2)規(guī)則顯示器
(3)臨時(shí)規(guī)則配置器
重點(diǎn):其實(shí)它并不是流控規(guī)則的核心属百,真正的核心是:<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
二记劝、配置過(guò)程:
- pom
<dependencies>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
- applicaiton.yml
請(qǐng)注意john_rule
的配置
server:
port: 3600
spring:
application:
name: sentinel-rule-presistence
cloud:
nacos:
discovery:
server-addr: localhost:8848
sentinel:
# transport:
# dashboard: localhost:8080
# port: 8719
datasource:
ds1:
nacos:
server-addr: localhost:8848
dataId: john-rule
groupId: DEFAULT_GROUP
data-type: json
rule_type: flow
- controller及啟動(dòng)類
@RestController
public class TestController {
@GetMapping("/test1")
public String test1(){
return "test1";
}
@GetMapping("/test2")
public String test2(){
return "test2";
}
}
@SpringBootApplication
public class SentinelRuleApp {
public static void main(String[] args) {
SpringApplication.run(SentinelRuleApp.class,args);
}
}
- john_rule的內(nèi)容:
[
{
"resource": "/test1",
"limitApp":"default",
"grade":1,
"count":1,
"strategy":0,
"controlBehavior":0,
"clusterMode":false
},
{
"resource": "/test2",
"limitApp":"default",
"grade":1,
"count":2,
"strategy":0,
"controlBehavior":0,
"clusterMode":false
}
]
- 最后,在nacos中加入配置:
三族扰、最后使用sentinel server
啟動(dòng) sentinel 厌丑,并打開以下注釋:
sentinel:
transport:
dashboard: localhost:8080
port: 8719
結(jié)果: