Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exposing Actuator endpoints on different port

In my application I exposed actuator endpoints. Everything works until I tried to expose actuator endpoints on other port than my application. I'm trying to change port putting in my application.yml:

management.server.port=9001

If I try to use another port get error:

Parameter 4 of method webEndpointServletHandlerMapping in com.finture.pp.dm.ewmservices.configuration.SpringFoxConfig required a bean of type 'org.springframework.boot.actuate.autoconfigure.endpoint.web.CorsEndpointProperties' that could not be found.

Action:

Consider defining a bean of type 'org.springframework.boot.actuate.autoconfigure.endpoint.web.CorsEndpointProperties' in your configuration.

Class that uses CorsEndpointProperties Bean:

@Configuration
public class SpringFoxConfig {

  @Bean
  public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(
      WebEndpointsSupplier webEndpointsSupplier,
      ServletEndpointsSupplier servletEndpointsSupplier,
      ControllerEndpointsSupplier controllerEndpointsSupplier,
      EndpointMediaTypes endpointMediaTypes,
      CorsEndpointProperties corsProperties,
      WebEndpointProperties webEndpointProperties,
      Environment environment) {
//Do something
}

application.yml:

management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      group:
        readiness:
          include: db, rabbit
      show-details: ALWAYS
      probes:
        enabled: true
  server:
    port: 9001

Any idea how can I resolve this problem? Is there a way to expose actuator on different port in that case?

like image 887
David89 Avatar asked Oct 20 '25 04:10

David89


1 Answers

Editted

Previous Comment :

I use Spring Boot 2.7.14 and Java 11

Adding below annotation to Configuration class solved my problem. My application can run now, but i can not access actuator via management.server.port. Actuator still run on application port even I set management.server.port.

@EnableConfigurationProperties({CorsEndpointProperties.class})

Config class

@Configuration
@EnableConfigurationProperties({CorsEndpointProperties.class,
        WebEndpointProperties.class})
public class SwaggerBaseConfiguration {

    private final CorsEndpointProperties corsEndpointProperties;
    private final WebEndpointProperties webEndpointProperties;

    public SwaggerBaseConfiguration(CorsEndpointProperties corsEndpointProperties, WebEndpointProperties webEndpointProperties) {
        this.corsEndpointProperties = corsEndpointProperties;
        this.webEndpointProperties = webEndpointProperties;
    }

   // some beans

}

If i can solve management.server.port problem, i will edit this answer.

Final Solution :

I removed custom custom WebMvcEndpointHandlerMapping bean definition. After then, I faced below exception.

Failed to start bean 'documentationPluginsBootstrapper'

I added BeanPostProcessor bean and it solves it. My final config class is like below. After this changes i can expose actuator on different port.

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import springfox.documentation.spring.web.plugins.WebFluxRequestHandlerProvider;
import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Configuration
public class SwaggerBaseConfiguration {

    @Bean
    public InternalResourceViewResolver defaultViewResolver() {
        return new InternalResourceViewResolver();
    }

    @Bean
    public BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {
        return new BeanPostProcessor() {
            @Override
            public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
                if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider) {
                    customizeSpringfoxHandlerMappings(getHandlerMappings(bean));
                }
                return bean;
            }

            private <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings(List<T> mappings) {
                List<T> copy = mappings.stream().filter(mapping -> mapping.getPatternParser() == null)
                        .collect(Collectors.toList());
                mappings.clear();
                mappings.addAll(copy);
            }

            @SuppressWarnings("unchecked")
            private List<RequestMappingInfoHandlerMapping> getHandlerMappings(Object bean) {
                try {
                    Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings");
                    if (Objects.isNull(field)) {
                        return new ArrayList<>();
                    }
                    field.setAccessible(true);
                    return (List<RequestMappingInfoHandlerMapping>) field.get(bean);
                } catch (IllegalArgumentException | IllegalAccessException e) {
                    throw new IllegalStateException(e);
                }
            }
        };
    }

}
like image 176
enesgonez Avatar answered Oct 22 '25 20:10

enesgonez