Commit e9bba5d6 by Johannes Stelzer

Rename SpringBootAdminRegistrator to ApplicationRegistrator; Do some polish and add deregister()

parent 7ad11421
...@@ -3,7 +3,7 @@ info: ...@@ -3,7 +3,7 @@ info:
stage: test stage: test
logging: logging:
file: boot-admin-sample.log file: target/boot-admin-sample.log
spring: spring:
application: application:
......
...@@ -29,7 +29,7 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar; ...@@ -29,7 +29,7 @@ import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import de.codecentric.boot.admin.actuate.LogfileMvcEndpoint; import de.codecentric.boot.admin.actuate.LogfileMvcEndpoint;
import de.codecentric.boot.admin.services.SpringBootAdminRegistrator; import de.codecentric.boot.admin.services.ApplicationRegistrator;
import de.codecentric.boot.admin.web.BasicAuthHttpRequestInterceptor; import de.codecentric.boot.admin.web.BasicAuthHttpRequestInterceptor;
/** /**
...@@ -46,8 +46,8 @@ public class SpringBootAdminClientAutoConfiguration { ...@@ -46,8 +46,8 @@ public class SpringBootAdminClientAutoConfiguration {
*/ */
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public SpringBootAdminRegistrator registrator(AdminProperties adminProps, AdminClientProperties clientProps) { public ApplicationRegistrator registrator(AdminProperties adminProps, AdminClientProperties clientProps) {
return new SpringBootAdminRegistrator(createRestTemplate(adminProps), adminProps, clientProps); return new ApplicationRegistrator(createRestTemplate(adminProps), adminProps, clientProps);
} }
protected RestTemplate createRestTemplate(AdminProperties adminProps) { protected RestTemplate createRestTemplate(AdminProperties adminProps) {
...@@ -66,7 +66,7 @@ public class SpringBootAdminClientAutoConfiguration { ...@@ -66,7 +66,7 @@ public class SpringBootAdminClientAutoConfiguration {
* TaskRegistrar that triggers the RegistratorTask every ten seconds. * TaskRegistrar that triggers the RegistratorTask every ten seconds.
*/ */
@Bean @Bean
public ScheduledTaskRegistrar taskRegistrar(final SpringBootAdminRegistrator registrator, AdminProperties adminProps) { public ScheduledTaskRegistrar taskRegistrar(final ApplicationRegistrator registrator, AdminProperties adminProps) {
ScheduledTaskRegistrar registrar = new ScheduledTaskRegistrar(); ScheduledTaskRegistrar registrar = new ScheduledTaskRegistrar();
Runnable registratorTask = new Runnable() { Runnable registratorTask = new Runnable() {
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
package de.codecentric.boot.admin.services; package de.codecentric.boot.admin.services;
import java.util.Collections; import java.util.Collections;
import java.util.concurrent.atomic.AtomicReference;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -33,22 +34,25 @@ import de.codecentric.boot.admin.model.Application; ...@@ -33,22 +34,25 @@ import de.codecentric.boot.admin.model.Application;
/** /**
* Registers the client application at spring-boot-admin-server * Registers the client application at spring-boot-admin-server
*/ */
public class SpringBootAdminRegistrator { public class ApplicationRegistrator {
private static final Logger LOGGER = LoggerFactory.getLogger(SpringBootAdminRegistrator.class); private static final Logger LOGGER = LoggerFactory
.getLogger(ApplicationRegistrator.class);
private static HttpHeaders HTTP_HEADERS = createHttpHeaders(); private static HttpHeaders HTTP_HEADERS = createHttpHeaders();
private AdminClientProperties clientProps; private final AtomicReference<Application> registeredSelf = new AtomicReference<Application>();
private AdminProperties adminProps; private AdminClientProperties client;
private AdminProperties admin;
private final RestTemplate template; private final RestTemplate template;
public SpringBootAdminRegistrator(RestTemplate template, AdminProperties adminProps, public ApplicationRegistrator(RestTemplate template, AdminProperties admin,
AdminClientProperties clientProps) { AdminClientProperties client) {
this.clientProps = clientProps; this.client = client;
this.adminProps = adminProps; this.admin = admin;
this.template = template; this.template = template;
} }
...@@ -64,34 +68,59 @@ public class SpringBootAdminRegistrator { ...@@ -64,34 +68,59 @@ public class SpringBootAdminRegistrator {
* @return true if successful * @return true if successful
*/ */
public boolean register() { public boolean register() {
Application app = createApplication(); Application self = createApplication();
String adminUrl = adminProps.getUrl() + '/' + adminProps.getContextPath(); String adminUrl = admin.getUrl() + '/' + admin.getContextPath();
try { try {
ResponseEntity<Application> response = template.postForEntity(adminUrl, ResponseEntity<Application> response = template.postForEntity(adminUrl,
new HttpEntity<Application>(app, HTTP_HEADERS), Application.class); new HttpEntity<Application>(self, HTTP_HEADERS), Application.class);
if (response.getStatusCode().equals(HttpStatus.CREATED)) { if (response.getStatusCode().equals(HttpStatus.CREATED)) {
LOGGER.debug("Application registered itself as {}", response.getBody()); if (registeredSelf.get() == null) {
if (registeredSelf.compareAndSet(null, response.getBody())) {
LOGGER.info("Application registered itself as {}", response.getBody());
return true;
}
}
LOGGER.debug("Application refreshed itself as {}", response.getBody());
return true; return true;
} }
else if (response.getStatusCode().equals(HttpStatus.CONFLICT)) {
LOGGER.warn("Application failed to registered itself as {} because of conflict in registry.", app);
}
else { else {
LOGGER.warn("Application failed to registered itself as {}. Response: {}", app, response.toString()); LOGGER.warn(
"Application failed to registered itself as {}. Response: {}",
self, response.toString());
} }
} }
catch (Exception ex) { catch (Exception ex) {
LOGGER.warn("Failed to register application as {} at spring-boot-admin ({}): {}", app, adminUrl, LOGGER.warn(
ex.getMessage()); "Failed to register application as {} at spring-boot-admin ({}): {}",
self, adminUrl, ex.getMessage());
} }
return false; return false;
} }
public void deregister() {
Application self = registeredSelf.get();
if (self != null) {
String adminUrl = admin.getUrl() + '/' + admin.getContextPath() + "/"
+ self.getId();
registeredSelf.set(null);
try {
template.delete(adminUrl);
}
catch (Exception ex) {
LOGGER.warn(
"Failed to deregister application as {} at spring-boot-admin ({}): {}",
self, adminUrl, ex.getMessage());
}
}
}
protected Application createApplication() { protected Application createApplication() {
Application app = new Application(clientProps.getUrl(), clientProps.getName()); return new Application(client.getUrl(), client.getName());
return app;
} }
} }
...@@ -10,7 +10,7 @@ import org.springframework.boot.test.EnvironmentTestUtils; ...@@ -10,7 +10,7 @@ import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import de.codecentric.boot.admin.actuate.LogfileMvcEndpoint; import de.codecentric.boot.admin.actuate.LogfileMvcEndpoint;
import de.codecentric.boot.admin.services.SpringBootAdminRegistrator; import de.codecentric.boot.admin.services.ApplicationRegistrator;
public class SpringBootAdminClientAutoConfigurationTest { public class SpringBootAdminClientAutoConfigurationTest {
...@@ -26,13 +26,13 @@ public class SpringBootAdminClientAutoConfigurationTest { ...@@ -26,13 +26,13 @@ public class SpringBootAdminClientAutoConfigurationTest {
@Test @Test
public void not_active() { public void not_active() {
load(); load();
assertTrue(context.getBeansOfType(SpringBootAdminRegistrator.class).isEmpty()); assertTrue(context.getBeansOfType(ApplicationRegistrator.class).isEmpty());
} }
@Test @Test
public void active_nologfile() { public void active_nologfile() {
load("spring.boot.admin.url:http://localhost:8081"); load("spring.boot.admin.url:http://localhost:8081");
context.getBean(SpringBootAdminRegistrator.class); context.getBean(ApplicationRegistrator.class);
assertTrue(context.getBeansOfType(LogfileMvcEndpoint.class).isEmpty()); assertTrue(context.getBeansOfType(LogfileMvcEndpoint.class).isEmpty());
} }
...@@ -40,14 +40,14 @@ public class SpringBootAdminClientAutoConfigurationTest { ...@@ -40,14 +40,14 @@ public class SpringBootAdminClientAutoConfigurationTest {
public void active_logfile() { public void active_logfile() {
load("spring.boot.admin.url:http://localhost:8081", "logging.file:spring.log"); load("spring.boot.admin.url:http://localhost:8081", "logging.file:spring.log");
context.getBean(LogfileMvcEndpoint.class); context.getBean(LogfileMvcEndpoint.class);
context.getBean(SpringBootAdminRegistrator.class); context.getBean(ApplicationRegistrator.class);
} }
@Test @Test
public void active_logfile_supressed() { public void active_logfile_supressed() {
load("spring.boot.admin.url:http://localhost:8081", "logging.file:spring.log", load("spring.boot.admin.url:http://localhost:8081", "logging.file:spring.log",
"endpoints.logfile.enabled:false"); "endpoints.logfile.enabled:false");
context.getBean(SpringBootAdminRegistrator.class); context.getBean(ApplicationRegistrator.class);
assertTrue(context.getBeansOfType(LogfileMvcEndpoint.class).isEmpty()); assertTrue(context.getBeansOfType(LogfileMvcEndpoint.class).isEmpty());
} }
......
...@@ -25,6 +25,7 @@ import static org.mockito.Mockito.when; ...@@ -25,6 +25,7 @@ import static org.mockito.Mockito.when;
import java.util.Collections; import java.util.Collections;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
...@@ -38,10 +39,16 @@ import de.codecentric.boot.admin.config.AdminClientProperties; ...@@ -38,10 +39,16 @@ import de.codecentric.boot.admin.config.AdminClientProperties;
import de.codecentric.boot.admin.config.AdminProperties; import de.codecentric.boot.admin.config.AdminProperties;
import de.codecentric.boot.admin.model.Application; import de.codecentric.boot.admin.model.Application;
public class SpringBootAdminRegistratorTest { public class ApplicationRegistratorTest {
private ApplicationRegistrator registrator;
private RestTemplate restTemplate;
private HttpHeaders headers;
@Before
public void setup() {
restTemplate = mock(RestTemplate.class);
@Test
public void register_successful() {
AdminProperties adminProps = new AdminProperties(); AdminProperties adminProps = new AdminProperties();
adminProps.setUrl("http://sba:8080"); adminProps.setUrl("http://sba:8080");
...@@ -49,19 +56,22 @@ public class SpringBootAdminRegistratorTest { ...@@ -49,19 +56,22 @@ public class SpringBootAdminRegistratorTest {
clientProps.setUrl("http://localhost:8080"); clientProps.setUrl("http://localhost:8080");
clientProps.setName("AppName"); clientProps.setName("AppName");
RestTemplate restTemplate = mock(RestTemplate.class); registrator = new ApplicationRegistrator(restTemplate, adminProps, clientProps);
headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
}
@Test
public void register_successful() {
when( when(
restTemplate.postForEntity(isA(String.class), isA(HttpEntity.class), restTemplate.postForEntity(isA(String.class), isA(HttpEntity.class),
eq(Application.class))).thenReturn( eq(Application.class))).thenReturn(
new ResponseEntity<Application>(HttpStatus.CREATED)); new ResponseEntity<Application>(HttpStatus.CREATED));
SpringBootAdminRegistrator registrator = new SpringBootAdminRegistrator(restTemplate, adminProps, clientProps);
boolean result = registrator.register(); boolean result = registrator.register();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
assertTrue(result); assertTrue(result);
verify(restTemplate).postForEntity("http://sba:8080/api/applications", verify(restTemplate).postForEntity("http://sba:8080/api/applications",
new HttpEntity<Application>(new Application("http://localhost:8080", new HttpEntity<Application>(new Application("http://localhost:8080",
...@@ -71,38 +81,26 @@ public class SpringBootAdminRegistratorTest { ...@@ -71,38 +81,26 @@ public class SpringBootAdminRegistratorTest {
@Test @Test
public void register_failed() { public void register_failed() {
AdminProperties adminProps = new AdminProperties();
adminProps.setUrl("http://sba:8080");
AdminClientProperties clientProps = new AdminClientProperties();
clientProps.setUrl("http://localhost:8080");
clientProps.setName("AppName");
RestTemplate restTemplate = mock(RestTemplate.class);
when(restTemplate.postForEntity(isA(String.class), isA(Application.class), eq(Application.class))).thenThrow( when(restTemplate.postForEntity(isA(String.class), isA(Application.class), eq(Application.class))).thenThrow(
new RestClientException("Error")); new RestClientException("Error"));
SpringBootAdminRegistrator registrator = new SpringBootAdminRegistrator(restTemplate, adminProps, clientProps);
boolean result = registrator.register(); boolean result = registrator.register();
assertFalse(result); assertFalse(result);
} }
@Test @Test
public void register_failed_conflict() { public void deregister() {
AdminProperties adminProps = new AdminProperties(); when(
adminProps.setUrl("http://sba:8080"); restTemplate.postForEntity(isA(String.class), isA(HttpEntity.class),
AdminClientProperties clientProps = new AdminClientProperties(); eq(Application.class))).thenReturn(
clientProps.setUrl("http://localhost:8080"); new ResponseEntity<Application>(new Application("http://test", "url",
clientProps.setName("AppName"); "-id-"), HttpStatus.CREATED));
RestTemplate restTemplate = mock(RestTemplate.class);
when(restTemplate.postForEntity(isA(String.class), isA(Application.class), eq(Application.class))).thenReturn(
new ResponseEntity<Application>(HttpStatus.CONFLICT));
SpringBootAdminRegistrator registrator = new SpringBootAdminRegistrator(restTemplate, adminProps, clientProps); registrator.register();
boolean result = registrator.register(); registrator.deregister();
assertFalse(result); verify(restTemplate).delete("http://sba:8080/api/applications/-id-");
} }
} }
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