【问题标题】:Using spring-boot-admin for monitoring non-spring-boot application via HTTP使用 spring-boot-admin 通过 HTTP 监控非 spring-boot 应用程序
【发布时间】:2019-09-12 08:23:17
【问题描述】:

我有一个用“纯”Spring(Spring 4,没有 Spring Boot)编写的应用程序。我想在 Spring Boot Admin 中与其他应用程序一起监视它。是否可以?我该怎么做?

只检查健康状况对我来说已经足够了。

【问题讨论】:

    标签: spring spring-boot-admin


    【解决方案1】:

    我花了一些时间在 Wireshark 和“逆向工程”的 SBA 通信上。我发现需要两件事:

    1) 将嵌入式 Tomcat 添加到模块并像这样设置RestController

    @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);
        }
    }
    

    由于某种原因,我无法使用 Spring 4.3.16 的最新 (9.0) Tomcat,所以我使用了 8.5.45

    pom.xmldependencies:spring-webmvc、spring-core、javax.servlet-api(提供)、tomcat-embed-core、tomcat-embed-jasper、jackson-databind。

    2) 每 10 秒向 SBA 发布一次“心跳”。我这样做是用预定的方法创建新的 bean:

    @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 是转换为 JSON 的对象,每 10 秒发送到 SBA。

    这两个步骤足以让我的模块在 SBA 上成为绿色 - 仅关于健康。添加对所有其他 SBA 功能的支持有点毫无意义 - 添加 Spring Boot 并启用实际 SBA 比尝试重新实现 SBA 要好得多。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-21
      • 2016-07-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-29
      • 2020-05-06
      • 2019-01-25
      相关资源
      最近更新 更多