Commit 08b16417 by Johannes Edmeier

Remember how the application was registered

From this commit on the source from wich the application was registered is remembered. We use this information for two things: * For applications which wasn't registered via the http-api the remove button is hidden. * The `ApplicationDiscoveryListener` only removes stale applications (those which are not anymore found via discovery) if the were registered via the `ApplicationDiscoeryListener`. closes #328
parent 3e16bad1
......@@ -47,7 +47,7 @@
<sba-status-info status-info="application.statusInfo"></sba-status-info>
<span ng-show="application.refreshing"><i class="fa fa-spinner fa-pulse fa-lg"></i></span>
</td>
<td>
<td style="text-align: right;">
<sba-notification-settings ng-if="notificationFilters" application="application" filters="notificationFilters" refresh-callback="loadFilters()"></sba-notification-settings>
<div class="btn-group">
<a ng-if="views.length > 0" ng-href="{{views[0].href}}" target="{{views[0].target}}" class="btn btn-success" ng-bind-html="views[0].title"></a>
......@@ -60,7 +60,7 @@
</li>
</ul>
</div>
<div class="btn-group" title="remove">
<div class="btn-group" title="remove" ng-if="application.source == 'http-api'">
<a class="btn btn-danger" ng-click="remove(application)"><i class="fa fa-times"></i></a>
</div>
</td>
......
......@@ -39,6 +39,7 @@ import de.codecentric.boot.admin.registry.ApplicationRegistry;
public class ApplicationDiscoveryListener {
private static final Logger LOGGER = LoggerFactory
.getLogger(ApplicationDiscoveryListener.class);
private static final String SOURCE = "discovery";
private final DiscoveryClient discoveryClient;
private final ApplicationRegistry registry;
private final HeartbeatMonitor monitor = new HeartbeatMonitor();
......@@ -98,7 +99,8 @@ public class ApplicationDiscoveryListener {
protected final Set<String> getAllApplicationIdsFromRegistry() {
Set<String> result = new HashSet<>();
for (Application application : registry.getApplications()) {
if (!ignoredServices.contains(application.getName())) {
if (!ignoredServices.contains(application.getName())
&& SOURCE.equals(application.getSource())) {
result.add(application.getId());
}
}
......@@ -108,6 +110,7 @@ public class ApplicationDiscoveryListener {
protected String register(ServiceInstance instance) {
try {
Application application = converter.convert(instance);
application = Application.create(application).withSource(SOURCE).build();
if (application != null) {
LOGGER.debug("Registering discovered application {}", application);
return registry.register(application).getId();
......
......@@ -32,7 +32,7 @@ import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
*/
@JsonDeserialize(using = Application.Deserializer.class)
public class Application implements Serializable {
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 2L;
private final String id;
private final String name;
......@@ -40,6 +40,7 @@ public class Application implements Serializable {
private final String healthUrl;
private final String serviceUrl;
private final StatusInfo statusInfo;
private final String source;
protected Application(Builder builder) {
Assert.hasText(builder.name, "name must not be empty!");
......@@ -51,6 +52,7 @@ public class Application implements Serializable {
this.name = builder.name;
this.id = builder.id;
this.statusInfo = builder.statusInfo;
this.source = builder.source;
}
public static Builder create(String name) {
......@@ -68,6 +70,7 @@ public class Application implements Serializable {
private String healthUrl;
private String serviceUrl;
private StatusInfo statusInfo = StatusInfo.ofUnknown();
private String source;
private Builder(String name) {
this.name = name;
......@@ -80,6 +83,7 @@ public class Application implements Serializable {
this.name = application.name;
this.id = application.id;
this.statusInfo = application.statusInfo;
this.source = application.source;
}
public Builder withName(String name) {
......@@ -112,6 +116,11 @@ public class Application implements Serializable {
return this;
}
public Builder withSource(String source) {
this.source = source;
return this;
}
public Application build() {
return new Application(this);
}
......@@ -141,6 +150,10 @@ public class Application implements Serializable {
return statusInfo;
}
public String getSource() {
return source;
}
@Override
public String toString() {
return "Application [id=" + id + ", name=" + name + ", managementUrl="
......
......@@ -51,13 +51,14 @@ public class RegistryController {
/**
* Register an application within this admin application.
*
* @param app The application infos.
* @param application The application infos.
* @return The registered application.
*/
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<Application> register(@RequestBody Application app) {
LOGGER.debug("Register application {}", app.toString());
Application registeredApp = registry.register(app);
public ResponseEntity<Application> register(@RequestBody Application application) {
application = Application.create(application).withSource("http-api").build();
LOGGER.debug("Register application {}", application.toString());
Application registeredApp = registry.register(application);
return ResponseEntity.status(HttpStatus.CREATED).body(registeredApp);
}
......
......@@ -101,6 +101,8 @@ public class ApplicationDiscoveryListenerTest {
public void deregister_removed_app() {
registry.register(Application.create("ignored").withHealthUrl("http://health")
.withId("abcdef").build());
registry.register(Application.create("different-source").withHealthUrl("http://health2")
.withId("abcdef").withSource("http-api").build());
listener.setIgnoredServices(Collections.singleton("ignored"));
List<ServiceInstance> instances = new ArrayList<>();
......@@ -113,12 +115,14 @@ public class ApplicationDiscoveryListenerTest {
listener.onApplicationEvent(new HeartbeatEvent(new Object(), new Object()));
assertEquals(2, registry.getApplicationsByName("service").size());
assertEquals(1, registry.getApplicationsByName("ignored").size());
assertEquals(1, registry.getApplicationsByName("different-source").size());
instances.remove(0);
listener.onApplicationEvent(new HeartbeatEvent(new Object(), new Object()));
assertEquals(1, registry.getApplicationsByName("service").size());
assertEquals(1, registry.getApplicationsByName("ignored").size());
assertEquals(1, registry.getApplicationsByName("different-source").size());
}
}
......@@ -19,7 +19,6 @@ import de.codecentric.boot.admin.client.registration.Application;
public class ApplicationTest {
@Test
public void test_json_format() throws JsonProcessingException, IOException {
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().build();
......
......@@ -30,7 +30,7 @@ public class DefaultApplicationFactoryTest {
private AdminClientProperties client = new AdminClientProperties();
private ServerProperties server = new ServerProperties();
private ManagementServerProperties management = new ManagementServerProperties();
DefaultApplicationFactory factory = new DefaultApplicationFactory(client, management,
private DefaultApplicationFactory factory = new DefaultApplicationFactory(client, management,
server, "/health");
@Before
......@@ -166,7 +166,6 @@ public class DefaultApplicationFactoryTest {
assertThat(app.getServiceUrl(), is("http://127.0.0.1:8080"));
}
@Test
public void test_allcustom() {
client.setHealthUrl("http://health");
......@@ -194,7 +193,7 @@ public class DefaultApplicationFactoryTest {
try {
return InetAddress.getLocalHost().getCanonicalHostName();
} catch (UnknownHostException e) {
throw new RuntimeException(e);
throw new IllegalStateException(e);
}
}
......
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