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

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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限流起到了作用。 

 

 

 

 

 

 

 

 

 

### Spring Cloud Alibaba Sentinel Gateway 使用指南 #### 一、简介 SpringCloud Alibaba系列提供了丰富的组件来支持微服务架构下的开发工作,其中网关Sentinel流控降级API管理是重要组成部分之一[^1]。通过集成这些工具,可以实现对请求流量的有效控制和服务熔断保护。 #### 二、环境准备 在使用Gateway前需搭建好基于Spring Cloud的微服务体系,并选用合适的注册中心如Nacos完成服务发现与配置管理等功能[^4]。对于想要快速上手的朋友来说,可以从官方提供的资源链接获取最新版本软件并按照说明文档操作安装部署[^5]。 #### 三、依赖设置 为了使项目能够顺利运行,在POM文件中加入必要的库引用: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <!-- 添加sentinel的支持 --> <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-core</artifactId> </dependency> ``` 以上代码片段展示了如何向Maven工程添加所需的Java包以启用相应的特性和功能模块。 #### 四、核心概念解析 - **动态路由**: 支持根据条件自动匹配路径映射关系,简化了URL转发逻辑的设计过程;同时允许外部化配置规则以便于维护更新[^2]。 - **数据结构选择**: 在某些场景下会采用`TreeMap<String, Object>`形式保存参数列表,利用字符串忽略大小写的比较器(`String.CASE_INSENSITIVE_ORDER`)确保键名唯一性的同时提高了查找效率[^3]。 #### 五、具体实践案例分享 下面给出一段简单的示例程序用于展示怎样结合上述提到的技术要点来进行实际项目的编码实现: ```java import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; @Component public class CustomFilter { @Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder){ return builder.routes() .route(r -> r.path("/foo/**") .filters(f->f.addRequestHeader("X-Test","Foo-Bar")) .uri("http://example.org")) .build(); } } ``` 这段代码定义了一个过滤器类CustomFilter并通过Bean注入的方式创建了一条通往指定目标站点(example.org)的新路由规则,当访问带有/foo开头的子目录时将会附加额外HTTP头信息(X-Test: Foo-Bar)。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值