Alibaba Sentinel 使用nacos持久化 限流配置

使用spring-cloud-starter-alibaba-sentinel完成基本限流控制很容易,具体参考https://blog.csdn.net/u013792404/article/details/98874930

但是重启sentinel-dashboard后,配置的规则就没有了,需要重新配置。本文使用nacos保存sentinel配置。nacos启动参考:

https://blog.csdn.net/u013792404/article/details/98479190

 

启动Sentinel-dashborad:   访问http://localhost:8080

启动Nacos-Server :   访问http://localhost:8848

 

pom.xml配置 ,使用 spring-cloud-starter-alibaba-sentinel :2.1.0.RELEASE(groupId是com.alibaba.cloud,好像是不久前Springcloud-alibaba项目代码迁移到alibaba了) 其中依赖 sentinel 1.6.3  , 所以sentinel-datasource-nacos也使用1.6.3了

<?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 http://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 /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.mei</groupId>
	<artifactId>SpringBootWeb</artifactId>
	<version>0.01</version>
	<name>SpringBootWeb</name>

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

	<dependencies>

		<!-- 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>com.alibaba.csp</groupId>
			<artifactId>sentinel-datasource-nacos</artifactId>
			<version>1.6.3</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>

application.properties

#====================================================
spring.application.name: SpringBootWeb
#server
server.port=80
server.servlet.context-path=/


#sentinel控制台
spring.cloud.sentinel.transport.dashboard=localhost:8080
spring.cloud.sentinel.eager=true

#sentinel数据源连接nacos
spring.cloud.sentinel.datasource.ds.nacos.server-addr=localhost:8848
spring.cloud.sentinel.datasource.ds.nacos.dataId=${spring.application.name}-sentinel
spring.cloud.sentinel.datasource.ds.nacos.groupId=DEFAULT_GROUP
spring.cloud.sentinel.datasource.ds.nacos.ruleType=flow

关于配置解释:

spring.cloud.sentinel.transport.dashboard=localhost:8080  # sentinel后台访问地址
spring.cloud.sentinel.eager=true

#sentinel数据源连接nacos
spring.cloud.sentinel.datasource.ds.nacos.server-addr=localhost:8848  #nacos后台访问体质
spring.cloud.sentinel.datasource.ds.nacos.dataId=${spring.application.name}-sentinel   #nacos存储使用到的dataId
spring.cloud.sentinel.datasource.ds.nacos.groupId=DEFAULT_GROUP    #nacos存储使用到的groupId
spring.cloud.sentinel.datasource.ds.nacos.ruleType=flow    #限流规则 

不同版本可能配置会不同, 本人使用时一直提示 spring.cloud.sentinel.datasource.ds.nacos.ruleType不能为空,配置后有报错提示:Type 'com.alibaba.cloud.sentinel.datasource.config.NacosDataSourceProperties' has no property 'rule-type' , 以为哪里配置错了或版本不对, 最后发现忽略即可, 项目可正常启动。 

点击上面的配置key,即可查看相关类。主要以下几个类

com.alibaba.cloud.sentinel.SentinelProperties.java

com.alibaba.cloud.sentinel.datasource.config.DataSourcePropertiesConfiguration.java

com.alibaba.cloud.sentinel.datasource.config.NacosDataSourceProperties.java

com.alibaba.cloud.sentinel.datasource.config.AbstractDataSourceProperties.java

com.alibaba.cloud.sentinel.datasource.RuleType.java

一个普通的controller

@RestController
public class HelloController {

	@RequestMapping("/helloworld")
	public String helloworld() {
		return "hello Sentinel !" ;
	}
}

项目正常启动。

 

在nacos添加限流配置:

其中DataID / Group  需要和application.properties中的一致。 

配置内容:

[
    {
        "resource": "/helloworld",
        "limitApp": "default",
        "grade": 1,
        "count": 2,
        "strategy": 0,
        "controlBehavior": 0,
        "clusterMode": false
    }
]

resource : 资源名,限流规则作用对象,一般为请求URI

limitApp : 控流针对的调用来源,default则不区分调用来源

grade :  限流阈值类型;0表示根据并发量来限流,1表示根据QPS来进行限流

count :  限流阈值

strategy : 调用关系限流策略

controlBehavior : 限流控制行为(快速失败 、warm up 、排队等候)

clusterMode : 是否为集群模式 

这写也可以在Sentinel后台中看到

 

快速访问应用:localhost/helloworld   ,   限流配置起到了作用

 

查看Sentinel后台:发现流控规则下面多了一条记录

 

把项目、nacos、sentinel都重启后,限流仍起作用, 说明nacos持久化 限流配置起到了作用。 

 

1、在sentinel后台修改的限流规则后不能推送到nacos

2、修改后以sentinel中的限流值为准。

3、如果在sentinel中删除了限流规则,限流则不起作用了(不知道nacos会不会隔一段时间推送数据或者nacos-client隔一段时间拉取数据,这块暂时没去了解)。可以在nacos后台修改配置,重新发布,发布的时候会将配置重新推送到应用,限流规则就起作用了,sentinel后台也可以看到。 

 

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值