@javazjm
2017-09-10T17:48:48.000000Z
字数 4204
阅读 1873
Springboot
监控
官方文档:Spring Boot Actuator
<!-- 监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 监控认证需要 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
#管理端点上下文路径,默认项目根目录;http://ip:端口/项目名/manage/端点名
management.context-path=/manage
#更改端点默认端口
management.port=9090
#开启所有端点
endpoints.enabled=true
#开启敏感(需使用用户名,密码登录查看)
#endpoints.sensitive=true
#开启认证
security.basic.enabled=true
#针对/manage路径进行认证
security.basic.path=/manage
#认证使用的用户名
security.user.name=admin
#认证使用的密码
security.user.password=Zhxabc
#角色
management.security.role=SUPERUSER
#不需安全认证的端点
#endpoints.info.sensitive=false
#endpoints.health.sensitive=false
#设置作者
info.author.realname = 张晋苗
info.author.nickname = jimzhang
#logfile端点测试失败了
logging.file=logs.ingfo
logging.path=/file
#启用shutdown 关闭路径:post 方式 curl -u admin:Zhxabc -X POST http://localhost:9095/xiamen-smzf/manage/shutdown
endpoints.shutdown.enabled=true
各端点的使用,查看docs文档,需加入依赖spring-boot-actuator-docs,
访问:http://localhost:9095/xiamen-smzf/manage/docs/
{
status: "UP",
diskSpace: {
status: "UP",
total: 500104687616,
free: 311986614272,
threshold: 10485760
},
db: {
status: "UP",
database: "MySQL",
hello: 1
}
}
info 查看应用信息
默认情况下,只会返回一个空的 json 内容。我们可以在 application.properties 配置文件中通过 info 前缀来设置一些属性
metrics 应用基本指标
获取当前应用的各类重要度量指标,比如:内存信息、线程信息、垃圾回收信息等
trace 基本的HTTP跟踪信息
查看基本的 HTTP 跟踪信息。默认情况下,跟踪信息的存储采用 org.springframework.boot.actuate.trace.InMemoryTraceRepository 实现的内存方式,始终保留最近的 100 条请求记录。
shutdown关闭当前应用
认是不启用的,我们可以在 application.properties 中开启。此外,shutdown 端点不支持 GET 请求,我们需要通过 POST 方法访问。
endpoints.shutdown.enabled=true
命令:
curl -u admin:Zhxabc -X POST http://127.0.0.1:9095/xiamen-smzf/manage/shutdown
内置 HealthIndicator 监控检测
Name | Description |
---|---|
CassandraHealthIndicator | Checks that a Cassandra database is up. |
DiskSpaceHealthIndicator | Checks for low disk space. |
DataSourceHealthIndicator | Checks that a connection to DataSource can be obtained. |
ElasticsearchHealthIndicator | Checks that an Elasticsearch cluster is up. |
JmsHealthIndicator | Checks that a JMS broker is up. |
MailHealthIndicator | Checks that a mail server is up. |
MongoHealthIndicator | Checks that a Mongo database is up. |
RabbitHealthIndicator | Checks that a Rabbit server is up. |
RedisHealthIndicator | Checks that a Redis server is up. |
SolrHealthIndicator | Checks that a Solr server is up. |
两种方式:
实现HealthIndicator 接口
继承AbstractHealthIndicator 类(推荐)
参考系统已实现的类,模仿着写。
/**
* 自定义磁盘空间健康检测类
* Created by admin on 2017/8/2.
*/
@Component
public class MyDiskSpaceHealthIndicator extends AbstractHealthIndicator {
private final FileStore fileStore;
private final long thresholdBytes;
@Autowired
public MyDiskSpaceHealthIndicator(@Value("${health.filestore.path:/}") String path,
@Value("${health.filestore.threshold.bytes:10485760}")long thresholdBytes) throws IOException {
fileStore = Files.getFileStore(Paths.get(path));
this.thresholdBytes = thresholdBytes;
}
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
long diskFreeInBytes = fileStore.getUnallocatedSpace();
if (diskFreeInBytes >= thresholdBytes) {
builder.up();
}else {
builder.down();
}
long totalSpace = fileStore.getTotalSpace();
builder.withDetail("disk.free", diskFreeInBytes);
builder.withDetail("disk.total", totalSpace);
}
}
访问:
http://localhost:8081/manage/health
输出:
{
status: "UP",
myDiskSpace: {
status: "UP",
disk.free: 307116134400,
disk.total: 500104687616
},
rocketMQ: {
status: "UP"
},
diskSpace: {
status: "UP",
total: 500104687616,
free: 307116134400,
threshold: 10485760
}
}
参考系统已实现的类,模仿着写。
1.继承 AbstractEndpoint 抽象类
/**
* 查看服务器的当前时间
* Created by admin on 2017/8/2.
*/
@ConfigurationProperties(prefix = "endpoints.servertime")
public class ServerTimeEndpoint extends AbstractEndpoint<Map<String, Object>> {
public ServerTimeEndpoint() {
super("servertime", false); // servertime 端点名称
}
@Override
public Map<String, Object> invoke() {
Map<String, Object> result = new HashMap<String, Object>();
DateTime dateTime = DateTime.now();
result.put("server_time", dateTime.toString());
result.put("ms_format", dateTime.getMillis());
return result;
}
}
2.创建端点配置类
@Configuration
public class EndpointConfig {
@Bean
public static Endpoint<Map<String, Object>> servertime() {
return new ServerTimeEndpoint();
}
}
s输出:
{
server_time: "2017-09-07T15:10:17.910+08:00",
ms_format: 1504768217910
}