Sentinel 基于Nacos规则持久化-推模式

一、推模式架构图

在这里插入图片描述

图片引用自 https://github.com/alibaba/Sentinel/wiki/在生产环境中使用-Sentinel

二、原理简述
2.1. 组件版本关系
Spring Cloud Alibaba VersionSentinel VersionNacos VersionRocketMQ VersionDubbo VersionSeata Version
2.2.6.RELEASE1.8.11.4.24.4.02.7.81.3.0
2021.1 or 2.2.5.RELEASE or 2.1.4.RELEASE or 2.0.4.RELEASE1.8.01.4.14.4.02.7.81.3.0
Spring Cloud VersionSpring Cloud Alibaba VersionSpring Boot VersionNacos Versionjdk
Spring Cloud Hoxton.SR92.2.6.RELEASE2.3.2.RELEASE1.4.21.8.202
2.2. 控制台推送规则

将规则推送到Nacos或其他远程配置中心
Sentinel客户端链接Nacos,获取规则配置;并监听Nacos配置变化,如发生变化,就更新本地缓存(从而让本地缓存总是和Nacos一致)
控制台监听Nacos配置变化,如发生变化就更新本地缓存(从而让控制台本地缓存总是和Nacos一致)

三、Sentinel控制台改造

控制台改造主要是为规则实现
DynamicRuleProvider:从Nacos上读取配置
DynamicRulePublisher:将规则推送到Nacos上

3.1. 下载源码

这里使用1.8版本演示
https://github.com/alibaba/Sentinel/tags
在这里插入图片描述

3.2. 修改pom

在这里插入图片描述

   <!-- for Nacos rule publisher sample -->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
            <scope>test</scope>
        </dependency>

改为

   <!-- for Nacos rule publisher sample -->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
        </dependency>
3.3. 重要文件复制

由于sentinel持久化默认不支持持久化,但是官方关于sentinel提供了nacos、zookeeper、apollo这3种方式,但是需要自己集成。

找到 sentinel-dashboard/src/test/java/com/alibaba/csp/sentinel/dashboard/rule/nacos目录,将整个目录拷贝到 sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos,如图:

在这里插入图片描述

3.4. 注册地址修改

在这里插入图片描述

3.5. 请求实例需改

修改sentinel页面端保存规则请求的url控制层
在这里插入图片描述
修改后的配置
在这里插入图片描述

    @Autowired
    @Qualifier("flowRuleNacosProvider")
    private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
    @Autowired
    @Qualifier("flowRuleNacosPublisher")
    private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;
3.6. 菜单新增

修改 sentinel-dashboard/src/main/webapp/resources/app/scripts/directives/sidebar/sidebar.html,找到:
在这里插入图片描述
修改后
在这里插入图片描述
齐活儿啦!终于把流控规则改造成推模式持久化啦!

四、编译 & 启动
4.1. 先启动nacos

https://nacos.io/zh-cn/docs/quick-start.html
这里一windows环境演示

startup.cmd -m standalone

在这里插入图片描述
在这里插入图片描述
里面的配置先忽略

4.2. 编译打包

执行 mvn clean package -DskipTests
在项目的 target 目录找到sentinel-dashboard.jar ,执行 java -jar sentinel-dashboard.jar 启动控制台。
在这里插入图片描述

登录
在这里插入图片描述

在这里插入图片描述

4.3. 创建微服务 && 启动

创建微服务模块
添加依赖

 <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
   <dependencyManagement>
        <dependencies>
            <!--spring-cloud-alibaba 版本控制-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.6.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

配置文件

server:
  port: 9000
spring:
  cloud:
    nacos:
      discovery:
        service: product-serv
      server-addr: localhost:8848
    sentinel:
      transport:
        dashboard: localhost:8080
      filter:
        enabled: false
management:
  endpoints:
    web:
      exposure:
        include: '*'


创建测试方法

package com.gblfy.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProductController {

    //http://localhost:9000/product/" + productId
    @GetMapping("/product/{productId}")
    public String getProductName(@PathVariable Integer productId) {
        return "IPhone 12";
    }
}

启动应用

请求url
http://localhost:9000/product/1

在这里插入图片描述

4.4. 刷新sentinel

在这里插入图片描述

4.5. 登录nacos

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

4.6. sentinel 规则删除

在这里插入图片描述

4.7. nacos规则查看

在这里插入图片描述
规则也随之删除了

4.8. (企业案例)规则扩展

以上只是将流控规则扩展好了,其他需要其他模块,还需要根据官网文档自行扩展

附上:
(企业案例)使用Nacos持久化规则,改造sentinel-dashboard

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

gblfy

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值