1、Sentinel介绍
(1)Hystrix缺点:
- 需要手工搭建监控平台;
- 没有界面监控细粒度化的配置;
- Sentinel是一个可以独立出来的单独组件,界面化统一配置;
(2)作用:
- 从流量监控、熔断降级、负载均衡保护等多个维度保护服务的稳定性;
(3)特性
- 丰富的应用场景:秒杀、削峰、熔断等多场景使用;
- 完备的实时监控:实时监控;
- 广泛的开源生态:开箱即用,快速整合;
- 完善的SPI扩展点:快速扩展定制逻辑;
2、下载安装
(1)下载:https://github.com/alibaba/Sentinel/releases (2)接下来步骤查看:https://blog.csdn.net/weixin_45176509/article/details/123583281
3、使用
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>0.9.0.RELEASE</version>
</dependency>
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloud2022</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.consumer8083</groupId>
<artifactId>cloud-alibaba-consumer8083</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>0.9.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>0.9.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.commons</groupId>
<artifactId>commons</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
</dependencies>
</project>
server:
port: 8083
spring:
application:
name: consumer-nacos
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
config:
server-addr: localhost:8848
file-extension: yaml
sentinel:
transport:
dashboard: localhost:8080
port: 8719
management:
endpoints:
web:
exposure.include: '*'
package com.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class NacosConsumerDemoApplication {
public static void main(String[] args) {
SpringApplication.run(NacosConsumerDemoApplication.class, args);
}
}
- 业务Controller:未作变化,与Nacos相同
package com.consumer.Controller;
import com.commons.Entity.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@RestController
@RequestMapping("nacos/")
@RefreshScope
public class ConsumerController {
@Resource
private RestTemplate restTemplate;
@Value("${spring.datasource.username}")
private String configInfo;
@Value("${service-url.nacos-user-service}")
private String url;
@GetMapping("/{id}")
public Result saveEntity(@PathVariable("id") Integer id) {
return restTemplate.getForObject(url+"/nacos/"+id,Result.class);
}
@GetMapping("/getInfo")
public String getInfo() {
return configInfo;
}
}
- 启动项目,首次访问sentinel空空如也,他是懒加载机制,要访问接口之后才会加载,访问Controller接口,再次访问,效果如下:
4、规则
(1)簇点链路
(2)流控规则
配置说明:
- 阈值类型
配置QPS快速失败: QPS:御敌于国门之外; 线程数:关门打狗; - 流控模式
直接:默认模式;
关联:当与自己关联的资源达到阈值后就限流自己;
链路:多个请求操作同一个微服务
快速失败:默认处理;
WarmUp(预热):长期处于低访问,在某一时刻高访问,需要通过限流慢慢启动,预热时长之后才会慢慢达到单价阈值;
排队等待:排队挨个挨个处理
(3)熔断降级
Sentinel断路器非开即断,没有半开状态;
-
RT(平均响应时间):当1秒内进入5个请求,平均响应时间均超过阈值,那么接下来的时间窗口之内,调用这个方法就会自动熔断,时间窗口期结束,关闭熔断。默认为4900ms,若要设置上线需要配置; -
异常比例:当统计时间内,请求次数达到设置数,异常次数达到异常数,则发生熔断,超过熔断时长,关闭熔断; -
异常数:当统计时间内,请求次数达到设置数,异常比列到达设置比例,则发生熔断,超过熔断时长,关闭熔断;
(4)热点Key规则
- 热点即为经常访问的数据;
- 访问时根据路径是否携带约定参数来决定是否需要限流;
实现:
- 配置:当第一个参数存在,并且1秒内访问超过1次,就使用配置方法。
- 接口:
@SentinelResource(value = "nacosId",blockHandler = "dealHandler")
该注解执行的是配置的热点规则,如果出现java异常,不会执行限流方法;
package com.consumer.Controller;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.commons.Entity.Result;
import com.sun.deploy.security.BlockedException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@RestController
@RequestMapping("nacos/")
@RefreshScope
public class ConsumerController {
@Resource
private RestTemplate restTemplate;
@Value("${spring.datasource.username}")
private String configInfo;
@Value("${service-url.nacos-user-service}")
private String url;
@GetMapping("/{id}")
@SentinelResource(value = "nacosId",blockHandler = "dealHandler")
public Result saveEntity(@PathVariable("id") Integer id,
@RequestParam(value = "p1",required = false) String p1,
@RequestParam(value = "p2",required = false) String p2) {
return restTemplate.getForObject(url+"/nacos/"+id,Result.class);
}
public Result dealHandler(Integer id, String p1, String p2, BlockException blockException){
Result result = new Result(404,"/","");
return result;
}
}
踩坑:com.alibaba.csp.sentinel.slots.block.flow.FlowException
- 是否与原方法参数一致;
- 是否与原方法返回值一致;
- 是否添加BlockException参数;
参数例外项
- 以上例子指定某个位置参数后,就会对该参数进行统计限流,但是有时候希望该参数携带某个特殊值时,对它进行特殊限流规则;
- 当第一个参数值为5时,阈值可达100才会限流;
(5)系统自适应规则
- 从整体维度应用入口进行控制,该规则会对整个服务应用起作用;
维度 | 描述 |
---|
Load自适应 | 系统最高负载,建议取值 CPU cores * 2.5 | 并发线程数 | 单机应用的最大线程并发数 | 入口QPS | 单机应用维度入口QPS | 平均RT | 单机应用所有请求的平均RT | CPU使用率 | CPU使用率,取值范围[0, 1] |
5、配置使用
5.1 @SentinelResource配置
不支持private方法;
@RestController
public class RateLimitController {
@GetMapping("/getRate")
@SentinelResource(value = "getRate" ,blockHandler = "error")
public Result get(){
return new Result(200,"正常访问","200");
}
public Result error(BlockException blockException){
return new Result(404,blockException.getClass().getName()+"异常访问","404");
}
}
存在2种配置:
- 使用限流资源名称配置:getRate
- 使用访问Url配置:/getRate
如果有配置blockHandler 方法,则使用该方法,没有则使用系统默认方法;
以上配置导致业务代码和处理代码混合,每个方法都需要配置,导致代码膨胀,改造如下:
- 提出单独的Handler处理规则,注意方法参数值和返回类型要一致
package com.consumer.Handler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.commons.Entity.Result;
public class BlockHandler {
public static Result error1(BlockException blockException){
return new Result(404,blockException.getClass().getName()+"异常访问1","404");
}
public static Result error2(BlockException blockException){
return new Result(404,blockException.getClass().getName()+"异常访问2","404");
}
}
- 更改注解配置:
@SentinelResource(value = "getRate" ,blockHandlerClass = BlockException.class,blockHandler = "error1")
package com.consumer.Controller;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.commons.Entity.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RateLimitController {
@GetMapping("/getRate")
@SentinelResource(value = "getRate" ,blockHandlerClass = BlockException.class,blockHandler = "error1")
public Result get(){
return new Result(200,"正常访问","200");
}
}
Sentinel核心API:Sphu(定义资源)、Tracer(定义统计)、ContextUtil(定义上下文)
6、整合
6.1 Ribbon搭建
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>0.9.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>0.9.0.RELEASE</version>
</dependency>
application.yml
spring:
application:
name: consumer-nacos
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
config:
server-addr: localhost:8848
file-extension: yaml
sentinel:
transport:
dashboard: localhost:8080
port: 8719
management:
endpoints:
web:
exposure.include: '*'
主启动
package com.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class NacosConsumerDemoApplication {
public static void main(String[] args) {
SpringApplication.run(NacosConsumerDemoApplication.class, args);
}
}
业务Controller
package com.consumer.Controller;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.commons.Entity.Result;
import com.sun.deploy.security.BlockedException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@RestController
@RequestMapping("nacos/")
@RefreshScope
public class ConsumerController {
@Resource
private RestTemplate restTemplate;
@Value("${spring.datasource.username}")
private String configInfo;
@Value("${service-url.nacos-user-service}")
private String url;
@GetMapping("/{id}")
@SentinelResource(value = "nacosId")
public Result saveEntity(@PathVariable("id") Integer id,
@RequestParam(value = "p1",required = false) String p1,
@RequestParam(value = "p2",required = false) String p2) {
return restTemplate.getForObject(url+"/nacos/"+id,Result.class);
}
}
RestTemplate配置
package com.consumer.Config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
6.2 fallback处理运行时异常,blockHandler处理配置违规
只改造业务Controller,其他相同
package com.consumer.Controller;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.commons.Entity.Result;
import com.sun.deploy.security.BlockedException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@RestController
@RequestMapping("nacos/")
@RefreshScope
public class ConsumerController {
@Resource
private RestTemplate restTemplate;
@Value("${spring.datasource.username}")
private String configInfo;
@Value("${service-url.nacos-user-service}")
private String url;
@GetMapping("/{id}")
@SentinelResource(value = "nacosId",fallback = "dealHandler1" ,blockHandler = "dealHandler2")
public Result saveEntity(@PathVariable("id") Integer id,
@RequestParam(value = "p1",required = false) String p1,
@RequestParam(value = "p2",required = false) String p2) {
return restTemplate.getForObject(url+"/nacos/"+id,Result.class);
}
public Result dealHandler1(Integer id, String p1, String p2, BlockException blockException){
Result result = new Result(404,"/","1");
return result;
}
public Result dealHandler2(Integer id, String p1, String p2, BlockException blockException){
Result result = new Result(404,"/","2");
return result;
}
}
当二者同时出现方法调用时,blockHandler 优先级高于fallback ;
异常忽略 exceptionsToIgnore
@GetMapping("/{id}")
@SentinelResource(value = "nacosId",fallback = "dealHandler1" ,blockHandler = "dealHandler2",
exceptionsToIgnore = {IllegalArgumentException.class})
public Result saveEntity(@PathVariable("id") Integer id,
@RequestParam(value = "p1",required = false) String p1,
@RequestParam(value = "p2",required = false) String p2) {
return restTemplate.getForObject(url+"/nacos/"+id,Result.class);
}
配置该属性之后,如果发生IllegalArgumentException异常不会执行fallback 指定方法;
断路方式对比
Sentinel | Hystrix |
---|
信号量隔离 | 线程池、信号量隔离 | 基于响应时间、异常比率、异常数 | 基于异常比率 | 基于QPS、调用关系限流 | 有限的限流支持 |
7 、规则持久化
- 重启服务,配置的规则回消失,需要将规则持久化保存;
- 可以通过eureka、nacos、consul、redis等持久化媒介实现,官方推荐nacos。
7.1 实现
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloud2022</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.consumer8083</groupId>
<artifactId>cloud-alibaba-consumer8083</artifactId>
<dependencies>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>0.9.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>0.9.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.commons</groupId>
<artifactId>commons</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
</dependencies>
</project>
datasource:
ds1:
nacos:
server-addr: localhost:8848
dataId: cloudalibaba-sentinel-service
groupId: DEFAULT_GROUP
data-type: json
rule-type: flow
server:
port: 8083
spring:
application:
name: consumer-nacos
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
config:
server-addr: localhost:8848
file-extension: yaml
sentinel:
transport:
dashboard: localhost:8080
port: 8719
datasource:
ds1:
nacos:
server-addr: localhost:8848
dataId: cloudalibaba-sentinel-service
groupId: DEFAULT_GROUP
data-type: json
rule-type: flow
management:
endpoints:
web:
exposure.include: '*'
feign:
sentinel:
enabled: true
package com.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class NacosConsumerDemoApplication {
public static void main(String[] args) {
SpringApplication.run(NacosConsumerDemoApplication.class, args);
}
}
resourcenacosId:资源名称
limitAppdefalut: 来源应用
grade:阈值类型:0-线程数 1-QPS
count:单机阈值
strategy: 流控模式 0-直接 1-关联 2-链路
controlBehavior:流控效果:0-快速失败 2-WarmUp 3-表示排队等待
clusterModefalse :是否集群
- Sentinel流控规则
需要调用接口触发,否则此处为空; 总结
|