springboot整合sentinel完成限流

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

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

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

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

mvn clean package -DskipTests

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

java -jar sentinel-dashboard.jar

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

4、web页面

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

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

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>sentinel</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.7.RELEASE</version>
        <relativePath/>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
            <version>2.2.5.RELEASE</version>
        </dependency>
    </dependencies>
</project>

6、新建application.yml文件

server:
  port: 8001
spring:
  application:
    name: sentinel-service
  cloud:
    sentinel:
      transport:
        dashboard: localhost:8084
      eager: true
management:
  endpoint:
    web:
      exposure:
        include: '*'

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);
    }
}

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";
    }
}

9、启动项目,调用接口

http://localhost:8001/test/a
http://localhost:8001/test/b
有的时候在Sentinel控制台的簇点链路只显示/error和/**,无法显示相应的资源
解决方式:将这个依赖版本升级为2.2.5 即可
   <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
            <version>2.2.5.RELEASE</version>
        </dependency>

请添加图片描述

10、现在给/test/a限流

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

11、页面展示

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

Spring BootSentinel是两个独立的开源项目,可以通过一些配置和依赖来实现它们的整合。下面是一个简单的步骤: 1. 在你的Spring Boot项目中,添加Sentinel的依赖。你可以在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-spring-webmvc-adapter</artifactId> <version>x.x.x</version> </dependency> ``` 2. 创建一个配置类,用于配置Sentinel的相关参数。在该类上添加`@Configuration`注解,并使用`@PostConstruct`注解来初始化Sentinel的相关配置。例如: ```java import org.springframework.context.annotation.Configuration; @Configuration public class SentinelConfig { @PostConstruct public void init() { // 初始化Sentinel的相关配置,例如规则的加载等 } } ``` 3. 在你的Controller类上使用`@SentinelResource`注解来定义资源,并配置对应的限流规则。例如: ```java import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api") public class MyController { @GetMapping("/hello") @SentinelResource(value = "hello", blockHandler = "handleBlock") public String hello() { return "Hello, Sentinel!"; } public String handleBlock(BlockException ex) { return "Blocked by Sentinel"; } } ``` 在上面的例子中,`@SentinelResource`注解用于定义一个资源,其中`value`属性指定资源名称,`blockHandler`属性指定当资源被限流时的处理方法。 4. 最后,启动你的Spring Boot应用程序,并访问定义的API进行测试。 这只是一个简单的示例,你可以根据自己的需要在Spring Boot项目中进行更多的配置和调整。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值