在开发过程当中,项目被放置到生产服务器上运行时,有可能须要咱们全方位的监控应用服务的运行状况。此时SpringBoot提供了Actuator模块进行监控和管理html
在gradle中添加依赖java
compile("org.springframework.boot:spring-boot-starter-actuator")
而后启动项目后会发如今启动中发现以下的输出信息spring
这些端点信息是暴露在外面的原生信息,例如此时访问http://localhost:8080/health
会发如今网站中输出以下信息json
在
SpringBoot2.0
中映射的地址是/actuator/health
服务器
{ "status": "UP", "diskSpace": { "status": "UP", "total": 250685575168, "free": 172327690240, "threshold": 10485760 }, "db": { "status": "UP", "database": "MySQL", "hello": 1 } }
Actuator的端点可以进行监控和管理应用。SpringBoot有许多的内嵌的端点,若是还须要其余的也能够本身添加。例如health端点提供了基本的应用健康信息。app
每个独立的端点均可以进行选择暴露或者不暴露,默认状况下有些端点是开启的,若是不想暴露的话,那么能够在配置文件中进行配置endpoints + . + name
,举例以下:ide
endpoints.env.enabled=false
下面给出几个端点的简单介绍spring-boot
端点名 | 描述 | 是否默认暴露 |
---|---|---|
autoconfig | 展现出全部自动配置的报告,展现自动配置的先决条件,而且分段展现出配置成功的和配置失败的,而且展现出缘由,其中positiveMatches 是自动化配置成功的,negativeMatches 是自动化配置不成功的 |
true |
beans | 该端点用来获取应用上下文中建立的全部Bean | true |
configprops | 展现出来全部@ConfigurationProperties 的属性信息 |
true |
dump | 暴露出程序运行中的线程信息 | true |
env | 它用来获取应用全部可用的环境属性报告。包括:环境变量、JVM属性、应用的配置配置、命令行中的参数 | true |
health | 用来获取应用的各种健康指标信息 | true |
info | 该端点用来返回一些应用自定义的信息。默认状况下,该端点只会返回一个空的json内容。咱们能够在application.properties 配置文件中经过info前缀来设置一些属性 |
true |
metrics | 该端点用来返回当前应用的各种重要度量指标,好比:内存信息、线程信息、垃圾回收信息等 | true |
mappings | 展现出全部的@RequestMapping 路径 |
true |
trace | 该端点用来返回基本的HTTP跟踪信息。默认状况下,跟踪信息的存储采用 | true |
在添加了Actuator进行访问info端点的时候,咱们会发现页面中显示了一个空的json信息。若是想要显示信息的话,那么能够在配置文件中经过设置info.*
进行赋值,例如:gradle
info.app.encoding=UTF-8 info.app.java.source=1.8 info.app.java.target=1.8
这是访问localhost:8080/info
能够发现以下信息网站
{ "app": { "java": { "target": "1.8", "source": "1.8" }, "encoding": "UTF-8" }, "name": "BuXueWuShu" }
有时候自带的端点信息不符合咱们的需求,须要咱们自定义一些端点信息。在自定义端点信息以前咱们须要看一下Endpoint
这个SpringBoot中的类。
public interface Endpoint<T> { //暴露在外的Id值,例如health、env String getId(); //控制Id信息是否暴露 boolean isEnabled(); //用于权限的控制 boolean isSensitive(); //访问Id值返回的信息 T invoke(); }
发现暴露出来的端点都是实现了Endpoint
这个类,例如trace
这个端点。
@ConfigurationProperties(prefix = "endpoints.trace") public class TraceEndpoint extends AbstractEndpoint<List<Trace>> { private final TraceRepository repository; /** * Create a new {@link TraceEndpoint} instance. * @param repository the trace repository */ public TraceEndpoint(TraceRepository repository) { super("trace"); Assert.notNull(repository, "Repository must not be null"); this.repository = repository; } @Override public List<Trace> invoke() { return this.repository.findAll(); } }
而后发如今spring.factories
文件中自动配置了
org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration,\
点进去之后发如今其中已经自动将默认的端点注入进Spring容器中了
@Bean @ConditionalOnMissingBean -- 表示在容器中没有此实体Bean时建立 public TraceEndpoint traceEndpoint() { return new TraceEndpoint(this.traceRepository == null ? new InMemoryTraceRepository() : this.traceRepository); }
所以自定义端点也是相似的原理,咱们作个简单的以下:
public class MyEndPoint implements Endpoint { @Override public String getId() { return "buxuewushu"; } @Override public boolean isEnabled() { return true; } @Override public boolean isSensitive() { return true; } @Override public Object invoke() { User user=new User(); user.setName("不学无数"); user.setAddress("HeNan"); return user; } }
将其放入spring.factories
自动注入进容器中
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.example.FirstSpringBoot.EndPoint.MyEndPoint
而后启动项目输入localhost:8080/buxuewushu
出现如下的信息
{ "name": "不学无数", "address": "HeNan" }