Spring-Clould-Alibaba-Sentinel

1.环境准备

父工程pom

<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>com.alibaba.cloud</groupId>
			<artifactId>spring-cloud-alibaba-dependencies</artifactId>
			<version>2.2.1.RELEASE</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>

子工程pom

<dependency>
	<groupId>com.alibaba.cloud</groupId>
	<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

2.Sentinel Dashboard

2.1下载

You can download the latest Sentinel Dashboard jar from the release page.

2.2启动

使用java启动命令,默认端口号8080

java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar

3.应用纳入

3.1服务配置

spring:
  cloud:
    sentinel:
      eager: true
      auth:
        enabled: true
        username: sentinel
        password: sentinel
      transport:
        dashboard: 127.0.0.1:8088

也可以通过添加VM配置启动服务

-Dproject.name=nacos-provider -Dcsp.sentinel.dashboard.server=127.0.0.1:8088

在这里插入图片描述

3.2限流

流量控制规则

Field说明默认值
resource资源名,资源名是限流规则的作用对象
count限流阈值
grade限流阈值类型,QPS 模式(1)或并发线程数模式(0)QPS 模式
limitApp流控针对的调用来源default,代表不区分调用来源
strategy调用关系限流策略:直接、链路、关联根据资源本身(直接)
controlBehavior流控效果(直接拒绝/WarmUp/匀速+排队等待),不支持按调用关系限流直接拒绝
clusterMode是否集群限流

在这里插入图片描述
通过代码进行演示
配置类:

@Configuration
public class FlowConfig {
	
	private String LIMIT_KEY = "QPS1";
	
	// 初始限流流规则
	@PostConstruct
	public void inintFlowQpsRule() {
		List<FlowRule> rules = new ArrayList<FlowRule>();
		FlowRule flowRule = new FlowRule();
		// 限流的资源
		flowRule.setResource(LIMIT_KEY);
		// 限流规则
		flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
		// 设置并发数2(1s只能有2个请求进来)
		flowRule.setCount(2);
		// 应用来源(隔离用)
		flowRule.setLimitApp("default");
		rules.add(flowRule);
		
		FlowRuleManager.loadRules(rules);
	}

}

服务:

@Override
// @SentinelResource(value = "getInfo")
public String getInfo() {
	Entry entry = null;
	try {
		// sentinel获取许可的工具类
		entry = SphU.entry("QPS1");
	} catch (BlockException e) {
		log.warn("当前访问人数过多,请稍后再试");
		return "当前访问人数过多,请稍后再试";
	} finally {
		if (entry != null) {
			entry.exit();
		}
	}
	return "return info success";
}

3.3降级

熔断降级规则

Field说明默认值
resource资源名,即规则的作用对象
count阈值
grade熔断策略,支持秒级 RT/秒级异常比例/分钟级异常数秒级平均 RT
timeWindow降级的时间,单位为 s
rtSlowRequestAmountRT 模式下 1 秒内连续多少个请求的平均 RT 超出阈值方可触发熔断(1.7.0 引入)5
minRequestAmount异常熔断的触发最小请求数,请求数小于该值时即使异常比率超出阈值也不会熔断(1.7.0 引入)5

在这里插入图片描述
服务响应超过1000m后在接下来的3s内进行熔断

在这里插入图片描述
通过下图可进行查看

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值