Sentinel介绍
Sentinel 是面向分布式、多语言异构化服务架构的流量治理组件,主要以流量为切入点,从流量路由、流量控制、流量整形、熔断降级、系统自适应过载保护、热点流量防护等多个维度来帮助开发者保障微服务的稳定性。
下载 Releases · alibaba/Sentinel · GitHub
中文介绍 介绍 · alibaba/Sentinel Wiki · GitHub
Sentinel · alibaba/spring-cloud-alibaba Wiki · GitHub
Sentinel启动:
java -jar sentinel-dashboard-1.8.7.jar
http://localhost:8080 默认账号密码sentinel
maven依赖:
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency>
配置文件appliction.yml:
server:
port: 8401
spring:
application:
name: cloud-alibaba-sentinel-service
cloud:
nacos:
discovery:
server-addr: localhost:8848
sentinel:
transport:
dashboard: localhost:8080 #配置sentinel dashboard控制台服务地址
port: 8719 # 默认端口8719,如果被占用会从8719开始依次+1扫描,直至找到未被占用端口
其他配置项可参考:Sentinel · alibaba/spring-cloud-alibaba Wiki · GitHub
启动类和Controller:
import com.sunxiao.cloud.service.FlowLimitService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.TimeUnit;
@Slf4j
@RestController
public class FlowLimitController {
@Resource
private FlowLimitService flowLimitService;
@GetMapping("/testA")
public String testA() {
return "into ... A ...";
}
@GetMapping("/testB")
public String testB() {
return "into ... B ...";
}
@GetMapping("/testC")
public String testC() {
flowLimitService.common();
return "into ... C ...";
}
@GetMapping("/testD")
public String testD() {
flowLimitService.common();
return "into ... D ...";
}
@GetMapping("/testE")
public String testE() {
log.info("E: {}", System.currentTimeMillis());
return "into ... E ...";
}
@GetMapping("/testF")
public String testF() {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
log.info("熔断测试, 慢调用比例");
return "into ... F ...";
}
@GetMapping("/testG")
public String testG() {
throw new RuntimeException("模拟异常");
}
}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @author sunx
* @date 2024/4/2
*/
@SpringBootApplication
@EnableDiscoveryClient
public class Main8401 {
public static void main(String[] args) {
SpringApplication.run(Main8401.class, args);
}
}
启动后访问接口,查看效果:
因为sentinel默认是懒加载的,需要调用接口后才能看到.
Sentinel流控模式:
-
直接
-
关联
-
链路
Sentinel流控效果:
-
预热
-
排队等待
-
并发线程数
Sentinel熔断规则:
-
慢调用比例
-
异常比例
-
异常数
@SentinelResource注解:
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
public class RateLimitController {
@GetMapping("/rateLimitByUrl")
public String rateLimitByUrl() {
return "限流测试未使用注解";
}
@GetMapping("/rateLimitByResource")
@SentinelResource(value = "ByResource", blockHandler = "byResourceHandler")
public String rateLimitByResource() {
return "限流测试使用注解, 返回指定的字符串";
}
public String byResourceHandler(BlockException blockException) {
return "服务不可用, 这是自定义返回的字符串";
}
@GetMapping("/rateLimitByFallback/{i}")
@SentinelResource(value = "rateLimitByFallback", blockHandler = "byBlockHandler", fallback = "byFallback")
public String rateLimitByFallback(@PathVariable("i") Integer i) {
if (i == 0) {
throw new RuntimeException("i == 0 异常");
}
return "使用注解并使用, Fallback";
}
public String byBlockHandler(@PathVariable("i") Integer i, BlockException blockException) {
log.error("配置了自定义限流, {}", blockException.getMessage());
return "服务不可用, 这是自定义返回的字符串";
}
public String byFallback(@PathVariable("i") Integer i, Throwable throwable) {
log.error("程序逻辑异常, {}", throwable.getMessage());
return "逻辑异常, 这是自定义返回的字符串";
}
@GetMapping("/testHotKey")
@SentinelResource(value = "testHotKey", blockHandler = "testHotKeyBlockHandler")
public String testHotKey(@RequestParam(value = "p1", required = false) String p1,
@RequestParam(value = "p2", required = false) String p2) {
return "testHotKey ...";
}
public String testHotKeyBlockHandler(@RequestParam(value = "p1", required = false) String p1,
@RequestParam(value = "p2", required = false) String p2, BlockException blockException) {
return "testHotKey blockException ...";
}
}
标注在方法上,可以对方法进行限流
-
默认不用的情况下,rest接口+默认的熔断返回结果
-
标注方法上,自定义限流返回
-
标注方法上,自定义限流返回+服务降级处理。该情况下,达到规则条件返回blockHandler方法,方法内异常返回fallback。
Sentinel热点规则
-
针对某个参数,传递该参数时进行限流
-
支持例外项配置,如参数p1=1时不限流,p1等于其他值时限流
Sentinel授权规则
可以对请求方的来源进行判断和控制。具体来说,可以通过白名单和黑名单两种方式对调用方的来源进行控制。
-
自定义处理,获取参数serverName值:
-
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser; import jakarta.servlet.http.HttpServletRequest; import org.springframework.stereotype.Component; @Component public class CustomRequestOriginParser implements RequestOriginParser { @Override public String parseOrigin(HttpServletRequest httpServletRequest) { return httpServletRequest.getParameter("serverName"); } }
- 上述配置表示http://localhost:8401/testA?serverName=x x=1,2以外的访问会被拒绝。
Sentinel规则持久化到nacos
maven依赖:
<dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> </dependency>
application.yml:
spring:
cloud:
sentinel:
datasource: #sentinel持久化配置
ds1: #自定义的key
nacos:
server-addr: localhost:8848 #nacos地址
data-id: ${spring.application.name} #nacos配置中的dataid
group-id: DEFAULT_GROUP #nacos配置中的groupid
data-type: json #nacos配置中的datatype
rule-type: flow # com.alibaba.cloud.sentinel.datasource.RuleType中的 (flow代表流控)
在nacos中设置对应规则后,可以在sentinel控制台中看到:
Sentinel规则持久化有多种方式,更多关于Sentinel规则持久化可以参考【sentinel】Sentinel规则的持久化_sentinel持久化-CSDN博客