SpringCloud Alibaba学习(五):Sentinel的介绍与搭建

目录

一、概述

        1、官网 

        2、Sentinel是什么 

        3、Hystrix的缺点 

        4、Sentinel能做什么 

二、安装Sentinel控制台

        1、下载sentinel 

        2、运行 

        3、访问sentinel管理界面 

三、初始化工程

        1、运行Nacos

        2、在IDEA中新建微服务模块 

                (1)创建模块 

                (2)修改pom文件  

                (3)编写yml文件

                (4)编写主启动类 

                (5)编写业务逻辑代码 

        3、运行 


一、概述

        1、官网 

        英文:        https://github.com/alibaba/Sentinel 

        中文:        https://github.com/alibaba/Sentinel/wiki/%E4%BB%8B%E7%BB%8D 

        2、Sentinel是什么 

        用一句话来概述:Sentinel是一个轻量级的流量控制、熔断降级的Java库。

        直白点说:就是阿里版的Hystrix 。

        3、Hystrix的缺点 

        (1)需要程序员手工搭建监控平台

        (2)没有一套web界面可以给我们进行更加细粒度化的配置流控、速率控制、服务熔断、服务降级…… 

        4、Sentinel能做什么 

二、安装Sentinel控制台

        1、下载sentinel 

        下载地址 :        Release v1.7.0 · alibaba/Sentinel · GitHub

        2、运行 

        首先要保证有Java8 环境,并且8080端口不被占用。

        在sentinel的jar包所在的位置打开命令行,输入命令:

java -jar sentinel-dashboard-1.7.0.jar

        3、访问sentinel管理界面 

        访问        http://localhost:8080  

        出现登陆界面,账号密码均为sentinel。 

        这样sentinel就搭建起来了,一行代码都不用写,而Hystrix还要搭建自己的微服务。两者比较来看,可以说sentinel是相当方便了。 

三、初始化演示工程

        1、运行Nacos

        将Nacos运行起来。 

        访问:        http://localhost:8848/nacos/#/login 

        2、在IDEA中新建微服务模块 

                (1)创建模块 

                新建普通maven模块 cloudalibaba-sentinel-service8401 

                (2)修改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">
    <parent>
        <artifactId>cloud</artifactId>
        <groupId>com.shang.cloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloudalibaba-sentinel-service8401</artifactId>

    <dependencies>
        <!--SpringCloud ailibaba nacos -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!--SpringCloud ailibaba sentinel-datasource-nacos 后续做持久化用到-->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
        </dependency>
        <!--SpringCloud ailibaba sentinel -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <!--openfeign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!-- SpringBoot整合Web组件+actuator -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--日常通用jar包配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>4.6.3</version>
        </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>



    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

</project>

                (3)编写yml文件

server:
  port: 8401

spring:
  application:
    name: cloudalibaba-sentinel-service
  cloud:
    nacos:
      discovery:
        #Nacos服务注册中心地址
        server-addr: localhost:8848
    sentinel:
      transport:
        #配置Sentinel dashboard地址
        dashboard: localhost:8080
        #默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口
        port: 8719

management:
  endpoints:
    web:
      exposure:
        include: '*'

                (4)编写主启动类 

@EnableDiscoveryClient
@SpringBootApplication
public class MainApp8401 {
    public static void main(String[] args) {
        SpringApplication.run(MainApp8401.class, args);
    }
}

                (5)编写业务逻辑代码 

@RestController
public class FlowLimitController {

    @GetMapping("/testA")
    public String testA() {
        return "------testA";
    }

    @GetMapping("/testB")
    public String testB() {
        return "------testB";
    }
}

        

        3、运行 

        先启动Nacos(前面已经启动)

        再启动Sentinel (前面已经启动)

        最后启动微服务8401

         然后看看sentinel控制界面,发现啥也没有:

         

        因为sentinel采用的是懒加载机制,需要访问一次才能显示出来(相当于打个卡就能够记录了):

        访问        http://localhost:8401/testA

        访问        http://localhost:8401/testB 

         

        然后就可以看到我们的微服务了:

        这说明sentinel8080正在监控微服务8401 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值