Alibaba Sentinel spring-cloud-starter-alibaba-sentinel基本使用

本文详细介绍了如何使用Alibaba Sentinel进行流量控制。首先,通过下载并配置Sentinel控制台,接着在Spring Boot项目中引入spring-cloud-starter-alibaba-sentinel依赖。最后,通过实例演示了如何设置QPS限制来实现限流。

1、下载Sentinel控制台

下载地址: https://github.com/alibaba/Sentinel/releases

启动 :  java  -jar   sentinel-dashboard-1.6.3.jar   默认端口为8080

或    java  -Dserver.port=8088  -jar   sentinel-dashboard-1.6.3.jar    指定端口

访问:http://localhost:8080   ,   用户名和密码:sentinel 

.

 

2、springboot pom.xml

使用  com.alibaba.cloud   下的  spring-cloud-starter-alibaba-sentinel

引入:<dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
            <version>2.1.0.RELEASE</version>   对应SpringBoot 2.x
        </dependency>

 

<?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.mmt</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>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>

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

 

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

一个普通的controller


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

@RestController
public class HelloController {

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

 

 

 

3、测试

启动工程后,访问: http://localhost/hello    ,不论怎样刷新都是能正常返回结果的。 查看sentinel控制面板,拒绝QPS始终都是0。

 

开启流量控制:  设置QPS值为1 , 

 

 

再访问: http://localhost/hello   , 快速访问时会出现下图提示

 

sentinel限流起到了作用。 

 

 

 

 

 

 

 

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值