Sentinel控制台配置 持久化到nacos

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

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

sentinel源码地址

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

Sentinel: Sentinel 是什么 随着微服务的流行,服务和服务之间的稳定性变得越来越重要https://gitee.com/mirrors/Sentinel.git

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

1 源码目录

Sentinel控制台配置 持久化到nacos,sentinel

1.1、后台源码修改

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

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

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

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

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

Sentinel控制台配置 持久化到nacos,sentinel

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

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

Sentinel控制台配置 持久化到nacos,sentinelSentinel控制台配置 持久化到nacos,sentinel

nacos包中的4个类:

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

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

Sentinel控制台配置 持久化到nacos,sentinel

改造后

Sentinel控制台配置 持久化到nacos,sentinel

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提供的服务层

Sentinel控制台配置 持久化到nacos,sentinel

1.2 前端页面源码修改

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

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

搜FlowServiceV1 改为 FlowServiceV2

Sentinel控制台配置 持久化到nacos,sentinel

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

Sentinel控制台配置 持久化到nacos,sentinel

 1.2.2 修改页面中的路由地址

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

dashboard.flowV1定位57行去掉V1

Sentinel控制台配置 持久化到nacos,sentinel

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

注释掉回到单机页面按钮

Sentinel控制台配置 持久化到nacos,sentinel

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持久化配置文件

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

Sentinel控制台配置 持久化到nacos,sentinel

Sentinel控制台配置 持久化到nacos,sentinel

大功告成,去试试效果吧文章来源地址https://www.toymoban.com/news/detail-708643.html

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

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

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

相关文章

  • Spring Cloud Sentinel整合Nacos实现配置持久化

    sentinel配置相关配置后无法持久化,服务重启之后就没了,所以整合nacos,在nacos服务持久化,sentinel实时与nacos通信获取相关配置。 使用上一章节Feign消费者服务实现整合。 版本信息: nacos:1.4.1 Sentinel 控制台 1.7.2 spring-boot:2.3.3.RELEASE spring.cloud.version:Hoxton.SR8 spring.cloud.alibaba.v

    2024年02月08日
    浏览(44)
  • (十五)docker安装sentinel,客户端配置规则本地持久化

    操作系统: Linux  CentOS 7.3 64位 docker版本: 19.03.8 sentinel版本: 1.8.0 1、拉取镜像   2、运行容器   3.访问sentinel http://192.168.121.132:8858/ 账号密码默认都是sentinel ​ sentinel配置的规则默认是存在内存里的,不够稳定,所以我们需要持久化到本地文件中。 1.新建持久化处理类 在我

    2024年02月08日
    浏览(37)
  • Sentinel规则持久化

    首先 Sentinel 控制台通过 API 将规则推送至客户端并更新到内存中,接着注册的写数据源会将新的规则保存到本地的文件中。 示例代码: 1.编写处理类 2.添加配置 在resources下创建配置目录 META-INF/services ,然后添加文件 com.alibaba.csp.sentinel.init.InitFunc 在文件中添加配置类的全路径

    2024年02月13日
    浏览(31)
  • Sentinel 规则持久化

    需要大量修改源码,很麻烦,下面也只是修改了一小部分 阿里有一个收费的sentinel云服务,开源sentinel没有实现最佳版本可能就是为了给收费版让路吧 修改OrderService,让其监听Nacos中的sentinel规则配置。 具体步骤如下: 1.引入依赖 在order-service中引入sentinel监听nacos的依赖:

    2024年02月12日
    浏览(31)
  • Sentinel授权规则和规则持久化

    大家好我是苏麟 , 今天说说Sentinel规则持久化. 授权规则 授权规则可以对请求方来源做判断和控制。 授权规则 基本规则 授权规则可以对调用方的来源做控制,有白名单和黑名单两种方式。 白名单:来源(origin)在白名单内的调用者允许访问 黑名单:来源(origin)在黑名单内

    2024年02月08日
    浏览(37)
  • Java----Sentinel持久化规则启动

    java -jar -Dnacos.add=8848 你的sentinel源码修改包.jar 前期准备: 1.引入依赖 在order-service中引入sentinel监听nacos的依赖: 2.配置nacos地址 在order-service中的application.yml文件配置nacos地址及监听的配置信息: 3.启动 启动方式跟官方一样: java -jar -Dserver.port=8180 sentinel-dashboard.jar 如果要修改

    2024年02月11日
    浏览(26)
  • Sentinel 规则持久化到 Nacos

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

    2024年02月16日
    浏览(39)
  • 06-微服务OpenFeigh和Sentinel持久化

    OpenFeign是一种声明式、模板化的HTTP客户端。在Spring Cloud中使用OpenFeign,可以做到使用HTTP请求访问远程服务,就像调用本地方法一样的,开发者完全感知不到这是在调用远程方法,更感知不到在访问HTTP请求,用法其实就是编写一个接口,在接口上添加注解即可。 可以简单理解

    2024年01月19日
    浏览(30)
  • Sentinel-Dashboard-1.8持久化Nacos

    Sentinel-Dashboard-1.8持久化Nacos 一、客户端改造 1.引入pom.xml文件依赖 2.配置application.yml文件。 客户端改造完成 二、Sentinel-Dashboard源码改造 1.修改pom.xml中nacos的范围中【test】注释。 2.把test目录下com/alibaba/csp/sentinel/dashboard/rule下的nacos文件夹复制到main相同目录下 3.修改application.

    2024年02月04日
    浏览(25)
  • Spring Cloud Alibaba全家桶(八)——Sentinel规则持久化

    本文小新为大家带来 Sentinel规则持久化 相关知识,具体内容包括, Sentinel规则推送三种模式 介绍,包括: 原始模式 , 拉模式 , 推模式 ,并对 基于Nacos配置中心控制台实现推送 进行详尽介绍~ 不积跬步,无以至千里;不积小流,无以成江海。每天进步一点点,在成为强者

    2024年01月25日
    浏览(28)

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

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

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

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

二维码1

领取红包

二维码2

领红包