SpringCloud Alibaba - Sentinel 高级玩法,修改 Sentinel-dashboard 源码,实现 push 模式

目录

一、规则持久化

1.1、什么是规则持久化

1.1.1、使用背景

1.1.2、规则管理的三种模式

a)原始模式

b)pull 模式

c)push 模式

1.2、实现 push 模式

1.2.1、修改 order-service 服务,使其监听 Nacos 配置中心

1.2.2、修改 Sentinel-dashboard 源码,配置 nacos 数据源

1.2.3、修改 Sentinel-dashboard 源码,修改前端页面

1.2.4、重新编译、打包-dashboard源码

1.2.5、启动


一、规则持久化


1.1、什么是规则持久化

1.1.1、使用背景

前面我们学习了 Sentinel 中的常用玩法,但是在使用的过程中发现一个问题,就是每当我们的服务重启的时候,在 sentinel 控制台上配置的各种规则就都丢失了.  这是因为 sentinel 默认会把这些规则保存到内存中,重启自然就丢失了.  在生产环境中,肯定是不能容忍这种情况的.

通过 sentinel 的规则持久化就可解决这个问题.

1.1.2、规则管理的三种模式

a)原始模式

原始模式就是 sentinel 默认模式,这种模式下,sentinel 会把规则保存在内存中,一旦重启自然就丢失了.

b)pull 模式

在实际的开发当中,微服务一定是一个集群,那么在 Dashboard 中编写规则的时候,会把这个规则推送给微服务中 Sentinel 客户端,然后他会把这个规则先更新到内存中,然后持久化到 本地文件 或者 数据库 中.

那怎么知道这个规则有没有变化呢?

这里我们的微服务会定时轮询 文件 或者 数据库,当监听到 数据库 或者 文件 的内容发生改变的时候,就知道规则更新了,那么就可以去去更新缓存中的数据.

 缺陷

1. 时效性差:抛开硬盘的读取速度不说,他是定时的啊,就会导致数据库中的数据变化了,而缓存中的数据还没变化.  此时去读取缓存中的数据可能就是无效数据.

c)push 模式

push 模式中 Sentinel Dashboard 并不会把规则推送给任意一个 Sentinel 客户端,而是把这个规则保到远程的一个配置中心.

比如说 nacos ,微服务可以去监听 nacos,一旦发现变化,就立即更新这些数据到 Sentinel 客户端(说明 push 模式是实时更新的).

缺陷

实现起来比较复杂,还需要进行源码的修改

1.2、实现 push 模式

1.2.1、修改 order-service 服务,使其监听 Nacos 配置中心

在order-service中引入sentinel监听nacos的依赖:

<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-nacos</artifactId>
</dependency>

在order-service中的application.yml文件配置nacos地址及监听的配置信息:

spring:
  cloud:
    sentinel:
      datasource:
        flow:
          nacos:
            server-addr: localhost:8848 # nacos地址
            dataId: orderservice-flow-rules
            groupId: SENTINEL_GROUP
            rule-type: flow # 还可以是:degrade、authority、param-flow

1.2.2、修改 Sentinel-dashboard 源码,配置 nacos 数据源

从 github 上下载 Sentinel 源码包,进行解压,然后用 IDEA 打开

修改 sentinel-dashboard 源码的 pom 文件,将 sentinel-datasource-nacos 依赖的 scope 去掉

1.2.3、修改 Sentinel-dashboard 源码,修改前端页面

a)拷贝 test 目录下的 nacos 代码到 main 下的 com.alibaba.csp.sentinel.dashboard.rule 包:

b)修改刚刚拷贝的 nacos 包下的 NacosConfig 类,修改其中的nacos地址:

import java.util.List;

import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity;
import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.api.config.ConfigFactory;
import com.alibaba.nacos.api.config.ConfigService;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Eric Zhao
 * @since 1.4.0
 */
@Configuration
public class NacosConfig {

    @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 ConfigService nacosConfigService() throws Exception {
        return ConfigFactory.createConfigService("localhost:8848");
    }
}

c)修改 com.alibaba.csp.sentinel.dashboard.controller.v2 包下的 FlowControllerV2 类:

将以下代码:

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

修改为:

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

d)修改 src/main/webapp/resources/app/scripts/directives/sidebar/ 目录下的 sidebar.html 文件

将其中的这部分注释打开:

          <!--<li ui-sref-active="active" ng-if="entry.appType==0">-->
            <!--<a ui-sref="dashboard.flow({app: entry.app})">-->
              <!--<i class="glyphicon glyphicon-filter"></i>&nbsp;&nbsp;流控规则 V1</a>-->
          <!--</li>-->

修改其中的文本:

          <li ui-sref-active="active" ng-if="entry.appType==0">
            <a ui-sref="dashboard.flow({app: entry.app})">
              <i class="glyphicon glyphicon-filter"></i>&nbsp;&nbsp;流控规则 Nacos</a>
          </li>

1.2.4、重新编译、打包-dashboard源码

运行IDEA中的maven插件,编译和打包修改好的 Sentinel-Dashboard(记得去掉单元测试).

1.2.5、启动

通过以下命令,修改 nacos 地址并启动.

java -jar -Dnacos.addr=localhost:8848 sentinel-dashboard.jar

打开 Sentinel 客户端就可以看到这里多出了一个 流控规则.  在这上面配置的规则就不会随着服务的重启而消失.

猜你喜欢

转载自blog.csdn.net/CYK_byte/article/details/133494721