SpringBoot整合Sentinel

admin
2022-03-13 / 0 评论 / 183 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2022年03月26日,已超过755天没有更新,若内容或图片失效,请留言反馈。

SpringBoot整合Sentinel

1、引入依赖

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

2、下载安装Sentinel可视化控制台

下载自己引入sentinel对应的核心包版本,下载地址:https://github.com/alibaba/Sentinel/releases

运行Sentinel可视化控制台

java -jar sentinel-dashboard-1.8.1.jar

注意自己的版本。

打开http://127.0.0.1:8080/,默认账号密码sentinel

3、微服务配置sentinel

application.yml

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

port: 8719端口随意,只要不被占用,用于各微服务与控制台通讯。

4、查看监控信息

启动微服务,随意访问一个接口,Sentinel控制台即可看到实施监控信息。

5、启用相关功能

可在Sentinel控制台调整相应功能。默认所有流控设置都是保存在内存中,重启服务就没有了。

6、添加监控图标

引入审计start,sentinel会自动根据spring-boot-starter-actuator监控。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    <version>2.6.1</version>
</dependency>

开启监控图标

management.endpoints.web.exposure.include=*

7、自定义流控返回提示信息

import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSON;
import com.yanxizhu.common.utils.R;
import org.springframework.context.annotation.Configuration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @description: Sentinel流控信息提示自定i
 * @author: <a href="mailto:batis@foxmail.com">清风</a>
 * @date: 2022/3/13 20:14
 * @version: 1.0
 */
@Configuration
public class MySentinelConfig implements BlockExceptionHandler {

    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) throws Exception {
        R r = R.error(10000, "自定义提示");
        httpServletResponse.setContentType("application/json;charset=utf-8");
        httpServletResponse.getWriter().write(JSON.toJSONString(r));
    }
}

限流规则可参考官网限流文档

每一个微服务都有一个自己的自定义流控返回提示信息,其它配置一样,只是提示信息不同。

8、熔断、降级

针对Feign远程调用熔断

引入Feign远程调用依赖

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

打开 Sentinel 对 Feign 的支持:

feign.sentinel.enabled=true

熔断实现

@FeignClient(value = "renren-fast",fallback = SeckillFeignServiceFallBack.class)
public interface CardFeignService {
    @GetMapping("/sys/user/list")
    public R list(@RequestParam Map<String, Object> params);
}

熔断回调

@S1f4j
@Component
public class SeckillFeignServiceFallBack implements SeckillFeignService{
    @Override public R getskuseckillInfo(Long skuld) {
        Log.info("熔断方法调用...getskuSecki11Info");
        return R.error(com.yanxizhu.family.common.exception.BizCodeEnume.TOO_MWANY_REQUEST.getCode();
        com.yanxizhu.family.common.exception.BizCodeEnume.TOO_MANY_REQUEST.getAsg());
    }
}

调用方:

手动指定远程服务的降级策略。远程服务被降级处理。触发我们的熔断回调方法。

提供方:

超大浏览的时侯,必须牺牲一些远程服务。在服务的提供方(远程服务)指定降级策略;提供方是在运行。但是不运行自己的业务逻辑,返回的是默认的降级数据(限流的数据)。

@SentinelResource 注解用来标识资源是否被限流、降级。上述例子上该注解的属性 sayHello 表示资源名。

@SentinelResource 还提供了其它额外的属性如 blockHandlerblockHandlerClassfallback 用于表示限流或降级的操作(注意有方法签名要求),更多内容可以参考 Sentinel 注解支持文档。若不配置 blockHandlerfallback 等函数,则被流控降级时方法会直接抛出对应的 BlockException;若方法未定义 throws BlockException 则会被 JVM 包装一层 UndeclaredThrowableException

注:一般推荐将 @SentinelResource 注解加到服务实现上,而在 Web 层直接使用 Spring Cloud Alibaba 自带的 Web 埋点适配。Sentinel Web 适配同样支持配置自定义流控处理逻辑,参考 相关文档

9、自定义受保护的资源

1)、代码
try(Entry entry =SphU.entry("seckillSkus"))(
/业务逻辑
catch(Execption e)(}
2)、基于注解。
eSentinelResource(value ="getCurrentSeckiLLSkusResource",blockHandler ="blockHandler")
无论是1,2方式一定要配置被限流以后的默认返回

注:一般推荐将 @SentinelResource 注解加到服务实现上,而在 Web 层直接使用 Spring Cloud Alibaba 自带的 Web 埋点适配。Sentinel Web 适配同样支持配置自定义流控处理逻辑,参考 相关文档

10、网关限流

引入网关sentinel依赖

<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
    <version>x.y.z</version>
</dependency>

使用时只需注入对应的 SentinelGatewayFilter 实例以及 SentinelGatewayBlockExceptionHandler 实例即可(若使用了 Spring Cloud Alibaba Sentinel,则只需按照文档进行配置即可,无需自己加 Configuration)。

更多网关限流可参考官方文档

11、定制网关流控返回

参考官方文档

4

评论 (0)

取消