Skip to content

Commit

Permalink
feat(#1939): implement endpoint to trigger getting services from disc…
Browse files Browse the repository at this point in the history
…overy client
  • Loading branch information
SteKoe committed Dec 23, 2024
1 parent 060c1dd commit 339b3d1
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2023 the original author or authors.
* Copyright 2014-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -42,6 +42,7 @@
import de.codecentric.boot.admin.server.domain.values.InstanceId;
import de.codecentric.boot.admin.server.domain.values.Registration;
import de.codecentric.boot.admin.server.services.InstanceRegistry;
import de.codecentric.boot.admin.server.web.client.RefreshInstancesEvent;

/**
* Listener for Heartbeats events to publish all services to the instance registry.
Expand Down Expand Up @@ -105,6 +106,11 @@ public void onInstanceRegistered(InstanceRegisteredEvent<?> event) {
discover();
}

@EventListener
public void onRefreshInstances(RefreshInstancesEvent event) {
discover();
}

@EventListener
public void onParentHeartbeat(ParentHeartbeatEvent event) {
discoverIfNeeded(event.getValue());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2023 the original author or authors.
* Copyright 2014-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,7 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.accept.ContentNegotiationManager;
Expand Down Expand Up @@ -56,8 +57,9 @@ public InstancesController instancesController(InstanceRegistry instanceRegistry

@Bean
@ConditionalOnMissingBean
public ApplicationsController applicationsController(ApplicationRegistry applicationRegistry) {
return new ApplicationsController(applicationRegistry);
public ApplicationsController applicationsController(ApplicationRegistry applicationRegistry,
ApplicationEventPublisher applicationEventPublisher) {
return new ApplicationsController(applicationRegistry, applicationEventPublisher);
}

@Configuration(proxyBeanMethods = false)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2023 the original author or authors.
* Copyright 2014-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,18 +20,21 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import de.codecentric.boot.admin.server.domain.entities.Application;
import de.codecentric.boot.admin.server.services.ApplicationRegistry;
import de.codecentric.boot.admin.server.web.client.RefreshInstancesEvent;

/**
* REST controller for controlling registration of managed instances.
Expand All @@ -49,15 +52,23 @@ public class ApplicationsController {

private final ApplicationRegistry registry;

public ApplicationsController(ApplicationRegistry registry) {
private final ApplicationEventPublisher publisher;

public ApplicationsController(ApplicationRegistry registry, ApplicationEventPublisher publisher) {
this.registry = registry;
this.publisher = publisher;
}

@GetMapping(path = "/applications", produces = MediaType.APPLICATION_JSON_VALUE)
public Flux<Application> applications() {
return registry.getApplications();
}

@PostMapping(path = "/applications", produces = MediaType.APPLICATION_JSON_VALUE)
public void refreshApplications() {
publisher.publishEvent(new RefreshInstancesEvent(this));
}

@GetMapping(path = "/applications/{name}", produces = MediaType.APPLICATION_JSON_VALUE)
public Mono<ResponseEntity<Application>> application(@PathVariable("name") String name) {
return registry.getApplication(name).map(ResponseEntity::ok).defaultIfEmpty(ResponseEntity.notFound().build());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2014-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package de.codecentric.boot.admin.server.web.client;

import org.springframework.context.ApplicationEvent;

public class RefreshInstancesEvent extends ApplicationEvent {

public RefreshInstancesEvent(Object source) {
super(source);
}

}

0 comments on commit 339b3d1

Please sign in to comment.