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

这篇具有很好参考价值的文章主要介绍了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 客户端,然后他会把这个规则先更新到内存中,然后持久化到 本地文件 或者 数据库 中.

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

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

SpringCloud Alibaba - Sentinel 高级玩法,修改 Sentinel-dashboard 源码,实现 push 模式,SpringCloud & Alibaba,spring cloud,sentinel,java

 缺陷

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

c)push 模式

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

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

SpringCloud Alibaba - Sentinel 高级玩法,修改 Sentinel-dashboard 源码,实现 push 模式,SpringCloud & Alibaba,spring cloud,sentinel,java

缺陷

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

 

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 打开

SpringCloud Alibaba - Sentinel 高级玩法,修改 Sentinel-dashboard 源码,实现 push 模式,SpringCloud &amp; Alibaba,spring cloud,sentinel,java

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

SpringCloud Alibaba - Sentinel 高级玩法,修改 Sentinel-dashboard 源码,实现 push 模式,SpringCloud &amp; Alibaba,spring cloud,sentinel,java

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

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

SpringCloud Alibaba - Sentinel 高级玩法,修改 Sentinel-dashboard 源码,实现 push 模式,SpringCloud &amp; Alibaba,spring cloud,sentinel,java

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

SpringCloud Alibaba - Sentinel 高级玩法,修改 Sentinel-dashboard 源码,实现 push 模式,SpringCloud &amp; Alibaba,spring cloud,sentinel,java

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 文件

SpringCloud Alibaba - Sentinel 高级玩法,修改 Sentinel-dashboard 源码,实现 push 模式,SpringCloud &amp; Alibaba,spring cloud,sentinel,java

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

          <!--<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(记得去掉单元测试).

SpringCloud Alibaba - Sentinel 高级玩法,修改 Sentinel-dashboard 源码,实现 push 模式,SpringCloud &amp; Alibaba,spring cloud,sentinel,java

1.2.5、启动

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

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

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

SpringCloud Alibaba - Sentinel 高级玩法,修改 Sentinel-dashboard 源码,实现 push 模式,SpringCloud &amp; Alibaba,spring cloud,sentinel,java

SpringCloud Alibaba - Sentinel 高级玩法,修改 Sentinel-dashboard 源码,实现 push 模式,SpringCloud &amp; Alibaba,spring cloud,sentinel,java文章来源地址https://www.toymoban.com/news/detail-728091.html

到了这里,关于SpringCloud Alibaba - Sentinel 高级玩法,修改 Sentinel-dashboard 源码,实现 push 模式的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

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

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

相关文章

  • SpringCloud Alibaba微服务-- Sentinel的使用(保姆级)

    随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。 1、sentinel的特征 丰富的应用场景 : Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突

    2024年02月16日
    浏览(29)
  • springcloud Alibaba中gateway和sentinel联合使用

    看到这个文章相信你有一定的sentinel和gateway基础了吧。 官网的gateway和sentinel联合使用有些过时了,于是有了这个哈哈,给你看看官网的: 才sentinel1.6,现在都几了啊,所以有些过时。 下面开始讲解: 首先我们总的回顾一下,sentinel就是需要运行一个jar包,开启dashbord页面,来

    2024年01月17日
    浏览(34)
  • SpringCloud-Alibaba之Sentinel熔断与限流

    一、下载安装运行 http://localhost:8080进行访问 登录账号和密码均为sentinel 二、创建工程,并注册到nacos服务中心 依赖spring-cloud-starter-alibaba-nacos-discovery,spring-cloud-starter-alibaba-sentinel sentine-datasource-nacos (持久化) 配置文件 启动类 业务类 三、启动sentinel java -jar sentinel-dashboard-1.7.0

    2024年02月16日
    浏览(32)
  • Java之SpringCloud Alibaba【四】【微服务 Sentinel服务熔断】

    Java之SpringCloud Alibaba【一】【Nacos一篇文章精通系列】 跳转 Java之SpringCloud Alibaba【二】【微服务调用组件Feign】 跳转 Java之SpringCloud Alibaba【三】【微服务Nacos-config配置中心】 跳转 Java之SpringCloud Alibaba【四】【微服务 Sentinel服务熔断】 跳转 Java之SpringCloud Alibaba【五】【微服务

    2024年02月12日
    浏览(29)
  • SpringCloud Alibaba Demo(Nacos,OpenFeign,Gatway,Sentinel)

    ma/springcloud-alibaba-demo 参考:https://www.cnblogs.com/zys2019/p/12682628.html SpringBoot、SpringCloud 、SpringCloud Alibaba 以及各种组件存在版本对应关系。可参考下面 版本对应 启动nacos.   ./startup.cmd -m standalone  登陆nacos官方 localhost:8848   nacos/nacos 创建父工程spring-cloud-alibaba pom.xml如下: Naco

    2024年02月06日
    浏览(29)
  • 【springcloud 微服务】Spring Cloud Alibaba Sentinel使用详解

    目录 一、前言 二、分布式系统遇到的问题 2.1 服务可用性问题 2.1.1  单点故障

    2024年01月16日
    浏览(41)
  • 【springcloud 微服务】Spring Cloud Alibaba整合Sentinel详解

    目录 一、前言 二、环境准备 2.1 部署sentinel管控台 2.1.1 官网下载sentinel的jar包 2.1.2 启动控制台

    2023年04月09日
    浏览(39)
  • 【SpringCloud Alibaba】(六)使用 Sentinel 实现服务限流与容错

    今天,我们就使用 Sentinel 实现接口的限流,并使用 Feign 整合 Sentinel 实现服务容错的功能,让我们体验下微服务使用了服务容错功能的效果。 因为内容仅仅围绕着 SpringCloud Alibaba技术栈展开,所以,这里我们使用的服务容错组件是阿里开源的 Sentinel。 当然,能够实现服务容错

    2024年02月14日
    浏览(34)
  • SpringCloud学习6(Spring Cloud Alibaba)断路器Sentinel熔断降级

    SpringCloud、SpringCloudAlibaba、SpringBoot版本选择。为了避免各种千奇百怪的bug,我们还是采用官方推荐的毕业版本。 修改tomcat配置最大线程数 引入测试依赖 编写测试代码 这里同时我们在浏览器去请求该地址,响应会变得很慢 测试结论:此时会发现由于thread接口囤积大量请求,

    2023年04月08日
    浏览(42)
  • SpringCloud.04.熔断器Hystrix( Spring Cloud Alibaba 熔断(Sentinel))

    目录 熔断器概述 使用Sentinel工具 什么是Sentinel 微服务集成Sentinel 配置provider文件,在里面加入有关控制台的配置 实现一个接口的限流 基本概念 重要功能 Sentinel规则 流控规则 简单配置 配置流控模式 配置流控效果 降级规则 @SentinelResource的使用 Feign整合Sentinel 由于Hystrix已经停

    2024年01月19日
    浏览(33)

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

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

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

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

二维码1

领取红包

二维码2

领红包