1、引入依赖

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

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

Download自己引入sentinel对应的核心包Version,Download地址:https://github.com/alibaba/Sentinel/releases

RunSentinel可视化控制台

java -jar sentinel-dashboard-1.8.1.jar

注意自己的Version。

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

3、微服务配置sentinel

application.yml

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

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

4、查看监控信息

StartMicroservices,随意访问一个Interface,Sentinel控制台即可看到实施Monitoring信息。

5、启用相关功能

可在Sentinel控制台调整相应功能。默认所有流控Settings都是保存在内存中,RestartService就没有了。

6、添加监控图标

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

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

开启Monitoring图标

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));
    }
}

Rate Limiting规则可参考官网Rate Limiting文档

每一个Microservices都有一个自己的自定义流控Return提示信息,其它Configuration一样,只是提示信息不同。

8、熔断、降级

针对FeignRemote Call熔断

引入FeignRemote CallDependency

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

打开 Sentinel 对 Feign 的支持:

feign.sentinel.enabled=true

熔断Implementation

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

熔断Callback

@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());
    }
}

调用方:

手动指定远程Service的降级策略。远程Service被降级处理。触发我们的熔断CallbackMethod。

提供方:

超大浏览的时侯,必须牺牲一些远程Service。在Service的提供方(远程Service)指定降级策略;提供方是在Run。但是不Run自己的业务逻辑,Return的是默认的降级Data(Rate Limiting的Data)。

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

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

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

9、自定义受保护的资源

1)、Code try(Entry entry =SphU.entry(“seckillSkus”))( /业务逻辑 catch(Execption e)(} 2)、基于注解。 eSentinelResource(value =“getCurrentSeckiLLSkusResource”,blockHandler =“blockHandler”) 无论是1,2方式一定要Configuration被Rate Limiting以后的默认Return

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

10、网关限流

引入GatewaysentinelDependency

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

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

更多GatewayRate Limiting可参考官方文档

11、定制网关流控返回

参考官方文档