五、服务容错sentinel

一、整合sentinel

引入依赖

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

添加配置

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

访问 http://localhost:10000/actuator/sentinel 如图说明整合好了

二、sentinel控制台

第一步:下载sentinel控制台

官方版版本说明 · alibaba/spring-cloud-alibaba Wiki · GitHub

我们需要选择2.2.7对应的这一排组件版本,因此sentinel应该选1.8.1
下载地址 Releases · alibaba/Sentinel · GitHub
启动 java -jar sentinel-dashboard-1.8.1.jar
访问 http://localhost:8080/
如下图,说明启动成功,用户/密码:sentinel/sentinel

第二步:代码集成sentinel控制台

添加配置

spring:
  cloud:
    sentinel:
      transport:
        dashboard: localhost:8080

启动模块,发起请求,查看sentinel控制台,整合成功

sentinel会自动监控所有web端点,任何端点发生调用,sentinel的簇点链路中都会显示出来,会非常乱。但是一般情况下,我们只想对指定的端点做监控,添加如下配置

spring:
  cloud:
    sentinel:
      filter:
        # 关闭掉对spring MVC端点的保护
        enabled: false

对想要被sentinel监控的端点添加@SentinelResource注解,例如这里创建两个端点,只有一个加了@SentinelResource注解

package com.badcat.a.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.badcat.a.feignClient.BFeignClinet;
import com.badcat.a.feignClient.CFeignClinet;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/sentinel")
public class TestSentinelController {
    @Autowired
    private BFeignClinet bFeignClinet;

    @Autowired
    private CFeignClinet cFeignClinet;

    @GetMapping("/let_module-b_say_hello")
    @SentinelResource(value = "let_module-b_say_hello")
    public String letModuleBSayHello(){

        String result = this.bFeignClinet.sayHello();

        log.info("module-b say : [{}]", result);
        return String.format("module-b say : [%s]", result);
    }

    @GetMapping("/let_module-c_say_hello")
    public String letModuleCSayHello(){

        String result = this.cFeignClinet.sayHello();

        log.info("module-c say : [{}]", result);
        return String.format("module-c say : [%s]", result);
    }
}

分别对这两个端点发起调用,查看sentinel控制台,只有let_module-b_say_hello

推荐sentinel官网 https://github.com/alibaba/Sentinel/wiki

三、sentinel规则持久化

目前应用重启的话,配置的规则就会消失,需要做规则持久化。
实现持久化有两种方式:推模式、拉模式,这里就不多赘述了,本文记录使用nacos实现。
参考文章:Alibaba Sentinel 1.8.1规则持久化-推模式【基于Nacos 1.3.2】_Jax_u90b9的博客-CSDN博客

四、实现区分各种规则

目前规则触发后,统一会走block方法,那么想知道具体是出发的哪种规则,需要写代码区分,很简单,代码如下

public static String block(String a, BlockException e){
        AbstractRule rule = e.getRule();
        if (rule instanceof FlowRule){
            return "LetBSayHelloByFeign触发了流控规则, FlowRule";
        } else if (rule instanceof AuthorityRule) {
            return "LetBSayHelloByFeign触发了授权规则, AuthorityRule";
        } else if (rule instanceof DegradeRule) {
            return "LetBSayHelloByFeign触发了降级规则, DegradeRule";
        } else if (rule instanceof ParamFlowRule) {
            return "LetBSayHelloByFeign触发了热点规则, ParamFlowRule";
        } else if (rule instanceof SystemRule) {
            return "LetBSayHelloByFeign出发了系统规则, SystemRule";
        }
        return "LetBSayHelloByFeign未知规则保护";
    }

这部分代码放到了码云 study_badcat/spring-cloud-alibaba-study (gitee.com)
,v5.0分支

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容