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

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-starter-alibaba-sentinel是一款用于微服务架构中实现流量控制、熔断降级、系统负载保护等功能的API库。 首先,它可以实现流量控制,通过设置限流规则,对微服务进行限制,避免大量请求进入服务,导致服务不可用或资源耗尽。可以设置QPS、线程数、并发数等限制条件,对请求进行控制,保障系统的稳定性。 其次,它还支持熔断降级功能。当微服务出现异常或超时时,它会根据预设的熔断规则,将服务降级,避免故障的扩散。可以通过设置异常比例、异常数等规则,对服务进行自动降级,保障系统的可用性。 另外,它还支持系统负载保护功能。通过设置系统负载的阈值,当系统负载超过一定限制时,它会自动出发保护机制,拒绝服务请求,保护系统免受过载的影响。可以设置CPU使用率、内存使用率等指标来判断系统负载情况,保持系统的稳定运行。 此外,spring-cloud-starter-alibaba-sentinel还提供了实时监控、统计和报警功能,可以通过可视化的控制台查看服务的运行状态和性能指标,及时发现问题并进行相应的调整和优化。 总之,spring-cloud-starter-alibaba-sentinel是一款功能强大的API库,可以帮助开发人员在微服务架构中实现流量控制、熔断降级、系统负载保护等功能,确保系统的稳定性和可用性。它可以有效地保护系统不受高流量、异常情况和系统负载的影响,提高系统的弹性和可扩展性。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值