1

I'm using Spring Boot 2.7.5 and I want to create an actuator endpoint, that acts as a proxy, and forwards all requests to a different server, running on the same JVM instance, but on a different port (say 8082). Here's the gist of it:

@Component
@RestControllerEndpoint(id = "myEndpoint", enableByDefault = true)
public class MyEndpoint {


    @RequestMapping("**") 
    public Object myEndpoint() {
        // TODO Forward everything to port 8082
        return ...
    }
}

What do I need to do in order to achieve this?

Update 1:

The port (8082) is not available from the internet so I can't do a simple redirect.

Update 2:

I don't want to forward the request to a Spring Controller or Spring Bean. Port 8082 is a separate server started in the same process.

Johan
  • 37,479
  • 32
  • 149
  • 237

3 Answers3

1

What if instead of a redirect (as suggested in other replies) your actuator would perform a call to localhost:8082 and return it's return it's response? You could also return a ResponseEntity instead of just an Object to cascade the HTTP codes of the performed request.

    @Component
    @RestControllerEndpoint(id = "myEndpoint", enableByDefault = true)
    public class MyEndpoint {
        private static final String TARGET_HOST = "localhost";
        private static final int TARGET_PORT = 8082;

        @RequestMapping("**") 
        public Object myEndpoint(HttpServletRequest originalRequest) {
            Uri targetUri = UriComponentsBuilder.fromHttpRequest(originalRequest)
                .host(TARGET_HOST)
                .port(TARGET_PORT)
                .build();
            Object responseBody = /* code to perfom the call using your preferred HTTP client*/;
            return responseBody;
        }
    }
Tiago Leite
  • 1,063
  • 1
  • 9
  • 15
0

Spring Boot Actuator endpoint that delegates all calls to a different port.

  • For different port use this in your application.properties file

    management.server.port=8081
    management.endpoints.web.exposure.include=*
    

This will expose all actuator endpoints on port 8081.

Moeez Atlas
  • 116
  • 2
  • 11
  • This won't help. The problem is not exposing actuator endpoints, I want an (actuator) endpoint on port 8081 that forwards all requests to another port. – Johan Nov 17 '22 at 14:06
  • Do you mean request forwarding? – qingmu Nov 21 '22 at 08:28
0

The official document describes :

Base path for Web endpoints. Relative to the servlet context path (server.servlet.context-path) or WebFlux base path (spring.webflux.base-path) when the management server is sharing the main server port. Relative to the management server base path (management.server.base-path) when a separate management server port (management.server.port) is configured.

You can configure the following

management:
  server:
    port: 8081 
    servlet:
      context-path:
  endpoints:
    web:
      base-path: /
      path-mapping:
        prometheus: metrics
      exposure:
        include: [ "prometheus" ] 

Tip : This configuration must be different with the server.ports must be different.

For more details, please check appendix.application-properties.actuator

qingmu
  • 402
  • 2
  • 12