Search code examples
springspring-boot-admin

Using spring-boot-admin for monitoring non-spring-boot application via HTTP


I have an application written in "pure" Spring (Spring 4, without Spring Boot). I would like to monitor it together with other apps in Spring Boot Admin. Is it possible? How can I do it?

Checking only health is good enough for me.


Solution

  • I spend some time with Wireshark and "reverse engineered" SBA communication. I found out two things are required:

    1) Adding embedded Tomcat to module and setting up RestController like this:

    @RestController
    @RequestMapping(value = "/")
    public class HealthRestController {
    
        @RequestMapping(path = "health", method = RequestMethod.GET)
        @ResponseBody
        public ResponseEntity health() {
            final String body = "{\"status\": \"UP\"}";
            final MultiValueMap<String, String> headers = new HttpHeaders();
            headers.set(HttpHeaders.CONTENT_TYPE, "application/vnd.spring-boot.actuator.v1+json;charset=UTF-8");
            return new ResponseEntity<>(body, headers, HttpStatus.OK);
        }
    }
    

    For some reason, I was unable to use latest (9.0) Tomcat with Spring 4.3.16, so I used 8.5.45.

    pom.xml dependencies: spring-webmvc, spring-core, javax.servlet-api (provided), tomcat-embed-core, tomcat-embed-jasper, jackson-databind.

    2) Posting "heartbeat" to SBA every 10 seconds. I did it be creating new bean with scheduled method:

    @Component
    public class HeartbeatScheduledController {
    
        private static final String APPLICATION_URL = "http://myapp.example.com:8080/";
        private static final String HEALTH_URL = APPLICATION_URL + "health";
        private static final String SBA_URL = "http://sba.example.com/instances";
    
        @Scheduled(fixedRate = 10_000)
        public void postStatusToSBA() {
            StatusDTO statusDTO = new StatusDTO("MyModuleName", APPLICATION_URL, HEALTH_URL, APPLICATION_URL);
            final RestTemplate restTemplate = new RestTemplate();
            final HttpHeaders headers = new HttpHeaders();
            headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
            headers.setContentType(MediaType.APPLICATION_JSON);
            HttpEntity<Object> entity = new HttpEntity<>(statusDTO, headers);
            ResponseEntity<String> response = restTemplate.exchange(SBA_URL, HttpMethod.POST, entity, String.class);
        }
    
        public static class StatusDTO {
            private String name;
            private String managementUrl;
            private String healthUrl;
            private String serviceUrl;
            private Map<String, String> metadata;
        }
    }
    

    StatusDTO is object transformed to JSON, send to SBA every 10 seconds.

    This two steps were enough to get my module green on SBA - regarding health only. Adding support for all other SBA features is a bit pointless - its much better to add Spring Boot and enable actual SBA than try to reimplement SBA.