1.Sentinel介绍
1.1. 简介
随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。
Sentinel 具有以下特征:
- 丰富的应用场景:Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、集群流量控制、实时熔断下游不可用应用等。
- 完备的实时监控:Sentinel 同时提供实时的监控功能。您可以在控制台中看到接入应用的单台机器秒级数据,甚至 500 台以下规模的集群的汇总运行情况。
- 广泛的开源生态:Sentinel 提供开箱即用的与其它开源框架/库的整合模块,例如与 Spring Cloud、Apache Dubbo、gRPC、Quarkus 的整合。您只需要引入相应的依赖并进行简单的配置即可快速地接入 Sentinel。同时 Sentinel 提供 Java/Go/C++ 等多语言的原生实现。
- 完善的 SPI 扩展机制:Sentinel 提供简单易用、完善的 SPI 扩展接口。您可以通过实现扩展接口来快速地定制逻辑。例如定制规则管理、适配动态数据源等。
github及官方中文文档介绍:https://github.com/alibaba/Sentinel/wiki/%E4%BB%8B%E7%BB%8D
1.2. 主要特性
1.3. 开源生态
2.Sentinel控制台
2.1.概述
Sentinel 提供一个轻量级的开源控制台,它提供机器发现以及健康情况管理、监控(单机和集群),规则管理和推送的功能。
Sentinel 控制台包含如下功能:
- 查看机器列表以及健康情况:收集 Sentinel 客户端发送的心跳包,用于判断机器是否在线。
- 监控 (单机和集群聚合):通过 Sentinel 客户端暴露的监控 API,定期拉取并且聚合应用监控信息,最终可以实现秒级的实时监控。
- 规则管理和推送:统一管理推送规则。
- 鉴权:生产环境中鉴权非常重要。这里每个开发者需要根据自己的实际情况进行定制。
注意:Sentinel 控制台目前仅支持单机部署。Sentinel 控制台项目提供 Sentinel 功能全集示例,不作为开箱即用的生产环境控制台,若希望在生产环境使用请根据文档自行进行定制和改造。
2.2.启动控制台
2.2.1.获取控制台
从github下载最新的release版本的控制台jar包:https://github.com/alibaba/Sentinel/releases
2.2.2.启动
后台启动:
nohup java -Dserver.port=8080 -jar sentinel-dashboard.jar &
其中 -Dserver.port=8080
用于指定 Sentinel 控制台端口为 8080
。
从 Sentinel 1.6.0 起,Sentinel 控制台引入基本的登录功能,默认用户名和密码都是 sentinel
。
访问jar包运行所在的机器IP,对外端口,如:http://10.1.34.75:8080/
3.SpringBoot集成Sentinel
3.1.pom依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>0.9.0.RELEASE</version>
</dependency>
由于需要依赖Spring Cloud,还需要添加Cloud相关依赖。pom.xml文件完整内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
</parent>
<groupId>com.calvin</groupId>
<artifactId>sentinel-ratelimit</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sentinel-ratelimit</name>
<description>微服务限流方案sentinel</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud-alibaba.version>2.1.1.RELEASE</spring-cloud-alibaba.version>
<spring-cloud.version>Hoxton.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>0.9.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.2.添加sentinel配置
spring:
cloud:
sentinel:
eager: true
transport:
port: 8720
dashboard: 10.1.34.75:8080
heartbeat-interval-ms: 500
其中8720端口是应用端的sentinel和sentinel控制台通信的端口。
3.3.编写controller
package com.calvin.sentinel.controller;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("test")
public class MyController {
@GetMapping(value = "/hello")
@SentinelResource("hello")
public String hello() {
return "Hello Sentinel";
}
}
SentinelResource注解里的值是资源标识符,可以为这个资源标识符指定限流,熔断规则等。
启动服务后,查看控制台,会看到刚才启动的服务。
访问启动的服务API,可以看到没做任何限流操作,正常访问。
3.4.添加限流规则
QPS限制为每秒只能访问该资源1次,超过则限流提醒。
添加好限流规则后,连续请求API,发现已经被拦截。
Blocked by Sentinel (flow limiting)
这是Sentinel默认的限流提醒。
3.5.自定义限流提醒
3.5.1.自定义限流页面
新建config类,添加@PostConstruct方法。
package com.calvin.sentinel.config;
import com.alibaba.csp.sentinel.adapter.servlet.config.WebServletConfig;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
* @Author calvin
* @Date 2021/8/26 10:44
* @Description
* @Version 1.0
*/
@Component
public class RateLimitConfig {
@PostConstruct
public void init() {
WebServletConfig.setBlockPage("https://www.baidu.com");
}
}
限流后,跳转到自定义的页面或者网址
3.5.2.自定义handler实现类
新建CustomUrlBlockHandler类,实现UrlBlockHandler接口
package com.calvin.sentinel.handler;
import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlBlockHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @Author calvin
* @Date 2021/8/26 10:14
* @Description
* @Version 1.0
*/
public class CustomUrlBlockHandler implements UrlBlockHandler {
@Override
public void blocked(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) throws IOException {
httpServletResponse.setHeader("Content-Type","application/json;charset=UTF-8");
String message = "{\"code\":999,\"msg\":\"访问人数过多\"}";
httpServletResponse.getWriter().write(message);
}
}
设置UrlBlockHandler
package com.calvin.sentinel.config;
import com.alibaba.csp.sentinel.adapter.servlet.callback.WebCallbackManager;
import com.calvin.sentinel.handler.CustomUrlBlockHandler;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
* @Author calvin
* @Date 2021/8/26 10:44
* @Description
* @Version 1.0
*/
@Component
public class RateLimitConfig {
@PostConstruct
public void init() {
WebCallbackManager.setUrlBlockHandler(new CustomUrlBlockHandler());
}
}
限流后,可以看到根据自定义handler的处理来返回限流结果。
3.6.存储限流规则
我们会发现一个问题,如果应用服务重启了,控制台看不到我们设置的规则了,因此我们需要存储我们设置的限流规则。
3.6.1.通过本地json创建规则
[
{
"resource": "/test/hello",
"controlBehavior": 0,
"count": 1,
"grade": 1,
"limitApp": "default",
"strategy": 0
}
]
修改配置文件,添加datasource
spring:
cloud:
sentinel:
eager: true
transport:
port: 8720
dashboard: 10.1.34.75:8080
heartbeat-interval-ms: 500
datasource:
ds1:
file:
file: classpath:flowrule.json
data-type: json
rule-type: flow
重启应用服务后,查看控制台,可以看到我们在flowrule.json
文件中定义的限流规则。
继续访问API,发现限流是OK的。
3.6.2.使用Nacos存储规则
pom.xml文件中引入Sentinel-nacos依赖
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
配置文件中加入nacos信息
spring:
application:
name: sentinel-ratelimit
cloud:
sentinel:
eager: true
transport:
port: 8720
dashboard: 10.1.34.75:8080
heartbeat-interval-ms: 500
datasource:
# ds1:
# file:
# file: classpath:flowrule.json
# data-type: json
# rule-type: flow
ds2:
nacos:
server-addr: localhost:8848
dataId: ${spring.application.name}-sentinel
groupId: DEFAULT_GROUP
rule-type: flow
nacos已经安装,如果还没有安装的,参考Nacos官方文档安装:https://nacos.io/zh-cn/docs/quick-start.html
在nacos控制台新建配置
查看Sentinel控制台,发现已经有对应的限流规则了。
访问API,发现限流OK。
3.7.总结
以上完整代码git地址:https://gitee.com/calvin-sheng/sentinel-ratelimit