Commit e0768a00 by Johannes Stelzer

Be a bit more lenient to model changes.

There is currently a problem if the Admin Client has version 1.2.0 and the Admin Server has version 1.2.1 due to changes to the model. The server is able to handle the old format but the client is not able to handle the new Format. To prevent this problem in future the response is not deserialized. Only the needed inforamtion (the id) is exctracted.
parent 946f3bd6
......@@ -16,6 +16,7 @@
package de.codecentric.boot.admin.services;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import org.slf4j.Logger;
......@@ -36,12 +37,11 @@ import de.codecentric.boot.admin.model.Application;
*/
public class ApplicationRegistrator {
private static final Logger LOGGER = LoggerFactory
.getLogger(ApplicationRegistrator.class);
private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationRegistrator.class);
private static HttpHeaders HTTP_HEADERS = createHttpHeaders();
private final AtomicReference<Application> registeredSelf = new AtomicReference<Application>();
private final AtomicReference<String> registeredId = new AtomicReference<String>();
private AdminClientProperties client;
......@@ -65,6 +65,7 @@ public class ApplicationRegistrator {
/**
* Registers the client application at spring-boot-admin-server.
*
* @return true if successful
*/
public boolean register() {
......@@ -73,12 +74,13 @@ public class ApplicationRegistrator {
try {
self = createApplication();
ResponseEntity<Application> response = template.postForEntity(adminUrl,
new HttpEntity<Application>(self, HTTP_HEADERS), Application.class);
@SuppressWarnings("rawtypes")
ResponseEntity<Map> response = template.postForEntity(adminUrl,
new HttpEntity<Application>(self, HTTP_HEADERS), Map.class);
if (response.getStatusCode().equals(HttpStatus.CREATED)) {
if (registeredSelf.get() == null) {
if (registeredSelf.compareAndSet(null, response.getBody())) {
if (registeredId.get() == null) {
if (registeredId.compareAndSet(null, response.getBody().get("id").toString())) {
LOGGER.info("Application registered itself as {}", response.getBody());
return true;
}
......@@ -86,46 +88,37 @@ public class ApplicationRegistrator {
LOGGER.debug("Application refreshed itself as {}", response.getBody());
return true;
} else {
LOGGER.warn("Application failed to registered itself as {}. Response: {}", self,
response.toString());
}
else {
LOGGER.warn(
"Application failed to registered itself as {}. Response: {}",
self, response.toString());
}
}
catch (Exception ex) {
LOGGER.warn(
"Failed to register application as {} at spring-boot-admin ({}): {}",
self, adminUrl, ex.getMessage());
} catch (Exception ex) {
LOGGER.warn("Failed to register application as {} at spring-boot-admin ({}): {}", self,
adminUrl, ex.getMessage());
}
return false;
}
public void deregister() {
Application self = registeredSelf.get();
if (self != null) {
String adminUrl = admin.getUrl() + '/' + admin.getContextPath() + "/"
+ self.getId();
registeredSelf.set(null);
String id = registeredId.get();
if (id != null) {
String adminUrl = admin.getUrl() + '/' + admin.getContextPath() + "/" + id;
try {
template.delete(adminUrl);
}
catch (Exception ex) {
registeredId.set(null);
} catch (Exception ex) {
LOGGER.warn(
"Failed to deregister application as {} at spring-boot-admin ({}): {}",
self, adminUrl, ex.getMessage());
"Failed to deregister application (id={}) at spring-boot-admin ({}): {}",
id, adminUrl, ex.getMessage());
}
}
}
protected Application createApplication() {
return Application.create(client.getName())
.withHealthUrl(client.getHealthUrl())
.withManagementUrl(client.getManagementUrl())
.withServiceUrl(client.getServiceUrl()).build();
return Application.create(client.getName()).withHealthUrl(client.getHealthUrl())
.withManagementUrl(client.getManagementUrl()).withServiceUrl(client.getServiceUrl())
.build();
}
}
......@@ -24,6 +24,7 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Collections;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
......@@ -65,46 +66,41 @@ public class ApplicationRegistratorTest {
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
}
@SuppressWarnings("rawtypes")
@Test
public void register_successful() {
when(
restTemplate.postForEntity(isA(String.class), isA(HttpEntity.class),
eq(Application.class))).thenReturn(
new ResponseEntity<Application>(HttpStatus.CREATED));
when(restTemplate.postForEntity(isA(String.class), isA(HttpEntity.class), eq(Map.class)))
.thenReturn(new ResponseEntity<Map>(Collections.singletonMap("id", "-id-"),
HttpStatus.CREATED));
boolean result = registrator.register();
assertTrue(result);
verify(restTemplate).postForEntity(
"http://sba:8080/api/applications",
new HttpEntity<Application>(Application.create("AppName")
.withHealthUrl("http://localhost:8080/health")
.withManagementUrl("http://localhost:8080/mgmt")
.withServiceUrl("http://localhost:8080").build(),
headers), Application.class);
verify(restTemplate)
.postForEntity("http://sba:8080/api/applications",
new HttpEntity<Application>(Application.create("AppName")
.withHealthUrl("http://localhost:8080/health")
.withManagementUrl("http://localhost:8080/mgmt")
.withServiceUrl("http://localhost:8080").build(), headers),
Map.class);
}
@Test
public void register_failed() {
when(
restTemplate.postForEntity(isA(String.class), isA(HttpEntity.class),
eq(Application.class))).thenThrow(
new RestClientException("Error"));
when(restTemplate.postForEntity(isA(String.class), isA(HttpEntity.class),
eq(Application.class))).thenThrow(new RestClientException("Error"));
boolean result = registrator.register();
assertFalse(result);
}
@SuppressWarnings("rawtypes")
@Test
public void deregister() {
when(
restTemplate.postForEntity(isA(String.class),
isA(HttpEntity.class), eq(Application.class)))
.thenReturn(
new ResponseEntity<Application>(Application
.create("foo").withId("-id-").build(),
HttpStatus.CREATED));
when(restTemplate.postForEntity(isA(String.class), isA(HttpEntity.class), eq(Map.class)))
.thenReturn(new ResponseEntity<Map>(Collections.singletonMap("id", "-id-"),
HttpStatus.CREATED));
registrator.register();
registrator.deregister();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment