网站设计与网页制作项目教程,个人网站的域名,网站建设与维护培训,网上怎么注册公司免费的文章目录 一、sentinel简介Sentinel 是什么#xff1f;Sentinel安装 二、sentinel整合工程新建cloudalibaba-sentinel-service8401微服务引入依赖yml配置主启动类添加EnableDiscoveryClient业务类测试 三、sentinel流控规则基本介绍流控模式直接#xff08;默认#xff09;关… 文章目录 一、sentinel简介Sentinel 是什么Sentinel安装 二、sentinel整合工程新建cloudalibaba-sentinel-service8401微服务引入依赖yml配置主启动类添加EnableDiscoveryClient业务类测试 三、sentinel流控规则基本介绍流控模式直接默认关联链路 一、sentinel简介
分布式系统的流量防卫兵sentinel官网地址 添加链接描述 sentinel - github地址 添加链接描述
Sentinel 是什么
随着微服务的流行服务和服务之间的稳定性变得越来越重要。Sentinel 以流量为切入点从流量控制、流量路由、熔断降级、系统自适应过载保护、热点流量防护等多个维度保护服务的稳定性。
Sentinel 具有以下特征:
丰富的应用场景Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景例如秒杀即突发流量控制在系统容量可以承受的范围、消息削峰填谷、集群流量控制、实时熔断下游不可用应用等。
完备的实时监控Sentinel 同时提供实时的监控功能。您可以在控制台中看到接入应用的单台机器秒级数据甚至 500 台以下规模的集群的汇总运行情况。
广泛的开源生态Sentinel 提供开箱即用的与其它开源框架/库的整合模块例如与 Spring Cloud、Apache Dubbo、gRPC、Quarkus 的整合。您只需要引入相应的依赖并进行简单的配置即可快速地接入 Sentinel。同时 Sentinel 提供 Java/Go/C 等多语言的原生实现。
完善的 SPI 扩展机制Sentinel 提供简单易用、完善的 SPI 扩展接口。您可以通过实现扩展接口来快速地定制逻辑。例如定制规则管理、适配动态数据源等。Sentinel 的主要特性 Sentinel安装
1、下载Sentinel控制台程序jar文件
Sentinel下载 添加链接描述 2、将控制台程序上传的Linux服务器, 并且通过命令启动
java -jar sentinel-dashboard-1.8.1.jar
命令 nohup java -jar sentinel-dashboard-1.8.1.jar sentinel.log 21
该命令可以让jar程序在后台运行
注意需要服务器预留8080端口3、访问sentinel控制台程序
http://192.168.195.135:8080
注意账号密码都为sentinel二、sentinel整合工程
新建cloudalibaba-sentinel-service8401微服务 引入依赖
!--SpringCloud ailibaba nacos --
dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-nacos-discovery/artifactId
/dependency
!--SpringCloud ailibaba sentinel-datasource-nacos 后续做持久化用到--
dependencygroupIdcom.alibaba.csp/groupIdartifactIdsentinel-datasource-nacos/artifactId
/dependency
!--SpringCloud ailibaba sentinel --
dependencygroupIdcom.alibaba.cloud/groupIdartifactIdspring-cloud-starter-alibaba-sentinel/artifactId
/dependencyyml配置
server:port: 8401spring:application:name: cloudalibaba-sentinel-servicecloud:nacos:discovery:server-addr: 192.168.10.132:8848 #Nacos服务注册中心地址sentinel:transport:dashboard: 192.168.10.132:8081 #配置Sentinel dashboard地址port: 8719 #默认8719端口,加入被占用会自动从87191扫描,直到找到未被占用的端口management:endpoints:web:exposure:include: *feign:sentinel:enabled: true # 激活Sentinel对Feign的支持
主启动类添加EnableDiscoveryClient
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;EnableDiscoveryClient
SpringBootApplication
public class SentinelApp8401 {public static void main(String[] args) {SpringApplication.run(SentinelApp8401.class, args);}
}
业务类
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;/*** 流空*/
RestController
Slf4j
public class FlowLimitController {GetMapping(/testA)public String testA() {return ------testA;}GetMapping(/testB)public String testB() {log.info(Thread.currentThread().getName() \t ...testB);return ------testB;}}测试
1.启动sentinel服务 2.启动 cloudalibaba-sentinel-service8401
访问 http://192.168.10.132:8081 查看服务 因为sentinel采用的是懒加载所以需要执行一次访问即可 访问
http://localhost:8401/testA 三、sentinel流控规则
基本介绍 资源名唯一名称默认请求路径
针对来源Sentinel可以针对调用者进行限流填写微服务名指定对哪个微服务进行限流 默认default(不区分来源全部限制)阈值类型/单机阈值
QPS(每秒钟的请求数量)当调用该接口的QPS达到了阈值的时候进行限流
线程数【关门打狗】当调用该接口的线程数达到阈值时进行限流是否集群
不需要集群流控模式
直接接口达到限流条件时直接限流
关联当关联的资源达到阈值时就限流自己
链路只记录指定链路上的流量指定资源从入口资源进来的流量如果达到阈值就可以限流[api级别的针对来源]流控效果
快速失败直接失败抛异常
Warm Up即请求 QPS 从 threshold / 3 开始经预热时长逐渐升至设定的 QPS 阈值
排队等待匀速排队让请求以匀速的速度通过阈值类型必须设置为QPS否则无效流控模式
直接默认 快速访问 http://localhost:8401/testA 关联
当关联的的资源达到阈值时就限制自己当与A关联的资源B达到阀值后就限流A自己。 使用posaman或者jmter并发访问 testB 访问testA B满了导致A不可用
链路