Sentinel使用配置文件配置限流规则

在一些情况下,不需要使用nacos等其他组件保存限流规则,且限流规则一般不改变。就可以使用配置文件的方式配置限流规则了。

限流参数解释: 对应的值可以在com.alibaba.csp.sentinel.slots.block.RuleConstant.java中查到

Field说明默认值
resource资源名,资源名是限流规则的作用对象 
count限流阈值 
grade限流阈值类型,QPS 或线程数模式QPS 模式
limitApp流控针对的调用来源default,代表不区分调用来源
strategy判断的根据是资源自身,还是根据其它关联资源 (refResource),还是根据链路入口根据资源本身
controlBehavior流控效果(直接拒绝 / 排队等待 / 慢启动模式)直接拒绝

同一个资源可以同时有多个限流规则。

public final class RuleConstant {

    public static final int FLOW_GRADE_THREAD = 0; //限流 基于线程数 
    public static final int FLOW_GRADE_QPS = 1; //限流 基于QPS

    public static final int DEGRADE_GRADE_RT = 0; //降级  , 代表一秒内该资源的平均响应时间 
    /**
     * Degrade by biz exception ratio in the current {@link IntervalProperty#INTERVAL} second(s).
     */
    public static final int DEGRADE_GRADE_EXCEPTION_RATIO = 1; // 降级 异常比例
    /**
     * Degrade by biz exception count in the last 60 seconds.
     */
    public static final int DEGRADE_GRADE_EXCEPTION_COUNT = 2;// 降级, 异常数

    public static final int AUTHORITY_WHITE = 0;// 认证, 白名单
    public static final int AUTHORITY_BLACK = 1;// 认证, 黑名单

    public static final int STRATEGY_DIRECT = 0; //
    public static final int STRATEGY_RELATE = 1;
    public static final int STRATEGY_CHAIN = 2;

    public static final int CONTROL_BEHAVIOR_DEFAULT = 0;// 限流行为,直接拒绝
    public static final int CONTROL_BEHAVIOR_WARM_UP = 1;// 限流行为,WARM_UP 
    public static final int CONTROL_BEHAVIOR_RATE_LIMITER = 2;// 限流行为,匀速排队
    public static final int CONTROL_BEHAVIOR_WARM_UP_RATE_LIMITER = 3;

    public static final String LIMIT_APP_DEFAULT = "default";
    public static final String LIMIT_APP_OTHER = "other";

    public static final int DEFAULT_SAMPLE_COUNT = 2;
    public static final int DEFAULT_WINDOW_INTERVAL_MS = 1000;

    private RuleConstant() {}
}

 

 

 

0、依赖包

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.6.RELEASE</version>
		<relativePath /> 
	</parent>
	<groupId>com.mei</groupId>
	<artifactId>SpringBootSentinel</artifactId>
	<version>0.01</version>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
			<exclusions>
				<exclusion>
					<groupId>org.springframework.boot</groupId>
					<artifactId>spring-boot-starter-logging</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-log4j</artifactId>
			<version>1.3.8.RELEASE</version>
		</dependency>
		<!--end springboot 整合 log4j -->

		<!-- spring-cloud-starter-alibaba-sentinel -->
		<dependency>
			<groupId>com.alibaba.cloud</groupId>
			<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
			<version>2.1.0.RELEASE</version>
		</dependency>


		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

 

1、开启注解

package com.mei.config;

import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AopConfiguration {

    @Bean
    public SentinelResourceAspect sentinelResourceAspect() {
        return new SentinelResourceAspect();
    }
}

2、资源

package com.mei.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;

@RestController
public class HelloController {
	
	@RequestMapping("/helloworld3")
	@SentinelResource(value="helloworld3",blockHandler="helloworld3Handler")
	public String helloworld3() {
		
		return "hello Sentinel333 !" ;
	}
	
	public static String helloworld3Handler(BlockException ex) {

		System.out.println("Oops: " + ex.getClass().getCanonicalName());
		return "系统限流了111....";
	}
	
}

 

3、application.properties

spring.application.name=sentinel-test
server.port=8081
management.endpoints.web.exposure.include=*

spring.cloud.sentinel.transport.dashboard=localhost:8080
spring.cloud.sentinel.eager=true

spring.cloud.sentinel.datasource.ds1.file.file=classpath: flowrule.json
spring.cloud.sentinel.datasource.ds1.file.data-type=json
spring.cloud.sentinel.datasource.ds1.file.rule-type=flow

#spring.cloud.sentinel.datasource.ds2.file.file=classpath: degraderule.json
#spring.cloud.sentinel.datasource.ds2.file.data-type=json
#spring.cloud.sentinel.datasource.ds2.file.rule-type=degrade

 

4、配置限流规则

[
  {
    "resource": "helloworld3",
    "controlBehavior": 2,
    "count": 5,
    "grade": 1,
    "limitApp": "default",
    "strategy": 0
  }
]

 

 

4、启动应用,启动sentinel控制台

登录sentintel控制台,发现限流规则已经存在了。

 

 

快速访问测试, http://localhost:8081/helloworld3  , 确实起到了限流作用

 

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值