天天看点

SpringBootAdmin结合Eureka注册中心自定义监控路径的设置

监控管理依赖:implementation 'org.springframework.boot:spring-boot-starter-actuator'
           

默认情况下监控根路径为:http://ip:port/actuator,port默认取server.port

下面说一下自定义监控路径和端口的方法:(下面只贴出部分关键配置,其他配置参考别的地方的资料)

微服务中添加如下配置:application.yml:

management:
  server:
     #自定义监控端口
     port: ${CONFIG_SERVICE_MGR_PORT:9009}
#    servlet:
      #设置健康监控的servlet路径,该配置需要配合management.server.servlet.port才起效
      #不设置默认使用server.servlet.context-path
#      context-path: /ctx
  endpoints:
    web:
      #默认/actuator, 自定义为/monitor
      base-path: /monitor
      exposure:
        include: '*'
      #允许跨域请求,支持GET/POST
      cors:
        allowed-origins: http://hello.com
        allowed-method: GET,POST
  endpoint:
    #允许调用shutdown停止监控进程
    shutdown:
      enabled: true
    #启用健康检查
    health:
      enabled: true
      show-details: when-authorized
  health:
    #启用默认的所有默认的健康检查indicator(指标)
#    defaults:
#      enable: true
    #启用磁盘检查
    diskspace:
      #可以定义检查的路径
      path: ./
      enabled: true
      threshold: 500MB
           

上述配置将默认http://ip:{server.port}/actuator配置为http://ip:自定义端口/monitor

此时服务注册到Eureka上还是默认监控路径/actuator, 需要下面的配置指定监控路径告诉Eureka和SpringBootAdmin当前监控的基础路径和健康检查的url等信息:

追加配置:

#eureka-client注册中心配置
eureka:
  instance:
    #spring-boot-admin中DefaultServiceInstanceConverter获取的metadata转换具体监控地址,默认是/actuator,
    #因为上面改变了默认路径/actuator,所以这里要指明监控路径
    metadata-map:
      management.context-path: /monitor
    #健康监控地址变化,需要另外配置,在eureka注册中心状态地址才能正确
    statusPageUrlPath: /monitor/info
    healthCheckUrlPath: /monitor/health
           

此时SpringBootAdmin中获取的就是正确的监控地址了,英文水平有限,看官方文档management.context-path不知道是放在哪配置的,启动时候springBootAdmin获取的还是/actuator,纠结了好久,最后费劲巴拉的看debug日志发现服务注册到Eureka中心时候携带的内容包含一个management.port在metadata里面,之后查看springBootAdmin日志发现有条日志:

de.codecentric.boot.admin.server.cloud.discovery.DefaultServiceInstanceConverter- Converting service 'CONFIG' running at 'http://192.168.72.115:9000' with metadata [email protected]
de.codecentric.boot.admin.server.cloud.discovery.InstanceDiscoveryListener- Registering discovered instance Registration(name=CONFIG, managementUrl=http://192.168.72.115:9009/monitor, healthUrl=http://192.168.72.115:9009/monitor/health, serviceUrl=http://192.168.72.115:9000/, source=discovery)
           

在从注册中心获取到服务以后,使用DefaultServiceInstanceConverter把注册信息转换了一次,查看类DefaultServiceInstanceConverter,里面包含监控转换的key,其中就有management.context-path。

各位可以看一下debug日志,日志很多,下面贴一下有用的日志,能过滤 吊 一小部分不用的。

Eureka注册中心:

logging:
  level:
    root: INFO
    org.apache.http.impl.conn.DefaultClientConnection: DEBUG
    org.apache.http.wire: DEBUG
    com.netflix.discovery: DEBUG
    org.apache.tomcat.util.net.NioEndpoint: DEBUG
    com.netflix.eureka: DEBUG
    org.apache.coyote.http11.Http11InputBuffer: DEBUG
    org.springframework.boot.actuate: DEBUG
    org.springframework.security: DEBUG
           

SpringBootAdmin日志配置:

logging:
  level:
    root: INFO
    reactor.netty: DEBUG
    org.springframework.web: DEBUG
    org.springframework.cloud.config: DEBUG
    de.codecentric.boot.admin.server: DEBUG
    org.springframework.security.web: DEBUG
    org.apache.http.impl.conn.DefaultClientConnection: DEBUG
           
SpringBootAdmin结合Eureka注册中心自定义监控路径的设置

参考:spring-boot-admin官方文档