在一些情况下,不需要使用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  , 确实起到了限流作用

 

 

GitHub 加速计划 / sentine / Sentinel
22.24 K
7.98 K
下载
alibaba/Sentinel: Sentinel 是阿里巴巴开源的一款面向分布式服务架构的流量控制、熔断降级组件,提供实时监控、限流、降级和系统保护功能,适用于微服务治理场景。
最近提交(Master分支:2 个月前 )
195150bc * fix issue 2485 which occur oom when using async servlet request. * optimize imports * 1. fix the same issue in the webmvc-v6x 2. improve based on review comments 2 个月前
b78b09d3 2 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐