JAVA-11240 Moved spring-cloud to spring-cloud-modules
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
## Spring Cloud
|
||||
|
||||
This module contains modules about Spring Cloud
|
||||
@@ -0,0 +1,95 @@
|
||||
<?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>
|
||||
<groupId>com.baeldung.spring.cloud</groupId>
|
||||
<artifactId>spring-cloud-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<name>spring-cloud-modules</name>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<modules>
|
||||
<module>spring-cloud-loadbalancer</module>
|
||||
<module>spring-cloud-config</module>
|
||||
<module>spring-cloud-eureka</module>
|
||||
<module>spring-cloud-hystrix</module>
|
||||
<module>spring-cloud-bootstrap</module>
|
||||
<module>spring-cloud-ribbon-client</module>
|
||||
<module>spring-cloud-rest</module>
|
||||
<module>spring-cloud-zookeeper</module>
|
||||
<module>spring-cloud-gateway</module>
|
||||
<module>spring-cloud-stream</module>
|
||||
<module>spring-cloud-stream-starters/twitterhdfs</module>
|
||||
<module>spring-cloud-connectors-heroku</module>
|
||||
<module>spring-cloud-aws</module>
|
||||
<module>spring-cloud-consul</module>
|
||||
<module>spring-cloud-zuul-eureka-integration</module>
|
||||
<module>spring-cloud-contract</module>
|
||||
<module>spring-cloud-kubernetes</module>
|
||||
<module>spring-cloud-open-service-broker</module>
|
||||
<module>spring-cloud-archaius</module>
|
||||
<module>spring-cloud-functions</module>
|
||||
<module>spring-cloud-vault</module>
|
||||
<module>spring-cloud-security</module>
|
||||
<module>spring-cloud-task</module>
|
||||
<module>spring-cloud-zuul</module>
|
||||
<module>spring-cloud-zuul-fallback</module>
|
||||
<module>spring-cloud-ribbon-retry</module>
|
||||
<module>spring-cloud-circuit-breaker</module>
|
||||
<module>spring-cloud-eureka-self-preservation</module>
|
||||
<module>spring-cloud-openfeign</module>
|
||||
<module>spring-cloud-netflix-feign</module>
|
||||
<module>spring-cloud-sentinel</module>
|
||||
<module>spring-cloud-dapr</module>
|
||||
<module>spring-cloud-docker</module>
|
||||
</modules>
|
||||
|
||||
<build>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit</groupId>
|
||||
<artifactId>junit-bom</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-dependencies</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<properties>
|
||||
<spring-cloud-dependencies.version>2021.0.0</spring-cloud-dependencies.version>
|
||||
<spring-cloud-starter-config.version>2.2.3.RELEASE</spring-cloud-starter-config.version>
|
||||
<spring-cloud-config-server.version>2.2.3.RELEASE</spring-cloud-config-server.version>
|
||||
<spring-cloud-starter-eureka.version>1.4.7.RELEASE</spring-cloud-starter-eureka.version>
|
||||
<spring-cloud-starter-feign.version>1.4.7.RELEASE</spring-cloud-starter-feign.version>
|
||||
<spring-cloud-starter-hystrix.version>1.4.7.RELEASE</spring-cloud-starter-hystrix.version>
|
||||
<spring-cloud-stream.version>3.1.3</spring-cloud-stream.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,21 @@
|
||||
# Spring Cloud Archaius
|
||||
|
||||
This module contains articles about Spring Cloud with Netflix Archaius
|
||||
|
||||
# Relevant Articles
|
||||
|
||||
- [Introduction to Netflix Archaius with Spring Cloud](https://www.baeldung.com/netflix-archaius-spring-cloud-integration)
|
||||
- [Netflix Archaius with Various Database Configurations](https://www.baeldung.com/netflix-archaius-database-configurations)
|
||||
|
||||
#### Basic Config
|
||||
This service has the basic, out-of-the-box Spring Cloud Netflix Archaius configuration.
|
||||
|
||||
#### Extra Configs
|
||||
This service customizes some properties supported by Archaius.
|
||||
|
||||
These properties are set up on the main method, since Archaius uses System properties, but they could be added as command line arguments when launching the app.
|
||||
|
||||
#### Additional Sources
|
||||
In this service we create a new AbstractConfiguration bean, setting up a new Configuration Properties source.
|
||||
|
||||
These properties have precedence over all the other properties in the Archaius Composite Configuration.
|
||||
@@ -0,0 +1,24 @@
|
||||
<?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>additional-sources-simple</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<name>additional-sources-simple</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.spring.cloud</groupId>
|
||||
<artifactId>spring-cloud-archaius</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.spring.cloud.archaius.additionalsources;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class AdditionalSourcesSimpleApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(AdditionalSourcesSimpleApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.spring.cloud.archaius.additionalsources.config;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
|
||||
import org.apache.commons.configuration.AbstractConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
import com.netflix.config.DynamicConfiguration;
|
||||
import com.netflix.config.FixedDelayPollingScheduler;
|
||||
import com.netflix.config.PolledConfigurationSource;
|
||||
import com.netflix.config.sources.URLConfigurationSource;
|
||||
|
||||
@Configuration
|
||||
public class ApplicationPropertiesConfigurations {
|
||||
|
||||
@Bean
|
||||
public AbstractConfiguration addApplicationPropertiesSource() throws IOException {
|
||||
URL configPropertyURL = (new ClassPathResource("other-config.properties")).getURL();
|
||||
PolledConfigurationSource source = new URLConfigurationSource(configPropertyURL);
|
||||
return new DynamicConfiguration(source, new FixedDelayPollingScheduler());
|
||||
}
|
||||
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.spring.cloud.archaius.additionalsources.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 {
|
||||
|
||||
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!");
|
||||
|
||||
private DynamicStringProperty propertyFourWithDynamic = DynamicPropertyFactory.getInstance()
|
||||
.getStringProperty("baeldung.archaius.properties.four", "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());
|
||||
properties.put(propertyFourWithDynamic.getName(), propertyFourWithDynamic.get());
|
||||
return properties;
|
||||
}
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
server.port=8082
|
||||
baeldung.archaius.properties.one=one FROM:application.properties
|
||||
baeldung.archaius.properties.two=two FROM:application.properties
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
baeldung.archaius.properties.one=one FROM:config.properties
|
||||
baeldung.archaius.properties.three=three FROM:config.properties
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
baeldung.archaius.properties.one=one FROM:other-config.properties
|
||||
baeldung.archaius.properties.four=four FROM:other-config.properties
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
package com.baeldung.spring.cloud.archaius.additionalsources;
|
||||
|
||||
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 ArchaiusAdditionalSourcesLiveTest {
|
||||
|
||||
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:other-config.properties");
|
||||
map.put("baeldung.archaius.properties.two", "two FROM:application.properties");
|
||||
map.put("baeldung.archaius.properties.three", "three FROM:config.properties");
|
||||
map.put("baeldung.archaius.properties.four", "four FROM:other-config.properties");
|
||||
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);
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.spring.cloud.archaius.additionalsources;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = AdditionalSourcesSimpleApplication.class)
|
||||
public class SpringContextTest {
|
||||
|
||||
@Test
|
||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration scan="true" scanPeriod="15 seconds" debug="false">
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>[%d{ISO8601}]-[%thread] %-5level %logger - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -0,0 +1,24 @@
|
||||
<?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>basic-config</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<name>basic-config</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.spring.cloud</groupId>
|
||||
<artifactId>spring-cloud-archaius</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.spring.cloud.archaius.basic;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class BasicArchaiusApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(BasicArchaiusApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
package com.baeldung.spring.cloud.archaius.basic.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.DynamicIntProperty;
|
||||
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;
|
||||
|
||||
@Value("${baeldung.archaius.properties.four:not found!}")
|
||||
private String propertyFourWithValue;
|
||||
|
||||
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!");
|
||||
|
||||
private DynamicStringProperty propertyFourWithDynamic = DynamicPropertyFactory.getInstance()
|
||||
.getStringProperty("baeldung.archaius.properties.four", "not found!");
|
||||
|
||||
private DynamicIntProperty intPropertyWithDynamic = DynamicPropertyFactory.getInstance()
|
||||
.getIntProperty("baeldung.archaius.properties.int", 0);
|
||||
|
||||
@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);
|
||||
properties.put("baeldung.archaius.properties.four", propertyFourWithValue);
|
||||
return properties;
|
||||
}
|
||||
|
||||
@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());
|
||||
properties.put(propertyFourWithDynamic.getName(), propertyFourWithDynamic.get());
|
||||
return properties;
|
||||
}
|
||||
|
||||
@GetMapping("/int-property")
|
||||
public Map<String, Integer> getIntPropertyFromDynamic() {
|
||||
Map<String, Integer> properties = new HashMap<>();
|
||||
properties.put(intPropertyWithDynamic.getName(), intPropertyWithDynamic.get());
|
||||
return properties;
|
||||
}
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
server.port=8080
|
||||
baeldung.archaius.properties.one=one FROM:application.properties
|
||||
baeldung.archaius.properties.two=two FROM:application.properties
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
baeldung.archaius.properties.one=one FROM:config.properties
|
||||
baeldung.archaius.properties.three=three FROM:config.properties
|
||||
|
||||
baeldung.archaius.properties.int=1
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
baeldung.archaius.properties.one=one FROM:other.properties
|
||||
baeldung.archaius.properties.four=four FROM:other.properties
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.spring.cloud.archaius.basic.BasicArchaiusApplication;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = BasicArchaiusApplication.class)
|
||||
public class SpringContextTest {
|
||||
|
||||
@Test
|
||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||
}
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.spring.cloud.archaius.basic;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
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.util.TestPropertyValues;
|
||||
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.netflix.config.DynamicPropertyFactory;
|
||||
import com.netflix.config.DynamicStringProperty;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class ArchaiusBasicConfigurationIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
ConfigurableApplicationContext context;
|
||||
|
||||
private DynamicStringProperty testPropertyWithDynamic = DynamicPropertyFactory.getInstance()
|
||||
.getStringProperty("baeldung.archaius.test.properties.one", "not found!");
|
||||
|
||||
@Test
|
||||
public void givenIntialPropertyValue_whenPropertyChanges_thenArchaiusRetrievesNewValue() {
|
||||
String initialValue = testPropertyWithDynamic.get();
|
||||
|
||||
TestPropertyValues.of("baeldung.archaius.test.properties.one=new-value")
|
||||
.applyTo(context);
|
||||
context.publishEvent(new EnvironmentChangeEvent(Collections.singleton("baeldung.archaius.test.properties.one")));
|
||||
String finalValue = testPropertyWithDynamic.get();
|
||||
|
||||
assertThat(initialValue).isEqualTo("test-one");
|
||||
assertThat(finalValue).isEqualTo("new-value");
|
||||
}
|
||||
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
package com.baeldung.spring.cloud.archaius.basic;
|
||||
|
||||
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 ArchaiusBasicConfigurationLiveTest {
|
||||
|
||||
private static final String BASE_URL = "http://localhost:8080";
|
||||
|
||||
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:application.properties");
|
||||
map.put("baeldung.archaius.properties.two", "two FROM:application.properties");
|
||||
map.put("baeldung.archaius.properties.three", "three FROM:config.properties");
|
||||
map.put("baeldung.archaius.properties.four", "not found!");
|
||||
return map;
|
||||
}
|
||||
|
||||
private static final String VALUE_PROPERTIES_URL = "/properties-from-value";
|
||||
private static final Map<String, String> EXPECTED_VALUE_PROPERTIES = createExpectedValueProperties();
|
||||
|
||||
private static Map<String, String> createExpectedValueProperties() {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("baeldung.archaius.properties.one", "one FROM:application.properties");
|
||||
map.put("baeldung.archaius.properties.two", "two FROM:application.properties");
|
||||
map.put("baeldung.archaius.properties.three", "not found!");
|
||||
map.put("baeldung.archaius.properties.four", "not found!");
|
||||
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 givenDefaultConfigurationSetup_whenRequestProperties_thenEndpointRetrievesValuesInFiles() {
|
||||
Map<String, String> initialResponse = this.exchangeAsMap(BASE_URL + DYNAMIC_PROPERTIES_URL, new ParameterizedTypeReference<Map<String, String>>() {
|
||||
});
|
||||
|
||||
assertThat(initialResponse).containsAllEntriesOf(EXPECTED_ARCHAIUS_PROPERTIES);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNonDefaultConfigurationFilesSetup_whenRequestSpringVisibleProperties_thenEndpointDoesntRetrieveArchaiusProperties() {
|
||||
Map<String, String> initialResponse = this.exchangeAsMap(BASE_URL + VALUE_PROPERTIES_URL, new ParameterizedTypeReference<Map<String, String>>() {
|
||||
});
|
||||
|
||||
assertThat(initialResponse).containsAllEntriesOf(EXPECTED_VALUE_PROPERTIES);
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.spring.cloud.archaius.basic;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class SpringContextTest {
|
||||
|
||||
@Test
|
||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||
}
|
||||
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
baeldung.archaius.test.properties.one=test-one
|
||||
baeldung.archaius.test.properties.two=test-two
|
||||
baeldung.archaius.test.properties.int=1
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration scan="true" scanPeriod="15 seconds" debug="false">
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>[%d{ISO8601}]-[%thread] %-5level %logger - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -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>dynamodb-config</artifactId>
|
||||
<name>dynamodb-config</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.spring.cloud</groupId>
|
||||
<artifactId>spring-cloud-archaius</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.amazonaws</groupId>
|
||||
<artifactId>aws-java-sdk-dynamodb</artifactId>
|
||||
<version>${aws.sdk.dynamo.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.derjust</groupId>
|
||||
<artifactId>spring-data-dynamodb</artifactId>
|
||||
<version>${spring.dynamo.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.netflix.archaius</groupId>
|
||||
<artifactId>archaius-aws</artifactId>
|
||||
<version>${archaius.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<aws.sdk.dynamo.version>1.11.407</aws.sdk.dynamo.version>
|
||||
<spring.dynamo.version>5.0.3</spring.dynamo.version>
|
||||
<archaius.version>0.7.6</archaius.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.spring.cloud.archaius.dynamosources;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class DynamoSourcesApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(DynamoSourcesApplication.class, args);
|
||||
}
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package com.baeldung.spring.cloud.archaius.dynamosources.config;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.apache.commons.configuration.AbstractConfiguration;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
|
||||
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
|
||||
import com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
|
||||
import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
|
||||
import com.amazonaws.services.dynamodbv2.util.TableUtils;
|
||||
import com.baeldung.spring.cloud.archaius.dynamosources.dynamodb.ArchaiusProperties;
|
||||
import com.baeldung.spring.cloud.archaius.dynamosources.dynamodb.ArchaiusPropertiesRepository;
|
||||
import com.netflix.config.DynamicConfiguration;
|
||||
import com.netflix.config.FixedDelayPollingScheduler;
|
||||
import com.netflix.config.PolledConfigurationSource;
|
||||
import com.netflix.config.sources.DynamoDbConfigurationSource;
|
||||
|
||||
@Configuration
|
||||
public class ApplicationPropertiesConfigurations {
|
||||
|
||||
@Autowired
|
||||
AmazonDynamoDB amazonDynamoDb;
|
||||
|
||||
@Autowired
|
||||
private ArchaiusPropertiesRepository repository;
|
||||
|
||||
@Bean
|
||||
public AbstractConfiguration addApplicationPropertiesSource() {
|
||||
// Normally, the DB Table would be already created and populated.
|
||||
// In this case, we'll do it just before creating the archaius config source that uses it
|
||||
initDatabase();
|
||||
PolledConfigurationSource source = new DynamoDbConfigurationSource(amazonDynamoDb);
|
||||
return new DynamicConfiguration(source, new FixedDelayPollingScheduler());
|
||||
}
|
||||
|
||||
private void initDatabase() {
|
||||
// Create the table
|
||||
DynamoDBMapper mapper = new DynamoDBMapper(amazonDynamoDb);
|
||||
CreateTableRequest tableRequest = mapper.generateCreateTableRequest(ArchaiusProperties.class);
|
||||
tableRequest.setProvisionedThroughput(new ProvisionedThroughput(1L, 1L));
|
||||
TableUtils.createTableIfNotExists(amazonDynamoDb, tableRequest);
|
||||
|
||||
// Populate the table
|
||||
ArchaiusProperties property = new ArchaiusProperties("baeldung.archaius.properties.one", "one FROM:dynamoDB");
|
||||
ArchaiusProperties property3 = new ArchaiusProperties("baeldung.archaius.properties.three", "three FROM:dynamoDB");
|
||||
repository.saveAll(Arrays.asList(property, property3));
|
||||
}
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.spring.cloud.archaius.dynamosources.config;
|
||||
|
||||
import org.socialsignin.spring.data.dynamodb.repository.config.EnableDynamoDBRepositories;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.amazonaws.auth.AWSCredentialsProvider;
|
||||
import com.amazonaws.auth.AWSStaticCredentialsProvider;
|
||||
import com.amazonaws.auth.BasicAWSCredentials;
|
||||
import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration;
|
||||
import com.amazonaws.regions.Regions;
|
||||
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
|
||||
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
|
||||
|
||||
@Configuration
|
||||
@EnableDynamoDBRepositories(basePackages = "com.baeldung.spring.cloud.archaius.dynamosources.dynamodb")
|
||||
public class DynamoDbConfiguration {
|
||||
|
||||
@Value("${amazon.dynamodb.endpoint}")
|
||||
private String amazonDynamoDBEndpoint;
|
||||
|
||||
@Value("${aws.accessKeyId}")
|
||||
private String amazonDynamoDBAccessKeyId;
|
||||
|
||||
@Value("${aws.secretKey}")
|
||||
private String amazonDynamoDBSecretKey;
|
||||
|
||||
@Bean
|
||||
public AmazonDynamoDB amazonDynamoDB() {
|
||||
AmazonDynamoDB amazonDynamoDB = AmazonDynamoDBClientBuilder.standard()
|
||||
.withCredentials(amazonAWSCredentials())
|
||||
.withEndpointConfiguration(setupEndpointConfiguration())
|
||||
.build();
|
||||
|
||||
return amazonDynamoDB;
|
||||
}
|
||||
|
||||
private AWSCredentialsProvider amazonAWSCredentials() {
|
||||
return new AWSStaticCredentialsProvider(new BasicAWSCredentials(amazonDynamoDBAccessKeyId, amazonDynamoDBSecretKey));
|
||||
}
|
||||
|
||||
private EndpointConfiguration setupEndpointConfiguration() {
|
||||
return new EndpointConfiguration(amazonDynamoDBEndpoint, Regions.DEFAULT_REGION.getName());
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.spring.cloud.archaius.dynamosources.controller;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
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 {
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user