Commit 8b355ad7 by Johannes Edmeier

Decouple route-refreshing from registrationEvents

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