Spring boot集成sentinel限流服务

Sentinel集成文档

Sentinel控制台

Sentinel本身不支持持久化,项目通过下载源码改造后,将规则配置持久化进nacos中,sentinel重启后,配置不会丢失。

架构图:

改造步骤:

接着我们就要改造Sentinel的源码。因为官网提供的Sentinel的jar是原始模式的,所以需要改造,所以我们需要拉取源码下来改造一下,然后自己编译jar包。

源码地址:https://github.com/alibaba/Sentinel

拉取下来之后,导入到IDEA中,然后我们可以看到以下目录结构。

首先修改sentinel-dashboard的pom.xml文件:

第二步,把test目录下的四个关于Nacos关联的类,移到rule目录下。

接着NacosConfig添加Nacos的地址配置。

最关键的是FlowControllerV1的改造,这是规则配置的增删改查的一些接口。

把移动到rule目录下的两个服务,添加到FlowControllerV1类中。

@Autowired

@Qualifier("flowRuleNacosProvider")

private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;

@Autowired

@Qualifier("flowRuleNacosPublisher")

private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;

添加私有方法publishRules(),用于推送配置:

private void publishRules(/*@NonNull*/ String app) throws Exception {

List<FlowRuleEntity> rules = repository.findAllByApp(app);

rulePublisher.publish(app, rules);

}

修改apiQueryMachineRules()方法。

修改apiAddFlowRule()方法。

修改apiUpdateFlowRule()方法。

修改apiDeleteFlowRule()方法。

Sentinel控制台的项目就改造完成了,用于生产环境就编译成jar包运行,如果是学习可以直接在IDEA运行。

附件包含已经改造好的包,直接修改配置文件中nacos的配置即可使用。

客户端工程(fssc或者console)

引入pom

<!--sentinel nacos-->

<dependency>

<groupId>com.alibaba.csp</groupId>

<artifactId>sentinel-datasource-nacos</artifactId>

<version>1.8.5</version>

</dependency>

<!--sentinel-->

<dependency>

<groupId>com.alibaba.cloud</groupId>

<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>

<version>2021.1</version>

</dependency>

添加配置

spring:

cloud:

sentinel:

enabled: true # 是否开启。默认为 true 开启

eager: true # 是否饥饿加载。默认为 false 关闭

transport:

#配置sentinel地址,端口

dashboard: 127.0.0.1:8080 #这里是sentinel控制台地址

#客户端IP(sentinel dashboard进行实时监控的主机ip地址)

默认端口8719端口假如被占用会自动从8719开始依次+1扫描,直到找到未被占用的端口

port: 8725

client-ip: 192.168.30.200 #这里是我windows地址

datasource:

flow:

nacos:

server-addr: ${spring.cloud.nacos.discovery.server-addr}

namespace: sentinel_rule

dataId: cai-demo-flow-rules

groupId: SENTINEL_GROUP

规则类型,取值见:

org.springframework.cloud.alibaba.sentinel.datasource.RuleType

rule-type: flow

data-type: json

添加注解配置类

添加统一异常处理

至此,单机模式集成完成

集群改造

集群只需要修改客户端即可:

添加集群pom:

<dependency>

<groupId>com.alibaba.csp</groupId>

<artifactId>sentinel-cluster-client-default</artifactId>

<version>1.8.5</version>

</dependency>

<dependency>

<groupId>com.alibaba.csp</groupId>

<artifactId>sentinel-cluster-server-default</artifactId>

<version>1.8.5</version>

</dependency>

添加InitFunc实现类(在sentinel源码demo可以找到)

继续添加实体类和常量类:

在resource中添加sentinel.properties配置文件(主要解决DemoClusterInitFunc类中无法读取配置文件的问题)

继续添加SPI文件

至此集群改造完成

相关推荐
AI+程序员在路上2 小时前
VS Code 完全使用指南:下载、安装、核心功能与 内置AI 编程助手实战
开发语言·人工智能·windows·开源
ss2733 小时前
食谱推荐系统功能测试如何写?
java·数据库·spring boot·功能测试
2301_811274313 小时前
基于SpringBoot的智能家居管理系统
spring boot·后端·智能家居
毕设源码_古学姐3 小时前
计算机毕业设计springboot智能家居项目管理系统 基于SpringBoot的智能家居项目管理平台设计与实现 SpringBoot技术驱动的智能家居项目管理系统开发
spring boot·智能家居·课程设计
毕设源码-张学姐3 小时前
计算机毕业设计springboot智能家居设备信息管理系统 基于SpringBoot的智能家居设备全生命周期管理平台 面向智慧家庭的SpringBoot设备资产与场景运营系统
spring boot·智能家居·课程设计
别来无恙blwy5 小时前
windows MongoDB升级-自动升级脚本-自动检测升级到任意版本
数据库·windows·mongodb
存在的五月雨6 小时前
SpringBoot 基于数据库的动态定时任务管理器实现方案
java·spring boot
程序员老邢8 小时前
【产品底稿 07】商助慧 Admin 运维模块落地:从 “能跑” 到 “能运维”,3 个页面搞定日常排障
java·运维·经验分享·spring boot·后端
0xDevNull8 小时前
Java项目中Redis热点Key自动检测方案详细教程
java·spring boot·redis
豆浆煮粉8 小时前
Ubuntu虚拟机对USB设备无响应且Windows主机设备管理器显示警告
windows·ubuntu