Commit 8b355ad7 by Johannes Edmeier

Decouple route-refreshing from registrationEvents

parent 275647ee
......@@ -23,11 +23,11 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpStatus;
import org.springframework.context.event.EventListener;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
......@@ -40,7 +40,9 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import de.codecentric.boot.admin.controller.JournalController;
import de.codecentric.boot.admin.controller.RegistryController;
import de.codecentric.boot.admin.event.ClientApplicationDeregisteredEvent;
import de.codecentric.boot.admin.event.ClientApplicationRegisteredEvent;
import de.codecentric.boot.admin.event.RoutesOutdatedEvent;
import de.codecentric.boot.admin.journal.ApplicationEventJournal;
import de.codecentric.boot.admin.journal.store.JournaledEventStore;
import de.codecentric.boot.admin.journal.store.SimpleJournaledEventStore;
......@@ -52,11 +54,20 @@ import de.codecentric.boot.admin.registry.store.ApplicationStore;
import de.codecentric.boot.admin.registry.store.SimpleApplicationStore;
@Configuration
public class AdminServerWebConfiguration extends WebMvcConfigurerAdapter implements
ApplicationContextAware {
public class AdminServerWebConfiguration extends WebMvcConfigurerAdapter
implements ApplicationContextAware {
private ApplicationContext applicationContext;
@Autowired
private ApplicationEventPublisher publisher;
@Autowired
private ApplicationStore applicationStore;
@Value("${spring.boot.admin.monitor.period:10000}")
private long monitorPeriod;
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
......@@ -81,12 +92,6 @@ public class AdminServerWebConfiguration extends WebMvcConfigurerAdapter impleme
return false;
}
@Autowired
private ApplicationStore applicationStore;
@Value("${spring.boot.admin.monitor.period:10000}")
private long monitorPeriod;
/**
* @return Controller with REST-API for spring-boot applications to register itself.
*/
......@@ -130,6 +135,12 @@ public class AdminServerWebConfiguration extends WebMvcConfigurerAdapter impleme
@EventListener
public void onClientApplicationRegistered(ClientApplicationRegisteredEvent event) {
statusUpdater().updateStatus(event.getApplication());
publisher.publishEvent(new RoutesOutdatedEvent());
}
@EventListener
public void onClientApplicationDeregistered(ClientApplicationDeregisteredEvent event) {
publisher.publishEvent(new RoutesOutdatedEvent());
}
@Bean
......
......@@ -15,8 +15,6 @@
*/
package de.codecentric.boot.admin.config;
import java.util.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
......@@ -28,18 +26,17 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.hazelcast.config.Config;
import com.hazelcast.core.EntryAdapter;
import com.hazelcast.core.EntryEvent;
import com.hazelcast.core.EntryListener;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import com.hazelcast.core.MapEvent;
import com.hazelcast.map.listener.MapListener;
import de.codecentric.boot.admin.event.ClientApplicationDeregisteredEvent;
import de.codecentric.boot.admin.event.ClientApplicationRegisteredEvent;
import de.codecentric.boot.admin.event.ClientApplicationStatusChangedEvent;
import de.codecentric.boot.admin.event.RoutesOutdatedEvent;
import de.codecentric.boot.admin.model.Application;
import de.codecentric.boot.admin.model.StatusInfo;
import de.codecentric.boot.admin.registry.store.ApplicationStore;
import de.codecentric.boot.admin.registry.store.HazelcastApplicationStore;
......@@ -52,6 +49,9 @@ public class HazelcastStoreConfiguration {
@Value("${spring.boot.admin.hazelcast.map:spring-boot-admin-application-store}")
private String hazelcastMapName;
@Autowired
private ApplicationEventPublisher publisher;
@Bean
@ConditionalOnMissingBean
public Config hazelcastConfig() {
......@@ -69,70 +69,23 @@ public class HazelcastStoreConfiguration {
public ApplicationStore applicationStore() {
IMap<String, Application> map = hazelcastInstance().getMap(hazelcastMapName);
map.addIndex("name", false);
map.addEntryListener(entryListener(), false);
map.addEntryListener((MapListener) entryListener(), false);
return new HazelcastApplicationStore(map);
}
@Bean
public EntryListener<String, Application> entryListener() {
return new ApplicationEntryListener();
}
private static class ApplicationEntryListener implements EntryListener<String, Application> {
@Autowired
private ApplicationEventPublisher publisher;
return new EntryAdapter<String, Application>() {
@Override
public void entryAdded(EntryEvent<String, Application> event) {
if (event.getValue() != null) {
publisher.publishEvent(new ClientApplicationRegisteredEvent(event.getValue()));
}
public void onEntryEvent(EntryEvent<String, Application> event) {
publisher.publishEvent(new RoutesOutdatedEvent());
}
@Override
public void entryRemoved(EntryEvent<String, Application> event) {
if (event.getValue() != null) {
publisher.publishEvent(new ClientApplicationDeregisteredEvent(event.getValue()));
}
public void onMapEvent(MapEvent event) {
publisher.publishEvent(new RoutesOutdatedEvent());
}
@Override
public void entryUpdated(EntryEvent<String, Application> event) {
if (!Objects.equals(event.getOldValue(), event.getValue())) {
if (event.getOldValue() != null) {
publisher.publishEvent(
new ClientApplicationDeregisteredEvent(event.getOldValue()));
}
if (event.getValue() != null) {
publisher.publishEvent(new ClientApplicationRegisteredEvent(event.getValue()));
}
} else {
StatusInfo from = event.getOldValue() != null ? event.getOldValue().getStatusInfo()
: StatusInfo.ofUnknown();
StatusInfo to = event.getValue() != null ? event.getValue().getStatusInfo()
: StatusInfo.ofUnknown();
if (!from.equals(to)) {
publisher.publishEvent(
new ClientApplicationStatusChangedEvent(event.getValue(), from, to));
}
}
}
@Override
public void entryEvicted(EntryEvent<String, Application> event) {
if (event.getValue() != null) {
publisher.publishEvent(new ClientApplicationDeregisteredEvent(event.getValue()));
}
}
@Override
public void mapEvicted(MapEvent event) {
publisher.publishEvent(new ClientApplicationDeregisteredEvent(null));
}
@Override
public void mapCleared(MapEvent event) {
publisher.publishEvent(new ClientApplicationDeregisteredEvent(null));
}
};
}
}
......@@ -39,13 +39,14 @@ import org.springframework.cloud.netflix.zuul.web.ZuulController;
import org.springframework.cloud.netflix.zuul.web.ZuulHandlerMapping;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;
import com.netflix.zuul.ZuulFilter;
import de.codecentric.boot.admin.controller.RegistryController;
import de.codecentric.boot.admin.event.RoutesOutdatedEvent;
import de.codecentric.boot.admin.registry.ApplicationRegistry;
import de.codecentric.boot.admin.zuul.ApplicationRouteLocator;
import de.codecentric.boot.admin.zuul.ApplicationRouteRefreshListener;
@Configuration
@EnableConfigurationProperties(ZuulProperties.class)
......@@ -135,9 +136,10 @@ public class RevereseZuulProxyConfiguration {
}
@Bean
public ApplicationRouteRefreshListener applicationRouteRefreshListener() {
return new ApplicationRouteRefreshListener(routeLocator(), zuulHandlerMapping());
@EventListener
public void onRoutesOutdatedEvent(RoutesOutdatedEvent event) {
routeLocator().resetRoutes();
zuulHandlerMapping().registerHandlers();
}
@Configuration
......
/*
* Copyright 2014 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
*
* http://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.event;
import java.io.Serializable;
/**
* Signals that the routes needs to be recalculated.
*
* @author Johannes Edmeier
*/
public class RoutesOutdatedEvent implements Serializable {
private static final long serialVersionUID = 1L;
}
/*
* Copyright 2014 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
*
* http://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.zuul;
import org.springframework.cloud.netflix.zuul.web.ZuulHandlerMapping;
import org.springframework.context.event.EventListener;
import de.codecentric.boot.admin.event.ClientApplicationDeregisteredEvent;
import de.codecentric.boot.admin.event.ClientApplicationRegisteredEvent;
/**
* Listener to trigger recomputation of the applications' routes.
*
* @author Johannes Stelzer
*/
public class ApplicationRouteRefreshListener {
private final ApplicationRouteLocator routeLocator;
private final ZuulHandlerMapping zuulHandlerMapping;
public ApplicationRouteRefreshListener(ApplicationRouteLocator routeLocator,
ZuulHandlerMapping zuulHandlerMapping) {
this.routeLocator = routeLocator;
this.zuulHandlerMapping = zuulHandlerMapping;
}
@EventListener
public void onClientApplicationDeregistered(ClientApplicationDeregisteredEvent event) {
refreshRoutes();
}
@EventListener
public void onClientApplicationRegistered(ClientApplicationRegisteredEvent event) {
refreshRoutes();
}
private void refreshRoutes() {
this.routeLocator.resetRoutes();
this.zuulHandlerMapping.registerHandlers();
}
}
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