SpringCloudAlibaba【五】整合Sentinel

背景

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

环境

Windows10
JDK1.8
IDEA2021
Maven2.6

安装Sentinel服务

Windows安装Sentinel

创建一个Maven项目

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

SpringCloudAlibaba【五】整合Sentinel_第1张图片

创建项目文件及配置

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

SpringCloudAlibaba【五】整合Sentinel_第2张图片

关联流控

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

SpringCloudAlibaba【五】整合Sentinel_第3张图片
SpringCloudAlibaba【五】整合Sentinel_第4张图片

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

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

SpringCloudAlibaba【五】整合Sentinel_第5张图片

请求/接口

SpringCloudAlibaba【五】整合Sentinel_第6张图片

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

SpringCloudAlibaba【五】整合Sentinel_第7张图片

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

项目代码

代码

你可能感兴趣的:(SPRINGBOOT,sentinel,spring,boot,java)