1.Sentinel和Hystrix
1.1.限流和熔断
限流 , 限制流量,这里的流量我们可以理解成请求数量,其实就是限制服务器的请求并发数量,为什么要这么做?如果不做限流,那么在大量并发请求下我们的服务器会慢慢的变慢然后顶不住压力而挂掉(类似堵车)。并不是说并发越大越好,有的时候我们的项目规模和业务决定了我们不需要那么大的并发性,当大量的并发请求访问到服务器时我们需要把部分请求拒绝在外,这个是流量限制 - 限流。
1.2.Sentinel介绍
Sentinel诞生于阿里巴巴,其主要目标是流量控制和服务熔断,2018年,Sentinel演变为一个开源项目现如今成为了Spring Cloud Alibaba的一个子项目。Sentinel是通过限制并发线程的数量(即信号隔离)来减少不稳定资源的影响,而不是使用线程池,省去了线程切换的性能开销。
当资源的响应时间变长时,线程将开始被占用。当线程数累积到一定数量时,新的传入请求将被拒绝。反之亦然,当资源恢复并变得稳定时,占用的线程也将被释放,新请求将被接受。
除了限制并发性外,Sentinel可以根据响应时间降级不稳定资源也是保证可靠性的有效方法。当资源的响应时间太大时,将在指定的时间窗口中拒绝所有对该资源的访问。-- 熔断机制
此外,Sentinel支持的熔断降级维度更多,可对多种指标进行流控、熔断,且提供了实时监控和控制面板,功能更为强大。
2.Sentinel限流实战
2.1.服务安装
1.下载jar包,地址: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
3.访问:http://127.0.0.1:1111
账号密码都是sentinel
登陆成功后
2.2.Sentinel 客户端接入
1.导入依赖
<!-- 限流 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
2.配置Sentinel
spring:
cloud:
sentinel:
transport:
dashboard: localhost:1111
3.资源限流
@RefreshScope //刷新配置
@RestController
public class UserController {
@Value("${temp.notify}")
private String notify;
//订单服务调用
@GetMapping("/user/{id}")
//限流降级
@SentinelResource(value="getById",blockHandler="exceptionHandler")
public User getById(@PathVariable Long id){
System.out.println("测试配置notify=" + notify + "host:" + host);
return new User(id,"我是zs");
}
// 限流与阻塞处理 : 参数要和 被降级的方法参数一样
public User exceptionHandler(@PathVariable Long id, BlockException exception) {
exception.printStackTrace();
System.out.println("限流了...");
return new User(-1L,"限流了");
}
}
提示:这里通过@SentinelResource的value属性为资源取名为 “getById” ,后续我们可以根据该资源名来进行限流。
同时这里通过 blockHandler 属性我配置了一个限流降级方法,即当“user”资源触发限流了会调用blockHandler指向的降级方法返回拖地数据,不至于抛出默认的限流异常信息给客户端(一串英文用户也看不懂) ,需要注意的是:降级方法要和被限流的方法参数一致,然后加上 BlockException异常对象。
当然,也可以通过 blockHandlerClass 属性把降级方法写在一个专门的类中,如:
@SentinelResource(value="user",blockHandler="exceptionHandler"
,blockHandlerClass=ExceptionUtil.Class)
降级类
public final class ExceptionUtil {
public static User exceptionHandler(Long id ,lockException ex) {
//...
}
}
2.3.Sentinel也可以直接在在“实时监控”列表中操作限流,设置流控规则和熔断规则
2.4限流测试
通过浏览器频发访问 “user”资源,当QPS大于1就会触发限流,效果如下:
3.Sentinel流控模式
一般项目中使用的都是默认方式,所以这里不做过多了解
4.Gateway使用Sentinel限流
1.导入依赖
<!-- 和gataway使用-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!--整合sentinel-->
<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>
<!-- 配置中心客户端-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- 服务中心 -->
<dependency>
<groupId>com.alibaba.cloud </groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
2.配置Sentinel地址
spring:
cloud:
sentinel:
transport:
dashboard: localhost:1111
3.配置限流规则
启动Gateway,登录sentinel控制台,对url资源进行流控限制,配置方式和前面的配置方式一样。