springcloud学习记录-流控规则,降级规则,热点key限流+@SentinelResource

sentinel的启动:
在jar包目录下通过命令行启动:
java -jar sentinel-dashboard-1.7.2.jar
创建工程:
pom:

    <dependencies>
        <dependency>
            <groupId>com.atguigu.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>

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

        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <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>
      
        <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: 8401

spring:
  application:
    name: cloudalibaba-sentinel-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    sentinel:
      transport:
        dashboard: localhost:8080
        port: 8719  #默认8719,假如被占用了会自动从8719开始依次+1扫描。直至找到未被占用的端口

management:
  endpoints:
    web:
      exposure:
        include: '*'

业务类:

@RestController
@Slf4j
public class FlowLimitController {
    @GetMapping("/testA")
    public String testA(){

        return "--testA";
    }
    @GetMapping("/testB")
    public String testB()
    {
        System.out.println("213");
        return "--testB";
    }

主启动类:

@SpringBootApplication
@EnableDiscoveryClient
public class MainApp8401 {
    public static void main(String[] args) {
        SpringApplication.run(MainApp8401.class,args);
    }
}

然后启动项目,访问http://localhost:8080登录sentinel
通过访问http://localhost:8401/testA
可以再sentinel上看见服务添加到控制台。

(1)流控:

在这里插入图片描述
在这里插入图片描述
(2)降级规则:
在这里插入图片描述
RT指平均响应时间:
1s内通过的n个请求的响应的平均时间超过RT,则会进行降级,降级时间为时间窗口的值。
在这里插入图片描述

异常比例:1s内异常请求超过异常比例则会进行降级,时间为时间窗口的值。

在这里插入图片描述
异常数:
1分钟内异常请求超过异常数就会降级,时间窗口大于60s。

(3)热点规则:
在这里插入图片描述

为了让返回前台界面更友好,开始自定义兜底方法:
controller加入:

@GetMapping("/testHotKey")
@SentinelResource(value = "testHotKey",blockHandler = "deal_testHotKey")
public String testHotKey(@RequestParam(value = "p1",required=false) String p1,
                         @RequestParam(value = "p2",required=false) String p2){
                return "--testHotKey";
    }

public String deal_testHotKey(String p1, String p2, BlockException exception){
        return "兜底方法";
    }

资源名填:/testHotKey对应@GetMapping("/testHotKey")唯一标识,
填testHotKey对应@SentinelResource(value = “testHotKey”)唯一标识。
参数名称对应"p1",0表明第0个参数。
然后访问:http://localhost:8401/testHotKey?p1=b
如果阈值超过1,则会报错并返回自己定义的兜底方法。
在这里插入图片描述
特殊情况:
在这里插入图片描述
当p1=5时,阈值为200,p1为其他值时阈值为原来定义的1。

问题:如果testHotKey方法有异常会怎么样?
结果是返回报错界面,因为sentinel管的是请求的数量,参数等等,与后台程序异常无关。后台异常交给@SentinelResource里的fallback=“异常处理方法名”。

问题:每个方法都加自定义的兜底方法耦合性太高。
解决方法,单独建一个类放限流异常的方法。

public class CustomerBlockHandler {

    public static CommonResult handlerException1(BlockException exception){
        return new CommonResult(4444,exception.getClass().getCanonicalName()+"\t"+"服务不可用--1");
    }
    public static CommonResult handlerException2(BlockException exception){
        return new CommonResult(4444,exception.getClass().getCanonicalName()+"\t"+"服务不可用--2");
    }
}

controller层加入:

    @GetMapping("/rateLimit/customerBlockHandler")
    @SentinelResource(value = "customerBlockHandler",
            blockHandlerClass = CustomerBlockHandler.class,
            blockHandler = "handlerException2")
    public CommonResult customerBlockHandler(){
        return new CommonResult(200,"按客户自定义限流",new Payment(2020L,"serial002"));
    }

当限流成功时,自定义限流信息找到了controller里的方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值