我有一个用“纯”Spring (Spring4,没有Spring Boot)编写的应用程序。我想和Spring Boot Admin中的其他应用程序一起监控它。有可能吗?我该怎么做呢?
对我来说,只检查健康状况就足够了。
发布于 2019-11-14 23:46:12
我花了一些时间与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.xml dependencies:spring-webmvc、spring-core、javax.servlet-api (已提供)、tomcat-embed-core、tomcat-embed-jasper、jackson-databind。
2)每10秒向SBA发送一次心跳。我用scheduled方法创建了一个新的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是object转换为JSON,每10秒发送一次。
这两个步骤足以让我的模块在SBA上变绿--仅仅是关于健康。添加对所有其他SBA特性的支持是没有意义的--添加Spring Boot并启用实际的SBA比尝试重新实现SBA要好得多。
https://stackoverflow.com/questions/57902622
复制相似问题