springCloud搭建分布式微服务学习--6--初识Sentinel监控

sentinel是什么

随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 是面向分布式服务架构的轻量级流量控制组件,主要以流量为切入点,从限流、流量整形、熔断降级、系统负载保护等多个维度来帮助您保障微服务的稳定性。

总的来说,sentinel包含了:服务降级,服务熔断,限流

步骤

  • 官网下载jar包

地址

  • cmd运行jar包
    java -jar sentinel-dashboard-1.7.x.jar

  • 引入Pom.xml

<dependencies>
        <!--springcloud alibaba nacos-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!--springcloud alibaba sentinel-datasource-nacos 后续做持久化用到-->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
        </dependency>
        <!--springcloud alibaba sentinel-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <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>
        <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>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>common</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>

  • 配置Yml
#yml配置
server:
  port: 9003

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: '*'

  • 编写controller
    下面的SentinelResource注解指定,如果超过控制台的控制后会执行的方法
@RestController
public class FlowLimitController {
    @GetMapping("/testHotKey")
    @SentinelResource(value = "testHotKey",blockHandler = "del_testHotKey")
    public String testHotKey(@RequestParam(value = "p1",required = false)String p1,@RequestParam(value = "p2",required = false)String p2){
        return "----testHotKey";
    }
    public String del_testHotKey(String p1, String p2, BlockException exception){
        return "...it is a pity,blocked";
    }
}
  • 配置控制台
    注意,要先请求一次testHotKey请求,控制台才会显现出来
    springCloud搭建分布式微服务学习--6--初识Sentinel监控_第1张图片
    springCloud搭建分布式微服务学习--6--初识Sentinel监控_第2张图片

- 代码解耦

上面把请求方法和降级方法写在一起,造成代码膨胀.
所以另外写一个类,专门配置blockHandler,

package main.controller;

import com.alibaba.csp.sentinel.slots.block.BlockException;

public class blockHandler {
    public static  String del_testHotKey(String p1, String p2, BlockException exception){
        return "...it is a pity,blocked";
    }
}

引用:


    @GetMapping("/testHotKey")
    @SentinelResource(value = "testHotKey",blockHandler = "del_testHotKey",blockHandlerClass = blockHandler.class)
    public String testHotKey(@RequestParam(value = "p1",required = false)String p1,@RequestParam(value = "p2",required = false)String p2){
        return "----testHotKey";
    }

你可能感兴趣的:(springCloud)