diff --git a/spring-boot-admin-server-cloud/src/main/java/de/codecentric/boot/admin/server/cloud/discovery/InstanceDiscoveryListener.java b/spring-boot-admin-server-cloud/src/main/java/de/codecentric/boot/admin/server/cloud/discovery/InstanceDiscoveryListener.java index 1cda69fa7f1..fd4a2c5d8ab 100644 --- a/spring-boot-admin-server-cloud/src/main/java/de/codecentric/boot/admin/server/cloud/discovery/InstanceDiscoveryListener.java +++ b/spring-boot-admin-server-cloud/src/main/java/de/codecentric/boot/admin/server/cloud/discovery/InstanceDiscoveryListener.java @@ -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. @@ -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. @@ -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()); diff --git a/spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/server/config/AdminServerWebConfiguration.java b/spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/server/config/AdminServerWebConfiguration.java index f4b7ee4357f..3a8918647f9 100644 --- a/spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/server/config/AdminServerWebConfiguration.java +++ b/spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/server/config/AdminServerWebConfiguration.java @@ -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. @@ -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; @@ -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) diff --git a/spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/server/web/ApplicationsController.java b/spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/server/web/ApplicationsController.java index b2570aa3117..5dfbc22bd20 100644 --- a/spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/server/web/ApplicationsController.java +++ b/spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/server/web/ApplicationsController.java @@ -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. @@ -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. @@ -49,8 +52,11 @@ 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) @@ -58,6 +64,11 @@ public Flux 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> application(@PathVariable("name") String name) { return registry.getApplication(name).map(ResponseEntity::ok).defaultIfEmpty(ResponseEntity.notFound().build()); diff --git a/spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/server/web/client/RefreshInstancesEvent.java b/spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/server/web/client/RefreshInstancesEvent.java new file mode 100644 index 00000000000..514e8552a6f --- /dev/null +++ b/spring-boot-admin-server/src/main/java/de/codecentric/boot/admin/server/web/client/RefreshInstancesEvent.java @@ -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); + } + +}