Spring Boot 入门 - 进阶篇(8)- 应用监控(Actuator)

作为Spring Boot的另外一大亮点,就是actuator模块,它是Spring Boot Starter中的一个特殊模块,用于集中采集应用的各项指标信息。

[b](1)开启监控[/b]

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


引入后不需要做任何修改,启动应用就会看到以下日志:
[quote]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()
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)
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()
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
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)
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()
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)
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)
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()
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()
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()
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()
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)
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<java.lang.String, java.lang.String>)
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()
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()
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()[/quote]

访问 http://localhost:8080/health 会返回以下json
[quote]{
"status" : "UP"
}[/quote]

[b](2)设置端点访问[/b]

[b]1-关闭验证[/b]
默认情况下很多端点是不允许访问的,会返回401:Unauthorized。

application.properties
[quote]management.security.enabled=false[/quote]

[b]2-开启HTTP basic认证[/b]

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


application.properties
[quote]security.user.name=admin
security.user.password=123456
management.security.enabled=true
management.security.role=ADMIN[/quote]

访问URL http://localhost:8080/env 后,就看到需要输入用户名和密码了。

[b]3-设置ContextPath[/b]

application.properties
[quote]management.contextPath=/manage[/quote]

那么URL就是:http://localhost:8080/manage/env

[b]4-设置端口[/b]

application.properties
[quote]management.port=8081[/quote]

那么URL就是:http://localhost:8081/manage/env

[b](2)端点Endpoint[/b]

[b]应用配置类[/b]
/autoconfig 获取应用的自动化配置报告
/beans 获取应用上下文中创建的所有Bean
/configprops 获取应用中配置的属性信息报告
/env 获取应用所有可用的环境属性报告
/mappings 获取应用所有Spring MVC的控制器映射关系报告
/info 获取应用自定义的信息

[b]度量指标类[/b]
/metrics 返回应用的各类重要度量指标信息
/health 返回应用的各类健康指标信息
/dump 返回程序运行中的线程信息
/trace 返回基本的HTTP跟踪信息

[b]操作控制类[/b]
/shutdown 用来远程关闭应用

[b](3)端点的开启或禁用[/b]
application.properties
[quote]endpoints.configprops.enabled=false
endpoints.shutdown.enabled=true[/quote]

[b](4)自定义端点[/b]

[b]1-自定义已有端点,比如 /health 端点[/b]
@Component
public class CustomHealth implements HealthIndicator {

public Health health() {
return Health.up().build();
}

}


访问URL:http://localhost:8081/manage/health

[b]2-创建新的端点[/b]
@Component
public class CustomEndpoint implements Endpoint<List<String>> {

public String getId() {
return "myep";
}

public boolean isEnabled() {
return true;
}

public boolean isSensitive() {
return true;
}

public List<String> invoke() {
List<String> messages = new ArrayList<String>();
messages.add("This is message 1");
messages.add("This is message 2");
return messages;
}
}


访问URL:http://localhost:8081/manage/myep

[b]3-列举所有端点[/b]
@Component
public class ListEndpoints extends AbstractEndpoint<List<Endpoint>> {
private List<Endpoint> endpoints;

@Autowired
public ListEndpoints(List<Endpoint> endpoints) {
super("allep");
this.endpoints = endpoints;
}

public List<Endpoint> invoke() {
return this.endpoints;
}
}


访问URL:http://localhost:8081/manage/allep

[b](5)自定义端点metrics[/b]
Spring Boot允许开发人员以编码的方式提供更丰富指标信息,通过/metrics端点来访问。counter是以Number类型来展现的指标;gauge是衡量双精度计算的指标。任何位置都可以注入CounterService或GaugeService。

counterService.increment("metricName");
counterService.decrement("metricName");
counterService.reset("metricName");


gaugeService.submit("metricName", 2.5);


参考:
http://www.baeldung.com/spring-boot-actuators
http://blog.didispace.com/spring-boot-actuator-1/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值