- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Spring Cloud Alibaba使用Sentinel实现接口限流由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
最近管点闲事浪费了不少时间,感谢网友 libinwalan 的留言提醒。及时纠正路线,继续跟大家一起学习spring cloud alibaba.
nacos作为注册中心和配置中心的基础教程,到这里先告一段落,后续与其他结合的内容等讲到的时候再一起拿出来说,不然内容会有点跳跃。接下来我们就来一起学习一下spring cloud alibaba下的另外一个重要组件:sentinel.
sentinel是什么 。
sentinel的官方标题是:分布式系统的流量防卫兵。从名字上来看,很容易就能猜到它是用来作服务稳定性保障的。对于服务稳定性保障组件,如果熟悉spring cloud的用户,第一反应应该就是hystrix。但是比较可惜的是netflix已经宣布对hystrix停止更新。那么,在未来我们还有什么更好的选择呢?除了spring cloud官方推荐的resilience4j之外,目前spring cloud alibaba下整合的sentinel也是用户可以重点考察和选型的目标.
sentinel的功能和细节比较多,一篇内容很难介绍完整。所以下面我会分多篇来一一介绍sentinel的重要功能。本文就先从限流入手,说说如何把sentinel整合到spring cloud应用中,以及如何使用sentinel dashboard来配置限流规则。通过这个简单的例子,先将这一套基础配置搭建起来.
使用sentinel实现接口限流 。
sentinel的使用分为两部分:
下面我们就分两部分来看看,如何使用sentienl来实现接口限流.
部署sentinel dashboard 。
本文采用的spring cloud alibaba版本是0.2.1,可以查看依赖发现当前版本使用的是sentinel 1.4.0。为了顺利完成本文的内容,建议挑选同版本的sentinel dashboard来使用是最稳妥的.
下载地址: sentinel-dashboard-1.4.0.jar 。
其他版本: sentinel/releases 。
同以往的spring cloud教程一样,这里也不推荐大家跨版本使用,不然可能会出现各种各样的问题.
通过命令启动:
1
|
java -jar sentinel-dashboard-
1.4
.
0
.jar
|
sentinel-dashboard不像nacos的服务端那样提供了外置的配置文件,比较容易修改参数。不过不要紧,由于sentinel-dashboard是一个标准的spring boot应用,所以如果要自定义端口号等内容的话,可以通过在启动命令中增加参数来调整,比如: -dserver.port=8888 .
默认情况下,sentinel-dashboard以8080端口启动,所以可以通过访问: localhost:8080 来验证是否已经启动成功,如果一切顺利的话,可以看到如下页面:
整合sentinel 。
第一步:在spring cloud应用的 pom.xml 中引入spring cloud alibaba的sentinel模块:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<dependencies>
<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>
</dependency>
<dependency>
<groupid>org.projectlombok</groupid>
<artifactid>lombok</artifactid>
<version>
1.18
.
2
</version>
<optional>
true
</optional>
</dependency>
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-test</artifactid>
<scope>test</scope>
</dependency>
</dependencies>
|
第二步:在spring cloud应用中通过 spring.cloud.sentinel.transport.dashboard 参数配置sentinel dashboard的访问地址,比如:
1
2
3
4
5
|
spring.application.name=alibaba-sentinel-rate-limiting
server.port=
8001
# sentinel dashboard
spring.cloud.sentinel.transport.dashboard=localhost:
8080
|
第三步:创建应用主类,并提供一个rest接口,比如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
@springbootapplication
public
class
testapplication {
public
static
void
main(string[] args) {
springapplication.run(testapplication.
class
, args);
}
@slf4j
@restcontroller
static
class
testcontroller {
@getmapping
(
"/hello"
)
public
string hello() {
return
"didispace.com"
;
}
}
}
|
第四步:启动应用,然后通过postman或者curl访问几下 localhost:8001/hello 接口.
1
2
|
$ curl localhost:
8001
/hello
didispace.com
|
此时,在上一节启动的sentinel dashboard中就可以当前我们启动的 alibaba-sentinel-rate-limiting 这个服务以及接口调用的实时监控了。具体如下图所示:
配置限流规则 。
在完成了上面的两节之后,我们在 alibaba-sentinel-rate-limiting 服务下,点击 簇点链路 菜单,可以看到如下界面:
其中 /hello 接口,就是我们上一节中实现并调用过的接口。通过点击 流控 按钮,来为该接口设置限流规则,比如:
这里做一个最简单的配置:
综合起来的配置效果就是,该接口的限流策略是每秒最多允许2个请求进入.
点击 新增 按钮之后,可以看到如下界面:
其实就是左侧菜单中 流控规则 的界面,这里可以看到当前设置的所有限流策略.
验证限流规则 。
在完成了上面所有内容之后,我们可以尝试一下快速的调用这个接口,看看是否会触发限流控制,比如:
1
2
3
4
5
6
|
$ curl localhost:
8001
/hello
didispace.com
$ curl localhost:
8001
/hello
didispace.com
$ curl localhost:
8001
/hello
blocked by sentinel (flow limiting)
|
可以看到,快速的调用两次 /hello 接口之后,第三次调用被限流了.
代码示例 。
本文介绍内容的客户端代码,示例读者可以通过查看下面仓库中的 alibaba-sentinel-rate-limiting 项目:
github:https://github.com/dyc87112/springcloud-learning/ 。
gitee: https://gitee.com/didispace/springcloud-learning/ 。
参考资料 。
下面是sentinel的仓库地址与官方文档,读者也可以自己查阅文档学习:
github sentinel官方文档 spring cloud alibaba sentinel文档 。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.
原文链接:http://blog.didispace.com/spring-cloud-alibaba-sentinel-1/ 。
最后此篇关于Spring Cloud Alibaba使用Sentinel实现接口限流的文章就讲到这里了,如果你想了解更多关于Spring Cloud Alibaba使用Sentinel实现接口限流的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我正在尝试在我的代码库中为我正在编写的游戏服务器更多地使用接口(interface),并了解高级概念以及何时应该使用接口(interface)(我认为)。在我的例子中,我使用它们将我的包相互分离,并使
我有一个名为 Widget 的接口(interface),它在我的整个项目中都在使用。但是,它也用作名为 Widget 的组件的 Prop 。 处理此问题的最佳方法是什么?我应该更改我的 Widget
有一个接口(interface)可以是多个接口(interface)之一 interface a {x:string} interface b {y:string} interface c {z:st
我遇到了一种情况,我需要调用第三方服务来获取一些信息。这些服务对于不同的客户可能会有所不同。我的界面中有一个身份验证功能,如下所示。 interface IServiceProvider { bool
在我的例子中,“RequestHandlerProxy”是一个结构,其字段为接口(interface)“IAdapter”,接口(interface)有可能被调用的方法,该方法的输入为结构“Reque
我有一个接口(interface)Interface1,它已由类A实现,并且设置了一些私有(private)变量值,并且我将类A的对象发送到下一个接受输入作为Interface2的类。那么我怎样才能将
假设我有这样的类和接口(interface)结构: interface IService {} interface IEmailService : IService { Task SendAs
有人知道我在哪里可以找到 XML-RPC 接口(interface)的定义(在 OpenERP 7 中)?我想知道创建或获取对象需要哪些参数和对象属性。每个元素的 XML 示例也将非常有帮助。 最佳答
最近,我一直在阅读有关接口(interface)是抽象的错误概念的文章。一篇这样的帖子是http://blog.ploeh.dk/2010/12/02/InterfacesAreNotAbstract
如果我有一个由第三方实现的现有 IInterface 后代,并且我想添加辅助例程,Delphi 是否提供了任何简单的方法来实现此目的,而无需手动重定向每个接口(interface)方法?也就是说,给定
我正在尝试将 Article 数组分配给我的 Mongoose 文档,但 Typescript 似乎不喜欢这样,我不知道为什么它显示此警告/错误,表明它不可分配. 我的 Mongoose 模式和接口(
我有两个接口(interface): public interface IController { void doSomething(IEntity thing); } public inte
是否可以创建一个扩展 Serializable 接口(interface)的接口(interface)? 如果是,那么扩展接口(interface)的行为是否会像 Serilizable 接口(int
我试图在两个存储之间创建一个中间层,它从存储 A 中获取数据,将其转换为相应类型的存储 B,然后存储它。由于我需要转换大约 50-100 种类型,我希望使用 map[string]func 并根据 s
我正在处理一个要求,其中我收到一个 JSON 对象,其中包含一个日期值作为字符串。我的任务是将 Date 对象存储在数据库中。 这种东西: {"start_date": "2019-05-29", "
我们的方法的目标是为我们现有的 DAO 和模型类引入接口(interface)。模型类由各种类型的资源 ID 标识,资源 ID 不仅仅是随机数,还带有语义和行为。因此,我们必须用对象而不是原始类型来表
Collection 接口(interface)有多个方法。 List 接口(interface)扩展了 Collection 接口(interface)。它声明与 Collection 接口(int
我有一个 Java 服务器应用程序,它使用 Jackson 使用反射 API 对 DTO 进行一般序列化。例如对于这个 DTO 接口(interface): package com.acme.libr
如果我在 Kotlin 中有一个接口(interface): interface KotlinInterface { val id: String } 我可以这样实现: class MyCla
我知道Java中所有访问修饰符之间的区别。然而,有人问了我一个非常有趣的问题,我很难找到答案:Java 中的 private 接口(interface)和 public 接口(interface)有什
我是一名优秀的程序员,十分优秀!