Sentinel Dashboard集成Nacos

这篇具有很好参考价值的文章主要介绍了Sentinel Dashboard集成Nacos。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1.前言

当项目上Sentinel Dashboard做流量监控的时候,我们可以通过Sentinel控制台修改限流配置,但当我们使用Nacos作为配置中心动态配置流控规则的时候,问题就来了。

首先我们要明白,Sentinel Dashboard的配置是从机器的内存中加载的,如果使用Nacos、Apollo、Zookeeper等作为我们动态加载限流规则的配置中心的时候,我们的配置信息流向是这样的:

Sentinel Dashboard集成Nacos,sentinel,dashboard,nacos,sentinel nacos

就是说客户端从配置中心获取配置,Sentinel Dashboard又从客户端机器的内存中获取配置。

在这种情况下,Sentinel Dashboard可以修改配置,Nacos也可以修改配置,这就会存在数据一致性问题,我们可以从两方面考虑

1)Nacos修改了配置,Sentinel Dashboard会同步更新吗?答案是肯定的,因为Nacos配置变更会通知客户端,客户端配置变更后也会通知Sentinel Dashboard

2)如果在Sentinel Dashboard修改了配置会通知Nacos吗?答案是不会。但是可以通过修改源码实现。

Sentinel控制台在做流控规则的时候,都会调用Sentinel-Dashboard源码中的 一个名叫:FlowControllerV1的接口类,因为流控规则的所有操作,调用的 接口都是如图所示:

Sentinel Dashboard集成Nacos,sentinel,dashboard,nacos,sentinel nacos

这个路径对应的就是FlowControllerV1这个类,但是同时还存在一个 FlowControllerV2的接口类,这个类主要提供的是流控规则的CURD,这两者有啥区别呢? 

区别在于:V2可以实现指定数据源的规则拉取和发布,这也就意味着Sentinel-Dashboard可以从Nacos等配置中心进行相互通信,从而实现数据一致性,Sentinel开发者已经做好封装,我们改一下源码配置一下就好了(后面会有教程)。这样我们的配置关系就变成了了这样:

Sentinel Dashboard集成Nacos,sentinel,dashboard,nacos,sentinel nacos

2.实战:Sentinel Dashboard集成Nacos

第一步:下载Sentinel源码,本案例用的是Sentinel-v1.8.6

Sentinel官网下载地址:

https://github.com/alibaba/Sentinel/releases

Sentinel Dashboard集成Nacos,sentinel,dashboard,nacos,sentinel nacos

下载好之后,解压,import project,可以看到sentinel-dashboard就是我们要改动的spring-boot项目

 Sentinel Dashboard集成Nacos,sentinel,dashboard,nacos,sentinel nacos

 第二步:将pom.xml中的sentinel-datasource-nacos的scope去掉

Sentinel Dashboard集成Nacos,sentinel,dashboard,nacos,sentinel nacos

 第三步:进入resources/app/scripts/directives/sidebar/sidebar.html,全局搜一下“dashboard.flowV1”,替换成"dashboard.flow",也就是将V1去除:去除之后就 会调用FlowControllerV2中的CURD的接口

Sentinel Dashboard集成Nacos,sentinel,dashboard,nacos,sentinel nacos

 其实Sentinel的开发者已经做好相关的适配,如下图,可以支持集成Apollo、Nacos、Zeekeeper,我们现在是以Nacos为例,进入src/test/java/com.alibaba.csp.sentinel.dashboard.rule.nacos目录,将整个nacos目录及其相关文件复制到src/main/java/com.alibaba.csp.sentinel.dashboard.rule.nacos

注:代码的红叉是因为我复制过去了,相同包名相同class导致的,不必在意,实在强迫症的话,可以把test里面的这几个删掉就好了,不删也不影响运行

Sentinel Dashboard集成Nacos,sentinel,dashboard,nacos,sentinel nacos

 复制过来之后,如下图:

Sentinel Dashboard集成Nacos,sentinel,dashboard,nacos,sentinel nacos

其中NacosConfigUtil就一些常量,我自定义了些常量并改名为NacosConfigConstant了,不在意细节

1.NacosConfigConstant.java

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

public class NacosConfigConstant { 
	public static final String DATA_ID_POSTFIX="-flow-rules";
	public static final String GROUP_ID="SENTINEL_GROUP";  
}

 2.配置nacos相关配置

 2.1: application.properties添加nacos配置:

#config my nacos
sentinel.nacos.serverAddr=192.168.1.105:8848
sentinel.nacos.namespace=
sentinel.nacos.groupId=SENTINEL_GROUP

2.2: 新建Nacos配置类NacosPropertiesConfiguration.java

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

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "sentinel.nacos")
public class NacosPropertiesConfiguration {
	
	private String serverAddr;
	private String dataId;
	private String groupId;
	private String namespace;

	public String getServerAddr() {
		return serverAddr;
	}

	public void setServerAddr(String serverAddr) {
		this.serverAddr = serverAddr;
	}

	public String getDataId() {
		return dataId;
	}

	public void setDataId(String dataId) {
		this.dataId = dataId;
	}

	public String getGroupId() {
		return groupId;
	}

	public void setGroupId(String groupId) {
		this.groupId = groupId;
	}

	public String getNamespace() {
		return namespace;
	}

	public void setNamespace(String namespace) {
		this.namespace = namespace;
	}

}

3.FlowRuleNacosProvider.java,稍微改动了一下,如下:

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

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity;
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider;
import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.utils.StringUtils;

@Service("flowRuleNacosProvider")
public class FlowRuleNacosProvider implements DynamicRuleProvider<List<FlowRuleEntity>> {
	
	@Autowired
	private NacosPropertiesConfiguration nacosPropertiesConfiguration;
	@Autowired
	private ConfigService configService;
	@Autowired
	private Converter<String, List<FlowRuleEntity>> converter;

	/**
	 * @Description: 通过ConfigService.getConfig方法从NacosConfigServer中读取指定配置信息,通过converter转化为FlowRule规则
	 * @Author: LiJianhong
	 * @Param: [appName]
	 * @Return: java.util.List<com.alibaba.csp.sentinel.dashboard.datasource.entity.
	 *          rule.FlowRuleEntity>
	 */
	@Override
	public List<FlowRuleEntity> getRules(String appName) throws Exception {
		String dataId = appName + NacosConfigConstant.DATA_ID_POSTFIX; //user-service-flow-rules
		String groupId = nacosPropertiesConfiguration.getGroupId();
		String rules = configService.getConfig(dataId, groupId, 3000);
		if (StringUtils.isEmpty(rules)) {
			return new ArrayList<>();
		}
		return converter.convert(rules);
	}
}

4.FlowRuleNacosPublisher.java,也是稍微改动了一下:

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

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity;
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher;
import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.csp.sentinel.util.AssertUtil;
import com.alibaba.nacos.api.config.ConfigService;

@Service("flowRuleNacosPublisher")
public class FlowRuleNacosPublisher implements DynamicRulePublisher<List<FlowRuleEntity>> {
	
	@Autowired
	private NacosPropertiesConfiguration nacosPropertiesConfiguration;
	@Autowired
	private ConfigService configService;
	@Autowired
	private Converter<List<FlowRuleEntity>, String> converter;

	
	@Override
	public void publish(String app, List<FlowRuleEntity> rules) throws Exception {
		AssertUtil.notEmpty(app, "appName connot be empty");
		if (rules == null) {
			return;
		}
		String dataId = new StringBuilder(app).append(NacosConfigConstant.DATA_ID_POSTFIX).toString();
		configService.publishConfig(dataId, nacosPropertiesConfiguration.getGroupId(), converter.convert(rules));
	}
	
}

 5. 修改com.alibaba.csp.sentinel.dashboard.controller.v2.FlowControllerV2,将注入的bean改成我们改过之后的bean

Sentinel Dashboard集成Nacos,sentinel,dashboard,nacos,sentinel nacos

6.最后mvn clean package打包运行jar即可。

接下来是测试环节:

运行改动后的sentinel-dashboard 

Sentinel Dashboard集成Nacos,sentinel,dashboard,nacos,sentinel nacos首先,我在Nacos配置中心配置了dataId=user-service-flow-rules限流规则,如下:

[
    {
        "app": "user-service",
        "clusterConfig": {
            "acquireRefuseStrategy": 0,
            "clientOfflineTime": 2000,
            "fallbackToLocalWhenFail": true,
            "flowId": 1001,
            "resourceTimeout": 2000,
            "resourceTimeoutStrategy": 0,
            "sampleCount": 10,
            "strategy": 0,
            "thresholdType": 1,
            "windowIntervalMs": 1000
        },
        "clusterMode": true,
        "controlBehavior": 0,
        "count": 30.0,
        "gmtModified": 1690385332131,
        "grade": 1,
        "id": 1001,
        "limitApp": "default",
        "resource": "com.lee.demo.dubbo.demo.user.ISentinelService",
        "strategy": 0
    },
    {
        "app": "user-service",
        "clusterConfig": {
            "acquireRefuseStrategy": 0,
            "clientOfflineTime": 2000,
            "fallbackToLocalWhenFail": true,
            "flowId": 1002,
            "resourceTimeout": 2000,
            "resourceTimeoutStrategy": 0,
            "sampleCount": 10,
            "strategy": 0,
            "thresholdType": 0,
            "windowIntervalMs": 1000
        },
        "clusterMode": true,
        "controlBehavior": 0,
        "count": 10.0,
        "gmtModified": 1690373927810,
        "grade": 1,
        "id": 1002,
        "limitApp": "default",
        "resource": "com.lee.demo.dubbo.demo.user.IHelloService",
        "strategy": 0
    }
]

com.lee.demo.dubbo.demo.user.ISentinelService的count配置的是30

com.lee.demo.dubbo.demo.user.IHelloService的count配置的是10

然后看下Sentinel-Dashboard的限流规则是与Nacos一致的:

Sentinel Dashboard集成Nacos,sentinel,dashboard,nacos,sentinel nacos

1)在Nacos修改配置查看Sentinel-Dashboard是否更新配置:

Sentinel Dashboard集成Nacos,sentinel,dashboard,nacos,sentinel nacos

可以看到Sentinel-Dashboard的配置是更新了

Sentinel Dashboard集成Nacos,sentinel,dashboard,nacos,sentinel nacos

 2)在Sentinel-Dashboard更新配置,检查是否同步到Nacos,这一步才是验证我们这么辛苦改代码的关键

Sentinel Dashboard集成Nacos,sentinel,dashboard,nacos,sentinel nacos

 可以看到,Nacos成功同步了Sentinel-Dashboard的配置修改Sentinel Dashboard集成Nacos,sentinel,dashboard,nacos,sentinel nacos

注:我发现改了之后,在Sentinel-Dashboard对配置的CRUD之后,页面没有刷新,要我们手动刷新一下才会看到最新数据~~o(>_<)o ~~,这个我没有过多去排查前端的异常,毕竟我只是后端开发嘛。

至此,Sentinel-Dashboard集成Nacos案例演示完毕。

最后分享一下集成Nacos过程中遇到的问题:

1)Sentinel-Dashboard控制台可以新增限流规则同步Nacos,但是“修改”配置的时候弹出警告“失败”,没提示啥原因,经过debug才发现,是因为新增和修改都需要进行限流规则的参数校验,校验的必要参数如下:

private <R> Result<R> checkEntityInternal(FlowRuleEntity entity) {
        if (entity == null) {
            return Result.ofFail(-1, "invalid body");
        }
        if (StringUtil.isBlank(entity.getApp())) {
            return Result.ofFail(-1, "app can't be null or empty");
        }
        if (StringUtil.isBlank(entity.getLimitApp())) {
            return Result.ofFail(-1, "limitApp can't be null or empty");
        }
        if (StringUtil.isBlank(entity.getResource())) {
            return Result.ofFail(-1, "resource can't be null or empty");
        }
        if (entity.getGrade() == null) {
            return Result.ofFail(-1, "grade can't be null");
        }
        if (entity.getGrade() != 0 && entity.getGrade() != 1) {
            return Result.ofFail(-1, "grade must be 0 or 1, but " + entity.getGrade() + " got");
        }
        if (entity.getCount() == null || entity.getCount() < 0) {
            return Result.ofFail(-1, "count should be at lease zero");
        }
        if (entity.getStrategy() == null) {
            return Result.ofFail(-1, "strategy can't be null");
        }
        if (entity.getStrategy() != 0 && StringUtil.isBlank(entity.getRefResource())) {
            return Result.ofFail(-1, "refResource can't be null or empty when strategy!=0");
        }
        if (entity.getControlBehavior() == null) {
            return Result.ofFail(-1, "controlBehavior can't be null");
        }
        int controlBehavior = entity.getControlBehavior();
        if (controlBehavior == 1 && entity.getWarmUpPeriodSec() == null) {
            return Result.ofFail(-1, "warmUpPeriodSec can't be null when controlBehavior==1");
        }
        if (controlBehavior == 2 && entity.getMaxQueueingTimeMs() == null) {
            return Result.ofFail(-1, "maxQueueingTimeMs can't be null when controlBehavior==2");
        }
        if (entity.isClusterMode() && entity.getClusterConfig() == null) {
            return Result.ofFail(-1, "cluster config should be valid");
        }
        return null;
    }

因此我把Nacos配置中心的限流规则根据这个必要参数都配置了一下之后,再从Sentinel Dashboard修改配置成功了。文章来源地址https://www.toymoban.com/news/detail-609553.html

到了这里,关于Sentinel Dashboard集成Nacos的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Docker 安装部署 Sentinel Dashboard

    官方 jar 包下载地址:https://github.com/alibaba/Sentinel/releases 或者点击 链接 直接跳转到下载页 进入链接下载你需要的版本 下载完毕(我这里统一放在一个 sentinel 目录内) 在 sentinel 目录内创建 Dockerfile 文件并填写以下内容: 在 sentinel 目录内创建一个脚本,这里我叫做 build-run.s

    2024年02月03日
    浏览(33)
  • 【漏洞复现】Sentinel Dashboard默认弱口令漏洞

            Sentinel Dashboard是一个轻量级的开源控制台,提供机器发现以及健康情况管理、监控、规则管理和推送的功能。它还提供了详细的被保护资源的实际访问统计情况,以及为不同服务配置的限流规则。         Sentinel Dashboard存在默认弱口令sentinel/sentinel,攻击者可

    2024年01月16日
    浏览(34)
  • 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

    2024年02月07日
    浏览(27)
  • Sentinel-dashboard安装(k8s部署)

    目录 Sentinel-dashboard安装(k8s部署) 一.拉取镜像并推送到私库 二.准备sentinel statefulset部署配置文件 三.部署并访问sentinel 一.拉取镜像并推送到私库 这里选择的是docker hub已经有人制作好的Sentinel镜像 二.准备sentinel statefulset部署配置文件 因为该配置使用了PVC,所以要新增一个

    2024年02月08日
    浏览(30)
  • sentinel-dashboard-1.8.0.jar开机自启动脚本

    启动阿里巴巴的流控组件控制面板需要运行一个jar包,通常需要运行如下命令: 建议在 15000 QPS 的情况下,sentinel-dashboard-1.8.0.jar 的-Xms 和 -Xmx 参数均设置为 4G 官方文档:https://sentinelguard.io/zh-cn/docs/dashboard.html 作为基础服务器,需要配置开机自启服务,方便后面自动伸缩以这

    2024年02月07日
    浏览(32)
  • Sentinel dashboard无法查询到应用的限流配置问题以及解决

    使用sentinle-dashboard控制台 项目整体升级后,发现控制台上无法看到流控规则了 之前的问题是无法注册上来 现在是注册上来了。结果看不到流控规则配置了。 关于注册不上来的问题,可以看另一篇文章 https://blog.csdn.net/a15835774652/article/details/132234943 项目的组件版本如下 sprin

    2024年02月11日
    浏览(28)
  • 【全栈开发指南】打包sentinel-dashboard镜像推送到Docker Hub镜像仓库

      Docker Hub是Docker官方提供的一个公共的镜像仓库,它是一个中央的存储库,用户可以在其中存储和分享Docker镜像。通过Docker Hub,用户可以方便地搜索、下载和共享Docker镜像,并可以将它们用于构建和部署容器化应用程序。Docker Hub还提供了一些其他功能,如自动构建、版本

    2024年02月13日
    浏览(34)
  • docker部署sentinel客户端在dashboard中遇到 Failed to fetch metric from 错误

    在dashboard中可以看到微服务的节点,但是没有任何其他数据进来。同时dashboard的控制台打印如下错误 sentinel客户端的配置文件的该部分 dashboard 项代表的是dashboard的部署ip和端口,客户端会向dashboard注册,表示自己所在ip为 client-ip ,通信端口为 port 。之后dashboard会按照 http:/

    2024年02月16日
    浏览(28)
  • sentinel + nacos 超详细使用步骤

    首先在sentinel-dashboard这个连接中下载对应的jar包。 下载完成后, 使用下面命令进行启动 看到一下日志表示启动成功(这里暂时没有遇到过启动失败的情况,又遇到失败的朋友可以评论沟通一下) 登录页面 sentinel首页,这里因为还没有在sentinel-dashboard接入,所以还看不到我们

    2023年04月08日
    浏览(28)
  • Sentinel 规则持久化到 Nacos

    Sentinel的控制台规则管理有三种模式: 原始模式:控制台配置的规则直接推送到Sentinel客户端,也就是我们的应用。然后保存在内存中,服务重启则丢失 pull模式:控制台将配置的规则推送到Sentinel客户端,而客户端会将配置规则保存在本地文件或数据库中。以后会定时去本地

    2024年02月16日
    浏览(40)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包