博客
关于我
Sentinel入门(二)
阅读量:533 次
发布时间:2019-03-07

本文共 2415 字,大约阅读时间需要 8 分钟。

Spring Boot 与 Sentinel 简单示例

1. 创建 Spring Boot 应用

创建一个名为“Sentinel-Quick-start”的Spring Boot应用。我们需要使用 Maven 进行依赖管理。

2. Maven 依赖

在项目的 pom.xml 中添加以下依赖:

Sentinel 核心依赖

com.alibaba.csp
sentinel-core
1.8.0

引入 HTTP 运输依赖

com.alibaba.csp
sentinel-transport-simple-http
1.8.0

注解支持依赖

com.alibaba.csp
sentinel-annotation-aspectj
1.8.0

3. 编写 Controller

@Controllerpublic class TestController {    @RequestMapping(path = "/hello", method = RequestMethod.GET)    @ResponseBody    public String hello() {        try {            Entry ignored = SphU.entry("Hello");            System.out.println("Hello Sentinel");            return "Hello Sentinel";        } catch (BlockException e) {            System.out.println("系统繁忙,请稍后");            e.printStackTrace();            return "系统繁忙,请稍后";        }    }}

异步调用支持

在启动类中添加 @EnableAsync

@SpringBootApplication@EnableAsyncpublic class SentinelQuickStartApplication {    public static void main(String[] args) {        SpringApplication.run(SentinelQuickStartApplication.class, args);    }}

创建异步服务类:

@Servicepublic class AsyncService {    @Async    public void hello() {        try {            Thread.sleep(5000L);        } catch (InterruptedException e) {            e.printStackTrace();        }    }}

Controller 中使用异步方法:

@Autowiredprivate AsyncService asyncService;@RequestMapping(path = "/async", method = RequestMethod.GET)public void async() {    try {        AsyncEntry asyncEntry = null;        asyncEntry = SphU.asyncEntry("Sentinel_Async");        asyncService.hello();        System.out.println("异步测试");    } catch (BlockException e) {        e.printStackTrace();        System.out.println("系统繁忙请稍后再试");    } finally {        if (asyncEntry != null) {            asyncEntry.exit();        }    }}

4. 在 Idea 中设置 JVM 启动参数

在 Idea 的项目设置中,添加以下 VM 参数:

-Dcsp.sentinel.dashboard.server=127.0.0.1:9000    # 指定 Sentinel 控制台地址和端口-Dproject.name=Sentinel-Quick-Start                  # 本地应用在控制台中的名称

5. 启动项目

运行 Maven �入りDeploy 上线,或者通过 Idea 直接运行启动类。

6. 启动 Sentinel 控制台

下载 Sentinel Dashboard 最新的 JAR 包,并在终端中执行:

java -Dserver.port=9000 -jar sentinel-dashboard-1.8.0.jar

登录控制台,用户名和密码均为“Sentinel”。

7.测试运行

第一次访问控制台,需要先访问被限流的接口“/hello”。此外,可以通过设置不同的限流规则,动态查看控制台中的统计数据。

通过这些步骤,成功地将 Spring Boot 应用与 Sentinel 控制台连接,实现了限流功能的管理和监控。

转载地址:http://ssinz.baihongyu.com/

你可能感兴趣的文章
Objective-C实现heap堆算法(附完整源码)
查看>>
Objective-C实现highest response ratio next高响应比优先调度算法(附完整源码)
查看>>
Objective-C实现hill climbing爬山法用来寻找函数的最大值算法(附完整源码)
查看>>
Objective-C实现Hopcroft算法(附完整源码)
查看>>
Objective-C实现hornerMethod霍纳法算法(附完整源码)
查看>>
Objective-C实现Http Post请求(附完整源码)
查看>>
Objective-C实现http下载文件 (附完整源码)
查看>>
Objective-C实现Http协议下载文件(附完整源码)
查看>>
Objective-C实现ID3贪心算法(附完整源码)
查看>>
Objective-C实现IIR 滤波器算法(附完整源码)
查看>>
Objective-C实现IIR数字滤波器(附完整源码)
查看>>
Objective-C实现insertion sort插入排序算法(附完整源码)
查看>>
Objective-C实现integer partition整数分区算法(附完整源码)
查看>>
Objective-C实现integerPartition整数划分算法(附完整源码)
查看>>
Objective-C实现interpolation search插值搜索算法(附完整源码)
查看>>
Objective-C实现Interpolation search插值查找算法(附完整源码)
查看>>
Objective-C实现intersection交集算法(附完整源码)
查看>>
Objective-C实现intro sort内省排序算法(附完整源码)
查看>>
Objective-C实现inverse matrix逆矩阵算法(附完整源码)
查看>>
Objective-C实现inversions倒置算法(附完整源码)
查看>>