@SentinelResource

按资源名称限流+后续处理

  • 启动Nacos成功

http://localhost:8848/nacos/#/login

  • 启动Sentinel成功

java -jar sentinel-dashboard-1.7.0.jar

Module

  • cloudalibaba-sentinel-service8401

POM

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>mscloud03</artifactId><groupId>com.atguigu.springcloud</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>cloudalibaba-sentinel-service8401</artifactId><dependencies><!--SpringCloud ailibaba nacos --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><dependency><!-- 引入自己定义的api通用包,可以使用Payment支付Entity --><groupId>com.atguigu.springcloud</groupId><artifactId>cloud-api-commons</artifactId><version>${project.version}</version></dependency><!--SpringCloud ailibaba sentinel-datasource-nacos 后续做持久化用到--><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-datasource-nacos</artifactId></dependency><!--SpringCloud ailibaba sentinel --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId></dependency><!--openfeign--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><!-- SpringBoot整合Web组件+actuator --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!--日常通用jar包配置--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>4.6.3</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies></project>

YML


server:port: 8401spring:application:name: cloudalibaba-sentinel-servicecloud:nacos:discovery:server-addr: localhost:8848 #Nacos服务注册中心地址sentinel:transport:dashboard: localhost:8080 #配置Sentinel dashboard地址port: 8719management:endpoints:web:exposure:include: '*'

业务类RateLimitController

package com.atguigu.springcloud.alibaba.controller;import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;/*** @auther zzyy* @create 2020-02-13 16:50*/
@RestController
public class RateLimitController
{@GetMapping("/byResource")@SentinelResource(value = "byResource",blockHandler = "handleException")public CommonResult byResource(){return new CommonResult(200,"按资源名称限流测试OK",new Payment(2020L,"serial001"));}public CommonResult handleException(BlockException exception){return new CommonResult(444,exception.getClass().getCanonicalName()+"\t 服务不可用");}
}

主启动


package com.atguigu.springcloud.alibaba;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;/*** @auther zzyy* @create 2019-12-09 18:49*/
@EnableDiscoveryClient
@SpringBootApplication
public class MainApp8401
{public static void main(String[] args) {SpringApplication.run(MainApp8401.class, args);}
}

配置流控规则

配置步骤

图形配置和代码关系

  • 表示1秒钟内查询次数大于1,就跑到我们自定义的处流,限流

测试

  • 1秒钟点击1下,OK
  • 超过上述,疯狂点击,返回了自己定义的限流处理信息,限流发生

额外问题

  • 此时关闭问服务8401看看
  • Sentinel控制台,流控规则消失了?????
  • 临时/持久?

按照Url地址限流+后续处理

业务类RateLimitController

package com.atguigu.springcloud.alibaba.controller;import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;/*** @auther zzyy* @create 2020-02-13 16:50*/
@RestController
public class RateLimitController
{@GetMapping("/byResource")@SentinelResource(value = "byResource",blockHandler = "handleException")public CommonResult byResource(){return new CommonResult(200,"按资源名称限流测试OK",new Payment(2020L,"serial001"));}public CommonResult handleException(BlockException exception){return new CommonResult(444,exception.getClass().getCanonicalName()+"\t 服务不可用");}@GetMapping("/rateLimit/byUrl")@SentinelResource(value = "byUrl")public CommonResult byUrl(){return new CommonResult(200,"按url限流测试OK",new Payment(2020L,"serial002"));}
}

访问一次

http://localhost:8401/rateLimit/byUrl

Sentinel控制台配置

测试

疯狂点击http://localhost:8401/rateLimit/byUrl

结果
会返回Sentinel自带的限流处理结果

上面兜底方案面临的问题

1 系统默认的,没有体现我们自己的业务要求。

2 依照现有条件,我们自定义的处理方法又和业务代码耦合在一块,不直观。

3 每个业务方法都添加一个兜底的,那代码膨胀加剧。

4 全局统一的处理方法没有体现。

客户自定义限流处理逻辑

创建CustomerBlockHandler类用于自定义限流处理逻辑

自定义限流处理类

CustomerBlockHandler

package com.atguigu.springcloud.alibaba.myhandler;import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.atguigu.springcloud.alibaba.entities.CommonResult;
import org.springframework.stereotype.Component;/*** @auther zzyy* @create 2019-12-10 13:01*/
public class CustomerBlockHandler
{public static CommonResult handleException(BlockException exception){return new CommonResult(2020,"自定义的限流处理信息......CustomerBlockHandler");}
}

RateLimitController

package com.atguigu.springcloud.alibaba.controller;import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.atguigu.springcloud.alibaba.myhandler.CustomerBlockHandler;
import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;/*** @auther zzyy* @create 2020-02-13 16:50*/
@RestController
public class RateLimitController
{@GetMapping("/byResource")@SentinelResource(value = "byResource",blockHandler = "handleException")public CommonResult byResource(){return new CommonResult(200,"按资源名称限流测试OK",new Payment(2020L,"serial001"));}public CommonResult handleException(BlockException exception){return new CommonResult(444,exception.getClass().getCanonicalName()+"\t 服务不可用");}@GetMapping("/rateLimit/byUrl")@SentinelResource(value = "byUrl")public CommonResult byUrl(){return new CommonResult(200,"按url限流测试OK",new Payment(2020L,"serial002"));}/*** 自定义通用的限流处理逻辑,blockHandlerClass = CustomerBlockHandler.classblockHandler = handleException2上述配置:找CustomerBlockHandler类里的handleException2方法进行兜底处理*//*** 自定义通用的限流处理逻辑*/@GetMapping("/rateLimit/customerBlockHandler")@SentinelResource(value = "customerBlockHandler",blockHandlerClass = CustomerBlockHandler.class, blockHandler = "handleException2")public CommonResult customerBlockHandler(){return new CommonResult(200,"按客户自定义限流处理逻辑");}}

启动微服务后先调用一次

http://localhost:8401/rateLimit/customerBlockHandler

Sentinel控制台配置


测试后我们自定义的出来了

进一步说明

更多注解属性说明


所有的代码都要用try-catch-finally方式进行处理,o(╥﹏╥)o

Sentinel主要有三个核心Api

  • SphU定义资源
  • Tracer定义统计
  • ContextUtil定义了上下文

SpringCloud Alibaba Sentinel实现熔断与限流-微服务(三十三)相关推荐

  1. SpringCloud Alibaba Sentinel实现熔断与限流-微服务(三十四)

    服务熔断功能 sentinel整合ribbon+openFeign+fallback Ribbon系列 启动nacos和sentinel 提供者9003/9004 新建cloudalibaba-pro ...

  2. springcloud(十六)--SpringCloud Alibaba Sentinel实现熔断与限流

    Sentinel实现熔断与限流 一.Sentinel 二.安装Sentinel控制台 Sentinel安装步骤: ①下载 ②运行命令 ③访问sentinel管理界面 三.初始化演示工程 1.启动Nao ...

  3. SpringCloud Alibaba Sentinel实现熔断与限流

    服务使用中的各种问题 服务雪崩   服务降级   服务熔断   服务限流 安装Sentinel sentinel和项目必须在同一个ip下   sentinel组件由2部分构成     后台     前 ...

  4. SpringCloud Alibaba Sentinel实现熔断与限流(下)

    在上一篇讲了sentinel的介绍,安装Sentinel控制台.初始化演示工以及程流控规则 接下来学习熔断规则,热点key限流,系统规则,@SentinelResource注解,服务熔断功能以及规则持 ...

  5. 【Spring Cloud Alibaba Sentinel 实现熔断与限流】 —— 每天一点小知识

  6. 「springcloud 2021 系列」sentinel实现熔断与限流 原来这么简单

    Sentinel 简介 随着微服务的流行,服务和服务之间的稳定性变得越来越重要.Sentinel 是面向分布式服务架构的流量控制组件,主要以流量为切入点,从流量控制.熔断降级.系统自适应保护等多个维度 ...

  7. Spring Cloud Alibaba:Sentinel实现熔断与限流

    摘要 Spring Cloud Alibaba 致力于提供微服务开发的一站式解决方案,Sentinel 作为其核心组件之一,具有熔断与限流等一系列服务保护功能,本文将对其用法进行详细介绍. Senti ...

  8. 算法高级(8)-Hystrix实现熔断、限流与服务保护中的算法详解

    上一章讲了常见的限流算法,本章我们来看看,Spring Cloud中的Hystrix组件在对请求进行熔断.限流与服务保护操作时的算法实践. 一.雪崩 分布式系统环境下,服务间依赖非常常见,一个业务调用 ...

  9. SpringCloud Alibaba Sentinel实现熔断与断流

    是什么 面向云原生微服务的流量控制,熔断降级组件 与Hystrix的区别 Hystrix 1.需要程序员自己手动搭建监控平台 2.没有一套web界面可以给我们进行更加细粒度化的配置流控,速率控制,服务 ...

最新文章

  1. 三巨头共聚AAA:ICapsule没有错,LeCun看好自监督,Bengio谈注意力
  2. mysqldump备份单表数据
  3. ExcelAndJSON的设计决策
  4. [python]-数据科学库Numpy学习
  5. php数组o m n mn,O(m + n)和O(mn)之间的区别?
  6. 29.课时29.【Django模板】url标签使用详解(Av61533158,P29)
  7. 目标检测的二十年发展史—从传统方法到深度学
  8. win11开机音乐怎么设置
  9. 机器学习常用损失函数
  10. good archtchre article
  11. 【算法学习】将MSRCR中的模糊处理由FFT修改为时域纯高斯模糊
  12. ofbiz webservice 例解
  13. nyoj592 蛇形填数
  14. Solr Facet 统计查询
  15. 学 C 语言,最经典的书有这样几本
  16. 前后端分离,使用AppNode管理前端部署-安装
  17. 中国电子标签(RFID)产业趋势研究及十四五发展规划建议报告2022-2028年版
  18. 《嵌入式 - 嵌入式大杂烩》PNP与NPN三极管的原理与使用方法
  19. 汉谟塔C语言,巴别塔的故事
  20. 打麻将技巧测试软件apk,学会这4招打麻将技巧,以后打麻将想不赢钱都难

热门文章

  1. python两个文件内容异或_python 异或两个文件 | 学步园
  2. 深度学习调参(炼丹)指南来了!
  3. Python:7-2 找出肇事者,循环与布尔逻辑(高教社,《Python编程基础及应用》习题6-6) (7分)
  4. 就业数据|北上广深不再是首选,应届生奔向新一线
  5. java javax.servlet_关于Java:无法解析导入javax.servlet
  6. 第七节:Nacos集群搭建
  7. MySQL时间分区的实现
  8. Ucloud创始人季昕华:办法总比困难多
  9. web前端+python后端打包发布
  10. 【阿冈评点】超女、我秀、好男和梦想的12项大PK(上)