This commit is contained in:
Jonathan Cook
2019-10-23 15:01:44 +02:00
parent db85c8f275
commit 684ec0d2e3
20486 changed files with 1642483 additions and 0 deletions
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>zookeeper-config</artifactId>
<name>zookeeper-config</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.spring.cloud</groupId>
<artifactId>spring-cloud-archaius</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-config</artifactId>
<version>${cloud.zookeeper.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zookeeper.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<properties>
<cloud.zookeeper.version>2.0.0.RELEASE</cloud.zookeeper.version>
<zookeeper.version>3.4.13</zookeeper.version>
</properties>
</project>
@@ -0,0 +1,12 @@
package com.baeldung.spring.cloud.archaius.zookeeperconfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ZookeeperConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ZookeeperConfigApplication.class, args);
}
}
@@ -0,0 +1,58 @@
package com.baeldung.spring.cloud.archaius.zookeeperconfig.config;
import org.apache.curator.framework.CuratorFramework;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
/**
*
* Ideally, we wouldn't need to initialize the zookeeper config values.
* Here we do it to verify that configurations are being retrieved.
*
*/
@Component
public class ZookeeperConfigsInitializer {
private static final String CONFIG_BASE_NODE_PATH = "/config";
private static final String APPLICATION_BASE_NODE_PATH = CONFIG_BASE_NODE_PATH + "/application";
@Autowired
CuratorFramework client;
@EventListener
public void appReady(ApplicationReadyEvent event) throws Exception {
String pathOne = APPLICATION_BASE_NODE_PATH + "/baeldung.archaius.properties.one";
String valueOne = "one FROM:zookeeper";
String pathThree = APPLICATION_BASE_NODE_PATH + "/baeldung.archaius.properties.three";
String valueThree = "three FROM:zookeeper";
createBaseNodes();
setValue(pathOne, valueOne);
setValue(pathThree, valueThree);
}
private void setValue(String path, String value) throws Exception {
if (client.checkExists()
.forPath(path) == null) {
client.create()
.forPath(path, value.getBytes());
} else {
client.setData()
.forPath(path, value.getBytes());
}
}
private void createBaseNodes() throws Exception {
if (client.checkExists()
.forPath(CONFIG_BASE_NODE_PATH) == null) {
client.create()
.forPath(CONFIG_BASE_NODE_PATH);
}
if (client.checkExists()
.forPath(APPLICATION_BASE_NODE_PATH) == null) {
client.create()
.forPath(APPLICATION_BASE_NODE_PATH);
}
}
}
@@ -0,0 +1,51 @@
package com.baeldung.spring.cloud.archaius.zookeeperconfig.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.netflix.config.DynamicPropertyFactory;
import com.netflix.config.DynamicStringProperty;
@RestController
public class ConfigPropertiesController {
@Value("${baeldung.archaius.properties.one:not found!}")
private String propertyOneWithValue;
@Value("${baeldung.archaius.properties.two:not found!}")
private String propertyTwoWithValue;
@Value("${baeldung.archaius.properties.three:not found!}")
private String propertyThreeWithValue;
private DynamicStringProperty propertyOneWithDynamic = DynamicPropertyFactory.getInstance()
.getStringProperty("baeldung.archaius.properties.one", "not found!");
private DynamicStringProperty propertyTwoWithDynamic = DynamicPropertyFactory.getInstance()
.getStringProperty("baeldung.archaius.properties.two", "not found!");
private DynamicStringProperty propertyThreeWithDynamic = DynamicPropertyFactory.getInstance()
.getStringProperty("baeldung.archaius.properties.three", "not found!");
@GetMapping("/properties-from-dynamic")
public Map<String, String> getPropertiesFromDynamic() {
Map<String, String> properties = new HashMap<>();
properties.put(propertyOneWithDynamic.getName(), propertyOneWithDynamic.get());
properties.put(propertyTwoWithDynamic.getName(), propertyTwoWithDynamic.get());
properties.put(propertyThreeWithDynamic.getName(), propertyThreeWithDynamic.get());
return properties;
}
@GetMapping("/properties-from-value")
public Map<String, String> getPropertiesFromValue() {
Map<String, String> properties = new HashMap<>();
properties.put("baeldung.archaius.properties.one", propertyOneWithValue);
properties.put("baeldung.archaius.properties.two", propertyTwoWithValue);
properties.put("baeldung.archaius.properties.three", propertyThreeWithValue);
return properties;
}
}
@@ -0,0 +1,4 @@
server.port=8082
baeldung.archaius.properties.one=one FROM:application.properties
baeldung.archaius.properties.two=two FROM:application.properties
spring.application.name=zookeeper-config
@@ -0,0 +1,53 @@
package com.baeldung.spring.cloud.archaius.zookeeperconfig;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ArchaiusZookeeperLiveTest {
private static final String BASE_URL = "http://localhost:8082";
private static final String DYNAMIC_PROPERTIES_URL = "/properties-from-dynamic";
private static final Map<String, String> EXPECTED_ARCHAIUS_PROPERTIES = createExpectedArchaiusProperties();
private static Map<String, String> createExpectedArchaiusProperties() {
Map<String, String> map = new HashMap<>();
map.put("baeldung.archaius.properties.one", "one FROM:zookeeper");
map.put("baeldung.archaius.properties.two", "two FROM:application.properties");
map.put("baeldung.archaius.properties.three", "three FROM:zookeeper");
return map;
}
@Autowired
ConfigurableApplicationContext context;
@Autowired
private TestRestTemplate template;
private <T> Map<T, T> exchangeAsMap(String uri, ParameterizedTypeReference<Map<T, T>> responseType) {
return template.exchange(uri, HttpMethod.GET, null, responseType)
.getBody();
}
@Test
public void givenNonDefaultConfigurationFilesSetup_whenRequestProperties_thenEndpointRetrievesValuesInFiles() {
Map<String, String> initialResponse = this.exchangeAsMap(BASE_URL + DYNAMIC_PROPERTIES_URL, new ParameterizedTypeReference<Map<String, String>>() {
});
assertThat(initialResponse).containsAllEntriesOf(EXPECTED_ARCHAIUS_PROPERTIES);
}
}
@@ -0,0 +1,20 @@
package com.baeldung.spring.cloud.archaius.zookeeperconfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* This Live tTest requires:
* * A Zookeeper instance running locally on port 2181 (e.g. using `docker run --name bael-zookeeper -p 2181:2181 --restart always zookeeper`)
*
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ZookeeperConfigApplication.class)
public class SpringContextLiveTest {
@Test
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
}
}