服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java之springcloud Sentinel案例讲解

Java之springcloud Sentinel案例讲解

2021-11-12 14:10深情以改 Java教程

这篇文章主要介绍了Java之springcloud Sentinel案例讲解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下

一、Sentinel是什么?

Sentinel (分布式系统的流量防卫兵) 是阿里开源的一套用于服务容错的综合性解决方案。
它以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度来保护服务的稳定性。

二、使用步骤

1.下载地址

下载地址:https://github.com/alibaba/Sentinel/releases
java -jar sentinel-dashboard-1.7.0.jar
访问:http://localhost:8080
用户名密码:sentinel/sentinel

2.导入依赖

?
1
2
3
4
5
<!--服务容错 每一个服务 都引入 sentinel 客户端-->                                                                                               
<dependency>                                                                                                                                                         
    <groupId>com.alibaba.cloud</groupId>                                                                                                                                                           
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>                                                                                                                                                         
</dependency>                                                                                                                                                        

3.添加配置

?
1
2
3
4
5
6
#spring:                                                                                                                                                           
#  cloud:                                                                                                                                                          
    sentinel:                                                                                                                                                          
      transport:                                                                                                                                                           
        port: 8081 #跟控制台交流的端口,随意指定一个未使用的端口即可                                                                                                                                                           
        dashboard: localhost:8080 # 指定控制台服务的地址                                                                                                                                                         

4.启动jar

Java之springcloud Sentinel案例讲解

5 运行接口后就才能被监控,设置流控规则

Java之springcloud Sentinel案例讲解

6 测试重复刷新后

Java之springcloud Sentinel案例讲解

该处使用的url网络请求的数据。

三 容错异常

?
1
FlowException)

降级
DegradeException)
参数热点异常
ParamFlowException
系统
SystemBlockException
授权
AuthorityException

自定义sentinel异常 BlockExceptionAspect

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@Slf4j                                                                                                                                                                                                             
@Component                                                                                                                                                                                                             
public class BlockExceptionAspect  implements UrlBlockHandler {                                                                                                                                                                                                            
                                                                                                                                                                                                                
    @Override                                                                                                                                                                                                              
    public void blocked(HttpServletRequest httpServletRequest,                                                                                                                                                                                                             
                        HttpServletResponse httpServletResponse, BlockException e) throws IOException {                                                                                                                                                                                                            
                                                                                                                                                                                                                
                                                                                                                                                                                                                
        System.out.println("进入。。。 MyUrlBlockHandler");                                                                                                                                                                                                             
        // 打印日志                                                                                                                                                                                                             R
        log.error(e.getMessage(),e);                                                                                                                                                                                                               
        // 统一返回结果                                                                                                                                                                                                              
        R appResult = null;                                                                                                                                                                                                            
        if (e instanceof FlowException) {                                                                                                                                                                                                              
            appResult = new R(ResponseEnum.FLOW_BLOCK,null);                                                                                                                                                                                                               
        } else if (e instanceof DegradeException) {                                                                                                                                                                                                            
            appResult = new R(ResponseEnum.DEG_BLOCK,null);                                                                                                                                                                                                            
        } else if (e instanceof ParamFlowException) {                                                                                                                                                                                                              
            appResult = new R(ResponseEnum.DEG_BLOCK,null);                                                                                                                                                                                                            
        } else if (e instanceof SystemBlockException) {                                                                                                                                                                                                            
            appResult = new R(ResponseEnum.DEG_BLOCK,null);                                                                                                                                                                                                            
        } else if (e instanceof AuthorityException) {                                                                                                                                                                                                              
            appResult = new R(ResponseEnum.DEG_BLOCK,null);                                                                                                                                                                                                            
        }                                                                                                                                                                                                              
        httpServletResponse.setCharacterEncoding("utf-8");                                                                                                                                                                                                             
        httpServletResponse.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);                                                                                                                                                                                                             
                                                                                                                                                                                                                
        httpServletResponse.getWriter().print(JSON.toJSONString(appResult));                                                                                                                                                                                                               
                                                                                                                                                                                                                
    }                                                                                                                                                                                                              
}                                                                                                                                                                                                              

一: QPS每秒查询率(Query Per Second)
原理:每天80%的访问集中在20%的时间里,这20%时间叫做峰值时间。

公式:( 总PV数 * 80% ) / ( 每天秒数 * 20% ) = 峰值时间每秒请求数(QPS) 。

PV(page view)即页面浏览量,通常是衡量一个网络新闻频道或网站甚至一条网络新闻的主要指标。网页浏览数是评价网站流量最常用的指标之一,简称为PV。

机器:峰值时间每秒QPS / 单台机器的QPS = 需要的机器 。

每天300w PV 的在单台机器上,这台机器需要多少QPS?

( 3000000 * 0.8 ) / (86400 * 0.2 ) = 139 (QPS)。

一般需要达到139QPS,因为是峰值。(200万pv才有100峰值qps)

二:TPS
TPS:Transactions Per Second(每秒传输的事物处理个数),即服务器每秒处理的事务数。

TPS包括一条消息入和一条消息出,加上一次用户数据库访问。(业务TPS = CAPS × 每个呼叫平均TPS)

一个事务是指一个客户机向服务器发送请求然后服务器做出反应的过程。客户机在发送请求时开始计时,收到服务器响应后结束计时,以此来计算使用的时间和完成的事务个数。

一般的,评价系统性能均以每秒钟完成的技术交易的数量来衡量。系统整体处理能力取决于处理能力最低模块的TPS值。

三:RT(Response-time)
响应时间是指:系统对请求作出响应的时间(一次请求耗时)。

响应时间:执行一个请求从开始到最后收到响应数据所花费的总体时间,即从客户端发起请求到收到服务器响应结果的时间。响应时间RT(Response-time),是一个系统最重要的指标之一,它的数值大小直接反应了系统的快慢。

四:Load(系统负载)
Linux的Load(系统负载),是一个让新手不太容易了解的概念。load的就是一定时间内计算机有多少个active_tasks,也就是说是计算机的任务执行队列的长度,cpu计算的队列。

top/uptime等工具默认会显示1分钟、5分钟、15分钟的平均Load。

具体来说,平均Load是指,在特定的一段时间内统计的正在CPU中运行的(R状态)、正在等待CPU运行的、处于不可中断睡眠的(D状态)的任务数量的平均值。

最后,说一下CPU使用率和Load的关系吧。如果主要是CPU密集型的程序在运行(If CPU utilization is near 100 percent (user + nice + system), the workload sampled is CPU-bound.),

那么CPU利用率高,Load一般也会比较高。而I/O密集型的程序在运行,

可能看到CPU的%user, %system都不高,%iowait可能会有点高,这时的Load通常比较高。

同理,程序读写慢速I/O设备(如磁盘、NFS)比较多时,Load可能会比较高,而CPU利用率不一定高。这种情况,还经常发生在系统内存不足并开始使用swap的时候,Load一般会比较高,而CPU使用率并不高。

五:PV
页面访问次数:Page View

六:UV
访客数(去重复):Unique Visitor

七:带宽
带宽(bps)=总流量数(bit)/产生流量的时长(秒)=(PV页面平均大小8)/统计时间(秒)

说明:公式中的 8 指的是将 Byte 转换为 bit,即 8b/B,因为带宽的单位是 bps(比特率),即bit per second,每秒二进制位数,而容量单位一般使用 Byte。

假设某站点的日均 PV 是 10w,页面平均大小 0.4 M,那么其平均带宽需求是:

平均带宽 = (10w * 0.4M * 8) / (60 * 60 * 24)= 3.7 Mbps

以上计算的仅仅是平均带宽,我们在进行容量预估时需要的是峰值带宽,即必须要保证站点在峰值流量时能够正常运转。假设,峰值流量是平均流量的5倍,这个5倍称为峰值因 子。按照这个计算,实际需要的带宽大约在3.7 Mbps * 5=18.5 Mbps 。

带宽需求 = 平均带宽 * 峰值因子

八:并发连接数
并发数是指系统同时能处理的请求数量,这个也是反应了系统的负载能力。

总结

功能 Sentinel Hystrix resilience4j
隔离策略 信号量隔离(并发线程数限流) 线程池隔离/信号量隔离 信号量隔离
熔断降级策略 基于响应时间、异常比率、异常数 基于异常比率 基于异常比率、响应时间
实时统计实现 滑动窗口(LeapArray) 滑动窗口 (基于 RxJava)
动态规则配置 支持多种数据源 支持多种数据源 有限支持
扩展性 多个扩展点 插件的形式 接口的形式
基于注解的支持 支持 支持 支持
限流 基于 QPS,支持基于调用关系的限流 有限的支持 Rate Limiter
流量整形 支持预热模式、匀速器模式、预热排队模式(流量规则处可配置) 不支持 简单的 Rate Limiter 模式
系统自适应保护 支持 不支持 不支持
控制台 提供开箱即用的控制台,可配置规则、查看秒级监控、机器发现等 简单的监控查看 不提供控制台,可对接其它监控系统

到此这篇关于Java之springcloud Sentinel案例讲解的文章就介绍到这了,更多相关Java之springcloud Sentinel内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_56219549/article/details/119514267

延伸 · 阅读

精彩推荐