Commit 69f85687 by Johannes Edmeier

Discover applications on ApplicationReadyEvent

Since there is no Heartbeat or InstanceRegisteredEvent when using the SimpleDiscoveryClient we need to listen on the ApplicationReadyEvent and register the configured instances closes #505
parent 7c3130b4
...@@ -44,7 +44,7 @@ See also the https://github.com/codecentric/spring-boot-admin/tree/master/spring ...@@ -44,7 +44,7 @@ See also the https://github.com/codecentric/spring-boot-admin/tree/master/spring
[[register-client-applications]] [[register-client-applications]]
=== Registering client applications === === Registering client applications ===
To register your application at the SBA Server you can either include the SBA Client or use http://projects.spring.io/spring-cloud/spring-cloud.html[Spring Cloud Discovery] (e.g. Eureka) To register your application at the SBA Server you can either include the SBA Client or use http://projects.spring.io/spring-cloud/spring-cloud.html[Spring Cloud Discovery] (e.g. Eureka, Consul, ...). There is also a <<spring-cloud-discovery-static-config,simple option using a static configuration on the SBA Server side>>.
[[register-clients-via-spring-boot-admin]] [[register-clients-via-spring-boot-admin]]
==== spring-boot-admin-starter-client ==== ==== spring-boot-admin-starter-client ====
......
[[spring-cloud-discovery-support]] [[spring-cloud-discovery-support]]
=== Spring Cloud Discovery === === Spring Cloud Discovery ===
The Spring Boot Admin Server can use Spring Clouds `DiscoveryClient` to discover applications. The advantage is that the clients don't have to include the `spring-boot-admin-starter-client`. You just have to add a DiscoveryClient to your admin server - everything else is done by AutoConfiguration. The Spring Boot Admin Server can use Spring Clouds `DiscoveryClient` to discover applications. The advantage is that the clients don't have to include the `spring-boot-admin-starter-client`. You just have to add a `DiscoveryClient` implementation to your admin server - everything else is done by AutoConfiguration.
The setup is explained <<discover-clients-via-spring-cloud-discovery,above>>.
[[spring-cloud-discovery-static-config]]
==== ServiceInstanceConverter ==== ==== SimpleDiscoveryClient configuration ====
Spring Boot Admin ships with the `SimpleDiscoveryClient` included. This allows you to specify client applications via configuration, without adding the SBA Client or a DiscoveryClient implementation to your monitored applications:
[source,yml]
.application.yml
----
spring:
cloud:
discovery:
client:
simple:
instances:
test:
- uri: http://instance1.intern:8080
metadata:
management.context-path: /actuator
- uri: http://instance2.intern:8080
metadata:
management.context-path: /actuator
----
==== Other DiscoveryClient implementations (Eureka, Zookeeper, Consul, ...) ====
Spring Boot Admin supports all other implementation of Spring Cloud's `DiscoveryClient`. You need to add it to the Spring Boot Admin Server and configure it properly.
An <<discover-clients-via-spring-cloud-discovery,example setup using Eureka>> is shown above.
==== Converting ServiceInstances into monitored applications ====
The information from the service registry are converted by the `ServiceInstanceConverter`. Spring Boot Admin ships with a default and Eureka converter implementation. The correct one is selected by AutoConfiguration. The information from the service registry are converted by the `ServiceInstanceConverter`. Spring Boot Admin ships with a default and Eureka converter implementation. The correct one is selected by AutoConfiguration.
...@@ -12,6 +38,28 @@ TIP: You can modify how the information from the registry is used to register th ...@@ -12,6 +38,28 @@ TIP: You can modify how the information from the registry is used to register th
NOTE: When using Eureka, the `healthCheckUrl` known to Eureka is used for health-checking, which can be set on your client using `eureka.instance.healthCheckUrl`. NOTE: When using Eureka, the `healthCheckUrl` known to Eureka is used for health-checking, which can be set on your client using `eureka.instance.healthCheckUrl`.
.Instance metadata options
|===
| Key |Value |Default value
| user.name +
user.password
| Credentials being used to access the endpoints.
|
| management.port
| The port is substituted in the service URL and will be used for accessing the actuator endpoints.
|
| management.context-path
| The path is appended to the service URL and will be used for accessing the actuator endpoints.
| `${spring.boot.admin.discovery.converter.mangement-context-path}`
| health.path
| The path is appended to the service URL and will be used for the health-checking. Ignored by the `EurekaServiceInstanceConverter`.
| `${spring.boot.admin.discovery.converter.health-endpoint}`
|===
.Discovery configuration options .Discovery configuration options
|=== |===
| Property name |Description |Default value | Property name |Description |Default value
...@@ -36,25 +84,3 @@ NOTE: When using Eureka, the `healthCheckUrl` known to Eureka is used for health ...@@ -36,25 +84,3 @@ NOTE: When using Eureka, the `healthCheckUrl` known to Eureka is used for health
| This services will be included when using discovery and registered as application. Supports simple patterns (e.g. "foo*", "*bar", "foo*bar*"). | This services will be included when using discovery and registered as application. Supports simple patterns (e.g. "foo*", "*bar", "foo*bar*").
| `"*"` | `"*"`
|=== |===
.Instance metadata options
|===
| Key |Value |Default value
| user.name +
user.password
| Credentials being used to access the endpoints.
|
| management.port
| The port is substituted in the service URL and will be used for accessing the actuator endpoints.
|
| management.context-path
| The path is appended to the service URL and will be used for accessing the actuator endpoints.
| `${spring.boot.admin.discovery.converter.mangement-context-path}`
| health.path
| The path is appended to the service URL and will be used for the health-checking. Ignored by the `EurekaServiceInstanceConverter`.
| `${spring.boot.admin.discovery.converter.health-endpoint}`
|===
/* /*
* Copyright 2014 the original author or authors. * Copyright 2014-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -15,12 +15,15 @@ ...@@ -15,12 +15,15 @@
*/ */
package de.codecentric.boot.admin.discovery; package de.codecentric.boot.admin.discovery;
import de.codecentric.boot.admin.model.Application;
import de.codecentric.boot.admin.registry.ApplicationRegistry;
import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.Collections;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.event.HeartbeatEvent; import org.springframework.cloud.client.discovery.event.HeartbeatEvent;
...@@ -30,17 +33,13 @@ import org.springframework.cloud.client.discovery.event.ParentHeartbeatEvent; ...@@ -30,17 +33,13 @@ import org.springframework.cloud.client.discovery.event.ParentHeartbeatEvent;
import org.springframework.context.event.EventListener; import org.springframework.context.event.EventListener;
import org.springframework.util.PatternMatchUtils; import org.springframework.util.PatternMatchUtils;
import de.codecentric.boot.admin.model.Application;
import de.codecentric.boot.admin.registry.ApplicationRegistry;
/** /**
* Listener for Heartbeats events to publish all services to the application registry. * Listener for Heartbeats events to publish all services to the application registry.
* *
* @author Johannes Edmeier * @author Johannes Edmeier
*/ */
public class ApplicationDiscoveryListener { public class ApplicationDiscoveryListener {
private static final Logger LOGGER = LoggerFactory private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationDiscoveryListener.class);
.getLogger(ApplicationDiscoveryListener.class);
private static final String SOURCE = "discovery"; private static final String SOURCE = "discovery";
private final DiscoveryClient discoveryClient; private final DiscoveryClient discoveryClient;
private final ApplicationRegistry registry; private final ApplicationRegistry registry;
...@@ -59,8 +58,7 @@ public class ApplicationDiscoveryListener { ...@@ -59,8 +58,7 @@ public class ApplicationDiscoveryListener {
*/ */
private Set<String> services = new HashSet<>(Collections.singletonList("*")); private Set<String> services = new HashSet<>(Collections.singletonList("*"));
public ApplicationDiscoveryListener(DiscoveryClient discoveryClient, public ApplicationDiscoveryListener(DiscoveryClient discoveryClient, ApplicationRegistry registry) {
ApplicationRegistry registry) {
this.discoveryClient = discoveryClient; this.discoveryClient = discoveryClient;
this.registry = registry; this.registry = registry;
} }
...@@ -71,12 +69,17 @@ public class ApplicationDiscoveryListener { ...@@ -71,12 +69,17 @@ public class ApplicationDiscoveryListener {
} }
@EventListener @EventListener
public void onApplicationReady(ApplicationReadyEvent event) {
discover();
}
@EventListener
public void onParentHeartbeat(ParentHeartbeatEvent event) { public void onParentHeartbeat(ParentHeartbeatEvent event) {
discoverIfNeeded(event.getValue()); discoverIfNeeded(event.getValue());
} }
@EventListener @EventListener
public void onApplicationEvent(HeartbeatEvent event) { public void onHeartbeat(HeartbeatEvent event) {
discoverIfNeeded(event.getValue()); discoverIfNeeded(event.getValue());
} }
...@@ -99,8 +102,7 @@ public class ApplicationDiscoveryListener { ...@@ -99,8 +102,7 @@ public class ApplicationDiscoveryListener {
} }
} }
for (String staleApplicationId : staleApplicationIds) { for (String staleApplicationId : staleApplicationIds) {
LOGGER.info("Application ({}) missing in DiscoveryClient services ", LOGGER.info("Application ({}) missing in DiscoveryClient services ", staleApplicationId);
staleApplicationId);
registry.deregister(staleApplicationId); registry.deregister(staleApplicationId);
} }
} }
...@@ -115,7 +117,7 @@ public class ApplicationDiscoveryListener { ...@@ -115,7 +117,7 @@ public class ApplicationDiscoveryListener {
protected boolean checkPatternIsMatching(String serviceId, Set<String> patterns) { protected boolean checkPatternIsMatching(String serviceId, Set<String> patterns) {
for (String pattern : patterns) { for (String pattern : patterns) {
if(PatternMatchUtils.simpleMatch(pattern, serviceId)) { if (PatternMatchUtils.simpleMatch(pattern, serviceId)) {
return true; return true;
} }
} }
...@@ -125,8 +127,9 @@ public class ApplicationDiscoveryListener { ...@@ -125,8 +127,9 @@ public class ApplicationDiscoveryListener {
protected final Set<String> getAllApplicationIdsFromRegistry() { protected final Set<String> getAllApplicationIdsFromRegistry() {
Set<String> result = new HashSet<>(); Set<String> result = new HashSet<>();
for (Application application : registry.getApplications()) { for (Application application : registry.getApplications()) {
if (!ignoreService(application.getName()) && registerService(application.getName()) if (!ignoreService(application.getName()) &&
&& SOURCE.equals(application.getSource())) { registerService(application.getName()) &&
SOURCE.equals(application.getSource())) {
result.add(application.getId()); result.add(application.getId());
} }
} }
......
/* /*
* Copyright 2014 the original author or authors. * Copyright 2014-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -15,31 +15,29 @@ ...@@ -15,31 +15,29 @@
*/ */
package de.codecentric.boot.admin.discovery; package de.codecentric.boot.admin.discovery;
import static java.util.Arrays.asList; import de.codecentric.boot.admin.model.Application;
import static java.util.Collections.singleton; import de.codecentric.boot.admin.registry.ApplicationRegistry;
import static org.junit.Assert.assertEquals; import de.codecentric.boot.admin.registry.HashingApplicationUrlIdGenerator;
import static org.mockito.Mockito.mock; import de.codecentric.boot.admin.registry.store.SimpleApplicationStore;
import static org.mockito.Mockito.when;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.cloud.client.DefaultServiceInstance; import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.event.HeartbeatEvent; import org.springframework.cloud.client.discovery.event.HeartbeatEvent;
import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent;
import org.springframework.cloud.client.discovery.event.ParentHeartbeatEvent; import org.springframework.cloud.client.discovery.event.ParentHeartbeatEvent;
import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisher;
import de.codecentric.boot.admin.model.Application; import static java.util.Arrays.asList;
import de.codecentric.boot.admin.registry.ApplicationRegistry; import static java.util.Collections.singleton;
import de.codecentric.boot.admin.registry.HashingApplicationUrlIdGenerator; import static org.junit.Assert.assertEquals;
import de.codecentric.boot.admin.registry.store.SimpleApplicationStore; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class ApplicationDiscoveryListenerTest { public class ApplicationDiscoveryListenerTest {
private ApplicationDiscoveryListener listener; private ApplicationDiscoveryListener listener;
...@@ -48,8 +46,7 @@ public class ApplicationDiscoveryListenerTest { ...@@ -48,8 +46,7 @@ public class ApplicationDiscoveryListenerTest {
@Before @Before
public void setup() { public void setup() {
registry = new ApplicationRegistry(new SimpleApplicationStore(), registry = new ApplicationRegistry(new SimpleApplicationStore(), new HashingApplicationUrlIdGenerator());
new HashingApplicationUrlIdGenerator());
registry.setApplicationEventPublisher(mock(ApplicationEventPublisher.class)); registry.setApplicationEventPublisher(mock(ApplicationEventPublisher.class));
discovery = mock(DiscoveryClient.class); discovery = mock(DiscoveryClient.class);
listener = new ApplicationDiscoveryListener(discovery, registry); listener = new ApplicationDiscoveryListener(discovery, registry);
...@@ -62,7 +59,19 @@ public class ApplicationDiscoveryListenerTest { ...@@ -62,7 +59,19 @@ public class ApplicationDiscoveryListenerTest {
(ServiceInstance) new DefaultServiceInstance("service", "localhost", 80, false))); (ServiceInstance) new DefaultServiceInstance("service", "localhost", 80, false)));
listener.setIgnoredServices(singleton("service")); listener.setIgnoredServices(singleton("service"));
listener.onInstanceRegistered(new InstanceRegisteredEvent<>(new Object(), null)); listener.onInstanceRegistered(null);
assertEquals(0, registry.getApplications().size());
}
@Test
public void test_application_ready() {
when(discovery.getServices()).thenReturn(Collections.singletonList("service"));
when(discovery.getInstances("service")).thenReturn(Collections.singletonList(
(ServiceInstance) new DefaultServiceInstance("service", "localhost", 80, false)));
listener.setServices(singleton("notService"));
listener.onApplicationReady(null);
assertEquals(0, registry.getApplications().size()); assertEquals(0, registry.getApplications().size());
} }
...@@ -74,7 +83,7 @@ public class ApplicationDiscoveryListenerTest { ...@@ -74,7 +83,7 @@ public class ApplicationDiscoveryListenerTest {
(ServiceInstance) new DefaultServiceInstance("service", "localhost", 80, false))); (ServiceInstance) new DefaultServiceInstance("service", "localhost", 80, false)));
listener.setServices(singleton("notService")); listener.setServices(singleton("notService"));
listener.onInstanceRegistered(new InstanceRegisteredEvent<>(new Object(), null)); listener.onInstanceRegistered(null);
assertEquals(0, registry.getApplications().size()); assertEquals(0, registry.getApplications().size());
} }
...@@ -86,7 +95,7 @@ public class ApplicationDiscoveryListenerTest { ...@@ -86,7 +95,7 @@ public class ApplicationDiscoveryListenerTest {
(ServiceInstance) new DefaultServiceInstance("service", "localhost", 80, false))); (ServiceInstance) new DefaultServiceInstance("service", "localhost", 80, false)));
listener.setIgnoredServices(singleton("rabbit-*")); listener.setIgnoredServices(singleton("rabbit-*"));
listener.onInstanceRegistered(new InstanceRegisteredEvent<>(new Object(), null)); listener.onInstanceRegistered(null);
Collection<Application> applications = registry.getApplications(); Collection<Application> applications = registry.getApplications();
assertEquals(1, applications.size()); assertEquals(1, applications.size());
...@@ -100,7 +109,7 @@ public class ApplicationDiscoveryListenerTest { ...@@ -100,7 +109,7 @@ public class ApplicationDiscoveryListenerTest {
(ServiceInstance) new DefaultServiceInstance("service", "localhost", 80, false))); (ServiceInstance) new DefaultServiceInstance("service", "localhost", 80, false)));
listener.setServices(singleton("ser*")); listener.setServices(singleton("ser*"));
listener.onInstanceRegistered(new InstanceRegisteredEvent<>(new Object(), null)); listener.onInstanceRegistered(null);
Collection<Application> applications = registry.getApplications(); Collection<Application> applications = registry.getApplications();
assertEquals(1, applications.size()); assertEquals(1, applications.size());
...@@ -117,7 +126,7 @@ public class ApplicationDiscoveryListenerTest { ...@@ -117,7 +126,7 @@ public class ApplicationDiscoveryListenerTest {
listener.setServices(singleton("ser*")); listener.setServices(singleton("ser*"));
listener.setIgnoredServices(singleton("service-*")); listener.setIgnoredServices(singleton("service-*"));
listener.onInstanceRegistered(new InstanceRegisteredEvent<>(new Object(), null)); listener.onInstanceRegistered(null);
Collection<Application> applications = registry.getApplications(); Collection<Application> applications = registry.getApplications();
assertEquals(1, applications.size()); assertEquals(1, applications.size());
...@@ -130,7 +139,7 @@ public class ApplicationDiscoveryListenerTest { ...@@ -130,7 +139,7 @@ public class ApplicationDiscoveryListenerTest {
when(discovery.getInstances("service")).thenReturn(Collections.singletonList( when(discovery.getInstances("service")).thenReturn(Collections.singletonList(
(ServiceInstance) new DefaultServiceInstance("service", "localhost", 80, false))); (ServiceInstance) new DefaultServiceInstance("service", "localhost", 80, false)));
listener.onInstanceRegistered(new InstanceRegisteredEvent<>(new Object(), null)); listener.onInstanceRegistered(null);
assertEquals(1, registry.getApplications().size()); assertEquals(1, registry.getApplications().size());
Application application = registry.getApplications().iterator().next(); Application application = registry.getApplications().iterator().next();
...@@ -150,19 +159,21 @@ public class ApplicationDiscoveryListenerTest { ...@@ -150,19 +159,21 @@ public class ApplicationDiscoveryListenerTest {
when(discovery.getInstances("service")).thenReturn(Collections.singletonList( when(discovery.getInstances("service")).thenReturn(Collections.singletonList(
(ServiceInstance) new DefaultServiceInstance("service", "localhost", 80, false))); (ServiceInstance) new DefaultServiceInstance("service", "localhost", 80, false)));
listener.onApplicationEvent(new HeartbeatEvent(new Object(), heartbeat)); listener.onHeartbeat(new HeartbeatEvent(new Object(), heartbeat));
assertEquals(0, registry.getApplications().size()); assertEquals(0, registry.getApplications().size());
listener.onApplicationEvent(new HeartbeatEvent(new Object(), new Object())); listener.onHeartbeat(new HeartbeatEvent(new Object(), new Object()));
assertEquals(1, registry.getApplications().size()); assertEquals(1, registry.getApplications().size());
} }
@Test @Test
public void deregister_removed_app() { public void deregister_removed_app() {
registry.register(Application.create("ignored").withHealthUrl("http://health") registry.register(Application.create("ignored").withHealthUrl("http://health").withId("abcdef").build());
.withId("abcdef").build()); registry.register(Application.create("different-source")
registry.register(Application.create("different-source").withHealthUrl("http://health2") .withHealthUrl("http://health2")
.withId("abcdef").withSource("http-api").build()); .withId("abcdef")
.withSource("http-api")
.build());
listener.setIgnoredServices(singleton("ignored")); listener.setIgnoredServices(singleton("ignored"));
List<ServiceInstance> instances = new ArrayList<>(); List<ServiceInstance> instances = new ArrayList<>();
...@@ -172,14 +183,14 @@ public class ApplicationDiscoveryListenerTest { ...@@ -172,14 +183,14 @@ public class ApplicationDiscoveryListenerTest {
when(discovery.getServices()).thenReturn(Collections.singletonList("service")); when(discovery.getServices()).thenReturn(Collections.singletonList("service"));
when(discovery.getInstances("service")).thenReturn(instances); when(discovery.getInstances("service")).thenReturn(instances);
listener.onApplicationEvent(new HeartbeatEvent(new Object(), new Object())); listener.onHeartbeat(new HeartbeatEvent(new Object(), new Object()));
assertEquals(2, registry.getApplicationsByName("service").size()); assertEquals(2, registry.getApplicationsByName("service").size());
assertEquals(1, registry.getApplicationsByName("ignored").size()); assertEquals(1, registry.getApplicationsByName("ignored").size());
assertEquals(1, registry.getApplicationsByName("different-source").size()); assertEquals(1, registry.getApplicationsByName("different-source").size());
instances.remove(0); instances.remove(0);
listener.onApplicationEvent(new HeartbeatEvent(new Object(), new Object())); listener.onHeartbeat(new HeartbeatEvent(new Object(), new Object()));
assertEquals(1, registry.getApplicationsByName("service").size()); assertEquals(1, registry.getApplicationsByName("service").size());
assertEquals(1, registry.getApplicationsByName("ignored").size()); assertEquals(1, registry.getApplicationsByName("ignored").size());
assertEquals(1, registry.getApplicationsByName("different-source").size()); assertEquals(1, registry.getApplicationsByName("different-source").size());
......
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