SpringCloud无介绍快使用,sentinel注解@SentinelResource的基本使用(二十三)
@TOC
问题背景
从零开始学springcloud微服务项目
注意事项:
- 约定 > 配置 > 编码
- IDEA版本2021.1
- 这个项目,我分了很多篇章,每篇文章一个操作步骤,目的是显得更简单明了
- controller调service,service调dao
- 默认安装nginx
- 项目源码以及sentinel安装包
@SentinelResource的使用
1 在cloudalibaba-sentinel-service8401添加pom依赖
<dependency><!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
<groupId>com.yg</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
2 新增RateLimitController
package com.yg.springcloud.controller;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.yg.springcloud.entities.CommonResult;
import com.yg.springcloud.entities.Payment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author suolong
* @Date 2022/6/21 21:44
* @Version 2.0
*/
@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"));
}
}
3 设置流控

4 新增自定义handler
package com.yg.springcloud.handler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.yg.springcloud.entities.CommonResult;
/**
* @Author suolong
* @Date 2022/6/22 10:04
* @Version 2.0
*/
public class CustomerBlockHandler {
public static CommonResult handleException2(BlockException exception) {
return new CommonResult(2020, "自定义的限流处理信息......CustomerBlockHandler");
}
}
5 在RateLimitController中添加
/**
* 自定义通用的限流处理逻辑,
blockHandlerClass = CustomerBlockHandler.class
blockHandler = handleException2
上述配置:找CustomerBlockHandler类里的handleException2方法进行兜底处理
*/
/**
* 自定义通用的限流处理逻辑
*/
@GetMapping("/rateLimit/customerBlockHandler")
@SentinelResource(value = "customerBlockHandler",
blockHandlerClass = CustomerBlockHandler.class, blockHandler = "handleException2")
public CommonResult customerBlockHandler() {
return new CommonResult(200, "按客户自定义限流处理逻辑");
}
6 启动,postman发送:http://localhost:8401/rateLimit/customerBlockHandler

7 设置流控



SpringCloud无介绍快使用,Seata处理分布式事务(二十五)
SpringCloud无介绍快使用,sentinel服务熔断功能(二十四)
SpringCloud无介绍快使用,sentinel注解@SentinelResource的基本使用(二十三)
SpringCloud无介绍快使用,sentinel热点key限流与系统规则的基本使用(二十二)
SpringCloud无介绍快使用,sentinel熔断降级和限流的基本使用(二十一)
SpringCloud无介绍快使用,Nacos集群和Nginx代理(二十)
SpringCloud无介绍快使用,nacos配置中心的基本使用(十九)
SpringCloud无介绍快使用,nacos注册中心的基本使用(十八)
SpringCloud无介绍快使用,gateway通过微服务名实现动态路由(十七)
SpringCloud无介绍快使用,gateway的基本使用(十六)
SpringCloud无介绍快使用,Ribbon负载均衡工具与OpenFeign的使用(十五)
SpringCloud无介绍快使用,使用Zookeeper替换Eureka服务注册与发现(十四)
SpringCloud无介绍快使用,服务发现Discovery和Eureka自我保护(十三)
SpringCloud无介绍快使用,集群cloud-provider-payment8002搭建(十二)
SpringCloud无介绍快使用,集群Eureka服务注册中心cloud-eureka-server7002搭建(十一)
SpringCloud无介绍快使用,单机Eureka服务注册中心cloud-eureka-server7001搭建(十)
SpringCloud无介绍快使用,新建cloud-api-commons公共模块module(九)
SpringCloud无介绍快使用,新建子module消费者订单模块(八)
SpringCloud无介绍快使用,热部署devtools配置(七)
SpringCloud无介绍快使用,子module提供者支付微服务业务开发(六)
SpringCloud无介绍快使用,新建子module提供者支付微服务yml整合和新建启动类(五)
SpringCloud无介绍快使用,新建子module提供者支付微服务pom整合(四)
SpringCloud无介绍快使用,springcloud父工程pom文件整理(三)
SpringCloud无介绍快使用,IDEA新建springcloud父工程(二)
SpringCloud无介绍快使用,与Spingboot之间的兼容版本选择(一)
作为程序员第 190 篇文章,每次写一句歌词记录一下,看看人生有几首歌的时间,wahahaha ...



Lyric: 你最好期许如果你跟我一样努力

喜欢的朋友记得点赞、收藏、关注哦!!!