spring-boot-starter-actuator配置案例;附带多配置文件抽取公共配置文件;

文章介绍了如何在SpringBoot项目中使用Actuator进行健康检查,包括引入依赖、配置文件的设定,如启用所有端点、关闭特定端点,以及如何处理未使用ES的健康检查问题。此外,还提到了通过公共配置文件实现多微服务的统一配置更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

需求需要为公司项目统一添加健康检查,存在很多微服务,难道要一个一个的修改配置文件吗?
可以使用公共配置文件,修改公共配置文件,做到一处修改,处处更新;


一、使用步骤

1.引入依赖

根pom.xml依赖管理:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>${spring-boot.version}</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

项目 pom.xml引入依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2.配置文件

抽取公共配置文件再引入或,将以下配置直接添加到现有配置文件中

#actuator监控
management:
  #  server:
  #    port: 8081 # 设置监控服务端口,如果没写默认就是服务器的端口
  endpoints:
    # 设置端点是否可用 默认只有shutdown可用
    enabled-by-default: true
    web:
      # 设置是否暴露端点 默认只有health和info可见
      exposure:
        # 包括所有端点
        include: "*" # 注意需要添加引号
        # 排除端点
        exclude: shutdown
      #访问地址
      #base-path: /actuator #默认
  endpoint:
    health:
      show-details: always
    #可以关闭指定的端点
    shutdown:
      enabled: false

2.1 直接添加到现有配置文件中

2.2 抽取公共配置文件再引入
  • 创建公共配置文件
    -在这里插入图片描述
  • bootstrap.yaml 引入
    shared-configs 共享配置
    dataId;group;refresh;
    在这里插入图片描述

3. 启动项目,访问地址

3.1 查询支持的检测

  ip:port/actuator  --> http://127.0.0.1:9333/actuator
{
  "_links": {
      其他 .... 
      "health": {
          "href": "http://127.0.0.1:9333/actuator/health", 
          "templated": false
      }, 
      其他 ....
  }
}

3.2 健康检测查询地址

 ip:port/actuator/health --> http://127.0.0.1:9333/actuator/health
{
    "status": "DOWN", 
    "components": {
        "db": {
            "status": "UP", 
            "details": {
                "database": "MySQL", 
                "validationQuery": "isValid()"
            }
        }, 
        "discoveryComposite": {
            "status": "UP", 
            "components": {
                "discoveryClient": {
                    "status": "UP", 
                    "details": {
                        "services": [
                            "erpa", 
                            "erpb", 
                            "mfc", 
                            "erpd", 
                            "erpe", 
                            "erpf"
                        ]
                    }
                }
            }
        }, 
        "diskSpace": {
            "status": "UP", 
            "details": {
                "total": 322122543104, 
                "free": 227919015936, 
                "threshold": 10485760, 
                "exists": true
            }
        }, 
        "elasticsearch": {
            "status": "DOWN", 
            "details": {
                "error": "java.net.ConnectException: Timeout connecting to [localhost/127.0.0.1:9200]"
            }
        }, 
        "nacosConfig": {
            "status": "UP"
        }, 
        "nacosDiscovery": {
            "status": "UP"
        }, 
        "ping": {
            "status": "UP"
        }, 
        "redis": {
            "status": "UP", 
            "details": {
                "version": "5.0.13"
            }
        }, 
        "refreshScope": {
            "status": "UP"
        }
    }
}

3.3 未使用ES导致健康检查失败

  • 排除无需检查的ES
#去除检查
management:
 health:
   elasticsearch:
     enabled: false
  • 再次检测
{
    "status": "UP", 
    "components": {
        "db": {
            "status": "UP", 
            "details": {
                "database": "MySQL", 
                "validationQuery": "isValid()"
            }
        }, 
        "discoveryComposite": {
            "status": "UP", 
            "components": {
                "discoveryClient": {
                    "status": "UP", 
                    "details": {
                        "services": [
                            "erp-a", 
                            "erp-b", 
                            "erp-c", 
                            "erp-d", 
                            "erp-e", 
                            "erp-f"
                        ]
                    }
                }
            }
        }, 
        "diskSpace": {
            "status": "UP", 
            "details": {
                "total": 322122543104, 
                "free": 227914055680, 
                "threshold": 10485760, 
                "exists": true
            }
        }, 
        "nacosConfig": {
            "status": "UP"
        }, 
        "nacosDiscovery": {
            "status": "UP"
        }, 
        "ping": {
            "status": "UP"
        }, 
        "redis": {
            "status": "UP", 
            "details": {
                "version": "5.0.13"
            }
        }, 
        "refreshScope": {
            "status": "UP"
        }
    }
}

总结

以上就是今天要讲的内容,本文仅仅简单介绍了spring-boot-actuator的简答配置使用,以及如何抽取公共配置.
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值