SpringBoot(Cloud)集成Sentinel

Sentinel 是面向分布式服务架构的高可用流量防护组件,主要以流量为切入点,从限流、流量整形、熔断降级、系统负载保护、热点防护等多个维度来帮助开发者保障微服务的稳定性。

服务端控制面板

首先,下载Sentinel控制台 Releases · alibaba/Sentinel · GitHub

后台启动

nohup java -Dserver.port=8858 -Dcsp.sentinel.dashboard.server=localhost:8858 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.6.jar
http://localhost:8858    #账号密码都是sentinel 

客户端

SpringCloud Hoxton.SR12

SpringBoot 2.2.4.RELEASE 

在项目pom文件中添加依赖

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
            <version>2.1.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-core</artifactId>
            <version>1.8.0</version>
        </dependency>

        <!-- sentinel客户端与dashboard通信依赖 -->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-transport-simple-http</artifactId>
            <version>1.8.0</version>
        </dependency>

在application.yml中添加sentinel配置

spring:
    application:
      name: sentinel-client1
    cloud:
      sentinel:
        transport:
          post: 8719  #跟控制台交流的端口,随意指定一个未使用的端口即可
          dashboard: 192.168.86.127:8858
        eager: true

sentinel是懒加载,如果服务不被调用,sentinel控制台无法感知到服务

我们写了一个测试接口

 @RestController("/i18n")
 public class TestController {

    @GetMapping(value = "/hello")
    public String hello() {
        System.out.println("Hello Sentinel");
        return "Hello Sentinel";
    }

 }


调用后接口后,可以在控制面板看到我们定义的资源

设置QPS为1

 

访问一次,可以正常调用

1秒内快速点击,则提示被限流:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值