栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

SpringCloud无介绍快使用,sentinel注解@SentinelResource的基本使用(二十三)
  • 问题背景
  • @SentinelResource的使用
  • Lyric: 你最好期许如果你跟我一样努力

问题背景

从零开始学springcloud微服务项目
注意事项:

  • 约定 > 配置 > 编码
  • IDEA版本2021.1
  • 这个项目,我分了很多篇章,每篇文章一个操作步骤,目的是显得更简单明了
  • controller调service,service调dao
  • 默认安装nginx
  • 项目源码以及sentinel安装包
@SentinelResource的使用

1 在cloudalibaba-sentinel-service8401添加pom依赖

        
            com.yg
            cloud-api-commons
            ${project.version}
        

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;


@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;




public class CustomerBlockHandler {

    public static CommonResult handleException2(BlockException exception) {
        return new CommonResult(2020, "自定义的限流处理信息......CustomerBlockHandler");
    }

}

5 在RateLimitController中添加

    
    
    @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: 你最好期许如果你跟我一样努力
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/988479.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号