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
访问
pom.xml
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-sentinelartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cspgroupId>
<artifactId>sentinel-datasource-nacosartifactId>
dependency>
application.yaml
server:
port: 8401
spring:
application:
name: sentinel-test-service
cloud:
nacos:
discovery:
server-addr: localhost:8848
sentinel:
transport:
# dashboard服务用户访问地址
dashboard: localhost:49157
# dashboard服务其他服务访问端口,默认8719,这里把端口整合了
port: 49158
#actuator
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
限流模式:QPS
参数索引:入参索引,索引从0开始
统计窗口时长+单机阈值:统计窗口时长内,允许多少访问量
参数例外项:需要单独配置的参数,参数在特定参数值下的阈值;前提,参数是基本数据类型或String类型
参数类型:数据类型
参数值:参数值
限流阈值:QPS阈值
响应时间,异常(比例/数量)
RT(Response Time):
异常比例:
异常数:
配置整个系统的规则,所有资源都遵守的规则
LOAD:一般为cpu-cores*2.5;前提,对Linux/Unix-like机器生效
RT:系统入口的平均RT,系统所有资源,单位ms
线程数:系统入口的并发线程数
入口QPS:系统入口的QPS
CPU使用率:[0.0,1.0]
@SentinelResource(value = “customerBlockHandler”,blockHandlerClass = CustomerBlockHandler.class,blockHandler = “handlerException”)
@SentinelResource注解为testHotKey资源配置触发规则异常后的兜底方法dealTestHostKey
Controller类
@RestController
@Slf4j
public class RateLimitController {
@GetMapping("/customerBlockHandler")
//那个类的那个方法为你这个URL或资源兜底
@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");
}
}
@SentinelResource(value = “fallback”, fallbackClass = CustomerFallbackHanlder.class, fallback = “handlerFallback”)
@RestController
public class CircleBreakController {
@GetMapping("/consumer/fallback/{id}")
// value资源名,fallback运行时异常兜底方法,exceptionsToIgnore(fallback不进行兜底的类)
@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);
}
}
@SentinelResource(value = “fallback”, fallback = “handlerFallback”, blockHandler = “blockHandler”, exceptionsToIgnore = {IllegalArgumentException.class})
@RestController
public class CircleBreakController {
@GetMapping("/consumer/fallback/{id}")
// value资源名,fallback运行时异常兜底方法,blockHandler规则异常兜底方法,exceptionsToIgnore(fallback不进行兜底的类)
@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);
}
}
pom.xml
server:
port: 8401
spring:
application:
name: sentinel-test-service
cloud:
nacos:
discovery:
server-addr: localhost:8848
sentinel:
transport:
# dashboard服务用户访问地址
dashboard: localhost:49157
# dashboard服务其他服务访问端口,默认8719,这里把端口整合了
port: 49158
#开启feign对sentinel的支持
feign:
sentinel:
enabled: true
#actuator
management:
endpoints:
web:
exposure:
include: "*"
main
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class OrderMain84 {
public static void main(String[] args) {
SpringApplication.run(OrderMain84.class, args);
}
}
服务调用接口声明+服务降级运行异常
PaymentService
// value服务名称,fallback服务降级类
@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中进行持久化
pom.xml
增加下面的依赖
<dependency>
<groupId>com.alibaba.cspgroupId>
<artifactId>sentinel-datasource-nacosartifactId>
dependency>
application.yaml
server:
port: 8401
spring:
application:
name: sentinel-test-service
cloud:
nacos:
discovery:
server-addr: localhost:8848
sentinel:
transport:
# dashboard服务用户访问地址
dashboard: localhost:49157
# dashboard服务其他服务访问端口,默认8719,这里把端口整合了
port: 49158
datasource: #配置sentinel持久化数据源
ds1:
nacos:
server-addr: localhost:49155
dataId: ${spring.application.name}
groupId: DEFAULT_GROUP
data-type: json
rule-type: flow #流控规则
#actuator
management:
endpoints:
web:
exposure:
include: "*"
main
@SpringBootApplication
@EnableDiscoveryClient
public class SentinelServiceMain8401 {
public static void main(String[] args) {
SpringApplication.run(SentinelServiceMain8401.class, args);
}
}
[
{
"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-排队等待
更多规则