One - One Code All

Blog Content

给java应用加上prometheus,解决/health访问报错Unauthorized

Java 容器化   2020-12-03 11:02:25

给java应用加上prometheus,解决/health访问报错Unauthorized


pom.xml添加依赖:



    org.springframework.boot
    spring-boot-starter-actuator


    org.springframework.boot
    spring-boot-actuator-autoconfigure


    io.micrometer
    micrometer-registry-prometheus

配置文件application.yaml添加管理端点:

management:
  server:
    port: 9090
  endpoints:
    web:
      exposure:
        include: health,info,env,metrics,prometheus
    base-path: /
  health:
    mail:
      enabled: false
    db:
      enabled: false
    ldap:
      enabled: false
    elasticsearch:
      enabled: false
    redis:
      enabled: false
  endpoint:
    health.show-details: always
    shutdown:
      enabled: true
  security:
    enabled: false

项目启动类添加注解:

@EnableAutoConfiguration(
        exclude = {
                org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration.class,
                org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class,
                org.springframework.boot.actuate.autoconfigure.security.servlet.ManagementWebSecurityAutoConfiguration.class
        })

当springboot2.0集成activiti7时,activiti使用了身份认证,集成后直接访问报错如下:

{
    "timestamp": "2019-12-13T02:54:05.254+0000",
    "status": 401,
    "error": "Unauthorized",
    "message": "Unauthorized",
    "path": "/"
}

解决办法:

启动类添加(排除认证的自动装配):

@SpringBootApplication(exclude ={SecurityAutoConfiguration.class, 
ManagementWebSecurityAutoConfiguration.class})

验证:

登录k8s pod,执行

curl http://localhost:9090/actuator/health
curl http://localhost:9090/actuator/prometheus



上一篇:k8s 挂载阿里云OSS

The minute you think of giving up, think of the reason why you held on so long.