SpringCloud Hoxton版 + SpringCloud alibaba学习笔记(12)-- SpringCloud Alibaba Sentinel @SentinelResource

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

1、启动Nacos成功

2、启动Sentinel成功

3、Module

①、cloudalibaba-sentinel-service8401
②、POM
<dependency>
    <groupId>com.atguigu.springcloud</groupId>
    <artifactId>cloud-api-commons</artifactId>
    <version>${project.version}</version>
</dependency>
③、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: '*'
④、业务类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.*;
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 服务不可用");
    }
 
⑤、主启动
package com.atguigu.springcloud.alibaba;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;


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

}

4、配置流控规则

①、配置步骤

在这里插入图片描述

②、图形配置和代码关系
③、表示1秒钟内查询次数大于1,就跑到我们自定义的处流,限流

5、测试

1秒钟点击1下,OK
超过上述问题,疯狂点击,返回了自己定义的限流处理信息,限流发送
在这里插入图片描述

6、额外问题

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

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

1、通过访问的URL来限流,会返回Sentinel自带默认的限流处理信息

2、业务类RateLimitController

@GetMapping("/rateLimit/byUrl")
@SentinelResource(value = "byUrl")
public CommonResult byUrl()
{
    return new CommonResult(200,"按url限流测试OK",new Payment(2020L,"serial002"));
}

3、访问一次

4、Sentinel控制台配置

在这里插入图片描述

5、测试

疯狂点击http://localhost:8401/rateLimit/byUrl
结果
在这里插入图片描述

三、上面兜底方法面临的问题

在这里插入图片描述

四、客户自定义限流处理逻辑

1、创建customerBlockHandler类用于自定义限流处理逻辑

2、自定义限流处理类

CustomerBlockHandler
在这里插入图片描述

package com.atguigu.springcloud.alibaba.myhandler;

import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.atguigu.springcloud.entities.*;

public class CustomerBlockHandler {

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

    }
}

3、RateLimitController

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

4、启动微服务后先调用一次

http://localhost:8401/rateLimit/customerBlockHandler

5、Sentinel控制台配置

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

7、进一步说明

在这里插入图片描述

五、更多注解属性说明

在这里插入图片描述
在这里插入图片描述

1、多说一句

在这里插入图片描述

2、Sentinel主要有三个核心API(了解一下)

在这里插入图片描述

posted @ 2021-01-09 16:10  暗影月色程序猿  阅读(37)  评论(0编辑  收藏  举报