视频:https://www.bilibili.com/video/BV12A411E7aX?from=search&seid=14060820702260122906
笔记:https://blog.csdn.net/m_awdawdw/article/details/109023535
---01---02---
对比:
访问这个资源得线程堆积到了一定得数量实现得隔离。
---03---
---04---
开通阿里云得AHAS控制台:
阿里云:https://www.aliyun.com/?spm=5176.12901015.0.i12901015.34ad525cj2YYlA
---05---
第一步:下载公网demo
第二步:启动公网得demo,直接就接入到阿里云得控制台了。
第三步:查看
第四步:点击监控详情
如何设置监控规则呢?
---06---
实际工作中:
package com.sentinel.hello;
import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
/**
* @Author fandayong
* @Date 2021/3/30 12:16 PM
* @description
*/
@RestController
public class TestController {
@GetMapping("/hello")
public String hello(){
try(Entry entry = SphU.entry("Hello")) {
return "Hello Sentinel";
} catch (Exception e) {
e.printStackTrace();
}
return "系统繁忙,请稍后";
}
/** 定义限流规则
* @PostConstruct 此注解的含义是:本类构造方法执行结束后执行
*/
@PostConstruct
public void init(){
//1.创建存放限流规则的集合
List<FlowRule> rules = new ArrayList<>();
//2.创建限流规则
FlowRule rule = new FlowRule();
//定义资源,表示Sentinel会对哪个资源生效
rule.setResource("Hello");
//定义限流的类型(此处使用QPS作为限流类型)
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
//定义QPS每秒通过的请求数
rule.setCount(2);
//3.将限流规则存放到集合中
rules.add(rule);
//4.加载限流规则
FlowRuleManager.loadRules(rules);
}
}
---07---
sentinel控制台:java -Dserver.port=9000 -jar sentinel-dashboard-1.7.0.jar
登录:localhost:9000
将本地的应用接入到控制台:
两个参数
---08---
第一种设置:写死代码
第二种设置:本地控制台动态的设置。
---09--10---
1.抛出异常
2.返回布尔值
@GetMapping("/boolean")
public boolean returnBoolean(){
if (SphO.entry("Sentinel-boolean")){
try {
System.out.println("Hello Sentinel");
return true;
}finally {
SphO.exit();//限流的出口
}
}else {
//限流后进行的操作
System.out.println("系统繁忙,请稍后再试");
return false;
}
}
---11---
支持异步调用的。
@RestController
public class AsyncController {
@Autowired
private AsyncService asyncService;
@GetMapping("/async")
public String async(){
//1.进行限流控制
AsyncEntry asyncEntry = null;
try {
asyncEntry = SphU.asyncEntry("Sentinel_Async"); //限流入口
asyncService.hello(); //异步调用方法
System.out.println("异步测试");
} catch (BlockException e) {
e.printStackTrace();
System.out.println("系统繁忙请稍后再试");
} finally {
if (asyncEntry != null){
asyncEntry.exit(); //限流出口
}
}
return "123";
}
}
---12---
注解:
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-annotation-aspectj</artifactId>
<version>1.8.0</version>
</dependency>
配置限流:
// value代表资源名称 blockHandler:设置限流或降级处理的类
@SentinelResource(value = "Sentinel_Ann",blockHandler = "exceptionHandler")
@GetMapping("/ann")
public String ann(){
//使用限流规则
return "Hello Sentinel";
}
public String exceptionHandler(BlockException e){
e.printStackTrace();
return "系统繁忙请稍后再试";
}
---13---
SpringCloud整合sentinel,之前我们在jvm参数配置了很多的参数,这次我们直接在yaml中配置了。
# 设置应用的名称
spring:
application:
name: springCloudSentinel123
cloud:
sentinel:
transport:
dashboard: localhost:8718 #设置Sentinel控制台的主机地址和端口号
---14---
sentinel整合feign
需要feign eureka。
第0步:其他的不变。
第一步:配置sentinel对feign的支持
第二步:新建Service
其中value是微服务的名称。
设置:
之前的资源名:
名字的命名规范:
http请求方式:协议://服务名/请求路径和参数
---15---
第一步:创建网关微服务
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
第二步:网关的配置
测试:
---
第三步:
@Component
public class GatewayConfiguration {
@PostConstruct
public void doInit(){
//设置限流或降级的回调函数
GatewayCallbackManager.setBlockHandler(new BlockRequestHandler() {
@Override
public Mono<ServerResponse> handleRequest(ServerWebExchange serverWebExchange, java.lang.Throwable throwable) {
return ServerResponse.status(200).syncBody("系统繁忙请稍后");
}
});
}
}
第四步:
spring:
cloud:
sentinel:
transport:
dashboard: 127.0.0.1:9000
第五步:设置两种维度的限流规则:
第一个维度:
路由信息的id:
第二个维度:
api管理,新增api分组。
1.随意
2.路由断言的设置的
---16---
规则功能:
resource是资源名称。
可以设置多个限流规则给一个资源的。
---17---
这个是熔断策略的-grade
rtSlow.....:设置一秒连续多少个请求这个操作的。
本地代码手动配置
---18---
---19---
---20---
---21---
---22---