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

至此集群改造完成

相关推荐
考虑考虑9 小时前
Jpa使用union all
java·spring boot·后端
阿杆19 小时前
同事嫌参数校验太丑,我直接掏出了更优雅的 SpEL Validator
java·spring boot·后端
昵称为空C1 天前
SpringBoot3 http接口调用新方式RestClient + @HttpExchange像使用Feign一样调用
spring boot·后端
麦兜*2 天前
MongoDB Atlas 云数据库实战:从零搭建全球多节点集群
java·数据库·spring boot·mongodb·spring·spring cloud
麦兜*2 天前
MongoDB 在物联网(IoT)中的应用:海量时序数据处理方案
java·数据库·spring boot·物联网·mongodb·spring
汤姆yu2 天前
基于springboot的毕业旅游一站式定制系统
spring boot·后端·旅游
计算机毕业设计木哥2 天前
计算机毕设选题推荐:基于Java+SpringBoot物品租赁管理系统【源码+文档+调试】
java·vue.js·spring boot·mysql·spark·毕业设计·课程设计
波波烤鸭2 天前
Redis 高可用实战源码解析(Sentinel + Cluster 整合应用)
数据库·redis·sentinel
路由侠内网穿透2 天前
本地部署 GPS 跟踪系统 Traccar 并实现外部访问
运维·服务器·网络·windows·tcp/ip
hdsoft_huge2 天前
Java & Spring Boot常见异常全解析:原因、危害、处理与防范
java·开发语言·spring boot