SpringBoot的健康监控

1 使用Actuator检查与监控的步骤

1.1 在 pom 文件中添加 Actuator 的坐标

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

1.2 在全局配置文件中设置关闭安全限制

# 在1.5.x版本中通过management.security.enabled=false来暴露所有端点
management.security.enabled=false
# 在2.x 的springboot中的properties文件格式
management.endpoints.web.exposure.include=*
# 在2.x的springboot版本中的yml格式
# 2.0 yml格式
management:
 endpoints:
  web:
   exposure:
    include: refresh

但是spring boot2.0以后actuatorsecurity的报错A global security auto-configuration is now provided,过时问题,主要是:

  • 报错原因: 在spring boot2.0以后的版本中,actuatorsecurity某些方法已经过期了,已有所改动.不过要是不介意继续用也没事(eclispe不会提示,但是idea会提示)

springboot启动成功后,直接127.0.0.1:8080/+以下请求URI即可

ID 描述 是否需要鉴权
actuator 为其他端点提供“发现页面”。 要求 Spring HATEOASclasspath 路径上 需要
auditevents 陈列当前应用程序的审计事件信息 需要
autoconfig 展示自动配置信息并且显示所有自动配置候选人以及他们“被不被”应用的原因 需要
beans 显示应用程序中所有 Spring bean 的完整列表 需要
configprops 显示所有配置信息 需要
dump dump 所有线程 需要
env 陈列所有的环境变量 需要
flyway Shows any Flyway database migrations that have been applied 需要
health 显示应用程序运行状况信息 不需要
info 显示应用信息 不需要
loggers 显示和修改应用程序中的 loggers 配置 需要
liquibase 显示已经应用的任何 Liquibase 数据库迁移 需要
metrics 显示当前应用程序的“指标”信息 需要
mappings 显示所有@RequestMapping 的 url 整理列表。 需要
shutdown 关闭应用(默认情况下不启用) 需要
trace 显示跟踪信息(默认最后 100 个 HTTP 请求) 需要

2 使用可视化的监控报表 Spring Boot Admin

2.1 搭建服务端

2.1.1 引入admin坐标

服务端其实也是一个 SpringBoot 项目,引入admin的坐标

<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>1.5.7</version>
</dependency>

2.1.2 修改启动类,添加@EnableAdminServer

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

2.2 搭建客户端

其实客户端就是我们需要监控的工程。

2.2.1 修改客户端的pom文件添加依赖

<dependency>
	<groupId>de.codecentric</groupId>
	<artifactId>spring-boot-admin-starter-client</artifactId>
	<!-- 此处的版本号和springboot版本号一致,或者比springboot版本更低也可以 -->
	<version>RELEASE</version>
</dependency>

2.2.2 修改客户端的 application.properteis 配置文件

2.2.2.1 关闭安全限制

# 当springboot是1.5.x时使用这个关闭安全限制如下
 management.security.enabled=false
# 当springboot是2.X时,就使用如下
management.endpoints.web.exposure.include=*
# 当springboot是2.X时,使用的yml格式:
management:
 endpoints:
  web:
   exposure:
 # 此处的星号是特殊字符,必须用引号引起来
    include: “*”

2.2.2.2 注册到springbootadmin的服务

#  http://localhost:9090 表示是 Spring Boot Admin 服务单的 IP 地址以及端口号
spring.boot.admin.client.url=http://localhost:9090

2.2.2.3 不让springboot admin通过主机名字 获取ip

# 必须在客户端配置 boot.admin.client.instance.service-url属性,
# 让Spring Boot Admin服务端可以通过网络获取客户端的数据(否则默认会通过主机名去获取)
spring.boot.admin.client.instance.service-url=127.0.0.1:9090
# prefer-ip是否使用注册的ip地址来取代上述各个url中hostname的值,默认值是false
spring.boot.admin.client.instance.prefer-ip=true

2.3 监控信息讲解

报错信息:
Calling [asyncError()] is not valid for a request with Async state [MUST_DISPATCH]这个是没有什么影响的,说是内置tomcat的问题,把spring-boot-admin-starter版本降低下,2.1.2和2.1.3可能不会让application页面刷新出来,这时候降低到2.1.0就可以了

发布了334 篇原创文章 · 获赞 186 · 访问量 31万+

猜你喜欢

转载自blog.csdn.net/u012060033/article/details/104182455