当前位置: 首页 > news >正文

Spring Boot(五十三):SpringBoot Actuator之简单实现

1 场景介绍

对于一个大型的几十个、几百个微服务构成的微服务架构系统,在线上时通常会遇到下面一些问题,比如:

        1. 如何知道哪些服务除了问题,如何快速定位? (健康状况)

        2. 如何统一监控各个微服务的性能指标(内存、jvm、并发数、线程池、Http 请求统计)

        3. 如何统一管理各个微服务的日志?(切换线上日志等级,快速搜索日志...)

        4. 如何优雅管理服务下线(正在运行的线程不发生中断)

所以在这种大型分布式应用的环境下,我们如何能够快速发现问题、快速解决问题, 必须要有监控平台、(链路追踪、日志)

2 SpringBoot Actuator

SpringBoot自带监控功能Actuator,可以帮助实现对程序内部运行情况监控,比如监控状况、Bean加载情况、环境变量、日志信息、线程信息等

2.1 实现

项目结构如下:

新建springboot项目 actuator_admin为父模块

修改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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.8-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>actuator</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>
        <module>../01_actuator</module>
    </modules>

    <name>actuator</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <releases>
                <enabled>false</enabled>
            </releases>
        </pluginRepository>
    </pluginRepositories>

</project>

新建springboot Module:01_actuator

修改pom文件中的parent为actuator_admin模块。

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
<!--        <groupId>org.springframework.boot</groupId>-->
<!--        <artifactId>spring-boot-starter-parent</artifactId>-->
<!--        <version>2.7.8-SNAPSHOT</version>-->
        <groupId>com.example</groupId>
        <artifactId>actuator</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.actuator</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <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.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <releases>
                <enabled>false</enabled>
            </releases>
        </pluginRepository>
    </pluginRepositories>

</project>

启动01_actuator运行

访问localhost:8080接口

 接口返回的内容如下:

{
	"_links": {
		"self": {
			"href": "http://localhost:8081/actuator",
			"templated": false
		},
		"health": {
			"href": "http://localhost:8081/actuator/health",
			"templated": false
		},
		"health-path": {
			"href": "http://localhost:8081/actuator/health/{*path}",
			"templated": true
		}
	}
}

以上暴露的接口都是可以访问的。但不是所有的接口的暴露出来了,下面我们将所有的接口全部暴露出来。

修改application.properties暴露所有监控信息为HTTP

server.port=8081
#以web方式暴露所有端点
management.endpoints.web.exposure.include= *

#暴露所有监控信息为HTTP
management.endpoints.enabled-by-default=true

#修改访问端口的后缀
management.endpoints.web.base-path= /handsome

访问localhost:8081/handsome

接口返回内容如下:

{
	"_links": {
		"self": {
			"href": "http://localhost:8081/handsome",
			"templated": false
		},
		"beans": {
			"href": "http://localhost:8081/handsome/beans",
			"templated": false
		},
		"caches-cache": {
			"href": "http://localhost:8081/handsome/caches/{cache}",
			"templated": true
		},
		"caches": {
			"href": "http://localhost:8081/handsome/caches",
			"templated": false
		},
		"health": {
			"href": "http://localhost:8081/handsome/health",
			"templated": false
		},
		"health-path": {
			"href": "http://localhost:8081/handsome/health/{*path}",
			"templated": true
		},
		"info": {
			"href": "http://localhost:8081/handsome/info",
			"templated": false
		},
		"conditions": {
			"href": "http://localhost:8081/handsome/conditions",
			"templated": false
		},
		"shutdown": {
			"href": "http://localhost:8081/handsome/shutdown",
			"templated": false
		},
		"configprops": {
			"href": "http://localhost:8081/handsome/configprops",
			"templated": false
		},
		"configprops-prefix": {
			"href": "http://localhost:8081/handsome/configprops/{prefix}",
			"templated": true
		},
		"env": {
			"href": "http://localhost:8081/handsome/env",
			"templated": false
		},
		"env-toMatch": {
			"href": "http://localhost:8081/handsome/env/{toMatch}",
			"templated": true
		},
		"loggers": {
			"href": "http://localhost:8081/handsome/loggers",
			"templated": false
		},
		"loggers-name": {
			"href": "http://localhost:8081/handsome/loggers/{name}",
			"templated": true
		},
		"heapdump": {
			"href": "http://localhost:8081/handsome/heapdump",
			"templated": false
		},
		"threaddump": {
			"href": "http://localhost:8081/handsome/threaddump",
			"templated": false
		},
		"metrics": {
			"href": "http://localhost:8081/handsome/metrics",
			"templated": false
		},
		"metrics-requiredMetricName": {
			"href": "http://localhost:8081/handsome/metrics/{requiredMetricName}",
			"templated": true
		},
		"scheduledtasks": {
			"href": "http://localhost:8081/handsome/scheduledtasks",
			"templated": false
		},
		"mappings": {
			"href": "http://localhost:8081/handsome/mappings",
			"templated": false
		}
	}
}

Health:监控状况

        一个组件指标状态为Down则总状态信息Down,很多组件中都定制了Health子节点指标: 比如jdbc、redis、等等

shutdown:优雅关闭

注意需要web服务器的支持

配置文件添加:

server.shutdown=graceful

Metrics:运行时指标

Loggers:日志记录 

配置文件添加

logging.file.name: D:/logs/xushu.log

相关文章:

  • 有哪些网站下载ppt是免费的/百度权重是什么意思
  • 微分销管理系统/小学生班级优化大师
  • 上海企业网上公示/西安关键词优化平台
  • 树莓派可以做网站空间吗/重庆森林百度云
  • 网站建设销售好做吗/竞价托管sem服务
  • wordpress 秀/网站流量分析工具
  • 【Python语言基础】——Python 集合
  • 关于Linux部署Tomcat的访问问题
  • 【学习笔记】CF1779G The Game of the Century
  • 一文带你秒懂十大排序
  • Linux---进程概念
  • 【C语言】数据结构基础(每日小细节025),有三数之和哦
  • 一文搞懂 python 中的 classmethod、staticmethod和普通的实例方法的使用场景
  • Redis缓存和数据库一致性
  • JavaScript - 代理与反射(代理基础)
  • 消息批处理端口说明
  • Win10注册表损坏进不了系统怎么U盘重装系统?
  • python学习笔记---面向对象编程【廖雪峰】