Commit 8715d2c6 by Johannes Edmeier

rework hazelcast support

Streamline the Hazelcast support with boot 1.3 and support storing events in hazelcast closes #113
parent 8b355ad7
......@@ -21,10 +21,6 @@
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-spring</artifactId>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
......
......@@ -17,19 +17,30 @@ package de.codecentric.boot.admin;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import com.hazelcast.config.Config;
import com.hazelcast.config.EvictionPolicy;
import com.hazelcast.config.ListConfig;
import com.hazelcast.config.MapConfig;
import de.codecentric.boot.admin.config.EnableAdminServer;
@Configuration
@EnableAutoConfiguration
@EnableAdminServer
@ImportResource({ "classpath:hazelcast-config.xml" })
public class SpringBootAdminApplication {
@Bean
public Config hazelcastConfig() {
return new Config().setProperty("hazelcast.jmx", "true")
.addMapConfig(new MapConfig("spring-boot-admin-application-store").setBackupCount(1)
.setEvictionPolicy(EvictionPolicy.NONE))
.addListConfig(new ListConfig("spring-boot-admin-application-store")
.setBackupCount(1).setMaxSize(1000));
}
public static void main(String[] args) {
SpringApplication.run(SpringBootAdminApplication.class, args);
}
}
......@@ -6,5 +6,3 @@ logging.file=/tmp/log.log
spring.application.name=@pom.artifactId@
spring.boot.admin.url=http://localhost:8081
\ No newline at end of file
hz.instance.name=Spring Boot Admin
\ No newline at end of file
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:hz="http://www.hazelcast.com/schema/spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.hazelcast.com/schema/spring
http://www.hazelcast.com/schema/spring/hazelcast-spring-3.3.xsd">
<hz:config id="hazelcastConfig">
<hz:instance-name>${hz.instance.name}</hz:instance-name>
<hz:properties>
<hz:property name="hazelcast.jmx">true</hz:property>
</hz:properties>
<hz:map name="spring-boot-admin-application-store" backup-count="1" eviction-policy="NONE" />
</hz:config>
</beans>
\ No newline at end of file
......@@ -30,7 +30,6 @@
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- Use Zuul WITHOUT Hystrix/Ribbon/Config Client -->
<dependency>
<groupId>com.netflix.zuul</groupId>
......@@ -48,14 +47,12 @@
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<!-- Optional Discovery Client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
<optional>true</optional>
</dependency>
<!-- Optional Hazelcast-Support -->
<dependency>
<groupId>com.hazelcast</groupId>
......@@ -69,12 +66,6 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-spring</artifactId>
<optional>true</optional>
</dependency>
<!-- Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
......
......@@ -17,63 +17,69 @@ package de.codecentric.boot.admin.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration;
import org.springframework.context.ApplicationEventPublisher;
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.IList;
import com.hazelcast.core.IMap;
import com.hazelcast.core.MapEvent;
import com.hazelcast.map.listener.MapListener;
import de.codecentric.boot.admin.event.ClientApplicationEvent;
import de.codecentric.boot.admin.event.RoutesOutdatedEvent;
import de.codecentric.boot.admin.journal.store.HazelcastJournaledEventStore;
import de.codecentric.boot.admin.journal.store.JournaledEventStore;
import de.codecentric.boot.admin.model.Application;
import de.codecentric.boot.admin.registry.store.ApplicationStore;
import de.codecentric.boot.admin.registry.store.HazelcastApplicationStore;
@Configuration
@ConditionalOnClass({ Hazelcast.class })
@ConditionalOnSingleCandidate(HazelcastInstance.class)
@ConditionalOnProperty(prefix = "spring.boot.admin.hazelcast", name = "enabled", matchIfMissing = true)
@AutoConfigureBefore(AdminServerWebConfiguration.class)
@AutoConfigureAfter(HazelcastAutoConfiguration.class)
public class HazelcastStoreConfiguration {
@Value("${spring.boot.admin.hazelcast.map:spring-boot-admin-application-store}")
@Value("${spring.boot.admin.hazelcast.application-store:spring-boot-admin-application-store}")
private String hazelcastMapName;
@Value("${spring.boot.admin.hazelcast.event-store:spring-boot-admin-event-store}")
private String eventListName;
@Autowired
private ApplicationEventPublisher publisher;
@Bean
@ConditionalOnMissingBean
public Config hazelcastConfig() {
return new Config();
}
@Bean(destroyMethod = "shutdown")
@ConditionalOnMissingBean
public HazelcastInstance hazelcastInstance() {
return Hazelcast.newHazelcastInstance(hazelcastConfig());
}
@Autowired
private HazelcastInstance hazelcastInstance;
@Bean
@ConditionalOnMissingBean
public ApplicationStore applicationStore() {
IMap<String, Application> map = hazelcastInstance().getMap(hazelcastMapName);
IMap<String, Application> map = hazelcastInstance.getMap(hazelcastMapName);
map.addIndex("name", false);
map.addEntryListener((MapListener) entryListener(), false);
return new HazelcastApplicationStore(map);
}
@Bean
@ConditionalOnMissingBean
public JournaledEventStore journaledEventStore() {
IList<ClientApplicationEvent> list = hazelcastInstance.getList(eventListName);
return new HazelcastJournaledEventStore(list);
}
@Bean
public EntryListener<String, Application> entryListener() {
return new EntryAdapter<String, Application>() {
@Override
......@@ -85,7 +91,6 @@ public class HazelcastStoreConfiguration {
public void onMapEvent(MapEvent event) {
publisher.publishEvent(new RoutesOutdatedEvent());
}
};
}
}
/*
* 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.journal.store;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import com.hazelcast.core.IList;
import de.codecentric.boot.admin.event.ClientApplicationEvent;
/**
* Event-Store backed by a Hazelcast-list.
*
* @author Johannes Edmeier
*/
public class HazelcastJournaledEventStore implements JournaledEventStore {
private IList<ClientApplicationEvent> store;
public HazelcastJournaledEventStore(IList<ClientApplicationEvent> store) {
this.store = store;
}
@Override
public Collection<ClientApplicationEvent> findAll() {
ArrayList<ClientApplicationEvent> list = new ArrayList<>(store);
Collections.reverse(list);
return list;
}
@Override
public void store(ClientApplicationEvent event) {
store.add(event);
}
}
......@@ -15,12 +15,18 @@
"defaultValue": "true"
},
{
"name": "spring.boot.admin.hazelcast.map",
"name": "spring.boot.admin.hazelcast.application-store",
"type": "java.lang.String",
"description": "Name of backing Hazelcast-Map",
"description": "Name of backing Hazelcast-Map for storing applications",
"defaultValue": "spring-boot-admin-application-store"
},
{
"name": "spring.boot.admin.hazelcast.map",
"type": "java.lang.String",
"description": "Name of backing Hazelcast-List for storing the journal",
"defaultValue": "spring-boot-admin-event-store"
},
{
"name": "spring.boot.admin.discovery.enabled",
"type": "java.lang.Boolean",
"description": "Enable Spring Cloud Discovery support.",
......
......@@ -31,12 +31,17 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext;
import org.springframework.boot.test.TestRestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import com.hazelcast.config.Config;
import com.hazelcast.config.EvictionPolicy;
import com.hazelcast.config.ListConfig;
import com.hazelcast.config.MapConfig;
import de.codecentric.boot.admin.config.EnableAdminServer;
import de.codecentric.boot.admin.model.Application;
......@@ -50,8 +55,15 @@ public class AdminApplicationHazelcastTest {
@Configuration
@EnableAutoConfiguration
@EnableAdminServer
@ImportResource("classpath:hazelcast-test.xml")
public static class TestAdminApplication {
@Bean
public Config hazelcastConfig() {
return new Config()
.addMapConfig(new MapConfig("spring-boot-admin-application-store")
.setBackupCount(1).setEvictionPolicy(EvictionPolicy.NONE))
.addListConfig(new ListConfig("spring-boot-admin-application-store")
.setBackupCount(1).setMaxSize(1000));
}
}
private RestTemplate template = new TestRestTemplate();
......@@ -63,12 +75,10 @@ public class AdminApplicationHazelcastTest {
System.setProperty("hazelcast.wait.seconds.before.join", "0");
instance1 = (EmbeddedWebApplicationContext) SpringApplication.run(
TestAdminApplication.class,
new String[] { "--server.port=0", "--spring.jmx.enabled=false",
"--spring.boot.admin.hazelcast.enabled=true" });
new String[] { "--server.port=0", "--spring.jmx.enabled=false" });
instance2 = (EmbeddedWebApplicationContext) SpringApplication.run(
TestAdminApplication.class,
new String[] { "--server.port=0", "--spring.jmx.enabled=false",
"--spring.boot.admin.hazelcast.enabled=true" });
new String[] { "--server.port=0", "--spring.jmx.enabled=false" });
}
@After
......@@ -114,8 +124,8 @@ public class AdminApplicationHazelcastTest {
private ResponseEntity<Application> getApp(String id, EmbeddedWebApplicationContext context) {
int port = context.getEmbeddedServletContainer().getPort();
ResponseEntity<Application> getResponse = template.getForEntity("http://localhost:" + port
+ "/api/applications/" + id, Application.class);
ResponseEntity<Application> getResponse = template.getForEntity(
"http://localhost:" + port + "/api/applications/" + id, Application.class);
return getResponse;
}
......@@ -130,8 +140,8 @@ public class AdminApplicationHazelcastTest {
private ResponseEntity<Collection<Application>> getAppByName(String name,
EmbeddedWebApplicationContext context) {
int port = context.getEmbeddedServletContainer().getPort();
ResponseEntity<?> getResponse = template.getForEntity("http://localhost:" + port
+ "/api/applications?name={name}", ApplicationList.class,
ResponseEntity<?> getResponse = template.getForEntity(
"http://localhost:" + port + "/api/applications?name={name}", ApplicationList.class,
Collections.singletonMap("name", name));
return (ResponseEntity<Collection<Application>>) getResponse;
}
......
package de.codecentric.boot.admin.config;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.isA;
import static org.junit.Assert.assertThat;
......@@ -12,15 +13,23 @@ import java.util.List;
import org.junit.After;
import org.junit.Test;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration;
import org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration;
import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.cloud.client.discovery.noop.NoopDiscoveryClientAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import com.hazelcast.config.Config;
import de.codecentric.boot.admin.discovery.ApplicationDiscoveryListener;
import de.codecentric.boot.admin.journal.store.HazelcastJournaledEventStore;
import de.codecentric.boot.admin.journal.store.JournaledEventStore;
import de.codecentric.boot.admin.journal.store.SimpleJournaledEventStore;
import de.codecentric.boot.admin.notify.MailNotifier;
import de.codecentric.boot.admin.registry.store.ApplicationStore;
import de.codecentric.boot.admin.registry.store.HazelcastApplicationStore;
......@@ -63,42 +72,62 @@ public class AdminServerWebConfigurationTest {
@Test
public void simpleConfig() {
load("spring.boot.admin.hazelcast.enabled:false",
"spring.boot.admin.discovery.enabled:false");
assertTrue(context.getBean(ApplicationStore.class) instanceof SimpleApplicationStore);
load("spring.boot.admin.discovery.enabled:false");
assertThat(context.getBean(ApplicationStore.class),
is(instanceOf(SimpleApplicationStore.class)));
assertTrue(context.getBeansOfType(ApplicationDiscoveryListener.class).isEmpty());
assertTrue(context.getBeansOfType(MailNotifier.class).isEmpty());
assertThat(context.getBean(JournaledEventStore.class),
is(instanceOf(SimpleJournaledEventStore.class)));
}
@Test
public void simpleConfig_mail() {
load("spring.mail.host:localhost", "spring.boot.admin.hazelcast.enabled:false",
"spring.boot.admin.discovery.enabled:false");
assertTrue(context.getBean(MailNotifier.class) instanceof MailNotifier);
load("spring.mail.host:localhost", "spring.boot.admin.discovery.enabled:false");
assertThat(context.getBean(MailNotifier.class), is(instanceOf(MailNotifier.class)));
}
@Test
public void hazelcastConfig() {
load("spring.boot.admin.hazelcast.enabled:true",
"spring.boot.admin.discovery.enabled:false");
assertTrue(context.getBean(ApplicationStore.class) instanceof HazelcastApplicationStore);
load(TestHazelcastConfig.class, "spring.boot.admin.discovery.enabled:false");
assertThat(context.getBean(ApplicationStore.class),
is(instanceOf(HazelcastApplicationStore.class)));
assertThat(context.getBean(JournaledEventStore.class),
is(instanceOf(HazelcastJournaledEventStore.class)));
assertTrue(context.getBeansOfType(ApplicationDiscoveryListener.class).isEmpty());
}
@Test
public void discoveryConfig() {
load("spring.boot.admin.hazelcast.enabled:false",
"spring.boot.admin.discovery.enabled:true");
assertTrue(context.getBean(ApplicationStore.class) instanceof SimpleApplicationStore);
load("spring.boot.admin.discovery.enabled:true");
assertThat(context.getBean(ApplicationStore.class),
is(instanceOf(SimpleApplicationStore.class)));
context.getBean(ApplicationDiscoveryListener.class);
}
@Configuration
static class TestHazelcastConfig {
@Bean
Config config() {
return new Config();
}
}
private void load(String... environment) {
load(null, environment);
}
private void load(Class<?> config, String... environment) {
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
if (config != null) {
applicationContext.register(config);
}
applicationContext.register(PropertyPlaceholderAutoConfiguration.class);
applicationContext.register(ServerPropertiesAutoConfiguration.class);
applicationContext.register(NoopDiscoveryClientAutoConfiguration.class);
applicationContext.register(MailSenderAutoConfiguration.class);
applicationContext.register(HazelcastAutoConfiguration.class);
applicationContext.register(MailNotifierConfiguration.class);
applicationContext.register(HazelcastStoreConfiguration.class);
applicationContext.register(DiscoveryClientConfiguration.class);
......
......@@ -2,5 +2,4 @@ server.port=8080
info.version=1.0.0
spring.application.name=spring-boot-admin-server-test
spring.boot.admin.url=http://localhost:8080
spring.boot.admin.hazelcast.enabled=false
spring.boot.admin.discovery.enabled=false
\ No newline at end of file
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:hz="http://www.hazelcast.com/schema/spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.hazelcast.com/schema/spring
http://www.hazelcast.com/schema/spring/hazelcast-spring-3.3.xsd">
<hz:config id="hazelcastConfig">
<hz:network port="5701">
<hz:join>
<hz:multicast enabled="false"/>
<hz:tcp-ip enabled="true">
<hz:member>localhost</hz:member>
</hz:tcp-ip>
</hz:join>
</hz:network>
</hz:config>
</beans>
\ No newline at end of file
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