• springboot整合sentinel完成限流


    1、直入正题,下载sentinel的jar包

    1.1 直接到Sentinel官网里的releases下即可下载最新版本,Sentinel官方下载地址,直接下载jar包即可。不过慢,可能下载不下来
    在这里插入图片描述
    1.2 可以去gitee去下载jar包
    在这里插入图片描述

    1.3 下载完成后,进行打包,请添加图片描述
    1.4 执行命令

    打开命令行窗口,进入到项目里面,执行:

    mvn clean package -DskipTests
    
    • 1

    2、打包完成后,直接启动

    java -jar sentinel-dashboard.jar
    
    • 1

    3、启动成功请添加图片描述

    4、web页面

    浏览器输入localhost:8084
    账号密码:sentinel
    
    • 1
    • 2

    5、编写项目,导入pom依赖

    
    
        4.0.0
    
        org.example
        sentinel
        1.0-SNAPSHOT
    
        
            8
            8
            UTF-8
        
        
            org.springframework.boot
            spring-boot-starter-parent
            2.2.7.RELEASE
            
        
        
            
                org.springframework.boot
                spring-boot-starter-web
                2.2.4.RELEASE
            
            
                org.mybatis.spring.boot
                mybatis-spring-boot-starter
                1.3.2
            
            
                com.alibaba.cloud
                spring-cloud-starter-alibaba-sentinel
                2.2.5.RELEASE
            
        
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    6、新建application.yml文件

    server:
      port: 8001
    spring:
      application:
        name: sentinel-service
      cloud:
        sentinel:
          transport:
            dashboard: localhost:8084
          eager: true
    management:
      endpoint:
        web:
          exposure:
            include: '*'
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    7、新建启动类

    package com.xxx;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
    
    @SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
    public class SentinelServiceApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SentinelServiceApplication.class,args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    8、新建controller

    package com.xxx.controller;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping(value = "test")
    public class TestController {
    
        @GetMapping(value = "/a")
        public String test1() {
            return "Hello,Sentinel --> 1";
        }
    
        @GetMapping(value = "/b")
        public String test2() {
            return "Hello,Sentinel --> 2";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    9、启动项目,调用接口

    http://localhost:8001/test/a
    http://localhost:8001/test/b
    有的时候在Sentinel控制台的簇点链路只显示/error和/**,无法显示相应的资源
    解决方式:将这个依赖版本升级为2.2.5 即可
       
                com.alibaba.cloud
                spring-cloud-starter-alibaba-sentinel
                2.2.5.RELEASE
            
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    请添加图片描述

    10、现在给/test/a限流

    点击流控按钮,新增流控规则即可
    请添加图片描述

    11、页面展示

    连续点击三次,就会出现限流操作,至此 搭建完成。。。
    请添加图片描述

  • 相关阅读:
    深理解@State 变量 SwiftUI
    mysql面试题26:MySQL中什么是MVCC,它的底层原理是什么
    Kotlin面向对象
    Photoshop制作简洁清新的插画海报图片
    Linux上的监控工具:Zabbix、Prometheus、APM和ELK
    我最佩服的一位学生!他是哈工大在读NLP博士积累28W粉丝
    计算机操作系统 第五章 虚拟存储器(2)
    Java基础之《Ajax+JQuery(JavaEE开发进阶Ⅱ)》—JQuery与Ajax的应用(2)
    Redis的安装以及主从复制&高可用数据库的详解
    学生个人单页面网页作业 学生网页设计成品 静态HTML网页单页制作 dreamweaver网页设计与制作代码 web前端期末大作业
  • 原文地址:https://blog.csdn.net/qq_36151389/article/details/133124481