使用apollo為sentinel做數(shù)據(jù)持久化,sentinel的使用可以看sentinel使用,本項(xiàng)目打算采用spring-cloud-alibaba跟sentinel整合
背景
我們的項(xiàng)目即將上線了,但是缺少一個(gè)流量防衛(wèi)兵,這個(gè)很要命,于是一周前我就開始著手看sentinel
為什么是sentinel
正如你們所了解的市面上有很多服務(wù)降級(jí)方案,如我也用過的hystrix,但是它沒有完善的持久方案和后臺(tái)管理界面再加上我也看了一片sentinel作者宿何的一篇對(duì)比文章Sentinel-與-Hystrix-的對(duì)比,而且我們現(xiàn)在的技術(shù)不太適合使用spring cloud體系
參考文檔
整合流程
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>0.9.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-apollo</artifactId>
<version>1.6.0</version>
</dependency>
application.yml
spring:
application:
name: spring-boot-sentinel-apollo
cloud:
sentinel:
transport:
port: 8719 # 向sentinel-dashboard傳輸數(shù)據(jù)的端口 默認(rèn):8719
dashboard: localhost:8100 # sentinel-dashboard
log:
dir: ./logs # 默認(rèn)值${home}/logs/csp/
switch-pid: true # 日志帶上線程id
datasource:
flow: # 流控規(guī)則
apollo:
namespaceName: application
flowRulesKey: flowRules
rule-type: flow #flow,degrade,authority,system, param-flow
degrade: # 熔斷降級(jí)規(guī)則
apollo:
namespaceName: application
flowRulesKey: degrades
rule-type: degrade
authority: # 授權(quán)規(guī)則 未驗(yàn)證,官方不推薦
apollo:
namespaceName: application
flowRulesKey: authoritys
rule-type: authority
system: # 系統(tǒng)規(guī)則
apollo:
namespaceName: application
flowRulesKey: systems
rule-type: system
param-flow: # 熱點(diǎn)規(guī)則
apollo:
namespaceName: application
flowRulesKey: paramflows
rule-type: param-flow
app:
id: ${spring.application.name}
apollo:
meta: http://127.0.0.1:8080
cacheDir: ./apolloconfig # 緩存文件位置
java
@SpringBootApplication
@EnableApolloConfig // 開啟apollo
public class SpringSentinelApolloServer {
public static void main(String[] args) {
SpringApplication.run(SpringSentinelApolloServer.class, args);
}
}
jvm參數(shù)配置
-Denv=DEV
flow(流控規(guī)則)參數(shù)格式j(luò)son
[
{
"resource": "/hello",
"limitApp": "default",
"grade": 1,
"count": 3,
"strategy": 0,
"controlBehavior": 0,
"clusterMode": false
}
]
flow(流控規(guī)則)參數(shù)規(guī)則說明
| 字段 | 描述 | 默認(rèn)值 |
|---|---|---|
| resource | 資源名,即限流規(guī)則的作用對(duì)象 | |
| limitApp | 流控針對(duì)的調(diào)用來源,若為 default 則不區(qū)分調(diào)用來源 | default |
| grade | 限流閾值類型(QPS 或并發(fā)線程數(shù));0代表根據(jù)并發(fā)數(shù)量來限流,1代表根據(jù)QPS來進(jìn)行流量控制 | QPS 模式 |
| count | 限流閾值 | |
| strategy | 調(diào)用關(guān)系限流策略 | |
| controlBehavior | 流量控制效果(直接拒絕、Warm Up、勻速排隊(duì)) | 拒絕 |
| clusterMode | 是否為集群模式 |
system 系統(tǒng)規(guī)則參數(shù)格式,四個(gè)參數(shù)只能選擇一個(gè)不能設(shè)置-1
[{"qps": 2}]
system參數(shù)列表
| 參數(shù) | 說明 |
|---|---|
| avgLoad | 最大的 load
|
| avgRt | 所有入口流量的平均響應(yīng)時(shí)間 |
| maxThread | 入口流量的最大并發(fā)數(shù) |
| qps | 所有入口資源的 QPS |
degrade 參數(shù)格式 json
[
{
"resource": "/rt",
"count": 50,
"timeWindow": 5,
"grade": 0
},
{
"resource": "/count",
"count": 5,
"timeWindow": 8,
"grade": 2
},
{
"resource": "/erro",
"count": 0.5,
"timeWindow": 5,
"grade": 1
}
]
degrade(熔斷降級(jí)規(guī)則)參數(shù)規(guī)則說明
| 字段 | 描述 | 默認(rèn)值 |
|---|---|---|
| resource | 資源名,即限流規(guī)則的作用對(duì)象 | |
| count | 閾值 | |
| grade | 降級(jí)模式,根據(jù) RT (0)、異常數(shù)(2)、 異常比例(1) | RT (0) |
| timeWindow | 降級(jí)的時(shí)間,單位為 s |
param-flow(熱點(diǎn)規(guī)則) json
[
{
"resource": "/hello",
"grade": 1,
"paramIdx": 1,
"count": 10,
"paramFlowItemList": []
}
]
param-flow(熱點(diǎn)規(guī)則) 參數(shù)
| 字段 | 描述 | 默認(rèn)值 |
|---|---|---|
| resource | 資源名,必填 | |
| grade | 限流模式 | qps(1) |
| paramIdx | 熱點(diǎn)參數(shù)的索引,必填,對(duì)應(yīng) SphU.entry(xxx, args) 中的參數(shù)索引位置 |
|
| count | 限流閾值,必填 | |
| paramFlowItemList | 參數(shù)例外項(xiàng),可以針對(duì)指定的參數(shù)值單獨(dú)設(shè)置限流閾值,不受前面 count 閾值的限制。 |
apollo上配置
server.port = 7852
server.servlet.context-path = /sentinel
flowRules = [{"resource": "/hello","limitApp": "default","grade": 1,"count": 3,"strategy": 0,"controlBehavior": 0,"clusterMode": false}]
degrades = [{"resource": "/rt","count": 50,"timeWindow": 5,"grade": 0},{"resource": "/count","count": 5,"timeWindow": 8,"grade": 2},{"resource": "/erro","count": 0.5,"timeWindow": 5,"grade": 1}]
authoritys = [{"resource": "/hello","limitApp": "192.168.12.215","strategy": 1}]
paramflows = [{"resource": "/hello","grade": 1,"paramIdx": 1,"count": 10,"paramFlowItemList": []}]
systems = [{"qps": 20}]
拉去規(guī)則成功日志
2019-05-14 09:26:46.072 INFO 10100 --- [ main] o.s.c.a.s.c.SentinelDataSourceHandler : [Sentinel Starter] DataSource authority-sentinel-apollo-datasource load 1 AuthorityRule
2019-05-14 09:26:46.090 INFO 10100 --- [ main] o.s.c.a.s.c.SentinelDataSourceHandler : [Sentinel Starter] DataSource degrade-sentinel-apollo-datasource load 3 DegradeRule
2019-05-14 09:26:46.099 INFO 10100 --- [ main] o.s.c.a.s.c.SentinelDataSourceHandler : [Sentinel Starter] DataSource flow-sentinel-apollo-datasource load 1 FlowRule
2019-05-14 09:26:46.115 INFO 10100 --- [ main] o.s.c.a.s.c.SentinelDataSourceHandler : [Sentinel Starter] DataSource param-flow-sentinel-apollo-datasource load 1 ParamFlowRule
2019-05-14 09:26:46.122 INFO 10100 --- [ main] o.s.c.a.s.c.SentinelDataSourceHandler : [Sentinel Starter] DataSource system-sentinel-apollo-datasource load 1 SystemRule
測(cè)試接口
http://localhost:7852/sentinel/hello
最終效果圖

sentinel-dashboard

apollo-dashboard
結(jié)尾
相信堅(jiān)持看完的同學(xué)已經(jīng)崩潰了,拉模式的配置太頭疼了.的確為了完成這篇文章,里面的參數(shù)都是我在瀏覽器里面抓取過來的耗費(fèi)了我大量的時(shí)間,巧的是sentinel官方也意識(shí)到了這點(diǎn)他們提供了推模式只不過需要修改sentinel-dashboard
的源碼,后面我會(huì)把真理好的代碼提交在sentinel-dashboard-apollo倉(cāng)庫(kù),已經(jīng)需改了一點(diǎn),有興趣的可以先去看