首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用spring-boot-admin通过HTTP监控非spring-boot应用程序

使用spring-boot-admin通过HTTP监控非spring-boot应用程序
EN

Stack Overflow用户
提问于 2019-09-12 16:23:18
回答 1查看 369关注 0票数 0

我有一个用“纯”Spring (Spring4,没有Spring Boot)编写的应用程序。我想和Spring Boot Admin中的其他应用程序一起监控它。有可能吗?我该怎么做呢?

对我来说,只检查健康状况就足够了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-14 23:46:12

我花了一些时间与Wireshark和“逆向工程”的SBA通信。我发现有两件事是必须的:

1)将嵌入式Tomcat添加到模块中,设置RestController如下:

代码语言:javascript
复制
@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.xml dependencies:spring-webmvc、spring-core、javax.servlet-api (已提供)、tomcat-embed-core、tomcat-embed-jasper、jackson-databind。

2)每10秒向SBA发送一次心跳。我用scheduled方法创建了一个新的bean:

代码语言:javascript
复制
@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是object转换为JSON,每10秒发送一次。

这两个步骤足以让我的模块在SBA上变绿--仅仅是关于健康。添加对所有其他SBA特性的支持是没有意义的--添加Spring Boot并启用实际的SBA比尝试重新实现SBA要好得多。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57902622

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档