springcloud整合Sentinel实现接口限流

springcloud整合Sentinel实现接口限流

1.项目目录:

2.代码实现:

创建项目sentinel

添加依赖

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.6.2</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.cxh</groupId>
	<artifactId>sentinel</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>sentinel</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>8</java.version>
		<spring-cloud-alibaba-dependencies.version>2021.1</spring-cloud-alibaba-dependencies.version>
		<spring-cloud-dependencies.version>2021.0.0</spring-cloud-dependencies.version>
	</properties>

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

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
			<version>0.9.0.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.18.2</version>
			<optional>true</optional>
		</dependency>
	</dependencies>

yml配置

spring:
  application:
    name: sentinel

  cloud:
    sentinel:
      transport:
        dashboard: localhost:8080  #sentinel-dashboard的运行地址

server:
  port: 8001

控制层

@RestController
public class SentinelController {

    @Value("${server.port}")
    private String port;

    @GetMapping("/index")
    public String index() {
        return "index:" + port;
    }
}

3.实现效果:

先下载sentinel-dashboard.jar,命令运行

java -jar sentinel-dashboard-1.6.0.jar

浏览器打开:

http://localhost:8080/ 使用默认用户密码:sentinel进行登录

再运行项目sentinel,打开浏览器请求http://localhost:8001/index,刷新sentinel控制台就有sentinel项目的控制了。

点击簇点链路,流控按钮添加限流规则

新增后

快速多次 请求http://localhost:8001/index 出现:

Blocked by Sentinel (flow limiting)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

灰太狼_cxh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值