当前位置:   article > 正文

Springboot2.0集成actuator健康监控_spring boot actuator management.health.db.enabled

spring boot actuator management.health.db.enabled

1.引入Mavan依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-actuator</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.hateoas</groupId>
  7. <artifactId>spring-hateoas</artifactId>
  8. </dependency>

2.actuator 相关配置

修改application.yml文件

  1. server:
  2. port: 8081
  3. spring:
  4. application:
  5. name: springboot
  6. ## actuator配置
  7. management:
  8. endpoints:
  9. web:
  10. cors: # 跨域处理
  11. allowed-origins: '*'
  12. allowed-methods: '*'
  13. allow-credentials: true
  14. base-path: /manage # 访问路径 为 http://ip:port/monitor/manage
  15. exposure:
  16. include: '*'
  17. enabled-by-default: false
  18. server:
  19. servlet:
  20. context-path: /monitor # 访问路径 为 http://ip:port/monitor
  21. port: 1234 # management 的端口 http://ip:1234 如果不想通过HTTP公开端点,则可以将管理端口设置为-1,如以下示例所示:
  22. endpoint: #暴露出的endpoint
  23. shutdown:
  24. enabled: false # shutdown 可以关闭制定的端点 除shutdown以外的所有端点均已启用
  25. info:
  26. enabled: true
  27. health:
  28. enabled: true
  29. show-details: always #展示详情
  30. metrics:
  31. enabled: true
  32. health:
  33. db:
  34. enabled: false #关闭默认配置的db检查
  35. redis:
  36. enabled: false
  37. # /actuator/info 的信息的配置
  38. info:
  39. name: @project.artifactId@
  40. encoding: @project.build.sourceEncoding@
  41. java:
  42. source: @java.version@
  43. target: @java.version@

3.配置完成项目

      访问路径 http://ip:port/context-path/base-path/**

4.扩展actuator的监控功能

   自定义endPoint

  1. /**
  2. * @describe: 自定义端点暴漏此端点需要设置 management.endpoints.web.exposure.include=*
  3. */
  4. @Endpoint(id = "person")
  5. @Component
  6. public class MyEndpoint {
  7. private final Map<String, Person> people = new ConcurrentHashMap<>();
  8. MyEndpoint() {
  9. this.people.put("name1", new Person("n1"));
  10. this.people.put("name2", new Person("n2"));
  11. this.people.put("name3", new Person("3"));
  12. }
  13. @ReadOperation
  14. public List<Person> getAll() {
  15. return new ArrayList<>(this.people.values());
  16. }
  17. @ReadOperation
  18. public Person getPerson(@Selector String name) {
  19. return this.people.get(name);
  20. }
  21. @WriteOperation
  22. public void addOrUpdatePerson(@Selector String name, @Selector String person) {
  23. this.people.put(name, new Person(person));
  24. }
  25. class Person {
  26. private String name;
  27. Person(String name) {
  28. this.name = name;
  29. }
  30. public String getName() {
  31. return this.name;
  32. }
  33. }
  34. }

对health的扩展~实现接口方式

  1. @Component("myHealth_interface")
  2. public class MyHealthIndicator implements HealthIndicator {
  3. @Override
  4. public Health health() {
  5. //健康检查实现
  6. int errorCode = check();
  7. if (errorCode != 0) {
  8. return Health.down().withDetail("Error Code", errorCode).build();
  9. }
  10. return Health.up().build();
  11. }
  12. /**
  13. * 健康检查实现
  14. */
  15. public int check(){
  16. return 0;
  17. }
  18. }

对health的扩展~实现接口方式~实现抽象类

  1. @Component("myHealth_abstract")
  2. public class MyHealthIndicator1 extends AbstractHealthIndicator {
  3. @Override
  4. protected void doHealthCheck(Health.Builder builder) throws Exception {
  5. builder.up().withDetail("xxx", "xxxyyy");
  6. }
  7. }

扩展info信息

  1. public class MyContributor implements InfoContributor {
  2. @Value("${spring.application.name}")
  3. private String applicationName;
  4. @Override
  5. public void contribute(Info.Builder builder) {
  6. builder.withDetail("Application-info", Collections.singletonMap("name", applicationName));
  7. }
  8. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/856477
推荐阅读
相关标签
  

闽ICP备14008679号