【SpringCloud】Sentinel配置及使用

一、下载最新版Sentinel:

https://github.com/alibaba/Sentinel/releases

 

下载后以Jar包运行。

二、在需要限流的服务端添加配置:

<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> </dependency> <!--version 1.8.3--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>

编辑application.yml,添加配置:

spring: cloud: nacos: discovery: #nacos server server-addr: localhost:8848 sentinel: transport: dashboard: localhost:8080 application: name: provider server: port: 8081 management: endpoints: web: exposure: exclude: '*'

三、在controller 层编写测试服务请求类

package com.example.provider.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class providerController { @Value("${server.port}") private String port; @GetMapping("/index") public String index(){ return this.port; } @GetMapping("/list") public String list(){ return "list"; } }

四、启动服务层,浏览器访问测试类方法,Sentinel默认启动在8080端口。

 

 添加限流规则:

 

 编写测试类测试限流效果:

package com.example.provider; import org.springframework.web.client.RestTemplate; public class test { public static void main(String[] args) throws InterruptedException { RestTemplate restTemplate = new RestTemplate(); for (int i = 0; i < 1000; i++) { restTemplate.getForObject("http://localhost:8081/list",String.class); Thread.sleep(200); } } }

测试前请求返回端口号:

 

 启动测试类,显示限流成功:

 

完毕!

 


__EOF__

本文作者小李不背锅
本文链接https://www.cnblogs.com/lishilin-glut/p/15987514.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   小李不背锅  阅读(720)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示