[关闭]
@javazjm 2017-09-10T09:48:48.000000Z 字数 4204 阅读 1743

SpringBoot系列学习七:应用监控

Springboot 监控


官方文档:Spring Boot Actuator

模板

1. 依赖

  1. <!-- 监控-->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-actuator</artifactId>
  5. </dependency>
  6. <!-- 监控认证需要 -->
  7. <dependency>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-security</artifactId>
  10. </dependency>

2. 通用配置

  1. #管理端点上下文路径,默认项目根目录;http://ip:端口/项目名/manage/端点名
  2. management.context-path=/manage
  3. #更改端点默认端口
  4. management.port=9090
  5. #开启所有端点
  6. endpoints.enabled=true
  7. #开启敏感(需使用用户名,密码登录查看)
  8. #endpoints.sensitive=true
  9. #开启认证
  10. security.basic.enabled=true
  11. #针对/manage路径进行认证
  12. security.basic.path=/manage
  13. #认证使用的用户名
  14. security.user.name=admin
  15. #认证使用的密码
  16. security.user.password=Zhxabc
  17. #角色
  18. management.security.role=SUPERUSER
  19. #不需安全认证的端点
  20. #endpoints.info.sensitive=false
  21. #endpoints.health.sensitive=false
  22. #设置作者
  23. info.author.realname = 张晋苗
  24. info.author.nickname = jimzhang
  25. #logfile端点测试失败了
  26. logging.file=logs.ingfo
  27. logging.path=/file
  28. #启用shutdown 关闭路径:post 方式 curl -u admin:Zhxabc -X POST http://localhost:9095/xiamen-smzf/manage/shutdown
  29. endpoints.shutdown.enabled=true

2.1 详解

各端点的使用,查看docs文档,需加入依赖spring-boot-actuator-docs,
访问:http://localhost:9095/xiamen-smzf/manage/docs/

  1. {
  2. status: "UP",
  3. diskSpace: {
  4. status: "UP",
  5. total: 500104687616,
  6. free: 311986614272,
  7. threshold: 10485760
  8. },
  9. db: {
  10. status: "UP",
  11. database: "MySQL",
  12. hello: 1
  13. }
  14. }
  1. endpoints.shutdown.enabled=true

命令:
curl -u admin:Zhxabc -X POST http://127.0.0.1:9095/xiamen-smzf/manage/shutdown

3. 自定义监控

3.1 HealthIndicator 监控检测

内置 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.

两种方式:

  1. 实现HealthIndicator 接口

  2. 继承AbstractHealthIndicator 类(推荐)
    参考系统已实现的类,模仿着写。

  1. /**
  2. * 自定义磁盘空间健康检测类
  3. * Created by admin on 2017/8/2.
  4. */
  5. @Component
  6. public class MyDiskSpaceHealthIndicator extends AbstractHealthIndicator {
  7. private final FileStore fileStore;
  8. private final long thresholdBytes;
  9. @Autowired
  10. public MyDiskSpaceHealthIndicator(@Value("${health.filestore.path:/}") String path,
  11. @Value("${health.filestore.threshold.bytes:10485760}")long thresholdBytes) throws IOException {
  12. fileStore = Files.getFileStore(Paths.get(path));
  13. this.thresholdBytes = thresholdBytes;
  14. }
  15. @Override
  16. protected void doHealthCheck(Health.Builder builder) throws Exception {
  17. long diskFreeInBytes = fileStore.getUnallocatedSpace();
  18. if (diskFreeInBytes >= thresholdBytes) {
  19. builder.up();
  20. }else {
  21. builder.down();
  22. }
  23. long totalSpace = fileStore.getTotalSpace();
  24. builder.withDetail("disk.free", diskFreeInBytes);
  25. builder.withDetail("disk.total", totalSpace);
  26. }
  27. }

访问:
http://localhost:8081/manage/health
输出:

  1. {
  2. status: "UP",
  3. myDiskSpace: {
  4. status: "UP",
  5. disk.free: 307116134400,
  6. disk.total: 500104687616
  7. },
  8. rocketMQ: {
  9. status: "UP"
  10. },
  11. diskSpace: {
  12. status: "UP",
  13. total: 500104687616,
  14. free: 307116134400,
  15. threshold: 10485760
  16. }
  17. }

3.2 自定义端点

参考系统已实现的类,模仿着写。

1.继承 AbstractEndpoint 抽象类

  1. /**
  2. * 查看服务器的当前时间
  3. * Created by admin on 2017/8/2.
  4. */
  5. @ConfigurationProperties(prefix = "endpoints.servertime")
  6. public class ServerTimeEndpoint extends AbstractEndpoint<Map<String, Object>> {
  7. public ServerTimeEndpoint() {
  8. super("servertime", false); // servertime 端点名称
  9. }
  10. @Override
  11. public Map<String, Object> invoke() {
  12. Map<String, Object> result = new HashMap<String, Object>();
  13. DateTime dateTime = DateTime.now();
  14. result.put("server_time", dateTime.toString());
  15. result.put("ms_format", dateTime.getMillis());
  16. return result;
  17. }
  18. }

2.创建端点配置类

  1. @Configuration
  2. public class EndpointConfig {
  3. @Bean
  4. public static Endpoint<Map<String, Object>> servertime() {
  5. return new ServerTimeEndpoint();
  6. }
  7. }
  1. 运行访问
    http://localhost:8081/servertime

s输出:

  1. {
  2. server_time: "2017-09-07T15:10:17.910+08:00",
  3. ms_format: 1504768217910
  4. }

参考:

HTTP 应用监控
didi

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注