当前位置:   article > 正文

springcloud--Sentinel_springcloud sentinel

springcloud sentinel

参考:https://blog.csdn.net/ThinkWon/article/details/103770879

https://github.com/alibaba/Sentinel/wiki/%E6%96%B0%E6%89%8B%E6%8C%87%E5%8D%97

官网下载:https://github.com/alibaba/Sentinel/tags

在这里插入图片描述

下载完成后在命令行输入如下命令运行Sentinel控制台:

java -jar sentinel-dashboard-1.7.0.jar
  • 1

在这里插入图片描述
Sentinel控制台默认运行在8080端口上,登录账号密码均为sentinel,通过如下地址可以进行访问:http://localhost:8080

在这里插入图片描述
加pom:

<!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-sentinel -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    <version>2.2.1.RELEASE</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

在application.yml中添加相关配置,主要是配置了Nacos和Sentinel控制台的地址:

server:
  port: 9093

spring:
  application:
    name: business-9093

#  datasource:
#    #    url: jdbc:mysql://rm-2vc7l62g4u9rt2l82vo.mysql.cn-chengdu.rds.aliyuncs.com:3306/materialbasecloud?characterEncoding=utf8&useSSL=false&autoReconnect=true&rewriteBatchedStatements=true
#    #    username: root
#    #    password: jjqhc123456
#    url: jdbc:mysql://49.235.125.47:3306/live?characterEncoding=utf8&useSSL=false&autoReconnect=true&rewriteBatchedStatements=true
#    username: root
#    password: 123456
#    driver-class-name: com.mysql.cj.jdbc.Driver
#    hikari:
#      max-lifetime: 180000
#      minimum-idle: 20
#      #设置最大线程数量:
#      maximum-pool-size: 30
#      idle-timeout: 120000
#      #      加了之后容易超时
#      #      connection-test-query: SELECT 1

  #redis
  redis:
    # redis数据库索引(默认为0),我们使用索引为3的数据库,避免和其他数据库冲突,通过rdm查看时,在db3的下面
    database: 1
    # redis服务器地址(默认为loaclhost)
    host: 49.235.125.47
    # redis端口(默认为6379)
    port: 6379
    # redis访问密码(默认为空)
    password: 123456
    # redis连接超时时间(单位毫秒)
    timeout: 10000
    # redis连接池配置
    jedis:
      pool:
        # 最大可用连接数(默认为8,负数表示无限)
        max-active: 8
        # 最大空闲连接数(默认为8,负数表示无限)
        max-idle: 8
        # 最小空闲连接数(默认为0,该值只有为正数才有用)
        min-idle: 0
        # 从连接池中获取连接最大等待时间(默认为-1,单位为毫秒,负数表示无限)
        max-wait: -1

  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
        #    配置sentinel客户端,注册进控制台里
    sentinel:
      transport:
        dashboard: 127.0.0.1:8080
        # 为应用开启额外的端口,上报监控信息
        # 默认为8719端口,加入被占用将会自动从8719开始+1扫描,直至找到未被占用的端口地址
        port: 8719

        datasource: #配置流控规则持久化
          ds1:
            nacos:
              server-addr: localhost:8848
              dataId: cloudalibaba-sentinel-service
              groupId: DEFAULT_GROUP
              data-type: json
              rule-type: flow
## 监控相关
anagement:
  endpoints:
    web:
      exposure:
        include: '*'

#开启 sentinel 对 feign 的支持
feign:
  sentinel:
    enabled: true
  • 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
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79

要访问了controller之后,才会有值,懒加载!!
在这里插入图片描述

限流功能

  1. 按资源名称限流,需要指定限流处理逻辑
  2. 按url限流,有默认的限流处理逻辑
  3. 自定义限流处理逻辑

编写controller测试:

package com.example.businesssentinel9093.controller;

import cn.hutool.extra.tokenizer.Result;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.example.businesssentinel9093.myhandler.CustomerBlockHandler;
import com.example.dtestcommon.vo.Res;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/sentinel")
public class BusinessSentinelController {


    @PostMapping("/test")
    public String test(){
        return "9093";
    }

    /**
     * 按资源名称限流,需要指定限流处理逻辑
     *
     * @return
     */
    @PostMapping("/byResource")
    @SentinelResource(value = "byResource", blockHandler = "handleException")
    public Res byResource() {
        return  Res.success(200,"按资源名称限流");
    }

    /**
     * 按url限流,有默认的限流处理逻辑
     *
     * @return
     */
    @PostMapping("byUrl")
    @SentinelResource(value = "byUrl", blockHandler = "handleException")
    public Res byUrl() {
        return Res.success(200,"按url限流");
    }

    public Res handleException(BlockException exception) {
        return Res.success(200,exception.getClass().getCanonicalName());
    }

    @PostMapping("/customBlockHandler")
    @SentinelResource(value = "customBlockHandler", blockHandler = "handlerException", blockHandlerClass = CustomerBlockHandler.class)
    public Res blockHandler() {
        return Res.success(200,"限流成功");
    }




}
  • 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
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
package com.example.businesssentinel9093.myhandler;

import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.example.dtestcommon.vo.Res;

public class CustomerBlockHandler {

    public  static Res handlerException(BlockException exception)
    {
        return  Res.success(444,"客户自定义限流处理信息1,global handlerException-----1");
    }

    public  static Res handlerException2(BlockException exception)
    {
        return  Res.success(444,"客户自定义限流处理信息2,global handlerException-----2");
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

1. 按资源名称限流,需要指定限流处理逻辑

在这里插入图片描述

qps和线程的区别:

在这里插入图片描述

测试:
前:
在这里插入图片描述
超过阈值后:
在这里插入图片描述

2. 按url限流,有默认的限流处理逻辑

在这里插入图片描述

在这里插入图片描述

3. 自定义限流处理逻辑

配置可以自己选择,资源吗或者路径的!!!
在这里插入图片描述

在这里插入图片描述

关联:

在这里插入图片描述

当关联的资源达到限流条件时,该资源才会开启限流
配置请求 /customBlockHandler限流QPS,关联 /byResource

当关联的资源达到阈值时customBlockHandler,会对该资源byResource进行限流

使用jmeter进行对customBlockHandler资源的访问:
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

降级

1、配置rt:

在这里插入图片描述

写controller测试:

package com.example.businesssentinel9093.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.example.businesssentinel9093.myhandler.CustomerfallbackHandler;
import com.example.dtestcommon.vo.Res;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/downGroud")
public class DownGrandSentinelController {

//    匹配返回值要相同

    /**
     * 降级测试
     * @param
     * @return
     */
    @GetMapping("/time")
//    @SentinelResource(value = "time",fallback = "handleFallback2")
    public Res time(){
        //当请求参数为 1 时,响应延迟600ms 也就是RT可以配置允许时间比这个数低的值

        try {
            Thread.sleep(600);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return Res.success("jiangji test 当前时间:"+new Date().getTime());
    }

    @GetMapping("/time2")
    @SentinelResource(value = "time2",blockHandler = "handlerException",blockHandlerClass = CustomerfallbackHandler.class)
    public Res time2(){
        //当请求参数为 1 时,响应延迟600ms 也就是RT可以配置允许时间比这个数低的值

        try {
            Thread.sleep(600);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return Res.success("jiangji test 当前时间:"+new Date().getTime());
    }

    @GetMapping("/fallbackException/{id}")
    @SentinelResource(value = "fallbackException", fallback = "handleFallback2", exceptionsToIgnore = {NullPointerException.class})
    public Res fallbackException(@PathVariable Long id) {
        if (id == 1) {
            throw new IndexOutOfBoundsException();
        } else if (id == 2) {
            throw new NullPointerException();
        }

        return Res.success(1,"异常降级!!!");
    }

    public Res handleFallback(Long id) {
        return  Res.success(200,"服务降级返回1");
    }

    public Res handleFallback2(Long id, Throwable e) {
        return  Res.success(200,"服务降级返回2");
    }


}
  • 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
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
package com.example.businesssentinel9093.myhandler;

import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.example.dtestcommon.vo.Res;

public class CustomerfallbackHandler {

    public  static Res handlerException(BlockException exception)
    {
        return  Res.success(444,"客户自定义限流处理信息1,global handlerException-----1");
    }

    public  static Res handlerException2(BlockException exception)
    {
        return  Res.success(444,"客户自定义限流处理信息2,global handlerException-----2");
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

在这里插入图片描述

在这里插入图片描述

若时不用@SentinelResource注解,会使用默认的
在这里插入图片描述

在这里插入图片描述

2、异常降级

package com.example.businesssentinel9093.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.example.businesssentinel9093.myhandler.CustomerfallbackHandler;
import com.example.dtestcommon.vo.Res;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/downGroud")
public class DownGrandSentinelController {

    @GetMapping("/fallbackException/{id}")
    @SentinelResource(value = "fallbackException", fallback = "handleFallback2", exceptionsToIgnore = {NullPointerException.class})
    public Res fallbackException(@PathVariable Long id) {
        if (id == 1) {
            throw new IndexOutOfBoundsException();
        } else if (id == 2) {
            throw new NullPointerException();
        }

        return Res.success(1,"异常降级!!!");
    }

    public Res handleFallback(Long id) {
        return  Res.success(200,"服务降级返回1");
    }

    public Res handleFallback2(Long id, Throwable e) {
        return  Res.success(200,"服务降级返回2");
    }


}
  • 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
  • 37
  • 38

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/739432
推荐阅读
相关标签
  

闽ICP备14008679号