Skip to content

Commit e0768a0

Browse files
author
Johannes Stelzer
committed
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.
1 parent 946f3bd commit e0768a0

File tree

2 files changed

+43
-54
lines changed

2 files changed

+43
-54
lines changed

spring-boot-admin-starter-client/src/main/java/de/codecentric/boot/admin/services/ApplicationRegistrator.java

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package de.codecentric.boot.admin.services;
1717

1818
import java.util.Collections;
19+
import java.util.Map;
1920
import java.util.concurrent.atomic.AtomicReference;
2021

2122
import org.slf4j.Logger;
@@ -36,12 +37,11 @@
3637
*/
3738
public class ApplicationRegistrator {
3839

39-
private static final Logger LOGGER = LoggerFactory
40-
.getLogger(ApplicationRegistrator.class);
40+
private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationRegistrator.class);
4141

4242
private static HttpHeaders HTTP_HEADERS = createHttpHeaders();
4343

44-
private final AtomicReference<Application> registeredSelf = new AtomicReference<Application>();
44+
private final AtomicReference<String> registeredId = new AtomicReference<String>();
4545

4646
private AdminClientProperties client;
4747

@@ -65,6 +65,7 @@ private static HttpHeaders createHttpHeaders() {
6565

6666
/**
6767
* Registers the client application at spring-boot-admin-server.
68+
*
6869
* @return true if successful
6970
*/
7071
public boolean register() {
@@ -73,59 +74,51 @@ public boolean register() {
7374
try {
7475
self = createApplication();
7576

76-
ResponseEntity<Application> response = template.postForEntity(adminUrl,
77-
new HttpEntity<Application>(self, HTTP_HEADERS), Application.class);
77+
@SuppressWarnings("rawtypes")
78+
ResponseEntity<Map> response = template.postForEntity(adminUrl,
79+
new HttpEntity<Application>(self, HTTP_HEADERS), Map.class);
7880

7981
if (response.getStatusCode().equals(HttpStatus.CREATED)) {
80-
if (registeredSelf.get() == null) {
81-
if (registeredSelf.compareAndSet(null, response.getBody())) {
82+
if (registeredId.get() == null) {
83+
if (registeredId.compareAndSet(null, response.getBody().get("id").toString())) {
8284
LOGGER.info("Application registered itself as {}", response.getBody());
8385
return true;
8486
}
8587
}
8688

8789
LOGGER.debug("Application refreshed itself as {}", response.getBody());
8890
return true;
91+
} else {
92+
LOGGER.warn("Application failed to registered itself as {}. Response: {}", self,
93+
response.toString());
8994
}
90-
else {
91-
LOGGER.warn(
92-
"Application failed to registered itself as {}. Response: {}",
93-
self, response.toString());
94-
}
95-
}
96-
catch (Exception ex) {
97-
LOGGER.warn(
98-
"Failed to register application as {} at spring-boot-admin ({}): {}",
99-
self, adminUrl, ex.getMessage());
95+
} catch (Exception ex) {
96+
LOGGER.warn("Failed to register application as {} at spring-boot-admin ({}): {}", self,
97+
adminUrl, ex.getMessage());
10098
}
10199

102100
return false;
103101
}
104102

105103
public void deregister() {
106-
Application self = registeredSelf.get();
107-
if (self != null) {
108-
String adminUrl = admin.getUrl() + '/' + admin.getContextPath() + "/"
109-
+ self.getId();
110-
111-
registeredSelf.set(null);
104+
String id = registeredId.get();
105+
if (id != null) {
106+
String adminUrl = admin.getUrl() + '/' + admin.getContextPath() + "/" + id;
112107

113108
try {
114109
template.delete(adminUrl);
115-
}
116-
catch (Exception ex) {
110+
registeredId.set(null);
111+
} catch (Exception ex) {
117112
LOGGER.warn(
118-
"Failed to deregister application as {} at spring-boot-admin ({}): {}",
119-
self, adminUrl, ex.getMessage());
113+
"Failed to deregister application (id={}) at spring-boot-admin ({}): {}",
114+
id, adminUrl, ex.getMessage());
120115
}
121116
}
122117
}
123118

124119
protected Application createApplication() {
125-
return Application.create(client.getName())
126-
.withHealthUrl(client.getHealthUrl())
127-
.withManagementUrl(client.getManagementUrl())
128-
.withServiceUrl(client.getServiceUrl()).build();
120+
return Application.create(client.getName()).withHealthUrl(client.getHealthUrl())
121+
.withManagementUrl(client.getManagementUrl()).withServiceUrl(client.getServiceUrl())
122+
.build();
129123
}
130124
}
131-

spring-boot-admin-starter-client/src/test/java/de/codecentric/boot/admin/services/ApplicationRegistratorTest.java

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import static org.mockito.Mockito.when;
2525

2626
import java.util.Collections;
27+
import java.util.Map;
2728

2829
import org.junit.Before;
2930
import org.junit.Test;
@@ -65,46 +66,41 @@ public void setup() {
6566
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
6667
}
6768

69+
@SuppressWarnings("rawtypes")
6870
@Test
6971
public void register_successful() {
70-
when(
71-
restTemplate.postForEntity(isA(String.class), isA(HttpEntity.class),
72-
eq(Application.class))).thenReturn(
73-
new ResponseEntity<Application>(HttpStatus.CREATED));
72+
when(restTemplate.postForEntity(isA(String.class), isA(HttpEntity.class), eq(Map.class)))
73+
.thenReturn(new ResponseEntity<Map>(Collections.singletonMap("id", "-id-"),
74+
HttpStatus.CREATED));
7475

7576
boolean result = registrator.register();
7677

7778
assertTrue(result);
78-
verify(restTemplate).postForEntity(
79-
"http://sba:8080/api/applications",
80-
new HttpEntity<Application>(Application.create("AppName")
81-
.withHealthUrl("http://localhost:8080/health")
82-
.withManagementUrl("http://localhost:8080/mgmt")
83-
.withServiceUrl("http://localhost:8080").build(),
84-
headers), Application.class);
79+
verify(restTemplate)
80+
.postForEntity("http://sba:8080/api/applications",
81+
new HttpEntity<Application>(Application.create("AppName")
82+
.withHealthUrl("http://localhost:8080/health")
83+
.withManagementUrl("http://localhost:8080/mgmt")
84+
.withServiceUrl("http://localhost:8080").build(), headers),
85+
Map.class);
8586
}
8687

8788
@Test
8889
public void register_failed() {
89-
when(
90-
restTemplate.postForEntity(isA(String.class), isA(HttpEntity.class),
91-
eq(Application.class))).thenThrow(
92-
new RestClientException("Error"));
90+
when(restTemplate.postForEntity(isA(String.class), isA(HttpEntity.class),
91+
eq(Application.class))).thenThrow(new RestClientException("Error"));
9392

9493
boolean result = registrator.register();
9594

9695
assertFalse(result);
9796
}
9897

98+
@SuppressWarnings("rawtypes")
9999
@Test
100100
public void deregister() {
101-
when(
102-
restTemplate.postForEntity(isA(String.class),
103-
isA(HttpEntity.class), eq(Application.class)))
104-
.thenReturn(
105-
new ResponseEntity<Application>(Application
106-
.create("foo").withId("-id-").build(),
107-
HttpStatus.CREATED));
101+
when(restTemplate.postForEntity(isA(String.class), isA(HttpEntity.class), eq(Map.class)))
102+
.thenReturn(new ResponseEntity<Map>(Collections.singletonMap("id", "-id-"),
103+
HttpStatus.CREATED));
108104
registrator.register();
109105
registrator.deregister();
110106

0 commit comments

Comments
 (0)