sentinel简单使用

核心demo:

1 引入依赖:

复制代码
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-core</artifactId>
    <version>1.8.0</version>
</dependency>

2  核心代码:

3 限流保护代码:

以上就是sentinel最核心的代码使用逻辑。可以在代码中硬编码需要保护的资源,然后在调用资源之前使用Api做限流保护。

在实际使用过程中,几乎不会使用如上方式做资源保护:代码侵入性强,不能动态扩展需要保护的资源

使用注解完成资源定义(springboot中):

1 引入依赖:

复制代码
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-core</artifactId>
    <version>1.8.0</version>
</dependency>

2 核心代码:

使用注解可以减少代码的侵入性,但是不能动态的扩展(限流规则还是需要硬编码提前处理)

springboot + sentinel + sentinel dashboard

1 下载sentinel dashboard jar包

sentinel-dashboard-1.8.0.jar(当然也可以通过docker下载镜像启动)

2 添加启动参数完成dashbaord的启动

-Dserver.port=8718 -Dcsp.sentinel.dashboard.server=localhost:8718 -Dproject.name=sentinel-dashboard -Dcsp.sentinel.api.port=8719

其中:server.port=8719 表示dashbaord的http端口号

-Dcsp.sentinel.dashboard.server=localhost:8718 表示dashboard把自己本身也注册到dashboard上显示

-Dproject.name=sentinel-dashboard 显示的项目名称

-Dcsp.sentinel.api.port=8719 客户端通信端口

3 sprongboot应用中连接到sentinel dashboard上

a 依赖

b 添加启动命令

-Dcsp.sentinel.dashboard.server=192.168.56.102:8858 ---dashbaord地址

-Dproject.name=webDemo2 ---客户端限制名称

-Dcsp.sentinel.api.port=8719 ---通信端口号

c 配置:

复制代码
        

d 通过调用接口:http://localhost:8112/getStr

此时可以在dashboard的仪表盘上看到接入的应用了:

在spring cloud中的应用接入:

1 依赖:

2 配置

添加如上依赖和配置之后,无需额外配置就可在dashboard上看到相关的应用信息

至于:sentinel核心代码的源码分析和dashboard通信机制在后面的源码分析会补上

相关推荐
破烂pan13 小时前
Python 整合 Redis 哨兵(Sentinel)与集群(Cluster)实战指南
redis·python·sentinel
库库林_沙琪马2 天前
3、Sentinel
服务器·网络·sentinel
大大大大物~2 天前
Spring Cloud熔断与降级:核心区别与实践指南【怎么理解?解决了什么问题?各自的适用场景?Sentinel实现代码示例】
spring cloud·sentinel·熔断·降级
serendipity_hky3 天前
【SpringCloud | 第3篇】Sentinel 服务保护(限流、熔断降级)
java·后端·spring·spring cloud·微服务·sentinel
小毅&Nora3 天前
【后端】【诡秘架构】 ② 序列8:小丑 - 熔断降级的艺术:用 Sentinel 实现优雅降级,笑对流量洪峰
架构·sentinel·熔断降级
Haooog4 天前
微服务保护学习
java·学习·微服务·sentinel
布茹 ei ai4 天前
5、基于 GEE 的 Sentinel-1 SAR 地震滑坡变化检测系统:2022 泸定地震案例
javascript·sentinel·遥感·gee·云平台
lang201509284 天前
深入解析Sentinel熔断器核心机制
sentinel
lang201509285 天前
Sentinel系统保护规则深度解析
sentinel
lang201509285 天前
深入解析Sentinel熔断机制
java·前端·sentinel