Sentinel系列 -快速开始

x33g5p2x  于2021-12-21 转载在 其他  
字(1.6k)|赞(0)|评价(0)|浏览(214)

快速开始

本文讲解Sentinel的在项目中的快速开始使用,Sentinel在项目中使用的步骤还是比较简单的。

启动Sentinel 控制台

启动Sentinel 控制台的步骤本文不再多做介绍,不明白的可以参考sentinel控制台

项目整合Sentinel

第一步:在Spring Cloud应用的pom.xml中引入Spring Cloud Alibaba的Sentinel模块:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--   sentinel     -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

第二步:在application.yml文件中添加sentinel的配置,如下:

server:
  port: 8601
spring:
  application:
    name: cloudalibaba-sentinel-service-8601
  cloud:
    sentinel:
      transport: #dashboard地址
        dashboard: localhost:8088  #对应自己的sentinel控制台端口
        port: 8719  #默认端口,如果被占用则从8719依次+1扫描

第三步:创建一个restful风格的controller接口,进行测试如下:

@RestController
@Slf4j
public class LimitController {

    @GetMapping("/testA")
    public String testA(){
        try {
            TimeUnit.MILLISECONDS.sleep(800);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "---------testA";
}

第四步:启动应用,然后通过postman或者curl访问几下localhost:8601/testA接口,在sentinel控制台中看到如下界面,说明sentinel整合成功。

可能的异常

如果在项目添加完成,启动之后,sentinel的控制台依然无法接收到项目中定义的资源,需要查看sentinel是否添加成功。
查看的方式是直接查看IDEA的maven依赖:

总结

  • sentinel在项目中的整合相对来说是比较简单的,整合项目的过程中,一定要保证项目的依赖添加正确。
  • 下一章将会讲解sentinel的各种限流策略

相关文章