前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >用Sentinel实现项目的直接限流

用Sentinel实现项目的直接限流

作者头像
星哥玩云
发布2022-09-15 14:39:10
2890
发布2022-09-15 14:39:10
举报
文章被收录于专栏:开源部署

1、使用Spring Initializr创建Spring Boot 工程

Pom.xml:

代码语言:javascript
复制
<?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 https://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.2.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.tyschool</groupId>
    <artifactId>sentinel-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>sentinel-demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
       <!--Sentinel依赖-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <!--actuator依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </pluginRepository>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

</project>

2、添加配置

在application.properties文件中配置:

代码语言:javascript
复制
spring.application.name=Sentinel Demo
server.port=8088
# sentinel dashboard 指定sentinel 控制台地址
spring.cloud.sentinel.transport.dashboard=localhost:8080
# 暴露出所有actuator监控的端点
management.endpoints.web.exposure.include=*

3、限流埋点

限流埋点可以通过HTTP埋点和自定义埋点两种方式实现。

3.1、HTTP埋点

Sentinel的Starter默认为所有的HTTP服务提供了限流埋点。如果只想对某个HTTP服务进行限流,则只需要引入依赖,无须修改代码

3.2、自定义埋点

如果需要对某个特定的服务进行限流或降级,则可以通过注解@SentienlResource来完成

3.3、添加自定义埋点

代码语言:javascript
复制
@SpringBootApplication
public class SentinelDemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(SentinelDemoApplication.class, args);
	}

	@RestController
	public class TestController{

		@GetMapping("/hello")
		@SentinelResource("hello")
		public String hello(){
			return "Hello Sentinel Demo!";
		}
	}
}

4、创建快速失败的直接限流模式

image20200526004213965.png
image20200526004213965.png

5、测试快速访问失败的直接限流模式

多次快速访问 http://localhost:8088/hello,当QPS超过2时,直接返回页面错误

image20200526004832622.png
image20200526004832622.png
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、使用Spring Initializr创建Spring Boot 工程
  • 2、添加配置
  • 3、限流埋点
    • 3.1、HTTP埋点
      • 3.2、自定义埋点
        • 3.3、添加自定义埋点
        • 4、创建快速失败的直接限流模式
        • 5、测试快速访问失败的直接限流模式
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档