`
rensanning
  • 浏览: 3514366 次
  • 性别: Icon_minigender_1
  • 来自: 大连
博客专栏
Efef1dba-f7dd-3931-8a61-8e1c76c3e39f
使用Titanium Mo...
浏览量:37483
Bbab2146-6e1d-3c50-acd6-c8bae29e307d
Cordova 3.x入门...
浏览量:604351
C08766e7-8a33-3f9b-9155-654af05c3484
常用Java开源Libra...
浏览量:678121
77063fb3-0ee7-3bfa-9c72-2a0234ebf83e
搭建 CentOS 6 服...
浏览量:87298
E40e5e76-1f3b-398e-b6a6-dc9cfbb38156
Spring Boot 入...
浏览量:399823
Abe39461-b089-344f-99fa-cdfbddea0e18
基于Spring Secu...
浏览量:69078
66a41a70-fdf0-3dc9-aa31-19b7e8b24672
MQTT入门
浏览量:90494
社区版块
存档分类
最新评论

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

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

(1)开启监控

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


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


访问 http://localhost:8080/health 会返回以下json
引用
{
    "status" : "UP"
}


(2)设置端点访问

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

application.properties
引用
management.security.enabled=false


2-开启HTTP basic认证

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


application.properties
引用
security.user.name=admin
security.user.password=123456
management.security.enabled=true
management.security.role=ADMIN


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

3-设置ContextPath

application.properties
引用
management.contextPath=/manage


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

4-设置端口

application.properties
引用
management.port=8081


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

(2)端点Endpoint

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

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

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

(3)端点的开启或禁用
application.properties
引用
endpoints.configprops.enabled=false
endpoints.shutdown.enabled=true


(4)自定义端点

1-自定义已有端点,比如 /health 端点
@Component
public class CustomHealth implements HealthIndicator {
 
    public Health health() {
    	return Health.up().build();
    }
 
}


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

2-创建新的端点
@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

3-列举所有端点
@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

(5)自定义端点metrics
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/
分享到:
评论
1 楼 MCLoginandPwd 2017-08-11  
分享一款代码生成器,拖拽式组件结合流式处理,很容易的访问数据库、http、文件读写操作等等,支持编写javascript、java、freemaker,功能丰富强大,编制规则,可生成一切想要的代码,详见:http://blog.csdn.net/LoginandPwd/article/details/76944900,更多资讯:http://blog.csdn.net/LoginandPwd/article

相关推荐

Global site tag (gtag.js) - Google Analytics