Spring boot + Sentinel + Nacos 整合

今天分享一个sentinel入门demo文章,不讲解任何理论只上干货,主要实现Sentinel针对微服务的限流基础应用,结合Nacos(官网)配置中心实现限流规则的动态更新和持久化, 希望对想学习的朋友有所帮助。

一、sentinel_dashboard的引入

下载 sentinel-dashboard-1.6.3.jar
dashboard是springboot的项目,在CMD模式下使用命令
Java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.6.3.jar
进行控制看板服务的启动。
其中,-Dserver.port=8080 代表看板项目的端口号,-Dcsp.sentinel.dashboard.server=localhost:8080代表本看板服务将会注册到自己的看板上,-Dproject.name=sentinel-dashboard代表本看板服务的项目名称。访问localhost:8080;输入用户名,密码,均是sentinel,如果要自定义用户名和密码,在启动命令加上-Dsentinel.dashboard.auth.username=sentinel,
-Dsentinel.dashboard.auth.password=123456即可。

在这里插入图片描述
在这里插入图片描述

二、新建Spring boot服务

springboot 版本为:2.3.4.RELEASE
springcloud 版本为:Hoxton.SR1

编写测试类:

@RestController
@RequestMapping("/index")
public class IndexAction {
    protected static final Logger log = LoggerFactory.getLogger(IndexAction.class);

    @RequestMapping("/hello")
    //@SentinelResource(value = "hello", fallback = "helloError")
    public String sentinelTest(String name) {
        log.debug("success,你好!!!," + name);
        return "success,你好," + name;
    }

    @RequestMapping("/test")
    public String sentinelTest2() {
        log.info("success,你好!!! test");
        return "success,你好!!! test";
    }

    public String helloError(String name, Throwable e) {
        log.error("限流控制,name:{}",name, e.getMessage());
        return "error," + name;
    }
}

添加相关依赖包:

<dependencyManagement>
<dependencies>
    <!-- SpringCloud -->
    <dependency>
	    <groupId>org.springframework.cloud</groupId>
	    <artifactId>spring-cloud-dependencies</artifactId>
	    <version>Hoxton.SR1</version>
	    <type>pom</type>
	    <scope>import</scope>
    </dependency>
    <dependency>
	    <groupId>com.alibaba.cloud</groupId>
	    <artifactId>spring-cloud-alibaba-dependencies</artifactId>
	    <version>2.2.5.RELEASE</version>
	    <type>pom</type>
	    <scope>import</scope>
    </dependency>
</dependencies>
</dependencyManagement>

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    <version>2.1.0.RELEASE</version>
</dependency>
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-datasource-nacos</artifactId>
    <version>1.8.0</version>
</dependency>
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-transport-simple-http</artifactId>
    <version>1.7.2</version>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    <version>0.2.2.RELEASE</version>
</dependency>

Springboot服务添加配置:
注意看配置的注释

spring:
  application:
    name: sentinel-test
  profiles:
    active: test,dev
  cloud:
    nacos:
      discovery:
        #nacos服务注册中心地址
        server-addr: localhost:8848
    sentinel:
      transport:
        dashboard: localhost:8090
      eager: false
      datasource:
        # 名字随意
        ds:
          nacos:
            # nacos的访问地址,,根据上面准备工作中启动的实例配置
            server-addr: localhost:8848
            # nacos中存储规则的groupId
            groupId: sentinel
            # nacos中存储规则的dataId
            dataId: ${spring.application.name}-rules
            # 用来定义存储的规则类型
            rule-type: flow
            data-type: json

3、进入nacos控制台-配置列表添加配置

dataId: sentinel-test-rules
groupId: sentinel

这里的sentinel-test是Spring boot服务名称

[
    {
        "resource":"/index/hello",
        "limitApp":"default",
        "grade":1,
        "count":1,
        "strategy":0,
        "controlBehavior":0,
        "clusterMode":false
    }
]

resource: 资源名称
limitApp: 来源应用
grade:阈值类型,0表示线程数,1表示QPS;
count:单机阈值;
strategy: 流控模式,0表示直接,1表示关联,2表示链路
controlBehavior:流控效果,0表示快速失败,1表示Warm Up,2表示排队等待;
clusterMode:是否集群

在这里插入图片描述

4、验证限流功能

启动Nacos服务
启动spring boot服务
启动sentinel-dashboard服务

连续快速请求接口
在这里插入图片描述

登录sentinel-dashboard查看是否有从Nacos配置中心同步到的限流规则
在这里插入图片描述

根据配置限流规则连续访问接口,出现Blocked by Sentinel (flow limiting) 表示限流成功
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值