SpringCloudAlibaba【五】整合Sentinel

背景

Sentinel作为SpringCloudAlibaba服务监控的重要组件,在微服务开发中经常使用到,下面我们就来了解一下如何在SpringBoot中整合Sentinel以及Sentinel的基础用法

环境

Windows10
JDK1.8
IDEA2021
Maven2.6

安装Sentinel服务

Windows安装Sentinel

创建一个Maven项目

创建如下Maven项目以及目录结构

在这里插入图片描述

创建项目文件及配置

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>springcloud-sentinel</artifactId>
    <version>1.0-SNAPSHOT</version>

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

    <!--add-->

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
    </parent>

    <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>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.5.RELEASE</version>
                <scope>import</scope>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

YML

server:
  port: 9000

spring:
  cloud:
    sentinel:
      transport:
        dashboard: localhost:8080

management:
  endpoints:
    web:
      exposure:
        include: "*"

SentinelController

package com.sentinel.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class SentinelController {

    @GetMapping("/")
    public String get() {
        return "sentinel";
    }

    @GetMapping("/list")
    public String getList() {
        return "list";
    }

    public static void main(String[] args) throws InterruptedException {
        while (true) {
            RestTemplate restTemplate = new RestTemplate();
            String forObject = restTemplate.getForObject("http://localhost:9000/list", String.class);
            System.out.println(forObject);
            Thread.sleep(1000);
        }
    }
}

SentinelApplication

package com.sentinel;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

测试

启动项目,看到/接口已经注册到了Sentinel

在这里插入图片描述

关联流控

我们来测试下关联流控是否生效,在/接口中新增流控规则,配置如下

在这里插入图片描述
在这里插入图片描述

说明:当/list接口流量大于1时,接口/则会熔断

我们启动接口中的main函数,模拟大流量请求/list接口场景,测试请求/接口是否会熔断

在这里插入图片描述

请求/接口

在这里插入图片描述

停止main函数,再次请求/接口

在这里插入图片描述

小结:关联流控正常,其他Sentinel功能自测

项目代码

代码

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BirdMan98

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值