2025-alibaba-Sentinel组件

什么是服务雪崩

在微服务调用链路中,因服务提供者的不可用导致服务调用者的不可用,并将不可用逐渐放大的过程,就叫服务雪崩效应。

导致服务不可用的原因:

解决方案

1、超时机制

2、服务限流

3、舱壁隔离(资源隔离)

4、服务熔断降级

技术选型: Sentinel or Hystrix

对比

Sentinel快速开始

spring boot 微服务 应用接入Sentinel控制台

<!--引入依赖 sentinel -->

复制代码
<!--引入依赖 sentinel -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

配置 application.yml 文件

复制代码
server:
  port: 8800

#开启Sentinel 对 Feign 的支持
feign:
  sentinel:
    enabled: true

spring:
  application:
    name: mall-user-sentinel-demo
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.56.1:8848
        username: nacos
        password: nacos

    openfeign: #openfeign的配置
      client:
        config:
          mall-order: # 对应微服务
            loggerLevel: FULL
            # 连接超时时间
            connectTimeout: 3000
            # 请求处理超时时间
            readTimeout: 5000
      okhttp:
        enabled: true

    sentinel:
      transport:
        dashboard: 192.168.116.156:8858
#      datasource:
#        flow-rules:
#          nacos:
#            server-addr: 192.168.56.1:8848
#            namespace: xulk
#            username: nacos
#            password: nacos
#            dataId: ${spring.application.name}-flow-rules
#            groupId: SENTINEL_GROUP
#            data-type: json
#            rule-type: flow

  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/ms_user?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
    username: root
    password: root
    druid:
      initial-size: 10
      max-active: 100
      min-idle: 10
      max-wait: 60000
      pool-prepared-statements: true
      max-pool-prepared-statement-per-connection-size: 20
      time-between-eviction-runs-millis: 60000
      min-evictable-idle-time-millis: 300000
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false
      stat-view-servlet:
        enabled: true
        url-pattern: /druid/*
      filter:
        stat:
          log-slow-sql: true
          slow-sql-millis: 1000
          merge-sql: false
        wall:
          config:
            multi-statement-allow: true



#暴露actuator端点   http://localhost:8800/actuator/sentinel
management:
  endpoints:
    web:
      exposure:
        include: '*'


logging:
  level:
    com.tuling.mall.sentineldemo.feign: debug
    com.alibaba.cloud.nacos.client.NacosPropertySourceBuilder: debug

controller 类

复制代码
    @RequestMapping(value = "/findOrderByUserId/{id}")
    //@SentinelResource(value = "findOrderByUserId",blockHandler = "handleException")
    public R  findOrderByUserId(@PathVariable("id") Integer id) {
        return R.ok( "11111111111");
    }

远程调用 feign 接口

复制代码
import com.tuling.common.utils.R;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
 
@FeignClient(value = "mall-order",path = "/order",fallbackFactory = FallbackOrderFeignServiceFactory.class)
public interface OrderFeignService {

    @RequestMapping("/findOrderByUserId/{userId}")
    public R findOrderByUserId(@PathVariable("userId") Integer userId);
}

进入降级类

复制代码
import com.tuling.common.utils.R;
import com.tuling.mall.sentineldemo.feign.OrderFeignService;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

@Component
public class FallbackOrderFeignServiceFactory implements FallbackFactory<OrderFeignService> {
    @Override
    public OrderFeignService create(Throwable throwable) {

        return new OrderFeignService() {
            @Override
            public R findOrderByUserId(Integer userId) {
                return R.error(-1,"=======服务降级了5555555========");
            }
        };
    }
}

自定义异常 MyBlockExceptionHandler

复制代码
mport com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
import com.alibaba.csp.sentinel.slots.system.SystemBlockException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tuling.common.utils.R;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

import java.io.PrintWriter;
 
@Slf4j
@Component
public class MyBlockExceptionHandler implements BlockExceptionHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) throws Exception {
        log.info("BlockExceptionHandler BlockException================"+e.getRule());
        R r = null;

        if (e instanceof FlowException) {
            r = R.error(100,"接口限流了111");

        } else if (e instanceof DegradeException) {
            r = R.error(101,"服务降级了2222");

        } else if (e instanceof ParamFlowException) {
            r = R.error(102,"热点参数限流了3333");

        } else if (e instanceof SystemBlockException) {
            r = R.error(103,"触发系统保护规则了444");

        } else if (e instanceof AuthorityException) {
            r = R.error(104,"授权规则不通过5555");
        }

        //返回json数据
        response.setStatus(500);
        response.setCharacterEncoding("utf-8");
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        new ObjectMapper().writeValue(response.getWriter(), r);

    }
}

配置流控规则 默认没持久化,重启服务 后会丢失

测试 http://localhost:8800/user/findOrderByUserId/1

降级/熔断处理配置

sentinel 持久化

复制代码
<!--sentinel 从nacos拉取规则 -->
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-nacos</artifactId>
</dependency>

配置文件 添加nacos的地址和配置文件

访问 http://localhost:8800/user/findOrderByUserId/1

自动持久化到 nacos

相关推荐
Helibo445 分钟前
GESPC++六级复习
java·数据结构·算法
serve the people31 分钟前
解决osx-arm64平台上conda默认源没有提供 python=3.7 的官方编译版本的问题
开发语言·python·conda
柒七爱吃麻辣烫1 小时前
在Linux中安装JDK并且搭建Java环境
java·linux·开发语言
极小狐1 小时前
极狐GitLab 容器镜像仓库功能介绍
java·前端·数据库·npm·gitlab
极小狐1 小时前
如何构建容器镜像并将其推送到极狐GitLab容器镜像库?
开发语言·数据库·机器学习·gitlab·ruby
努力的搬砖人.2 小时前
如何让rabbitmq保存服务断开重连?保证高可用?
java·分布式·rabbitmq
_星辰大海乀2 小时前
数据库约束
java·数据结构·数据库·sql·链表
多多*2 小时前
Java反射 八股版
java·开发语言·hive·python·sql·log4j·mybatis
正在走向自律2 小时前
从0到1:Python机器学习实战全攻略(8/10)
开发语言·python·机器学习
码农飞哥2 小时前
互联网大厂Java面试实战:Spring Boot到微服务的技术问答解析
java·数据库·spring boot·缓存·微服务·消息队列·面试技巧