Use Spring Boot Actuator in Docker?

流过昼夜 提交于 2021-02-10 06:21:14

问题


I have got a Spring Boot Microservice with custom Spring Boot Acturators. When i run the Jar directly i can access all of my Acturators, when i run the same Jar inside a Docker-Image i get a 404 Error.

SecurityConfig:

@Configuration
public class ActuatorSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .requestMatcher(EndpointRequest.toAnyEndpoint())
                .anonymous()
                //.authorizeRequests()
                //.anyRequest().authenticated()
                .and()
                .httpBasic();
    }
}

Application.yaml:

spring:
    profiles: actuator
management.endpoints:
    web.exposure.include: "*"
    health.show-details: always

This is like the "Boilerplate-Code" of my Acturators:

@Component
@RestControllerEndpoint(id = "acturatorName")
public class acturatorNameActurator {
    @GetMapping(value = "/foo", produces = "application/json")
    public String bar(){
        return "{\"status\":\"started\"}";
    }
    ...
}

None of my Custom Acturators that work when i just run the Jar run Inside docker? /actuator/info Does work for example but /actuator/metrics doesnt. What can i do to fix this? Ty in advanced

Edit

  • Is maybe my SecurityConfiguration wrong? Does Spring maybe block the Request because the Container is in another (Docker) Network? But then i would get something different then 404 right?

  • Spring is Bind on IP 0.0.0.0 Port 8080, i can access my REST-Endpoints normally


回答1:


TL;DR

i forgot to change/add the actuator spring profile

Long Version

The problem was inside my Dockerfile:

FROM openjdk:8-jdk-alpine

ADD target/app.jar /jar/

VOLUME /tmp

EXPOSE 8080

ENV SPRINGPROFILES=prod

CMD ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "-Dserver.port=8080", "-Dserver.address=0.0.0.0", "/jar/app.jar", "--spring.profiles.active=${SPRINGPROFILES}"]
  1. i forgot to pass the SPRINGPROFILE variable (= prod,actuator)
  2. it did not recognize the variable

after i changed the dockerfile to

FROM openjdk:8-jdk-alpine

ADD target/app.jar /jar/

VOLUME /tmp

EXPOSE 8080

ENV SPRINGPROFILES=prod

ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom","-Dspring.profiles.active=${SPRINGPROFILES}","-jar", "-Dserver.port=8080", "-Dserver.address=0.0.0.0", "/jar/app.jar"]

and adding the env variable to my docker-compose-file it worked

environment:
          - "SPRINGPROFILES=prod,actuator"


来源:https://stackoverflow.com/questions/54052732/use-spring-boot-actuator-in-docker

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!