Skip to content
This repository was archived by the owner on Jun 29, 2018. It is now read-only.

Commit e9bba5d

Browse files
Johannes StelzerJohannes Stelzer
authored andcommitted
Rename SpringBootAdminRegistrator to ApplicationRegistrator; Do some polish and add deregister()
1 parent 7ad1142 commit e9bba5d

5 files changed

Lines changed: 87 additions & 60 deletions

File tree

spring-boot-admin-samples/spring-boot-admin-sample/src/main/resources/application.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ info:
33
stage: test
44

55
logging:
6-
file: boot-admin-sample.log
6+
file: target/boot-admin-sample.log
77

88
spring:
99
application:

spring-boot-admin-starter-client/src/main/java/de/codecentric/boot/admin/config/SpringBootAdminClientAutoConfiguration.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import org.springframework.web.client.RestTemplate;
3030

3131
import de.codecentric.boot.admin.actuate.LogfileMvcEndpoint;
32-
import de.codecentric.boot.admin.services.SpringBootAdminRegistrator;
32+
import de.codecentric.boot.admin.services.ApplicationRegistrator;
3333
import de.codecentric.boot.admin.web.BasicAuthHttpRequestInterceptor;
3434

3535
/**
@@ -46,8 +46,8 @@ public class SpringBootAdminClientAutoConfiguration {
4646
*/
4747
@Bean
4848
@ConditionalOnMissingBean
49-
public SpringBootAdminRegistrator registrator(AdminProperties adminProps, AdminClientProperties clientProps) {
50-
return new SpringBootAdminRegistrator(createRestTemplate(adminProps), adminProps, clientProps);
49+
public ApplicationRegistrator registrator(AdminProperties adminProps, AdminClientProperties clientProps) {
50+
return new ApplicationRegistrator(createRestTemplate(adminProps), adminProps, clientProps);
5151
}
5252

5353
protected RestTemplate createRestTemplate(AdminProperties adminProps) {
@@ -66,7 +66,7 @@ protected RestTemplate createRestTemplate(AdminProperties adminProps) {
6666
* TaskRegistrar that triggers the RegistratorTask every ten seconds.
6767
*/
6868
@Bean
69-
public ScheduledTaskRegistrar taskRegistrar(final SpringBootAdminRegistrator registrator, AdminProperties adminProps) {
69+
public ScheduledTaskRegistrar taskRegistrar(final ApplicationRegistrator registrator, AdminProperties adminProps) {
7070
ScheduledTaskRegistrar registrar = new ScheduledTaskRegistrar();
7171

7272
Runnable registratorTask = new Runnable() {

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

Lines changed: 49 additions & 20 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.concurrent.atomic.AtomicReference;
1920

2021
import org.slf4j.Logger;
2122
import org.slf4j.LoggerFactory;
@@ -33,22 +34,25 @@
3334
/**
3435
* Registers the client application at spring-boot-admin-server
3536
*/
36-
public class SpringBootAdminRegistrator {
37+
public class ApplicationRegistrator {
3738

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

4042
private static HttpHeaders HTTP_HEADERS = createHttpHeaders();
4143

42-
private AdminClientProperties clientProps;
44+
private final AtomicReference<Application> registeredSelf = new AtomicReference<Application>();
4345

44-
private AdminProperties adminProps;
46+
private AdminClientProperties client;
47+
48+
private AdminProperties admin;
4549

4650
private final RestTemplate template;
4751

48-
public SpringBootAdminRegistrator(RestTemplate template, AdminProperties adminProps,
49-
AdminClientProperties clientProps) {
50-
this.clientProps = clientProps;
51-
this.adminProps = adminProps;
52+
public ApplicationRegistrator(RestTemplate template, AdminProperties admin,
53+
AdminClientProperties client) {
54+
this.client = client;
55+
this.admin = admin;
5256
this.template = template;
5357
}
5458

@@ -64,34 +68,59 @@ private static HttpHeaders createHttpHeaders() {
6468
* @return true if successful
6569
*/
6670
public boolean register() {
67-
Application app = createApplication();
68-
String adminUrl = adminProps.getUrl() + '/' + adminProps.getContextPath();
71+
Application self = createApplication();
72+
String adminUrl = admin.getUrl() + '/' + admin.getContextPath();
6973

7074
try {
7175
ResponseEntity<Application> response = template.postForEntity(adminUrl,
72-
new HttpEntity<Application>(app, HTTP_HEADERS), Application.class);
76+
new HttpEntity<Application>(self, HTTP_HEADERS), Application.class);
7377

7478
if (response.getStatusCode().equals(HttpStatus.CREATED)) {
75-
LOGGER.debug("Application registered itself as {}", response.getBody());
79+
if (registeredSelf.get() == null) {
80+
if (registeredSelf.compareAndSet(null, response.getBody())) {
81+
LOGGER.info("Application registered itself as {}", response.getBody());
82+
return true;
83+
}
84+
}
85+
86+
LOGGER.debug("Application refreshed itself as {}", response.getBody());
7687
return true;
7788
}
78-
else if (response.getStatusCode().equals(HttpStatus.CONFLICT)) {
79-
LOGGER.warn("Application failed to registered itself as {} because of conflict in registry.", app);
80-
}
8189
else {
82-
LOGGER.warn("Application failed to registered itself as {}. Response: {}", app, response.toString());
90+
LOGGER.warn(
91+
"Application failed to registered itself as {}. Response: {}",
92+
self, response.toString());
8393
}
8494
}
8595
catch (Exception ex) {
86-
LOGGER.warn("Failed to register application as {} at spring-boot-admin ({}): {}", app, adminUrl,
87-
ex.getMessage());
96+
LOGGER.warn(
97+
"Failed to register application as {} at spring-boot-admin ({}): {}",
98+
self, adminUrl, ex.getMessage());
8899
}
89100

90101
return false;
91102
}
92103

104+
public void deregister() {
105+
Application self = registeredSelf.get();
106+
if (self != null) {
107+
String adminUrl = admin.getUrl() + '/' + admin.getContextPath() + "/"
108+
+ self.getId();
109+
110+
registeredSelf.set(null);
111+
112+
try {
113+
template.delete(adminUrl);
114+
}
115+
catch (Exception ex) {
116+
LOGGER.warn(
117+
"Failed to deregister application as {} at spring-boot-admin ({}): {}",
118+
self, adminUrl, ex.getMessage());
119+
}
120+
}
121+
}
122+
93123
protected Application createApplication() {
94-
Application app = new Application(clientProps.getUrl(), clientProps.getName());
95-
return app;
124+
return new Application(client.getUrl(), client.getName());
96125
}
97126
}

spring-boot-admin-starter-client/src/test/java/de/codecentric/boot/admin/config/SpringBootAdminClientAutoConfigurationTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
1111

1212
import de.codecentric.boot.admin.actuate.LogfileMvcEndpoint;
13-
import de.codecentric.boot.admin.services.SpringBootAdminRegistrator;
13+
import de.codecentric.boot.admin.services.ApplicationRegistrator;
1414

1515
public class SpringBootAdminClientAutoConfigurationTest {
1616

@@ -26,28 +26,28 @@ public void close() {
2626
@Test
2727
public void not_active() {
2828
load();
29-
assertTrue(context.getBeansOfType(SpringBootAdminRegistrator.class).isEmpty());
29+
assertTrue(context.getBeansOfType(ApplicationRegistrator.class).isEmpty());
3030
}
3131

3232
@Test
3333
public void active_nologfile() {
3434
load("spring.boot.admin.url:http://localhost:8081");
35-
context.getBean(SpringBootAdminRegistrator.class);
35+
context.getBean(ApplicationRegistrator.class);
3636
assertTrue(context.getBeansOfType(LogfileMvcEndpoint.class).isEmpty());
3737
}
3838

3939
@Test
4040
public void active_logfile() {
4141
load("spring.boot.admin.url:http://localhost:8081", "logging.file:spring.log");
4242
context.getBean(LogfileMvcEndpoint.class);
43-
context.getBean(SpringBootAdminRegistrator.class);
43+
context.getBean(ApplicationRegistrator.class);
4444
}
4545

4646
@Test
4747
public void active_logfile_supressed() {
4848
load("spring.boot.admin.url:http://localhost:8081", "logging.file:spring.log",
4949
"endpoints.logfile.enabled:false");
50-
context.getBean(SpringBootAdminRegistrator.class);
50+
context.getBean(ApplicationRegistrator.class);
5151
assertTrue(context.getBeansOfType(LogfileMvcEndpoint.class).isEmpty());
5252
}
5353

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

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import java.util.Collections;
2727

28+
import org.junit.Before;
2829
import org.junit.Test;
2930
import org.springframework.http.HttpEntity;
3031
import org.springframework.http.HttpHeaders;
@@ -38,30 +39,39 @@
3839
import de.codecentric.boot.admin.config.AdminProperties;
3940
import de.codecentric.boot.admin.model.Application;
4041

41-
public class SpringBootAdminRegistratorTest {
42+
public class ApplicationRegistratorTest {
43+
44+
private ApplicationRegistrator registrator;
45+
private RestTemplate restTemplate;
46+
private HttpHeaders headers;
47+
48+
@Before
49+
public void setup() {
50+
restTemplate = mock(RestTemplate.class);
4251

43-
@Test
44-
public void register_successful() {
4552
AdminProperties adminProps = new AdminProperties();
4653
adminProps.setUrl("http://sba:8080");
4754

4855
AdminClientProperties clientProps = new AdminClientProperties();
4956
clientProps.setUrl("http://localhost:8080");
5057
clientProps.setName("AppName");
5158

52-
RestTemplate restTemplate = mock(RestTemplate.class);
59+
registrator = new ApplicationRegistrator(restTemplate, adminProps, clientProps);
60+
61+
headers = new HttpHeaders();
62+
headers.setContentType(MediaType.APPLICATION_JSON);
63+
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
64+
}
65+
66+
@Test
67+
public void register_successful() {
5368
when(
5469
restTemplate.postForEntity(isA(String.class), isA(HttpEntity.class),
5570
eq(Application.class))).thenReturn(
5671
new ResponseEntity<Application>(HttpStatus.CREATED));
5772

58-
SpringBootAdminRegistrator registrator = new SpringBootAdminRegistrator(restTemplate, adminProps, clientProps);
5973
boolean result = registrator.register();
6074

61-
HttpHeaders headers = new HttpHeaders();
62-
headers.setContentType(MediaType.APPLICATION_JSON);
63-
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
64-
6575
assertTrue(result);
6676
verify(restTemplate).postForEntity("http://sba:8080/api/applications",
6777
new HttpEntity<Application>(new Application("http://localhost:8080",
@@ -71,38 +81,26 @@ public void register_successful() {
7181

7282
@Test
7383
public void register_failed() {
74-
AdminProperties adminProps = new AdminProperties();
75-
adminProps.setUrl("http://sba:8080");
76-
AdminClientProperties clientProps = new AdminClientProperties();
77-
clientProps.setUrl("http://localhost:8080");
78-
clientProps.setName("AppName");
79-
80-
RestTemplate restTemplate = mock(RestTemplate.class);
8184
when(restTemplate.postForEntity(isA(String.class), isA(Application.class), eq(Application.class))).thenThrow(
8285
new RestClientException("Error"));
8386

84-
SpringBootAdminRegistrator registrator = new SpringBootAdminRegistrator(restTemplate, adminProps, clientProps);
8587
boolean result = registrator.register();
8688

8789
assertFalse(result);
8890
}
8991

9092
@Test
91-
public void register_failed_conflict() {
92-
AdminProperties adminProps = new AdminProperties();
93-
adminProps.setUrl("http://sba:8080");
94-
AdminClientProperties clientProps = new AdminClientProperties();
95-
clientProps.setUrl("http://localhost:8080");
96-
clientProps.setName("AppName");
97-
98-
RestTemplate restTemplate = mock(RestTemplate.class);
99-
when(restTemplate.postForEntity(isA(String.class), isA(Application.class), eq(Application.class))).thenReturn(
100-
new ResponseEntity<Application>(HttpStatus.CONFLICT));
93+
public void deregister() {
94+
when(
95+
restTemplate.postForEntity(isA(String.class), isA(HttpEntity.class),
96+
eq(Application.class))).thenReturn(
97+
new ResponseEntity<Application>(new Application("http://test", "url",
98+
"-id-"), HttpStatus.CREATED));
10199

102-
SpringBootAdminRegistrator registrator = new SpringBootAdminRegistrator(restTemplate, adminProps, clientProps);
103-
boolean result = registrator.register();
100+
registrator.register();
101+
registrator.deregister();
104102

105-
assertFalse(result);
103+
verify(restTemplate).delete("http://sba:8080/api/applications/-id-");
106104
}
107105

108106
}

0 commit comments

Comments
 (0)