为了账号安全,请及时绑定邮箱和手机立即绑定

spring boot监控与管理

标签:
JQuery

引言

在微服务架构中,我们把单块系统拆分成多个服务。在部署时,一个服务很可能分布在不同的主机,甚至是不同的机房。随着服务的不断增多,服务间相互调用、相互依赖。为了掌握服务的状态及服务所在主机的情况,就显得尤为重要!

Spring Boot Actuator

spring boot actuator为我们暴露很多端点(endpoints),可以用来获取环境属性、线程信息、健康状况等。下表列出了常用的端点。

路径描述
/health显示应用程序的状态
/metrics显示内存、GC、线程、堆栈等信息
/trace显示最近100条请求详细信息
/mappings显示所有通过@RequestMapping配置的路径
/env显示全部环境属性,包括profiles、systemProperties、systemEnvironment、applicationConfig
/dump显示线程快照
/autoconfig显示自动配置的生效及未生效列表,对了解bean的加载很有帮助
/beans查看程序中所有的bean
/shutdown关闭应用程序,默认是不可用的,可以通过endpoints.shutdown.enabled=true开启

如何开启

引入spring-boot-startr-actuator后自动开启。

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

虽然很简单,如果大家不方便自己搭建,可以通过git下载一下例子

git clone https://github.com/feelgood3000/actuator.git

下载后运行ActuatorApplication的main()方法即可。

在浏览器打开如下地址:http://localhost:8080/metrics,返回内容如下:

{"mem": 446758,"mem.free": 316977,"processors": 4,"instance.uptime": 39453,"uptime": 46245,"systemload.average": 3.32568359375,"heap.committed": 391680,"heap.init": 131072,"heap.used": 74702,"heap": 1864192,"nonheap.committed": 55872,"nonheap.init": 2496,"nonheap.used": 55079,"nonheap": 0,"threads.peak": 25,"threads.daemon": 21,"threads.totalStarted": 28,"threads": 23,"classes": 7793,"classes.loaded": 7793,"classes.unloaded": 0,"gc.ps_scavenge.count": 10,"gc.ps_scavenge.time": 170,"gc.ps_marksweep.count": 2,"gc.ps_marksweep.time": 130,"httpsessions.max": -1,"httpsessions.active": 0,"datasource.primary.active": 0,"datasource.primary.usage": 0}

其他可以自己看一下,返回内容通过字段就可以知道含义。

配置

端点开关

支持对每个端点进行开关,配置为endpoints.[endpointName].enabled=true,endpointName为端点的名字,比如我们想禁用info端点,在application.properties中添加如下内容即可:

endpoints.info.enabled=false

如果我们只想开启其中一两个端点,我们可以先把所有端点禁用,再单独配置要开启的端点。

endpoints.enabled=falseendpoints.info.enabled=true

端点端口号

有时为了安全考虑,我们不想跟服务占用同一个端口,可以单独给actuator配置端口。

server.port=8080management.port=8888

自定义路径

我们也可以修改默认的路径,来起到安全的目的。  
配置格式为endpoints.[endpointsName].id=[path]  
比如我们想把metrics的访问路径从http://localhost:8080/metrics修改为http://localhost:8080/feelgood3000,则配置为:

endpoints.metrics.id=feelgood3000

添加前缀

我们可以通过配置修改访问endpoints的路径。

management.context-path=/manage

添加如上配置后,所有端点的路径从原来的http://localhost:8080/endpointName,改为http://localhost:8080/manage/endpointName了。

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消