码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • SpringCloud无介绍快使用,sentinel注解@SentinelResource的基本使用(二十三)


    SpringCloud无介绍快使用,sentinel注解@SentinelResource的基本使用(二十三)

    • 问题背景
    • @SentinelResource的使用
    • Lyric: 你最好期许如果你跟我一样努力

    问题背景

    从零开始学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>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    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"));
        }
    
    }
    
    • 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
    • 33
    • 34
    • 35
    • 36

    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");
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    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, "按客户自定义限流处理逻辑");
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    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: 你最好期许如果你跟我一样努力

  • 相关阅读:
    【技术课堂】从批到流:pull or not pull, that's a question
    机器人控制器编程实践指导书旧版-实践八 机器人综合设计
    数据同步,还看Canal
    设计模式-单例模式
    PWM控制蜂鸣器
    什么是 KNIME Hub(2024)
    【Webpack】样式处理 - 样式预处理
    numpy函数总结
    数据结构
    多题库查题、独立后台、响应速度快、全网平台可查、功能最全!
  • 原文地址:https://blog.csdn.net/cucgyfjklx/article/details/125435038
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号