使用 spring-boot-admin 通过 HTTP 监控非 spring-boot 应用程序
Using spring-boot-admin for monitoring non-spring-boot application via HTTP
我有一个用 "pure" Spring 编写的应用程序(Spring 4,没有 Spring 引导)。我想在 Spring Boot Admin 中与其他应用程序一起监控它。可能吗?我该怎么做?
仅检查健康状况对我来说就足够了。
我花了一些时间使用 Wireshark 和 "reverse engineered" 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);
}
}
由于某些原因,我无法将最新的 (9.0
) Tomcat 与 Spring 4.3.16
一起使用,所以我使用了 8.5.45
。
pom.xml
dependencies
: spring-webmvc, spring-core, javax.servlet-api (提供), tomcat-embed-core,tomcat-embed-jasper,jackson-databind。
2) 每 10 秒向 SBA 发布一次 "heartbeat"。我是用预定方法创建新 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 引导并启用实际 SBA 比尝试重新实现 SBA 要好得多。
我有一个用 "pure" Spring 编写的应用程序(Spring 4,没有 Spring 引导)。我想在 Spring Boot Admin 中与其他应用程序一起监控它。可能吗?我该怎么做?
仅检查健康状况对我来说就足够了。
我花了一些时间使用 Wireshark 和 "reverse engineered" 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);
}
}
由于某些原因,我无法将最新的 (9.0
) Tomcat 与 Spring 4.3.16
一起使用,所以我使用了 8.5.45
。
pom.xml
dependencies
: spring-webmvc, spring-core, javax.servlet-api (提供), tomcat-embed-core,tomcat-embed-jasper,jackson-databind。
2) 每 10 秒向 SBA 发布一次 "heartbeat"。我是用预定方法创建新 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 引导并启用实际 SBA 比尝试重新实现 SBA 要好得多。