springboot(二)Actuator的作用和使用
Actuator
actuator是springboot中的一个附加功能,官方是对它这样介绍的:
Spring Boot includes a number of additional features to help you monitor and manage your application when you push it to production. You can choose to manage and monitor your application by using HTTP endpoints or with JMX. Auditing, health, and metrics gathering can also be automatically applied to your application.
用法:
1.首先在springboot工程中引入依赖:
Maven方式:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- 1
- 2
- 3
- 4
Gradle:
dependencies { compile("org.springframework.boot:
spring-boot-starter-actuator")
- 1
- 2
2.启动springboot项目
在启动log日志中,可以看到springboot把一些默认的地址都映射好了,其中我们配置的actuator中health和info已经被映射上了,我们打开地址:http://localhost:8080/actuator/info 或者 http://localhost:8080/actuator/health

springboot 官方提供的Actuator
当然,这只是一部分,我这里不全部粘贴出来了,感兴趣的同学可以到官网上去看,springboot2.0之后,在Http环境下将默认的endpoint只设置为info和health,要想开启其他的监控功能,需要手动配置
打开application.properties或者application.yml:
备注:application.yml文件作为例子:
server:
port: 8080 #web服务端口
management: #actuator
server:
port: 8081
endpoints:
web:
base-path: /
exposure:
include: "*"
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
如果将include这只为*,则开启全部监控功能,如果只想开启某个功能的监控,则,include中传你要开启的endpoint的名称,例如,我要开启beans和metrics功能include: beans,metrics
从图中可以看出,所有的功能都被mapping到响应的URL上了
通过访问http://localhost:8081/httptrace
我们可以看到最近浏览的网页记录

总结
通过Actuator可以在生产环境监控当前应用的健康,虚拟机等信息,通过前端以可视化的界面展示出来
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)