Springcloud Alibaba Sentinel学习及使用

Springcloud Alibaba Sentinel学习及使用,第1张

Springcloud Alibaba Sentinel学习及使用

Springcloud Alibabasentinel:熔断、降级、限流
  • 一、概念介绍
  • 二、解决方案
  • 三、集成使用
  • 四、扩展使用
  • 五、网关监控

一、概念介绍

1、什么是熔断?

2、什么是降级?

3、区别?

4、什么是限流?

二、解决方案

springcloud的Hystrix和Alibaba的Sentinel,两者对比如下:

三、集成使用

关于springcloud和springcloud-alibaba和各组件的版本对应可参考github官网
https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E
1、springboot引入Sentinel依赖
直接指定版本号


    com.alibaba.cloud
    spring-cloud-starter-alibaba-sentinel
    2.2.3.RELEASE

或者引入spring-cloud-alibaba,不加版本号默认会跟据spring-cloud-alibaba选取合适的版本


    com.alibaba.cloud
    spring-cloud-starter-alibaba-sentinel


        
            
                com.alibaba.cloud
                spring-cloud-alibaba-dependencies
                2.2.5.RELEASE
                pom
                import
            
        
    

2、下载与2.2.3.RELEASE中核心包core-1.8.0对应的控制台
https://github.com/alibaba/Sentinel/releases/tag/v1.8.0
如果github下载过慢,可以使用我的这个1.8版本的包core-1.8.0

3、使用java -jar sentinel-dashboard-1.8.0.jar来启动控制台(默认端口为8080,可使用--server.port=8888来修改启动端口)

4、springboot配置Sentinel相关信息

spring:
  cloud:
    sentinel:
      transport:
        port: 8719
        dashboard: localhost:8888

5、配置规则每秒最多请求两次,F5快速刷新测试请求


注意:默认情况下Sentinel只能监控直接调用,如果要监控Feign远程调用,需要加上配置

feign:
  sentinel:
    enabled: true

这里有三个问题,
①监控图表没显示(搞版本已经解决了此问题,比如我用的1.8.0)
②所有配置都在内存中缓存着,重启失效
③超过配置的访问量后返回的信息是Sentinel自带的,需要自定义

5-1、解决低版本监控图表出不来
引入依赖


    org.springframework.boot
    spring-boot-starter-actuator

添加配置

management.endpoints.web.exposure.include=*

5-2、配置持久化
5-3、自定义违反规则处理参考下面:四、扩展使用

四、扩展使用

1、新建配置类SentinelConfig,作用于所有默认规则资源,比如像比如在@GetMapping("/testSentinel")这种请求,自定义请求地址资源返回内容
注意:如果是springboot1.x,内置spring4,使用下面的代码


如果是springboot2.x,内置spring5,使用webflux,代码如下:

import com.alibaba.csp.sentinel.adapter.spring.webflux.callback.BlockRequestHandler;
import com.alibaba.csp.sentinel.adapter.spring.webflux.callback.WebFluxCallbackManager;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Configuration
public class SentinelConfig {
    public SentinelConfig() {
        WebFluxCallbackManager.setBlockHandler(new BlockRequestHandler() {
            @Override
            public Mono handleRequest(ServerWebExchange serverWebExchange, Throwable throwable) {
                Mono body = ServerResponse.ok().body(Mono.just("{"msg":"请求流量过大","code":0}"), String.class);
                return body;
            }
        });
    }
}

2、在整个请求里面再细分多个监控降级
比如在@GetMapping("/testSentinel")里处理业务代码有A、B两步,需要对B步骤单独监控限流降级,可以使用以下几种方法
(1)try{}catch{}代码块,自定义异常

try (Entry entry = SphU.entry("B");){
		//...执行B业务的代码
        } catch (BlockException e) {
            e.printStackTrace();
        }

此时新增限流规则,设置限流每秒最多一个请求后,在快速刷新请求,就会抛出异常
(2)、基于注解
①、在将B业务抽取成B方法,在B方法上加入注解 @SentinelResource(value = "B",blockHandler = "handlerB")//参数一:规则名称;参数二:B方法违反规则后的处理方法
②、新建handlerB方法,此方法需跟B方法的返回值、参数完全相同,在里面返回自定义提示信息即可

五、网关监控

Sentinel不仅能监控请求、方法,在业务层去定义规则,也可以在网关层限流熔断,比如对Gateway进行监控限流
1、在网关服务的pom中添加依赖

  
  
      com.alibaba.cloud
      spring-cloud-starter-alibaba-sentinel
      2.2.3.RELEASE
  

  
      com.alibaba.cloud
      spring-cloud-alibaba-sentinel-gateway
      2.1.0.RELEASE
  

2、添加配置

spring:
  cloud:
    sentinel:
      transport:
        port: 8719
        dashboard: localhost:8888

3、测试使用

4、网关异常统一处理,和业务异常统一处理一样
新建SentinelGatewayConfig类,代码如下

import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.BlockRequestHandler;
import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Configuration
public class SentinelGatewayConfig {

    public SentinelGatewayConfig() {
        GatewayCallbackManager.setBlockHandler(new BlockRequestHandler() {
            @Override
            public Mono handleRequest(ServerWebExchange serverWebExchange, Throwable throwable) {
                Mono body = ServerResponse.ok().body(Mono.just("{"msg":"请求流量过大","code":0}"), String.class);
                return body;
            }
        });
    }
}

测试使用,f5快速刷新

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5681949.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存