资讯专栏INFORMATION COLUMN

SpringBoot 1024行代码 - 系统监控工具 Actuator简介

darkerXi / 2842人阅读

摘要:前言在生产环境中,我们比较关心任意时刻一个的运行情况。调用接口返回结果我们猜测这个应该是表示应用程序正在正常运行。调用接口返回结果似乎什么都没发生,没关系,我们继续进行。我们继续试了上文列举的若干接口,发现大部分返回结果都是未授权。

前言

在生产环境中,我们比较关心任意时刻一个JVM的运行情况。SpringBoot为我们提供了一个方便的功能模块Actuator。只要简单几步就可以为我们的应用添加查询系统各项指标的功能。

准备工作

完成SpringBoot 1024行代码 - Getting Started(一个简单的web应用)

代码罗列
1. 在pom.xml中加入的引用

    org.springframework.boot
    spring-boot-starter-actuator

2. 启动应用

我们可以发现,SpringBoot在控制台上输出了如下几行内容

2017-10-28 15:07:41.538  INFO 37061 --- [main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/health || /health.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint.invoke(javax.servlet.http.HttpServletRequest,java.security.Principal)
2017-10-28 15:07:41.539  INFO 37061 --- [main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/info || /info.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-10-28 15:07:41.539  INFO 37061 --- [main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/configprops || /configprops.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-10-28 15:07:41.540  INFO 37061 --- [main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/beans || /beans.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-10-28 15:07:41.541  INFO 37061 --- [main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/trace || /trace.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-10-28 15:07:41.542  INFO 37061 --- [main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/heapdump || /heapdump.json],methods=[GET],produces=[application/octet-stream]}" onto public void org.springframework.boot.actuate.endpoint.mvc.HeapdumpMvcEndpoint.invoke(boolean,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) throws java.io.IOException,javax.servlet.ServletException
2017-10-28 15:07:41.544  INFO 37061 --- [main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/metrics/{name:.*}],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.MetricsMvcEndpoint.value(java.lang.String)
2017-10-28 15:07:41.545  INFO 37061 --- [main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/metrics || /metrics.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-10-28 15:07:41.548  INFO 37061 --- [main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/mappings || /mappings.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-10-28 15:07:41.552  INFO 37061 --- [main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/auditevents || /auditevents.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public org.springframework.http.ResponseEntity org.springframework.boot.actuate.endpoint.mvc.AuditEventsMvcEndpoint.findByPrincipalAndAfterAndType(java.lang.String,java.util.Date,java.lang.String)
2017-10-28 15:07:41.554  INFO 37061 --- [main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/autoconfig || /autoconfig.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-10-28 15:07:41.555  INFO 37061 --- [main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/env/{name:.*}],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EnvironmentMvcEndpoint.value(java.lang.String)
2017-10-28 15:07:41.555  INFO 37061 --- [main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/env || /env.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-10-28 15:07:41.558  INFO 37061 --- [main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/loggers/{name:.*}],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.LoggersMvcEndpoint.get(java.lang.String)
2017-10-28 15:07:41.559  INFO 37061 --- [main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/loggers/{name:.*}],methods=[POST],consumes=[application/vnd.spring-boot.actuator.v1+json || application/json],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.LoggersMvcEndpoint.set(java.lang.String,java.util.Map)
2017-10-28 15:07:41.559  INFO 37061 --- [main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/loggers || /loggers.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-10-28 15:07:41.560  INFO 37061 --- [main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/dump || /dump.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()

这表示"/health","/info","/configprops"等相对uri被映射到了一些系统自动创建的Bean的成员方法上。

几个实验

我们怀着好奇的心情调用了一个接口。

调用接口

curl 127.0.0.1:8080/health

返回结果

{"status":"UP"}

我们猜测这个应该是表示应用程序正在正常运行。我们紧接着又调用了第二个接口。

调用接口

curl 127.0.0.1:8080/info

返回结果

{}

似乎什么都没发生,没关系,我们继续进行。

调用接口

curl 127.0.0.1:8080/configprops

返回结果

{"timestamp":1509176216111,"status":401,"error":"Unauthorized","message":"Full authentication is required to access this resource.","path":"/configprops"}

英语过四级的朋友们已经猜到发生了什么了,我们没有被授权查看这个资源。我们继续试了上文列举的若干接口,发现大部分返回结果都是未授权。

知识点讲解 如何访问未授权接口

为什么大部分接口都是未授权?SpringBoot在设计中广泛采用了“约定优于配置(convention over configuration)”的设计思想。Actuator中提供的大部分功能都被默认配置成需要授权才可以访问,主要原因是这些资源大部分都比较敏感,在web应用被部署在直接可以被公网访问的环境而开发者不是特别清楚Actuator的功能的情况下web应用的一些重要数据就很容易被轻易获取。为了保护我们的web应用,Actuator的设计者将大部分敏感资源设计成默认需要授权才可以访问。

接下来我们来看看如何访问到这些资源。
通常有两种方式
1 将资源配置成无需授权
2 给用户授权
本文只介绍第一种

要将configprops配置成无需授权,只需在application.properties(要放到运行时的classpath下)文件中添加

endpoints.configprops.sensitive=false

之后再调用

curl 127.0.0.1:8080/configprops

就可以看到系统的各种配置属性信息了。

info接口怎么用

info如果开发者想让应用程序提供一写元数据查询功能,就可以使用info接口。
举例:
在application.properties文件中配置如下信息

info.project.name=SpringBoot-Demo

之后调用

curl 127.0.0.1:8080/info

结果变成了

{"project":{"name":"SpringBoot-Demo"}}

我们可以发现,通过info接口可以直接查询第一段为info的配置信息。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/67957.html

相关文章

  • SpringBoot-Admin的使用

    摘要:端项目依赖添加是为了使应用处于启动状态,会自动添加相关依赖。接下来以作为服务发现的示例来进行演示,实际上也可以使用或者。最后是一个用方式监听的使用连接端 【前情提要】Spring Boot Actuator 提供了对单个 Spring Boot 应用的监控,信息包含应用状态、内存、线程、堆栈等,比较全面的监控了 Spring Boot 应用的整个生命周期。但是这样监控也有一些问题:第一...

    vslam 评论0 收藏0
  • Spring Boot应用监控实战

    摘要:概述之前讲过容器的可视化监控,即监控容器的运行情况,包括使用率内存占用网络状况以及磁盘空间等等一系列信息。实战一下中添加依赖启动应用程序之后,只要在浏览器中输入端点信息就能获得应用的一些状态信息。 showImg(https://segmentfault.com/img/remote/1460000014684947); 概述 之前讲过Docker容器的可视化监控,即监控容器的运行情...

    mtunique 评论0 收藏0
  • SpringBoot 入门简介

    摘要:这里使用的是数据库启动类上加上注解在启动类中添加对包扫描扫描多个包下的可以有以下几种方法扫描会自动加载相关配置,数据源就会自动注入到中,会自动注入到中,可以直接使用。有配置文件下的使用扫描多个包下的可以有以下几种方法扫描 Spring-Boot 学习笔记 1 Spring-Boot 介绍 1.1 什么是Spring-Boot Spring-Boot是由Pivotal团队提供的全新框架...

    chuyao 评论0 收藏0
  • 如何优雅关闭 Spring Boot 应用

    摘要:除了,还有十余种,有的是特定操作,比如转储内存日志有的是信息展示,比如显示应用健康状态。 showImg(http://ww1.sinaimg.cn/large/006tNc79gy1g5qb2coyfoj30u00k0tan.jpg); 前言 随着线上应用逐步采用 SpringBoot 构建,SpringBoot应用实例越来多,当线上某个应用需要升级部署时,常常简单粗暴地使用 kil...

    xiyang 评论0 收藏0
  • 《 Kotlin + Spring Boot : 下一代 Java 服务端开发 》

    摘要:下一代服务端开发下一代服务端开发第部门快速开始第章快速开始环境准备,,快速上手实现一个第章企业级服务开发从到语言的缺点发展历程的缺点为什么是产生的背景解决了哪些问题为什么是的发展历程容器的配置地狱是什么从到下一代企业级服务开发在移动开发领域 《 Kotlin + Spring Boot : 下一代 Java 服务端开发 》 Kotlin + Spring Boot : 下一代 Java...

    springDevBird 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<