Skip to content

Commit

Permalink
Logfile endpoint available when not using spring.boot.admin.url
Browse files Browse the repository at this point in the history
Added inner-class for admin client registration configuration which is
conditional on spring.boot.admin.url. Logfile endpoint is now available
when just using Spring Cloud Discovery.
  • Loading branch information
steve-oakey committed Jun 29, 2015
1 parent 5f1be53 commit 460753a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,63 +34,67 @@
import de.codecentric.boot.admin.web.BasicAuthHttpRequestInterceptor;

/**
* This configuration adds a registrator bean to the spring context. This bean checks periodicaly, if the using
* application is registered at the spring-boot-admin application. If not, it registers itself.
* This configuration adds a registrator bean to the spring context. This bean
* checks periodicaly, if the using application is registered at the
* spring-boot-admin application. If not, it registers itself.
*/
@Configuration
@ConditionalOnProperty("spring.boot.admin.url")
@EnableConfigurationProperties({ AdminProperties.class, AdminClientProperties.class })
public class SpringBootAdminClientAutoConfiguration {

/**
* Task that registers the application at the spring-boot-admin application.
*/
@Bean
@ConditionalOnMissingBean
public ApplicationRegistrator registrator(AdminProperties admin,
AdminClientProperties client) {
return new ApplicationRegistrator(createRestTemplate(admin), admin, client);
}
@ConditionalOnProperty("spring.boot.admin.url")
public static class AdminClientRegistrationConfig {
/**
* Task that registers the application at the spring-boot-admin
* application.
*/
@Bean
@ConditionalOnMissingBean
public ApplicationRegistrator registrator(AdminProperties admin, AdminClientProperties client) {
return new ApplicationRegistrator(createRestTemplate(admin), admin, client);
}

protected RestTemplate createRestTemplate(AdminProperties admin) {
RestTemplate template = new RestTemplate();
template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
protected RestTemplate createRestTemplate(AdminProperties admin) {
RestTemplate template = new RestTemplate();
template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

if (admin.getUsername() != null) {
template.setInterceptors(Arrays.<ClientHttpRequestInterceptor> asList(new BasicAuthHttpRequestInterceptor(
admin.getUsername(), admin.getPassword())));
}
if (admin.getUsername() != null) {
template.setInterceptors(Arrays.<ClientHttpRequestInterceptor> asList(
new BasicAuthHttpRequestInterceptor(admin.getUsername(), admin.getPassword())));
}

return template;
}
return template;
}

/**
* TaskRegistrar that triggers the RegistratorTask every ten seconds.
*/
@Bean
public ScheduledTaskRegistrar taskRegistrar(final ApplicationRegistrator registrator,
AdminProperties admin, final AdminClientProperties client) {
ScheduledTaskRegistrar registrar = new ScheduledTaskRegistrar();
Runnable registratorTask = new Runnable() {
@Override
public void run() {
if (client.isServerInitialized()) {
registrator.register();
/**
* TaskRegistrar that triggers the RegistratorTask every ten seconds.
*/
@Bean
public ScheduledTaskRegistrar taskRegistrar(final ApplicationRegistrator registrator, AdminProperties admin,
final AdminClientProperties client) {
ScheduledTaskRegistrar registrar = new ScheduledTaskRegistrar();
Runnable registratorTask = new Runnable() {
@Override
public void run() {
if (client.isServerInitialized()) {
registrator.register();
}
}
}
};
};

registrar.addFixedRateTask(registratorTask, admin.getPeriod());
return registrar;
}
registrar.addFixedRateTask(registratorTask, admin.getPeriod());
return registrar;
}

/**
* ApplicationListener triggering registration after refresh/shutdown
*/
@Bean
public RegistrationApplicationListener registrationListener(final ApplicationRegistrator registrator,
final AdminProperties admin) {
return new RegistrationApplicationListener(admin, registrator);
}

/**
* ApplicationListener triggering registration after refresh/shutdown
*/
@Bean
public RegistrationApplicationListener registrationListener(
final ApplicationRegistrator registrator, final AdminProperties admin) {
return new RegistrationApplicationListener(admin, registrator);
}

@Configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ public void close() {
public void not_active() {
load();
assertTrue(context.getBeansOfType(ApplicationRegistrator.class).isEmpty());
assertTrue(context.getBeansOfType(LogfileMvcEndpoint.class).isEmpty());
}

public void not_active_logfile() {
load();
assertTrue(context.getBeansOfType(ApplicationRegistrator.class).isEmpty());
context.getBean(LogfileMvcEndpoint.class);
}

@Test
Expand Down

0 comments on commit 460753a

Please sign in to comment.