sentinel-dashboard-apollo 使用

sentinel-dashboard-apollo 使用

说明

使用前需要对 apollo 的基本使用有一定了解,如果对 apollo 还不是很了解,可以先看下 apollo 的使用文档。

获取 sentinel-dashboard-apollo

替换成实际版本

cd Sentinel
version=1.8.4
git checkout tags/dashboard/apollo/v${version} -b sentinel-dashboard-apollo-${version}
cd sentinel-dashboard
mvn clean package -DskipTests=true -Dmaven.javadoc.skip=true

打包好的 jar 在 target/sentinel-dashboard-apollo-${version}.jar

apollo 开放平台配置

参考:https://www.apolloconfig.com/#/zh/usage/apollo-open-api-platform(opens new window)

启动 dashboard

可以直接通过 java -jar 启动,在官方版本启动参数的基础上增加了 apollo 的配置,对官方版本启动参数完全兼容。

官方版本启动参数请查看:Sentinel 控制台 (opens new window),这里不做说明。

启动参数

参数 默认值 说明 版本支持
metric.store-type default 监控数据持久化类型:default, redis 1.8.6.1+
spring.redis.host 127.0.0.1 metric.store-type = redis 时需要配置 1.8.6.1+
spring.redis.port 6379 metric.store-type = redis 时需要配置 1.8.6.1+
rule.store.type default 规则配置持久化类型:default, apollo 1.8.6.1+
apollo.sentinel.portalUrl http://localhost:8080 apollo portal server 地址 1.8.4+
apollo.sentinel.token token apollo 开放平台 token,根据实际修改 1.8.4+
apollo.sentinel.operator apollo 数据提交的 apollo 用户 1.8.4+
apollo.sentinel.appId app-sentinel 数据持久化的 appId 1.8.4+
apollo.sentinel.env DEV 对应 apollo 环境 1.8.4+
apollo.sentinel.clusterName default apollo 集群,一般都是 default 1.8.4+
apollo.sentinel.namespace application 数据保存的命名空间 1.8.4+

启动命令示例

java -jar sentinel-dashboard-apollo.jar \
  --server.port=8081 \
  --metric.store-type=redis \
  --spring.redis.host=redis-demo \
  --spring.redis.port=6379 \
  --rule.store.type=apollo \
  --apollo.sentinel.portalUrl=http://192.168.1.10:8070 \
  --apollo.sentinel.token=7273d9b5892d8af51b0d7400b61401ef57f1bf4e \
  --apollo.sentinel.operator=halo \
  --apollo.sentinel.appId=halo-common \
  --apollo.sentinel.env=DEV \
  --apollo.sentinel.namespace=halo-sentinel-rule

docker 方式启动

docker run -d --name sentinel-dashboard \
-p 8080:8080 \
-e JAVA_OPTS="-Dlogging.level.com.alibaba.csp.sentinel=debug \
-Dserver.port=8080 \
-Dmetric.store-type=redis \
-Dspring.redis.host=redis-demo \
-Dspring.redis.port=6379 \
-Drule.store.type=apollo \
-Dapollo.sentinel.portalUrl=http://apollo-demo:8070 \
-Dapollo.sentinel.token=183d39a00a46656063ab286830f71e023d24c454 \
-Dapollo.sentinel.operator=apollo \
-Dapollo.sentinel.appId=sentinel-demo-server \
-Dapollo.sentinel.env=DEV \
-Dapollo.sentinel.namespace=sentinel-rule \
fengjx/sentinel-dashboard:1.8.6.1

dataId 命名规则

服务名-规则类型

共有 7 种规则类型,对应类型可以查看枚举com.alibaba.csp.sentinel.dashboard.rule.RuleType

/**
  * 流控规则
  */
FLOW("flow", FlowRuleEntity.class),
/**
  * 熔断规则
  */
DEGRADE("degrade", DegradeRuleEntity.class),
/**
  * 热点规则
  */
PARAM_FLOW("param-flow", ParamFlowRuleEntity.class),
/**
  * 系统规则
  */
SYSTEM("system", SystemRuleEntity.class),
/**
  * 授权规则
  */
AUTHORITY("authority", AuthorityRuleEntity.class),
/**
  * 网关流控规则
  */
GW_FLOW("gw-flow", GatewayFlowRuleEntity.class),
/**
  * api 分组
  */
GW_API_GROUP("gw-api-group", ApiDefinitionEntity.class);

apollo 中的配置示例

img

客户端配置

添加数据源扩展依赖依赖

<groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-apollo</artifactId>
    <version>对应官方版本号</version>
</dependency>

参数配置与官方配置完全一致

动态规则扩展 - 普通应用接入参考

文档:https://sentinelguard.io/zh-cn/docs/dynamic-rule-configuration.html(opens new window)

参考推模式:使用 Apollo 配置规则 章节

Spring Cloud Alibaba Sentinel 参考

文档:https://github.com/alibaba/spring-cloud-alibaba/wiki/Sentinel(opens new window)

配置示例

sentinel.apollo.namespace-name=halo-sentinel-rule
spring.cloud.sentinel.transport.dashboard=192.168.1.10:8081
spring.cloud.sentinel.log.dir=${LOG_PATH:./logs}/sentinel

# 流控规则
spring.cloud.sentinel.datasource.ds1.apollo.rule-type=flow
spring.cloud.sentinel.datasource.ds1.apollo.namespace-name=${sentinel.apollo.namespace-name}
spring.cloud.sentinel.datasource.ds1.apollo.flow-rules-key=${spring.application.name}-${spring.cloud.sentinel.datasource.ds1.apollo.rule-type}

# 熔断规则
spring.cloud.sentinel.datasource.ds2.apollo.rule-type=degrade
spring.cloud.sentinel.datasource.ds2.apollo.namespace-name=${sentinel.apollo.namespace-name}
spring.cloud.sentinel.datasource.ds2.apollo.flow-rules-key=${spring.application.name}-${spring.cloud.sentinel.datasource.ds2.apollo.rule-type}

## 热点规则
spring.cloud.sentinel.datasource.ds3.apollo.rule-type=param-flow
spring.cloud.sentinel.datasource.ds3.apollo.namespace-name=${sentinel.apollo.namespace-name}
spring.cloud.sentinel.datasource.ds3.apollo.flow-rules-key=${spring.application.name}-${spring.cloud.sentinel.datasource.ds3.apollo.rule-type}

# 系统规则
spring.cloud.sentinel.datasource.ds4.apollo.rule-type=system
spring.cloud.sentinel.datasource.ds4.apollo.namespace-name=${sentinel.apollo.namespace-name}
spring.cloud.sentinel.datasource.ds4.apollo.flow-rules-key=${spring.application.name}-${spring.cloud.sentinel.datasource.ds4.apollo.rule-type}

# 认证规则

spring.cloud.sentinel.datasource.ds5.apollo.rule-type=authority
spring.cloud.sentinel.datasource.ds5.apollo.namespace-name=${sentinel.apollo.namespace-name}
spring.cloud.sentinel.datasource.ds5.apollo.flow-rules-key=${spring.application.name}-${spring.cloud.sentinel.datasource.ds5.apollo.rule-type}

spring-boot(cloud) 整合 demo

https://github.com/fengjx/sentinel-dashboard-demo

转自