Sentinel控制台配置 持久化到nacos

 sentinel控制台,使用方便,功能强大。使用官方的jar包,配置不会持久化,sentinel重启后会导致,之前的规则全部丢失,下面一起改造源码实现规则数据的持久化

sentinel源码地址

(github访问太慢,直接上镜像版)

Sentinel: Sentinel 是什么 随着微服务的流行,服务和服务之间的稳定性变得越来越重要icon-default.png?t=N7T8https://gitee.com/mirrors/Sentinel.git

因为项目使用的是Spring-cloud-alibaba,Sentinel支持和nacos整合,就持久化到nacos数据库中,同时sentinel还能读取nacos中做的流控规则。

1 源码目录

1.1、后台源码修改

小惊喜:sentinel中有和nacos中对接的源码,只不过没有使用。

1、改成 默认后台使用sentinel对接nacos,而不是存到内存,

2、前台页面接口调用nacos对应的接口

1.1.1 sentinel-dashboard中需要改动的位置

pom.xml中将sentinel-datasource-nacos包的scope注释掉

1.1.2 源码持久化到nacos的实现位置

不多说,先复制到main目录rule包下

nacos包中的4个类:

  • FlowRuleNacosProvider: 动态获取Nacos配置中心流控规则,读取流控规则
  • FlowRuleNacosPublisher: publish上传流控规则到Nacos配置中心,写入流控规则
  • NacosConfig: Nacos配置
  • NacosConfigUtils: 流控规则在nacos中配置文件的一些细节:后缀、组别等
1.1.3 NacosConfig配置

只实现了本地nacos并且需要默认配置,需要支持自定义配置

改造后

NacosConfig源码

/*
 * Copyright 1999-2018 Alibaba Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.alibaba.csp.sentinel.dashboard.rule.nacos;

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.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigFactory;
import com.alibaba.nacos.api.config.ConfigService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.Properties;

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

    @Value("${sentinel.nacos.address}")
    private String nacosAddr;

    @Value("${sentinel.nacos.username}")
    private String nacosUsername;

    @Value("${sentinel.nacos.password}")
    private String nacosPassword;

    @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 {
        Properties properties = new Properties();
        properties.put(PropertyKeyConst.SERVER_ADDR, nacosAddr);
        properties.put(PropertyKeyConst.USERNAME, nacosUsername);
        properties.put(PropertyKeyConst.PASSWORD, nacosPassword);
        return ConfigFactory.createConfigService(properties);
    }
}
1.1.4 修改配置文件 application.properties

增加nacos配置信息

#Sentinel  连接nacos配置
sentinel.nacos.address= 192.168.1.109:8848
sentinel.nacos.username= nacos
sentinel.nacos.password= nacos
 1.1.5 配置v2版本controller,调用nacos提供的服务层

1.2 前端页面源码修改

1.2.1 配置中添加nacos接口并修改地址

文件 src/main/webapp/resources/app/scripts/controllers/identity.js

搜FlowServiceV1 改为 FlowServiceV2

/dashboard/flow/ 改为 /dashboard/v2/flow/

 1.2.2 修改页面中的路由地址

文件 src/main/webapp/resources/app/scripts/directives/sidebar/sidebar.html

dashboard.flowV1定位57行去掉V1

文件 src/main/webapp/resources/app/views/flow_v2.html

注释掉回到单机页面按钮

2.项目引用

2.1 微服务引入jar包 pom.xml
        <!-- SpringCloud Alibaba Nacos -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <!-- Sentinel -->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
        </dependency>
2.2 微服务sentinel相关配置
# Spring
spring:
  cloud:
    sentinel:
      eager: true
      # sentinel 地址
      transport:
        dashboard: ${sentinel.host}:${sentinel.port}
        filter:
          enabled: false
      datasource:
        ds1:
          nacos:
            server-addr: ${nacos.host}:${nacos.port}
            username: ${nacos.name}
            password: ${nacos.pwd}
            namespace: ${nacos.namespace}
            group-id: ${nacos.group}
            data-id: ${spring.application.name}-flow-rules
            data-type: json
            rule-type: flow

3 nacos增加sentinel持久化配置文件

以下文件后缀与组名需要对应

大功告成,去试试效果吧

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
Sentinel 控制台是一个基于 Spring Cloud 的应用,它提供了一个可视化的界面来管理和监控 Sentinel 的流控规则、降级规则、系统规则等。而 Nacos 是一个开源的注册中心和配置中心,它提供了服务注册、配置管理和服务发现等功能。将 Sentinel 控制台持久化Nacos 可以实现对 Sentinel 控制台配置的动态管理和版本控制。 将 Sentinel 控制台持久化Nacos,需要进行以下步骤: 1. 在 Nacos 控制台中创建一个命名空间和配置集,用来保存 Sentinel 控制台配置信息。 2. 在 Sentinel 控制台的 application.properties 配置文件中,添加以下配置: ``` # 配置 Nacos 的服务地址和命名空间 spring.cloud.nacos.config.server-addr=<Nacos 服务地址> spring.cloud.nacos.config.namespace=<Nacos 命名空间> # 配置 Sentinel 控制台配置信息 spring.cloud.sentinel.transport.dashboard=localhost:8080 management.endpoints.web.exposure.include=sentinel spring.cloud.sentinel.eager=true ``` 3. 在 Sentinel 控制台启动后,访问 http://localhost:8080/nacos/config 会自动将 Sentinel 控制台配置信息保存到 Nacos 中。 4. 在 Nacos 控制台中修改 Sentinel 控制台配置信息,可以实现对 Sentinel 控制台配置的动态管理和版本控制。 需要注意的是,将 Sentinel 控制台持久化Nacos 需要使用 Sentinel 控制台的 1.8.0 版本及以上,同时需要安装 Nacos配置中心服务。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

流浪书生gzs

原创不易,感谢支持打赏

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

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

打赏作者

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

抵扣说明:

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

余额充值