总结
sentinel组成:核心库(core)放到每个微服务中控制台(dashboard)独立运行 sentinel功能:服务限流、热点限流、降级熔断、系统规则 @SentinelResource(value = “fallback”, fallback = “handlerFallback”, blockHandler = “blockHandler”, exceptionsToIgnore = {IllegalArgumentException.class})兜底规则异常和运行异常 @FeignClient(value = “payment-provider”,fallback = PaymentFallbackService.class)sentinel结合openFeign进行fallback application.yaml中配置将规则持久化到nacos中
作用
服务限流:流量控制,调用关系限流,热点限流 服务降级:慢调用降级;作用-快速失败,避免影响其他服务 服务熔断:异常熔断 服务监控:实时监控 结合OpenFeign进行统一异常处理: 核心库(core)放到每个微服务中,控制台(dashboard)独立运行
使用/安装配置
核心库(core):放到每个微服务中 控制台(dashboard):独立运行 控制台(dashboard) sentinel-dashboard-compose.yaml
version: '3'
services:
sentinel-dashboard:
image: bladex/sentinel-dashboard:1.7.0
container_name: sentinel-dashboard
ports:
- 49157:8858
- 49158:8719
volumes:
- ./logs:/root/logs
restart: always
启动
docker-compose -f sentinel-dashboard-compose.yaml up -d
docker logs -f sentinel-dashboard
访问
- 地址
- 用户名密码 sentinel sentinel
使用/入门
pom.xml
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
application.yaml
server:
port: 8401
spring:
application:
name: sentinel-test-service
cloud:
nacos:
discovery:
server-addr: localhost:8848
sentinel:
transport:
dashboard: localhost:49157
port: 49158
management:
endpoints:
web:
exposure:
include: "*"
main
package com.xcrj.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class SentinelServiceMain8401 {
public static void main(String[] args) {
SpringApplication.run(SentinelServiceMain8401.class, args);
}
}
使用/懒加载
必须对放入sentinel核心库的服务进行访问之后,sentinel dashboard中才会出现信息
使用/服务限流
资源,限流条件,限流模式,限流效果 默认,限流模式-直接,限流效果-快速失败 限流效果-Warm Up预热,应用于秒杀系统,避免秒杀的瞬时流量一下就把系统撑死
- 资源名:资源路径,URL或@SentinelResource(value = “资源名”
- 针对来源:默认defaul,表示不区分来源;调用关系限流时,填写微服务名
- 阈值类型-单机阈值:
- QPS(query per-sencond):访问资源名中填写资源的QPS达到这里填写的阈值时限流
- 线程数:访问资源名中填写资源的线程数达到这里填写的阈值时限流
- 是否集群:
- 流控模式:
- 直接:资源达到限流条件,直接限流
- 关联:关联资源达到限流条件,限流自己(资源名填写的内容);点击关联,出现关联资源文本框
- 链路:链路上的入口资源达到限流条件,限流自己(资源名填写的内容);点击链路,出现入口资源文本框
- 流控效果:
- 快速失败:抛异常直接失败
- Warm Up:阈值in[阈值/codeFactor,阈值],默认codeFactor=3;点击Warm Up出现预热时长,单位s
- 排队等待:请求匀速通过,请求排队时间超过超时时间不再排队;前提,阈值类型设置为QPS;点击排队等待出现超时时间,单位ms
使用/服务限流/热点限流
对热点参数进行限流 资源名:资源路径,URL 限流模式:QPS 参数索引:入参索引,索引从0开始 统计窗口时长+单机阈值:统计窗口时长内,允许多少访问量 参数例外项:需要单独配置的参数,参数在特定参数值下的阈值;前提,参数是基本数据类型或String类型 参数类型:数据类型 参数值:参数值 限流阈值:QPS阈值
使用/服务降级
响应时间,异常(比例/数量) RT(Response Time):
- 开启服务降级,RT>阈值&&QPS>=5
- 关闭服务降级,时间窗口中填写时间过后关闭
- RT最大4900,需要修改-Dcsp.sentinel.statistic.max.rt=xxx才能生效
异常比例:
- 开启服务降级,异常比例>阈值&&QPS>=5
- 关闭服务降级,时间窗口中填写时间过后关闭
异常数:
- 开启服务降级,一分钟内 异常数>阈值
- 关闭服务降级,时间窗口中填写时间过后关闭
使用/系统规则
配置整个系统的规则,所有资源都遵守的规则 LOAD:一般为cpu-cores*2.5;前提,对Linux/Unix-like机器生效 RT:系统入口的平均RT,系统所有资源,单位ms 线程数:系统入口的并发线程数 入口QPS:系统入口的QPS CPU使用率:[0.0,1.0]
使用/规则异常blockHandler
@SentinelResource(value = “customerBlockHandler”,blockHandlerClass = CustomerBlockHandler.class,blockHandler = “handlerException”) @SentinelResource注解为testHotKey资源配置触发规则异常后的兜底方法dealTestHostKey
- value = “customerBlockHandler”,customerBlockHandler是资源名
- blockHandler = “handlerException”,handlerException触发规则异常后的兜底方法
- 运行异常blockHandler不会兜底
Controller类
@RestController
@Slf4j
public class RateLimitController {
@GetMapping("/customerBlockHandler")
@SentinelResource(value = "customerBlockHandler",blockHandlerClass = CustomerBlockHandler.class,blockHandler = "handlerException")
public CommonResult customerBlockHandler() {
return new CommonResult(200, "按customerBlockHandler限流测试ok", new Payment(2020L, "seria003"));
}
}
规则异常兜底方法类
public class CustomerBlockHandler {
public static CommonResult handlerException(BlockException bex){
return new CommonResult(200, "按customerBlockHandler限流测试ok,global BlockException处理-----2");
}
}
使用/运行异常fallback
@SentinelResource(value = “fallback”, fallbackClass = CustomerFallbackHanlder.class, fallback = “handlerFallback”)
- value = “fallback”,fallback是资源名
- fallback = “handlerFallback”,handlerFallback触发运行异常后的兜底方法
- exceptionsToIgnore = {IllegalArgumentException.class},不进行兜底的异常
@RestController
public class CircleBreakController {
@GetMapping("/consumer/fallback/{id}")
@SentinelResource(value = "fallback", fallbackClass = CustomerFallbackHanlder.class, fallback = "handlerFallback", exceptionsToIgnore = {IllegalArgumentException.class})
public CommonResult<Payment> fallback(@PathVariable Long id) {
CommonResult<Payment> result = restTemplate.getForObject(SERVER_URL + "/paymentSQL/" + id, CommonResult.class, id);
if (id == 4) {
throw new IllegalArgumentException("非法参数异常");
} else if (result.getData() == null) {
throw new NullPointerException("空指针异常");
}
return result;
}
}
CustomerFallbackHanlder
public class CustomerFallbackHanlder {
public CommonResult handlerFallback(@PathVariable Long id, Throwable e) {
Payment payment = new Payment(id, "null");
return new CommonResult(444, "兜底fallback,exception内容:" + e.getMessage(), payment);
}
}
使用/运行异常blockHandler+fallback
@SentinelResource(value = “fallback”, fallback = “handlerFallback”, blockHandler = “blockHandler”, exceptionsToIgnore = {IllegalArgumentException.class})
- value = “fallback”,fallback资源名
- blockHandler = “blockHandler”,blockHandler规则异常兜底方法
- fallback = “handlerFallback”,运行时异常兜底方法
- exceptionsToIgnore = {IllegalArgumentException.class},不进行兜底的异常
- 如果既触发了规则异常又触发了运行时异常,blockHandler去兜底
@RestController
public class CircleBreakController {
@GetMapping("/consumer/fallback/{id}")
@SentinelResource(value = "fallback", fallback = "handlerFallback", blockHandler = "blockHandler", exceptionsToIgnore = {IllegalArgumentException.class})
public CommonResult<Payment> fallback(@PathVariable Long id) {
CommonResult<Payment> result = restTemplate.getForObject(SERVER_URL + "/paymentSQL/" + id, CommonResult.class, id);
if (id == 4) {
throw new IllegalArgumentException("非法参数异常");
} else if (result.getData() == null) {
throw new NullPointerException("空指针异常");
}
return result;
}
public CommonResult handlerFallback(@PathVariable Long id, Throwable e) {
Payment payment = new Payment(id, "null");
return new CommonResult(111, "兜底fallback,exception内容:" + e.getMessage(), payment);
}
public CommonResult blockHandler(@PathVariable Long id, BlockException bex) {
Payment payment = new Payment(id, "null");
return new CommonResult(222, "兜底blockHandler,BlockException内容:" + bex.getMessage(), payment);
}
}
使用/sentinel结合openFeign进行fallback
pom.xml
- nacos
- openFeign
- sentinel
application.yaml
server:
port: 8401
spring:
application:
name: sentinel-test-service
cloud:
nacos:
discovery:
server-addr: localhost:8848
sentinel:
transport:
dashboard: localhost:49157
port: 49158
feign:
sentinel:
enabled: true
management:
endpoints:
web:
exposure:
include: "*"
main
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class OrderMain84 {
public static void main(String[] args) {
SpringApplication.run(OrderMain84.class, args);
}
}
服务调用接口声明+服务降级运行异常 PaymentService
@FeignClient(value = "payment-provider",fallback = PaymentFallbackService.class)
public interface PaymentService {
@GetMapping("/paymentSQL/{id}")
public CommonResult<Payment> paymentSQL(@PathVariable("id") Long id);
}
PaymentFallbackService
@Component
public class PaymentFallbackService implements PaymentService {
@Override
public CommonResult<Payment> paymentSQL(Long id) {
return new CommonResult<>(500, "java内部异常 兜底方法fallback");
}
}
使用/规则持久化到nacos
将规则文件配置到nacos中进行持久化
pom.xml 增加下面的依赖
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
application.yaml
server:
port: 8401
spring:
application:
name: sentinel-test-service
cloud:
nacos:
discovery:
server-addr: localhost:8848
sentinel:
transport:
dashboard: localhost:49157
port: 49158
datasource:
ds1:
nacos:
server-addr: localhost:49155
dataId: ${spring.application.name}
groupId: DEFAULT_GROUP
data-type: json
rule-type: flow
management:
endpoints:
web:
exposure:
include: "*"
main
@SpringBootApplication
@EnableDiscoveryClient
public class SentinelServiceMain8401 {
public static void main(String[] args) {
SpringApplication.run(SentinelServiceMain8401.class, args);
}
}
nacos
[
{
"resource":"byUrl",
"limitApp":"default",
"grade":1,
"count":1,
"clusterMode":false,
"strategy":0,
"controlBehavior":0
}
]
resource:资源名或URL limitApp:针对来源,默认defaul,表示不区分来源 grade:阈值类型,0-线程数,1-QPS count:1-单机阈值 clusterMode:是否集群 strategy:流控模式,0-直接,1-关联,2-链路 controlBehavior:流控效果,0-快速失败,1-Warm Up,2-排队等待
更多规则
|