一:Sentienl 简介
Sentinel诞生于阿里巴巴,其主要目标是流量控制和服务熔断
,2018年,Sentinel演变为一个开源项目现如今成为了Spring Cloud Alibaba的一个子项目。Sentinel是通过限制并发线程的数量来减少不稳定资源的影响
,而不是使用线程池,省去了线程切换的性能开销。
当资源的响应时间变长时,线程将开始被占用。当线程数累积到一定数量时,新的传入请求将被拒绝
。反之亦然,当资源恢复并变得稳定时,占用的线程也将被释放,新请求将被接受。
除了限制并发性外,Sentinel可以根据响应时间降级
不稳定资源也是保证可靠性的有效方法。当资源的响应时间太大时,将在指定的时间窗口中拒绝所有对该资源的访问
。-- 熔断机制
此外,Sentinel支持的熔断降级维度更多,可对多种指标进行流控、熔断,且提供了实时监控和控制面板,功能更为强大。
二: 使用
1. 下载客户端
https://github.com/alibaba/Sentinel/releases/download/1.6.0/sentinel-dashboard-1.6.0.jar
2.启动
java -jar -Dserver.port=1111 sentinel-dashboard-1.6.0.jar
启动参数
-
-Dsentinel.dashboard.auth.username=sentinel
: 用于指定控制台的登录用户名为 sentinel; -
-Dsentinel.dashboard.auth.password=123456
: 用于指定控制台的登录密码为 123456;如果省略这两个参数,默认用户和密码均为 sentinel -
-Dserver.servlet.session.timeout=7200
: 用于指定 Spring Boot 服务端 session 的过期时间,如 7200 表示 7200 秒;60m 表示 60 分钟,默认为 30 分钟; -
-Dserver.port=1111
:配置端口
3. Sentinel 客户端接入
1. user服务导入依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
2.配置Sentinel
spring:
cloud:
sentinel:
transport:
dashboard: localhost:1111
3. 资源限流方法
user接口的方法上加注解 @SentinelResource指定服务名 限流方法 降级方法(对应上)
@SentinelResource(value = "getUserById",blockHandler ="getUserByIdBlockHandler",fallback = "getUserByIdFallback")
@RefreshScope //刷新配置
@RestController
public class UserController {
@Value("${server.port}")
private String port;
@Value("${spring.redis.port}")
private String redisPort;
@SentinelResource(value = "getUserById",blockHandler ="getUserByIdBlockHandler",fallback = "getUserByIdFallback")
@GetMapping("/user/{id}")
public User getUserById(@PathVariable("id") long id){
return new User(id,"","user端口"+port+";redis端口"+redisPort);
}
// 限流方法
public User getUserByIdBlockHandler(@PathVariable long id, BlockException exception){
return new User(-1,"","服务器繁忙,请稍后重试!"+ exception);
}
// 降级方法
public User getUserByIdFallback(@PathVariable long id){
return new User(-1,"","服务器异常熔断了");
}
}
4. Sentinel设置限流策略
http://localhost:1111 进入 nocas 服务端 访问几次user服务 就可以在nocas客户端
通过“流控”按钮设置流控规则 (每秒钟访问次数小于2次)
5. 测试
http://localhost:1001/user/1 Qps超过设置数量就会被限流 走限流方法
// 限流方法
public User getUserByIdBlockHandler(@PathVariable long id, BlockException exception){
return new User(-1,"","服务器繁忙,请稍后重试!"+ exception);
}
6. Sentinel流控模式
可以配置不同的流控模式达到不同的流控效果
7.Gateway使用Sentinel限流
Spring Cloud Gateway 作为微服务的网关,它是微服务的访问入口,当请求的流量洪峰到来我们可以在Gateway网关层通过Sentinel对请求进行流控,把好第一道关。
1.导入sentinel基础依赖和 sentinel-gateway 整合依赖
<!-- 限流和gataway使用-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
2.配置Sentinel地址
spring:
application:
name: gateway-server
cloud:
sentinel:
transport:
dashboard: localhost:1111
3.配置限流规则
在sentinel客户端配置流控规则
4. 配置类
被网关getaway限流的接口都会走这
@Configuration
public class SentinelConfig {
public SentinelConfig(){
GatewayCallbackManager.setBlockHandler(new BlockRequestHandler() {
@Override
public Mono<ServerResponse> handleRequest(ServerWebExchange serverWebExchange, Throwable throwable) {
return ServerResponse.ok().body(Mono.just("限流啦,请求太频繁"),String.class);
}
});
}
}
8. sentinel 限流规则持久化到nacos
1.第一步:导入基础依赖
<!--Sentinel和Nacos做持久的-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<version>1.5.2</version>
</dependency>
2. 第二步:配置持久化nacos地址
server:
port: 1001 #端口
spring:
application:
name: service-user #服务名
cloud:
sentinel:
transport:
dashboard: localhost:1111
datasource:
flow:
nacos: #限流持久配置
server-addr: localhost:8848 #使用nacos的持久
dataId: application-user-flow-dev #获取限流的数据源的dataId
groupId: DEFAULT_GROUP
rule-type: flow #类型:限流
nacos:
discovery:
server-addr: localhost:8848 #注册中心地址
3. 在nacos添加配置 application-user-flow-dev json格式的配置规则
[
{
"resource": "getUserById",
"limitApp": "default",
"grade": 1,
"count": 2,
"strategy": 0,
"controlBehavior": 0,
"clusterMode": false
}
]
4. 测试 http://localhost:1003/service/user/user/1
9. Sentinel 熔断规则持久化到Nacos
1.第一步:在Nacos配置列表增加文件 如:application-user-degrade-dev ,json格式 内容如下
[
{
"resource": "getUserById",
"grade": 0,
"count": 10,
"timeWindow": 5
}
]
2.yaml
degrade: #降级的配置
nacos:
server-addr: 127.0.0.1:8848
dataId: application-user-degrade-dev
groupId: DEFAULT_GROUP
rule-type: degrade
3. 测试 http://localhost:1003/service/user/user/1
10. Sentinel熔断、
1. Sentinel熔断介绍
Sentinel的服务熔断机制会对调用链上的某个不稳定(宕机,异常,超时)的资源,做出请求限制,快速失败,避免影响到其它的服务而导致级联错误
。资源熔断后,在后续的一定时间(时间窗口)之内,对该服务的请求都自动熔断,抛出 DegradeException异常。
Sentinel拥有比Hystrix更强大和丰富的功能,能满足我们的各种应用场景,并且经历过淘宝双十一的考验,是微服务架构中熔断机制的不二选择。
2.Sentnel熔断实战
user服务接口使用@SentinelResource指定降级方法
@SentinelResource(value = "getUserById",blockHandler ="getUserByIdBlockHandler",fallback = "getUserByIdFallback")
// 降级方法
public User getUserByIdFallback(@PathVariable long id){
return new User(-1,"","服务器异常熔断了");
}
配置user降级策略
测试熔断 http://localhost:1001/user/1
3.降级策略
平均响应RT
异常比例
异常数
4.Feign整合Sentinel熔断
1.OpenFeign开启Sentinel进行服务熔断降级。
feign:
sentinel:
enabled: true #熔断
2..给Feign接口降级
// 配置服务名 - 通过服务名调用用户服务下面的 几个实例
@FeignClient(value = "service-user",fallback = UserClientFallback.class)
public interface UserFeignClient {
@GetMapping("/user/{id}")
User getUserById(@PathVariable("id") long id);
}
3..编写降级类
@Component
public class UserClientFallback implements UserFeignClient{
@Override
public User getUserById(long id) {
return new User(-1l," ","降级了");
}
}