Commit e9bba5d6 by Johannes Stelzer

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

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