点击关注公众号,Java干货及时送达

什么是sentinel

Sentinel,中文翻译为哨兵,是为微服务提供流量控制、熔断降级的功能,它和Hystrix提供的功能一样,可以有效的解决微服务调用产生的“雪崩”效应,为微服务系统提供了稳定性的解决方案。随着Hytrxi进入了维护期,不再提供新功能,Sentinel是一个不错的替代方案。通常情况,Hystrix采用线程池对服务的调用进行隔离,Sentinel才用了用户线程对接口进行隔离,二者相比,Hystrxi是服务级别的隔离,Sentinel提供了接口级别的隔离,Sentinel隔离级别更加精细,另外Sentinel直接使用用户线程进行限制,相比Hystrix的线程池隔离,减少了线程切换的开销。另外Sentinel的DashBoard提供了在线更改限流规则的配置,也更加的优化。

从官方文档的介绍,Sentinel 具有以下特征:

  • 丰富的应用场景:Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、实时熔断下游不可用应用等。

  • 完备的实时监控:Sentinel 同时提供实时的监控功能。您可以在控制台中看到接入应用的单台机器秒级数据,甚至 500 台以下规模的集群的汇总运行情况。

  • 广泛的开源生态:Sentinel 提供开箱即用的与其它开源框架/库的整合模块,例如与 Spring Cloud、Dubbo、gRPC 的整合。您只需要引入相应的依赖并进行简单的配置即可快速地接入 Sentinel。

  • 完善的 SPI 扩展点:Sentinel 提供简单易用、完善的 SPI 扩展点。您可以通过实现扩展点,快速的定制逻辑。例如定制规则管理、适配数据源等。

下载sentinel dashboard

Sentinel dashboard提供一个轻量级的控制台,它提供机器发现、单机资源实时监控、集群资源汇总,以及规则管理的功能。您只需要对应用进行简单的配置,就可以使用这些功能。

注意: 集群资源汇总仅支持 500 台以下的应用集群,有大概 1 - 2 秒的延时。

下载最新的sentinel dashboard,下载地址为https://github.com/alibaba/Sentinel/releases

下载完启动,启动端口为8748,启动命令如下:

java -Dserver.port=8748 -Dcsp.sentinel.dashboard.server=localhost:8748 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.1.jar

改造consumer

本次教程的例子在上一节的例子的基础上进行改造。

在consumer的pom文件加上spring cloud sentinel的依赖,如下:

      <dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId></dependency>

配置文件application.yml加上如下的配置:

server:port: 8763spring:application:name: consumercloud:nacos:discovery:server-addr: 127.0.0.1:8848sentinel:transport:port: 18763dashboard: localhost:8748feign:sentinel:enabled: true

通过feign.sentinel.enable开启Feign和sentinel的自动适配。配置sentinel的dashboard的地址。

通过这样简单的配置,Feign和sentinel就配置好了。再启动三个工程provider\consumer\gateway。

在浏览器上多次访问http://localhost:5000/consumer/hi-feign

在浏览器上访问localhost:8748,登陆sentinel的控制台,登陆用户为sentinel,密码为sentinel。

consumer服务的/hi-feign接口,增加一个流控规则:

qps为1,快速访问http://localhost:5000/consumer/hi-feign,会有失败的情况。

在Spring cloud  gateway上使用sentinel

Spring cloud gateway已经适配了sentinel,在gatewayg工程的pom文件加上相关依赖:

     <dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId></dependency>

在配置文件application.yml上加上sentinel dashboard的配置:

spring:cloud:sentinel:transport:port: 15000dashboard: localhost:8748

创建一个网关分组和网关的限流规则,代码如下,参考https://github.com/alibaba/Sentinel/wiki/%E7%BD%91%E5%85%B3%E9%99%90%E6%B5%81

@Configuration
public class GatewayConfiguration {private final List<ViewResolver> viewResolvers;private final ServerCodecConfigurer serverCodecConfigurer;public GatewayConfiguration(ObjectProvider<List<ViewResolver>> viewResolversProvider,ServerCodecConfigurer serverCodecConfigurer) {this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList);this.serverCodecConfigurer = serverCodecConfigurer;}@Bean@Order(Ordered.HIGHEST_PRECEDENCE)public SentinelGatewayBlockExceptionHandler sentinelGatewayBlockExceptionHandler() {// Register the block exception handler for Spring Cloud Gateway.return new SentinelGatewayBlockExceptionHandler(viewResolvers, serverCodecConfigurer);}@Bean@Order(-1)public GlobalFilter sentinelGatewayFilter() {return new SentinelGatewayFilter();}@PostConstructpublic void doInit() {initCustomizedApis();initGatewayRules();}private void initCustomizedApis() {Set<ApiDefinition> definitions = new HashSet<>();ApiDefinition api1 = new ApiDefinition("consumer").setPredicateItems(new HashSet<ApiPredicateItem>() {{add(new ApiPathPredicateItem().setPattern("/consumer/**").setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));}});ApiDefinition api2 = new ApiDefinition("provider").setPredicateItems(new HashSet<ApiPredicateItem>() {{add(new ApiPathPredicateItem().setPattern("/provider/**").setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX));}});definitions.add(api1);definitions.add(api2);GatewayApiDefinitionManager.loadApiDefinitions(definitions);}private void initGatewayRules() {Set<GatewayFlowRule> rules = new HashSet<>();rules.add(new GatewayFlowRule("consumer").setCount(10).setIntervalSec(1));rules.add(new GatewayFlowRule("consumer").setCount(2).setIntervalSec(2).setBurst(2).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_CLIENT_IP)));rules.add(new GatewayFlowRule("provider").setCount(10).setIntervalSec(1).setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER).setMaxQueueingTimeoutMs(600).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_HEADER).setFieldName("X-Sentinel-Flag")));rules.add(new GatewayFlowRule("provider").setCount(1).setIntervalSec(1).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM).setFieldName("pa")));rules.add(new GatewayFlowRule("provider").setCount(2).setIntervalSec(30).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM).setFieldName("type").setPattern("warn").setMatchStrategy(SentinelGatewayConstants.PARAM_MATCH_STRATEGY_CONTAINS)));rules.add(new GatewayFlowRule("provider").setResourceMode(SentinelGatewayConstants.RESOURCE_MODE_CUSTOM_API_NAME).setCount(5).setIntervalSec(1).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM).setFieldName("pn")));GatewayRuleManager.loadRules(rules);}
}

在sentinel 控制台为gateway创建一个限流规则,如下:

流控规则为qps=1,快速访问http://localhost:5000/consumer/hi-feign,在qps>1的情况下,会报以下的错误:

Blocked by Sentinel: FlowException

可见sentinel的配置已经生效,更多sentinel教程,请关注fangzhipeng.com网站。

参考文档

https://www.fangzhipeng.com/springcloud/2019/06/02/sc-sentinel.html

https://github.com/alibaba/Sentinel/releases

https://github.com/alibaba/Sentinel/tree/master/sentinel-dashboard

https://github.com/spring-cloud-incubator/spring-cloud-alibaba/wiki/Sentinel

https://github.com/alibaba/Sentinel/wiki/%E6%8E%A7%E5%88%B6%E5%8F%B0

源码下载

https://github.com/forezp/SpringCloudLearning/tree/master/sc-2020-chapter3

热门内容:服务端如何防止订单重复支付!
拜托!不要用“ ! = null " 做判空了
道友自诉:入职中软一个月(外包华为)就离职了!
23 种设计模式的通俗解释,看完秒懂
token多平台身份认证架构设计思路
最近面试BAT,整理一份面试资料《Java面试BAT通关手册》,覆盖了Java核心技术、JVM、Java并发、SSM、微服务、数据库、数据结构等等。获取方式:点“在看”,关注公众号并回复 666 领取,更多内容陆续奉上。

明天见(。・ω・。)ノ♡

SpringCloud 2020版本教程3:使用sentinel作为熔断器相关推荐

  1. SpringCloud 2020版本教程0:springcloud 2020版本概述

    Spring cloud赶在2020年最后几天发布了新版本,版本号取名为2020.0.0,取消了英国地铁的命名方式.从H版本之后,全新的命名为2020.x.x.马上快2021年了,为毛不取名为2021 ...

  2. SpringCloud 2020版本教程1:使用nacos作为注册中心和配置中心

    点击关注公众号,Java干货及时送达 本次教程旨在为读者提供2020版本的最佳实践方案,使用我认为最容易学习的组件,可能很多组件有很多替代方案,在这里不依依讲述.本次教程使用的组件如下: 注册中心:n ...

  3. SpringCloud 2020版本教程4:使用spring cloud sleuth+zipkin实现链路追踪

    点击关注公众号,Java干货及时送达 Spring Cloud Sleuth 主要功能就是在分布式系统中提供追踪解决方案,并且兼容支持了 zipkin,你只需要在pom文件中引入相应的依赖即可. 微服 ...

  4. SpringCloud 2020版本教程2:使用spring cloud gateway作为服务网关

    点击关注公众号,Java干货及时送达 Spring Cloud Gateway是Spring Cloud官方推出的第二代网关框架,取代Zuul网关.网关作为流量的,在微服务系统中有着非常作用,网关常见 ...

  5. 安装navicat之后双击就会闪退_Adobe 2020版本,安装教程来咯

    ☉ 分享类 ☉ 哈喽(,,・∀・)ノ゛hello,大家好呀 欢迎来到我的PR自习室 一不小心迷路找不到我啦?教大家一个小窍门星标哟,这样就能不定时接收到我给你的惊喜浮力啦~ 上一期,给大家分享了Ado ...

  6. 速成视频教程集锦|疯狂模渲大师正式版——折叠版从第A到O章节,本套教程在2020年6月1日之后将成为又一个旧版本教程,以后是图文

    找各种软件.办公.装机帮助手册--点击进入引申热文--各行业软件大全更新中[19年11月"一设计群"整理] 第三章3.6节:3dmax杀毒解决问题安全卫士--疯狂模渲大师镶嵌版使用 ...

  7. 英语不好的同学已经期盼已久了!IDEA、Clion、Pycharm等2020版本支持中文啦,汉化教程

    Jet brains公司的诸多IDE 2020.1版本 正式发布了!最大的一个亮点莫过于开始支持中文了.相信很多英语不好的同学已经期盼已久了. 如何开启中文汉化 以 IntelliJ Idea 202 ...

  8. 通过官方查看springCloud,springBoot版本对应关系

    文章目录 1 官方文档查看 2 更优的查看方式 1 官方文档查看 在官网的Spring Cloud版本Hoxton SR7的Reference Doc中的内容如下,指定支持的boot版本为2.3.2. ...

  9. EDA虚拟机分享2020版本(Synopsys2020)

    导言: 更新一波EDA虚拟机,两个版本,主要安装Synopsys软件,内部所有EDA软件更新到较新的2020版本,感兴趣的可以使用,以下虚拟机2个版本,按照需求下载,学习完毕尽快删除,不可用于商业目的 ...

最新文章

  1. 0x03.基本算法 — 前缀和与差分
  2. 对指针变量取地址_C语言指针简介(amp;和*运算符)
  3. XtraBackup全备与增量备份
  4. arcgis按属性设置符号大小
  5. 紫色管理系统UI bootstrap后台模板
  6. STC15W408读取HX711称重数据串口发送
  7. React传递参数的多种方式
  8. uboot加载linux内核加载那些内容,uBoot和Linux内核中涉及到的几个地址参数的理解...
  9. myeclipse 8.6安装freemarker插件
  10. 用DELPHI为ASP开发文件上载组件
  11. [HDU1754]I Hate It线段树裸题
  12. cmd xcopy 拷贝文件夹_cmd xcopy进行远程复制
  13. vue、vant上传附件功能实现
  14. 思科关闭日志_思科交换机日志管理
  15. 如何制作电子文档CHM(How to gernerate chm from assembly)
  16. 又一大型色情直播App被捣毁,女主播哭求别告诉家人
  17. 【转】我的大学六年(单片机大师郭天祥原创)
  18. 学习Struts框架系列(一):模拟Struts工作流程
  19. 建立时间setup time/保持时间 hold time
  20. 一颗 “不安分” 的螺丝钉

热门文章

  1. Pytorch中的广播机制
  2. 论文: Data-Driven Evolutionary Optimization: An Overview and Case Studies(2):五个实例分析
  3. 基于GA的TSP问题
  4. 虚拟机cenos 重置密码
  5. 事件流--事件冒泡现象及阻止
  6. C#时间格式化(Datetime)用法详解
  7. hdu 5366 简单递推
  8. Matlab随笔之矩阵入门知识
  9. js实现页面跳转的几种方式
  10. Numpy入门教程:07. 随机抽样