[Sentinel 一]使用Sentinel实现接口限流

927 阅读1分钟

环境

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.2.5.RELEASE</version>
	<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-dependencies</artifactId>
			<version>Hoxton.SR3</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
		<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>

服务端 Sentinel-dashboard

  1. 下载最新Sentinel-dashboard jar包

github.com/alibaba/Sen…

  1. 运行jar包
java -jar sentinel-dashboard-1.7.1.jar -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard
  1. 浏览器访问
    http://localhost:8080
    默认账号密码sentinel/sentinel
    image

客户端

引入依赖

<dependency>
   <groupId>com.alibaba.cloud</groupId>
   <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
  1. postmancurl 调用下程序内接口,在 sentinel dashboard 中可以看到实时监控

  2. 配置限流规则
    点击 簇点链路选择接口点击流控

    image

如图配置,实现效果(限流策略):每秒最多允许2个请求进入  
阈值类型:QPS  
单机阈值:2  

接口测试,快速调用第三次被sentinel限流

image