监控SpringBoot应用的两种方式

通过 https://start.spring.io/ 快速构建springboot应用。

一、使用Actuator进行监控

利用Actuator进行监控应用很简单,只需要两步:第一步在pom.xml文件中添加Actuator依赖,第二步在application.properties中关闭项目安全设置即可。

在pom.xml中添加Actuator依赖如下:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

在properties配置文件中配置如下:

# 关闭安全限制
manmanagement.security.enabled=false
# 放开所有的监控路径
management.endpoints.web.exposure.include=*

启动项目进行访问监控信息如下图所示:

Actuator提供的主要监控项如下图所示:

参考博文:
springboot的版本不同,用法有些区别,主要是1.x和2.x版本在访问路径上面多加了/actuator。
(1)https://blog.csdn.net/qq_22596931/article/details/90296809 (Actuator无法访问问题)
(2)https://blog.csdn.net/pengjunlee/article/details/80235390


二、使用可视化的监控报表——Spring Boot Admin

Actuator这种方式的监控应用唯一的缺点就是可视化效果不太好,不利于查看。而Spring Boot Admin对应用的各项监控指标提供了可视化面板,方便了查看,也就是把json串进行解析成可视化了。
使用Spring Boot Admin需要一个客户端和服务端,服务端收集监控数据进行可视化显示,客户端即需要被监控的应用提供监控数据给服务端。
springboot admin网址:https://github.com/codecentric/spring-boot-admin

1、springboot admin服务端搭建

pom.xml中添加依赖如下:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
    <version>2.2.0</version>
</dependency>

修改启动类,添加如下注解

@SpringBootApplication
@EnableAdminServer
public class ServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServerApplication.class, args);
    }

}

注意:需要注意一下版本,版本如果不对应将会导致启动失败。


2、springboot admin客户端搭建

pom.xml中添加依赖如下:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-client</artifactId>
    <version>2.2.4</version>
</dependency>

修改properties配置文件如下:

# 关闭安全限制
manmanagement.security.enabled=false
# 放开所有的监控路径
management.endpoints.web.exposure.include=*
# springboot admin服务端地址
spring.boot.admin.client.url=http://localhost:8081

启动项目即可看到springboot admin如下界面:

然后就可以查看应用的详细监控情况了。

参考博文:
(1)https://www.cnblogs.com/zeng1994/p/de3232ab0e1ce857b57eebb8e8922f0f.html

原文地址:https://www.cnblogs.com/jasonboren/p/15164713.html