Sentinel-Dashboard 与 apollo 规则的相互同步

一、为什么需要改造 Sentinel:

        Sentinel-Dashboard 控制台是流量控制、熔断降级规则统一配置和管理的入口,它为用户提供了机器自发现、簇点链路自发现、监控、规则配置等功能。在 Sentinel 控制台上,我们可以配置规则并实时查看流量控制效果。在前面我们也介绍了 sentinel-dashboard 的搭建部署、springcloud 集成 sentinel 进行流控熔断以及 将 Sentinel 规则持久化到 apollo 配置中心 等一系列操作,这一篇文章我们就介绍下如何对 Sentinel 进行改造,使其更加符合生产环境的要求。

​        那为什么需要对 Sentinel 进行改造呢?主要是因为所有规则都只能通过 Nacos 界面或 Apollo 界面来完成修改才能得到持久化存储,而在 Sentinel Dashboard 中修改限流规则虽然可以生效,但是不会被持久化到配置中心。而在这两个配置中心里存储的数据是一个 Json 格式,当存储的规则越来越多,对该 Json 配置的可读性与可维护性会变的越来越差。

        ​那么如何改造呢?要回答这个问题,我们需要先了解下 Sentinel 的规则管理方式。

1、Sentinel 的规则管理方式:

        Sentinel 提供了两种修改规则的方式,第一种是通过 API 编码直接修改(loadRules),但编码的方式一般用于测试和演示;第二种是通过 DataSource 适配不同数据源进行修改,生产环境一般使用该方式来动态管理规则,Sentinel 中动态规则源的管理共有三种模式:

(1)原生模式:Sentinel Dashboard 通过 API 将规则推送至 Sentinel Client 并直接更新到 Sentinel Client 的内存中,因为规则存储在内存中,只要项目重启就丢失了,所以不建议用于生产环境

(2)pull 模式:即拉模式, Sentinel Client 主动向某个规则管理中心定期轮询拉取规则,所以不能保证实时性,如果拉取过于频繁可能会导致性能问题。下图以本地文件数据源为例,推送过程如下图所示:

(3)push 模式:即推模式,由规则中心统一推送, Sentinel Client 通过注册监听器的方式时刻监听变化,比如使用 Nacos、Apollo、ZooKeeper 等配置中心,这种方式有更好的实时性和一致性保证。生产环境下推荐采用 push 模式的数据源,push 推送的操作应该由 Sentinel dashboard 和 Config Center Dashboard 控制台统一进行管理和推送,不应该经 Sentinel Client 推送至配置中心,Sentinel Client 仅负责获取配置中心推送的配置并更新到本地,因此推送规则正确做法应该是:配置中心控制台/Sentinel 控制台 → 配置中心 → Sentinel 客户端。

2、Sentinel 的改造内容:

2.1、改造前后的数据流程图:

        了解完 Sentinel 的规则管理方式后,我们肯定能明白在生产环境使用 Sentinel 时,需要将原生的规则管理模式改造 push 模式的原因了,Sentinel 改造前后的数据流向图如下所示:

  • 蓝色箭头 代表了限流规则由配置中心发起修改的更新路径
  • 橙色箭头代表了限流规则由 Sentinel Dashboard 发起修改的更新路径
  • 绿色箭头为公共公共部分,即:不论从配置中心修改,还是从 Sentinel Dashboard 修改都会触发的操作。

        从图中可以很明显的看到,在没有整合配置中心来存储规则前,Sentinel Dashboard 与业务服务之间是可以互通获取最新限流规则的。在整合配置中心后但未对 Sentinel-dashboard 改造前,配置中心的修改都可以实时的刷新到业务服务,再而被 Sentinel Dashboard 读取到,但是 Sentinel dashboard 对于这些规则的更新到达各个业务服务之后,并没有一个机制将规则同步到配置中心。但在改造后,从上图的两处修改起点看,所有涉及的部分都能获取到一致的限流规则了。

2.2、核心改造点:

由上图我们可知,我们需要改造的内容如下:

(1)Sentinel dashboard 控制台:

  • ① Sentinel dashboard 控制台将规则写入到 apollo/nacos 配置中心进行持久化
  • ② Sentinel dashboard 能实时更新 apollo/nacos 控制台所修改的规则

(2)Sentinel Client:

  • ① Sentinel Client 实时更新 apollo/nacos 配置中心所修改的规则

二、Sentinel 的改造:

文章该部分以 apollo 作为配置中心介绍如何对 Sentinel 进行改造,基于 Nacos 的改造方式推荐参考下面文章:

https://www.freesion.com/article/30571440125/

https://www.imooc.com/article/289464

https://github.com/tanjiancheng/alibaba-sentinel-dashboard-nacos

1、Sentinel dashboard 的改造:

        本文是基于 sentinel-dashboard 1.7.2 版本进行改造的,需要先下载官方源码到本地进行改造:https://github.com/alibaba/Sentinel/tree/release-1.7/sentinel-dashboard

1.1、添加 apollo 依赖与配置:

        sentinel-dashboard 整合 apollo 进行规则的持久化配置,主要方式是通过 apollo 开放平台的授权方式进行写入与读取,所以需要在 pom.xml 文件引入以下依赖:

 <dependency>
     <groupId>com.ctrip.framework.apollo</groupId>
     <artifactId>apollo-openapi</artifactId>
     <version>1.7.0</version>
     <scope>compile</scope>
 </dependency>

        引入之后,在 sentinel-dashboard 的配置文件添加 apollo 的配置,下列各配置参数的获取方式文章第2部分会介绍:

# apollo的中配置的appId
app.id=sentinel-rules
# 如果apollo配置中心控制台的访问地址
apollo.meta=http://portal.xxx.net
# token获取方法下文会介绍
apollo.token=ccc082b44f06f2ae9552bf67a710f36c36e6b777
# apollo的登录用户
apollo.user=apollo
# apollo的集群名称 没有的话请使用default
apollo.clusterName=default
# apollo的命名空间 默认使用application,但考虑到该空间需要给所有项目共同使用,因此单独创建了一个公共空间
apollo.namespaceName=EDU001.sentinel-rules

        接下来,我们按照下面两步核心步骤进行改造:

(1)第一步:添加与实现所有规则的 Provider 与 Publisher 的配置拉取与推送接口,改由从 apollo 中获取规则信息,并将添加与修改的规则保存到 apollo

(2)第二步:在 Controller 层中引入改造过后的 Provider 与 Publisher 接口,由改造后的 Provider 与 Publisher 接口负责处理 sentinel-dashboard 看板发起的操作

1.2、规则在 Apollo 的编码与存储配置:

(1)sentinel 规则在 apollo 的编码配置:

package com.alibaba.csp.sentinel.dashboard.rule.apollo;

import com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiDefinitionEntity;
import com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayFlowRuleEntity;
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.AuthorityRuleEntity;
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.DegradeRuleEntity;
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity;
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.ParamFlowRuleEntity;
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.SystemRuleEntity;
import com.alibaba.csp.sentinel.dashboard.domain.cluster.request.ClusterAppAssignMap;
import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.fastjson.JSON;
import com.ctrip.framework.apollo.openapi.client.ApolloOpenApiClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
public class ApolloConfig {

    @Value("${apollo.meta}")
    private String portalUrl;
    @Value("${apollo.token}")
    private String token;

    /**
     * 流控规则编码
     */
    @Bean
    public Converter<List<FlowRuleEntity>, String> flowRuleEntityEncoder() {
        return JSON::toJSONString;
    }

    /**
     * 流控规则解码
     */
    @Bean
    public Converter<String, List<FlowRuleEntity>> flowRuleEntityDecoder() {
        return s -> JSON.parseArray(s, FlowRuleEntity.class);
    }

    /**
     * 降级规则编码
     */
    @Bean
    public Converter<List<DegradeRuleEntity>, String> degradeRuleEntityEncoder() {
        return JSON::toJSONString;
    }

    /**
     * 降级规则解码
     */
    @Bean
    public Converter<String, List<DegradeRuleEntity>> degradeRuleEntityDecoder() {
        return s -> JSON.parseArray(s, DegradeRuleEntity.class);
    }

    /**
     * 授权规则编码
     */
    @Bean
    public Converter<List<AuthorityRuleEntity>, String> authorityRuleEntityEncoder() {
        return JSON::toJSONString;
    }

    /**
     * 授权规则解码
     */
    @Bean
    public Converter<String, List<AuthorityRuleEntity>> authorityRuleEntityDecoder() {
        return s -> JSON.parseArray(s, AuthorityRuleEntity.class);
    }

    /**
     * 系统规则编码
     */
    @Bean
    public Converter<List<SystemRuleEntity>, String> systemRuleEntityEncoder() {
        return JSON::toJSONString;
    }

    /**
     * 系统规则解码
     */
    @Bean
    public Converter<String, List<SystemRuleEntity>> systemRuleEntityDecoder() {
        return s -> JSON.parseArray(s, SystemRuleEntity.class);
    }

    /**
     * 热点规则编码
     */
    @Bean
    public Converter<List<ParamFlowRuleEntity>, String> paramFlowRuleEntityEncoder() {
        return JSON::toJSONString;
    }

    /**
     * 热点规则解码
     */
    @Bean
    public Converter<String, List<ParamFlowRuleEntity>> paramFlowRuleEntityDecoder() {
        return s -> JSON.parseArray(s, ParamFlowRuleEntity.class);
    }

    /**
     * 集群流控规则编码
     */
    @Bean
    public Converter<List<ClusterAppAssignMap>, String> clusterGroupEntityEncoder() {
        return JSON::toJSONString;
    }

    /**
     * 集群流控规则解码
     */
    @Bean
    public Converter<String, List<ClusterAppAssignMap>> clusterGroupEntityDecoder() {
        return s -> JSON.parseArray(s, ClusterAppAssignMap.class);
    }

    /**
     * API管理分组编码
     */
    @Bean
    public Converter<List<ApiDefinitionEntity>, String> apiDefinitionEntityEncoder() {
        return JSON::toJSONString;
    }

    /**
     * API管理分组解码
     */
    @Bean
    public Converter<String, List<ApiDefinitionEntity>> apiDefinitionEntityDecoder() {
        return s -> JSON.parseArray(s, ApiDefinitionEntity.class);
    }

    /**
     * 网关流控规则编码
     */
    @Bean
    public Converter<List<GatewayFlowRuleEntity>, String> gatewayFlowRuleEntityEncoder() {
        return JSON::toJSONString;
    }

    /**
     * 网关流控规则解码
     */
    @Bean
    public Converter<String, List<GatewayFlowRuleEntity>> gatewayFlowRuleEntityDecoder() {
        return s -> JSON.parseArray(s, GatewayFlowRuleEntity.class);
    }

    @Bean
    public ApolloOpenApiClient apolloOpenApiClient() {
        ApolloOpenApiClient client = ApolloOpenApiClient.newBuilder()
            .withPortalUrl(portalUrl)
            .withToken(token)
            .build();
        return client;
    }
}

(2)sentinel 规则在 Apollo 中存储的 key 值的前/后缀:

package com.alibaba.csp.sentinel.dashboard.rule.apollo;

public final class ApolloConfigUtil
{
    /**
     * 网关-api分组id
     */
    public static final String GATEWAY_API_GROUP_DATA_ID_POSTFIX = "gw-api-group-rules";

    /**
     * 网关-流控规则id
     */
    public static final String GATEWAY_FLOW_DATA_ID_POSTFIX = "gw-flow-rules";

    /**
     * 流控规则id
     */
    public static final String FLOW_DATA_ID_POSTFIX = "flow-rules";
    /**
     * 降级规则id
     */
    public static final String DEGRADE_DATA_ID_POSTFIX = "degrade-rules";
    /**
     * 热点规则id
     */
    public static final String PARAM_FLOW_DATA_ID_POSTFIX = "param-flow-rules";
    /**
     * 系统规则id
     */
    public static final String SYSTEM_DATA_ID_POSTFIX = "system-rules";
    /**
     * 授权规则id
     */
    public static final String AUTHORITY_DATA_ID_POSTFIX = "authority-rules";
    /**
     * 集群流控id
     */
    public static final String CLUSTER_GROUP_DATA_ID_POSTFIX = "cluster-group-rules";

    private ApolloConfigUtil()
    {
    }

    public static String getGatewayFlowDataId(String appName)
    {
        return String.format("%s-%s", appName, GATEWAY_FLOW_DATA_ID_POSTFIX);
    }

    public static String getGatewayApiGroupDataId(String appName)
    {
        return String.format("%s-%s", appName, GATEWAY_API_GROUP_DATA_ID_POSTFIX);
    }

    public static String getClusterGroupDataId(String appName)
    {
        return String.format("%s-%s", appName, CLUSTER_GROUP_DATA_ID_POSTFIX);
    }

    public static String getFlowDataId(String appName)
    {
        return String.format("%s-%s", appName, FLOW_DATA_ID_POSTFIX);
    }

    public static String getDegradeDataId(String appName)
    {
        return String.format("%s-%s", appName, DEGRADE_DATA_ID_POSTFIX);
    }

    public static String getParamFlowDataId(String appName)
    {
        return String.format("%s-%s", appName, PARAM_FLOW_DATA_ID_POSTFIX);
    }

    public static String getSystemDataId(String appName)
    {
        return String.format("%s-%s", appName, SYSTEM_DATA_ID_POSTFIX);
    }

    public static String getAuthorityDataId(String appName)
    {
        return String.format("%s-%s", appName, AUTHORITY_DATA_ID_POSTFIX);
    }
}

1.3、新增 Provider 与 Publisher:

        Sentinel Dashboard 从 1.4.0 版本开始就抽取出了接口用于向远程配置中心推送规则以及拉取规则:DynamicRuleProvider 拉取规则、DynamicRulePublisher 推送规则,所以,只需要通过这两个接口,实现对配置中心中存储规则的读写,就能实现 Sentinel Dashboard 中修改规则与配置中心存储同步的效果。

        这里仅提供对流控规则的修改案例,其他规则类型(流控/降级/热点/系统/授权)的修改基本与流控相同,就不重复介绍:

(1)新增 Provider,从 Apollo 配置中心查询持久化的规则:

@Component("flowRuleApolloProvider")
public class FlowRuleApolloProvider implements DynamicRuleProvider<List<FlowRuleEntity>> {

    @Autowired
    private ApolloOpenApiClient apolloOpenApiClient;
    @Autowired
    private Converter<String, List<FlowRuleEntity>> converter;
    @Value("${app.id}")
    private String appId;
    @Value("${spring.profiles.active}")
    private String env;
    @Value("${apollo.clusterName}")
    private String clusterName;
    @Value("${apollo.namespaceName}")
    private String namespaceName;

    @Override
    public List<FlowRuleEntity> getRules(String appName)
    {
        String flowDataId = ApolloConfigUtil.getFlowDataId(appName);

        OpenNamespaceDTO openNamespaceDTO = apolloOpenApiClient.getNamespace(appId, env, clusterName, namespaceName);

        String rules = openNamespaceDTO
                .getItems()
                .stream()
                .filter(p -> p.getKey().equals(flowDataId))
                .map(OpenItemDTO::getValue)
                .findFirst()
                .orElse("");

        if (StringUtil.isEmpty(rules)) {
            return new ArrayList<>();
        }
        return converter.convert(rules);
    }
}

(2)新增 Publisher,将流控规则持久化到 Apollo 配置中心里面:

@Component("flowRuleApolloPublisher")
public class FlowRuleApolloPublisher implements DynamicRulePublisher<List<FlowRuleEntity>> {

    @Autowired
    private ApolloOpenApiClient apolloOpenApiClient;
    @Autowired
    private Converter<List<FlowRuleEntity>, String> converter;
    @Value("${app.id}")
    private String appId;
    @Value("${spring.profiles.active}")
    private String env;
    @Value("${apollo.user}")
    private String user;
    @Value("${apollo.clusterName}")
    private String clusterName;
    @Value("${apollo.namespaceName}")
    private String namespaceName;

    @Override
    public void publish(String app, List<FlowRuleEntity> rules){
        AssertUtil.notEmpty(app, "app name cannot be empty");
        if (rules == null) {
            return;
        }

        filterField(rules);
        // Increase the configuration
        String flowDataId = ApolloConfigUtil.getFlowDataId(app);
        OpenItemDTO openItemDTO = new OpenItemDTO();
        openItemDTO.setKey(flowDataId);
        openItemDTO.setValue(converter.convert(rules));
        openItemDTO.setComment("Program auto-join");
        openItemDTO.setDataChangeCreatedBy(user);
        apolloOpenApiClient.createOrUpdateItem(appId, env, clusterName, namespaceName, openItemDTO);

        // Release configuration
        NamespaceReleaseDTO namespaceReleaseDTO = new NamespaceReleaseDTO();
        namespaceReleaseDTO.setEmergencyPublish(true);
        namespaceReleaseDTO.setReleaseComment("Modify or add configurations");
        namespaceReleaseDTO.setReleasedBy(user);
        namespaceReleaseDTO.setReleaseTitle("Modify or add configurations");
        apolloOpenApiClient.publishNamespace(appId, env, clusterName, namespaceName, namespaceReleaseDTO);
    }

    /**
     * 过滤不必要的字段
     */
    private void filterField(List<FlowRuleEntity> rules) {
        // 对不必要的信息进行过滤
        for (FlowRuleEntity rule : rules) {
            rule.setGmtCreate(null);
            rule.setGmtModified(null);
        }
    }
}

(3)在 Controller 中引入 Provider 和 Publisher:

    @Autowired
    @Qualifier("flowRuleApolloProvider")
    private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
    @Autowired
    @Qualifier("flowRuleApolloPublisher")
    private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;

(4)修改 controller 的查询接口,改由从 apollo 中查询 sentinel 的规则:

/**
 * 查询流控配置,用于展示在sentinel-dashboard上 
 */
@GetMapping("/rules")
@AuthAction(PrivilegeType.READ_RULE)
public Result<List<FlowRuleEntity>> apiQueryMachineRules(@RequestParam String app, @RequestParam String ip, @RequestParam Integer port) {

    if (StringUtil.isEmpty(app))
    {
        return Result.ofFail(-1, "app can't be null or empty");
    }
    if (StringUtil.isEmpty(ip))
    {
        return Result.ofFail(-1, "ip can't be null or empty");
    }
    if (port == null)
    {
        return Result.ofFail(-1, "port can't be null");
    }

    try {
        List<FlowRuleEntity> rules = ruleProvider.getRules(app);
        if (rules != null && !rules.isEmpty())
        {
            for (FlowRuleEntity entity : rules)
            {
                entity.setApp(app);
                if (entity.getClusterConfig() != null && entity.getClusterConfig().getFlowId() != null)
                {
                    entity.setId(entity.getClusterConfig().getFlowId());
                }
            }
        }

        repository.saveAll(rules);
        return Result.ofSuccess(rules);
    }
    catch (Throwable throwable)
    {
        logger.error("Error when querying flow rules", throwable);
        return Result.ofThrowable(-1, throwable);
    }
}

(5)修改 controller 的新增或修改接口,每次新增或修改都同步到 Apollo 中:

/**
 * 发布限流配置
 */
private CompletableFuture<Void> publishRules(String app, String ip, Integer port) {
    List<FlowRuleEntity> rules = repository.findAllByMachine(MachineInfo.of(app, ip, port));
    try {
        rulePublisher.publish(app, rules);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return sentinelApiClient.setFlowRuleOfMachineAsync(app, ip, port, rules);
}

2、配置 sentinel-dashboard 的 Apollo 空间:

2.1、创建 sentinel-dashboard 项目的 apollo 应用:

 2.2、创建一个用于存放 sentinel-dashboard 规则的公共 namespace 空间:

        进入应用后,点击 "添加Namespace",创建一个具体存储 Sentinel 各种限流、熔断降级等规则的 Apollo 存储空间,这里需要注意的是所创建的空间类型一定要是"public"公共空间,因为最终这些规则是需要具体的微服务应用去获取的,而在Apollo中应用下只有公共Namecspace才能被其他应用继承。

2.3、创建基于该应用的开放授权信息token:

(1)点击控制台右上角的管理员工具 -> 开放平台授权管理

 (2)第三方应用ID=AppId,第三方应用名称=请随意填写,项目负责人=配置文件中的apollo.user,第一次请点击创建, 如果右上角提示已经存在,则点击查询

 (3)授权类型请选择app:

        最终生成的Token信息将作为 sentinel-dashboard 与 Apollo 接口对接的重要凭证被配置。至此,通过上面几个步骤,就完成了 sentinel-dashboard 项目的改造,接下来将介绍其他项目如何与改造后的 sentinel-dashboard 进行集成并打通

3、spring boot 集成 sentinel 并持久化规则到 apollo 中:

此部分以 gateway-bbk 网关项目为例,如果非网关项目去掉网关相关配置

 3.1、引入依赖:

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    <version>2.2.6.RELEASE</version>
</dependency>
<!--apollo-->
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-apollo</artifactId>
    <version>1.5.2</version>
</dependency>

3.2、配置文件:

        sentinel-dashboard 是将规则写入它在 Apollo 所在应用的公共空间下 sentinel-rules,因此其他微服务可以通过 Apollo 继承并读取到该公共空间的配置。只是我们在进行 sentinel-dashboard的 改造将规则的写入编成了一定的前/后缀标识,所以 SpringCloud 微服务要想匹配到相应的规则,也需要在自身服务的配置中约定读取方式,具体以限流、熔断这两个规则为例进行配置,如下的 flowRulesKey 所示:

# sentinel + apollo进行规则持久化,RulesKey指定该规则在apollo中key的名称,命名格式需与sentinel-dashboard配置的格式保持一致
sentinel.datasource.rules.apollo.namespace-name = EDU001.sentinel-rules
# 从Apollo公共空间中EDU001.sentinel-rules读取限流规则
spring.cloud.sentinel.datasource.flow.apollo.ruleType = flow
spring.cloud.sentinel.datasource.flow.apollo.namespace-name = ${sentinel.datasource.rules.apollo.namespace-name}
spring.cloud.sentinel.datasource.flow.apollo.flowRulesKey = ${spring.application.name}-${spring.cloud.sentinel.datasource.flow.apollo.ruleType}-rules
# 从Apollo公共空间中EDU001.sentinel-rules读取熔断规则
spring.cloud.sentinel.datasource.degrade.apollo.ruleType = degrade
spring.cloud.sentinel.datasource.degrade.apollo.namespace-name = ${sentinel.datasource.rules.apollo.namespace-name}
spring.cloud.sentinel.datasource.degrade.apollo.flowRulesKey = ${spring.application.name}-${spring.cloud.sentinel.datasource.degrade.apollo.rule-ruleType}-rules
# 从Apollo公共空间中EDU001.sentinel-rules读取网关限流规则
spring.cloud.sentinel.datasource.gwFlow.apollo.ruleType = gw-flow
spring.cloud.sentinel.datasource.gwFlow.apollo.namespace-name = ${sentinel.datasource.rules.apollo.namespace-name}
spring.cloud.sentinel.datasource.gwFlow.apollo.flowRulesKey = ${spring.application.name}-${spring.cloud.sentinel.datasource.gwFlow.apollo.ruleType}-rules
# 从Apollo公共空间中EDU001.sentinel-rules读取网关API管理规则
spring.cloud.sentinel.datasource.gwApiGroup.apollo.ruleType = gw-api-group
spring.cloud.sentinel.datasource.gwApiGroup.apollo.namespace-name = ${sentinel.datasource.rules.apollo.namespace-name}
spring.cloud.sentinel.datasource.gwApiGroup.apollo.flowRulesKey = ${spring.application.name}-${spring.cloud.sentinel.datasource.gwApiGroup.apollo.ruleType}-rules
    
# sentinel看板的地址
spring.cloud.sentinel.transport.dashboard = 112.74.98.151:80
# 开启对sentinel看板的饥饿式加载
spring.cloud.sentinel.eager = true

# 此项为该项目在配置中心的项目名
app.id=gateway-bbk
# 加载的配置文件名,需引入 sentinel-dashboard 配置的公共空间
apollo.bootstrap.namespaces=application,EDU001.sentinel-rules
apollo.bootstrap.enabled=true
# 指定apollo的注册地址:
#本地开发环境 Local environment
local.meta=http://47.112.238.105:8004
#开发联调环境 Development environment
dev.meta=http://172.18.227.113:8080
#功能验收测试环境 Feature Acceptance Test environment
fat.meta=http://172.18.227.115:8080
#生产环境 Production environment
pro.meta=http://xxxx.xxx.net

        通过上述配置可以看出,我们是通过 Sentinel Client 依赖约定的配置方式,对各类规则通过命名规则进行了匹配(这里Sentinel规则的命名规则可以结合实际的管理需求进行约定,确保 sentinel-dashboard 写入与微服务读取匹配就行)!例如:如果从管理角度分类,可以加上{部门名称}.sentinel-rules,这要求创建namespace公共空间时带上部门名前缀。

4、集成测试:

4.1、新增规则:

        在 sentinel-dashboard 控制台中添加新的规则:

4.2、同步至Apollo中:

在 sentinel dashiboard 控制台添加成功后,我们进入 apollo 配置中心就可以看到新增的规则已经持久化到配置中心了 


相关阅读:

常见的服务器架构入门:从单体架构、EAI 到 SOA 再到微服务和 ServiceMesh

常见分布式理论(CAP、BASE)和一致性协议(Gosssip协议、Raft一致性算法)

一致性哈希算法原理详解

Nacos注册中心的部署与用法详细介绍

Nacos配置中心用法详细介绍

SpringCloud OpenFeign 远程HTTP服务调用用法与原理

什么是RPC?RPC框架dubbo的核心流程

服务容错设计:流量控制、服务熔断、服务降级

sentinel 限流熔断神器详细介绍

Sentinel 规则持久化到 apollo 配置中心

Sentinel-Dashboard 与 apollo 规则的相互同步

Spring Cloud Gateway 服务网关的部署与使用详细介绍

Spring Cloud Gateway 整合 sentinel 实现流控熔断

Spring Cloud Gateway 整合 knife4j 聚合接口文档

常见分布式事务详解(2PC、3PC、TCC、Saga、本地事务表、MQ事务消息、最大努力通知)

分布式事务Seata原理

RocketMQ事务消息原理


参考文章:https://zhuanlan.zhihu.com/p/64892865

  • 5
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
sentinel-dashboard-1.8.2是一个开源的项目,用于监控和管理Sentinel规则、实时流量、集群节点等信息。它是一个基于Java开发的Web应用程序,采用了Spring Boot框架和Vue.js前端框架。 首先,sentinel-dashboard-1.8.2源码的结构非常清晰和模块化。它分为后端和前端两部分,后端代码位于sentinel-dashboard模块,前端代码位于sentinel-dashboard-frontend模块。这种结构使得代码的维护和扩展变得更加容易。 在后端部分,sentinel-dashboard-1.8.2源码中包含了一系列的Java类,用于实现Sentinel规则管理、实时数据统计和集群节点的管理等功能。它提供了RESTful的接口用于前端页面的数据获取和交互。这些Java类使用了Spring框架提供的注解和特性,使得代码简洁、易读和易于维护。 在前端部分,sentinel-dashboard-1.8.2源码中的前端代码采用了Vue.js框架进行开发。它使用了一系列的组件来实现不同的功能模块,如规则管理、流量统计、集群节点管理等。前端页面具有良好的交互性和可视化效果,可以方便地进行规则的配置和流量的监控。 另外,sentinel-dashboard-1.8.2源码还使用了一些开源的技术和库,如Redis、MyBatis等,以提供更好的性能和扩展性。 总结来说,sentinel-dashboard-1.8.2源码是一个功能丰富、结构清晰和易于维护的开源项目。通过深入研究和理解源码,开发人员可以对Sentinel规则管理和流量监控等核心功能有更深入的了解,并根据自己的需求进行二次开发和定制化操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张维鹏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值