Skip to content

Commit 96e1728

Browse files
rozagerardomaibin
authored andcommitted
geroza/BAEL-395-Archaius_With_Spring_Cloud_Introduction (eugenp#4968)
* * added examples for archaius introduction, with basic configuration, extra configurations, adn setting up an additional config source * * fixed indentation in XML files
1 parent a966b54 commit 96e1728

27 files changed

Lines changed: 653 additions & 0 deletions

File tree

‎spring-cloud/pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
<module>spring-cloud-zuul-eureka-integration</module>
3232
<module>spring-cloud-contract</module>
3333
<module>spring-cloud-kubernetes</module>
34+
<module>spring-cloud-archaius</module>
3435
</modules>
3536

3637
<build>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Spring Cloud Archaius
2+
3+
#### Basic Config
4+
This service has the basic, out-of-the-box Spring Cloud Netflix Archaius configuration.
5+
6+
#### Extra Configs
7+
This service customizes some properties supported by Archaius.
8+
9+
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.
10+
11+
#### Additional Sources
12+
In this service we create a new AbstractConfiguration bean, setting up a new Configuration Properties source.
13+
14+
These properties have precedence over all the other properties in the Archaius Composite Configuration.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>additional-sources-simple</artifactId>
7+
<version>1.0.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
<name>additional-sources-simple</name>
10+
11+
<parent>
12+
<groupId>com.baeldung.spring.cloud</groupId>
13+
<artifactId>spring-cloud-archaius</artifactId>
14+
<version>1.0.0-SNAPSHOT</version>
15+
<relativePath>..</relativePath>
16+
</parent>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.springframework.boot</groupId>
21+
<artifactId>spring-boot-starter-web</artifactId>
22+
</dependency>
23+
</dependencies>
24+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.spring.cloud.archaius.additionalsources;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class AdditionalSourcesSimpleApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(AdditionalSourcesSimpleApplication.class, args);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.spring.cloud.archaius.additionalsources.config;
2+
3+
import org.apache.commons.configuration.AbstractConfiguration;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
7+
import com.netflix.config.DynamicConfiguration;
8+
import com.netflix.config.FixedDelayPollingScheduler;
9+
import com.netflix.config.PolledConfigurationSource;
10+
import com.netflix.config.sources.URLConfigurationSource;
11+
12+
@Configuration
13+
public class ApplicationPropertiesConfigurations {
14+
15+
@Bean
16+
public AbstractConfiguration addApplicationPropertiesSource() {
17+
PolledConfigurationSource source = new URLConfigurationSource("classpath:other-config.properties");
18+
return new DynamicConfiguration(source, new FixedDelayPollingScheduler());
19+
}
20+
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.spring.cloud.archaius.additionalsources.controller;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import org.springframework.beans.factory.annotation.Value;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
import com.netflix.config.DynamicPropertyFactory;
11+
import com.netflix.config.DynamicStringProperty;
12+
13+
@RestController
14+
public class ConfigPropertiesController {
15+
16+
private DynamicStringProperty propertyOneWithDynamic = DynamicPropertyFactory.getInstance()
17+
.getStringProperty("baeldung.archaius.properties.one", "not found!");
18+
19+
private DynamicStringProperty propertyTwoWithDynamic = DynamicPropertyFactory.getInstance()
20+
.getStringProperty("baeldung.archaius.properties.two", "not found!");
21+
22+
private DynamicStringProperty propertyThreeWithDynamic = DynamicPropertyFactory.getInstance()
23+
.getStringProperty("baeldung.archaius.properties.three", "not found!");
24+
25+
private DynamicStringProperty propertyFourWithDynamic = DynamicPropertyFactory.getInstance()
26+
.getStringProperty("baeldung.archaius.properties.four", "not found!");
27+
28+
@GetMapping("/properties-from-dynamic")
29+
public Map<String, String> getPropertiesFromDynamic() {
30+
Map<String, String> properties = new HashMap<>();
31+
properties.put(propertyOneWithDynamic.getName(), propertyOneWithDynamic.get());
32+
properties.put(propertyTwoWithDynamic.getName(), propertyTwoWithDynamic.get());
33+
properties.put(propertyThreeWithDynamic.getName(), propertyThreeWithDynamic.get());
34+
properties.put(propertyFourWithDynamic.getName(), propertyFourWithDynamic.get());
35+
return properties;
36+
}
37+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
server.port=8082
2+
baeldung.archaius.properties.one=one FROM:application.properties
3+
baeldung.archaius.properties.two=two FROM:application.properties
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
baeldung.archaius.properties.one=one FROM:config.properties
2+
baeldung.archaius.properties.three=three FROM:config.properties
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
baeldung.archaius.properties.one=one FROM:other-config.properties
2+
baeldung.archaius.properties.four=four FROM:other-config.properties
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.baeldung.spring.cloud.archaius.additionalsources;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.boot.test.context.SpringBootTest;
12+
import org.springframework.boot.test.web.client.TestRestTemplate;
13+
import org.springframework.context.ConfigurableApplicationContext;
14+
import org.springframework.core.ParameterizedTypeReference;
15+
import org.springframework.http.HttpMethod;
16+
import org.springframework.test.context.junit4.SpringRunner;
17+
18+
@RunWith(SpringRunner.class)
19+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
20+
public class ArchaiusAdditionalSourcesLiveTest {
21+
22+
private static final String BASE_URL = "http://localhost:8082";
23+
24+
private static final String DYNAMIC_PROPERTIES_URL = "/properties-from-dynamic";
25+
private static final Map<String, String> EXPECTED_ARCHAIUS_PROPERTIES = createExpectedArchaiusProperties();
26+
27+
private static Map<String, String> createExpectedArchaiusProperties() {
28+
Map<String, String> map = new HashMap<>();
29+
map.put("baeldung.archaius.properties.one", "one FROM:other-config.properties");
30+
map.put("baeldung.archaius.properties.two", "two FROM:application.properties");
31+
map.put("baeldung.archaius.properties.three", "three FROM:config.properties");
32+
map.put("baeldung.archaius.properties.four", "four FROM:other-config.properties");
33+
return map;
34+
}
35+
36+
@Autowired
37+
ConfigurableApplicationContext context;
38+
39+
@Autowired
40+
private TestRestTemplate template;
41+
42+
private <T> Map<T, T> exchangeAsMap(String uri, ParameterizedTypeReference<Map<T, T>> responseType) {
43+
return template.exchange(uri, HttpMethod.GET, null, responseType)
44+
.getBody();
45+
}
46+
47+
@Test
48+
public void givenNonDefaultConfigurationFilesSetup_whenRequestProperties_thenEndpointRetrievesValuesInFiles() {
49+
Map<String, String> initialResponse = this.exchangeAsMap(BASE_URL + DYNAMIC_PROPERTIES_URL, new ParameterizedTypeReference<Map<String, String>>() {
50+
});
51+
52+
assertThat(initialResponse).containsAllEntriesOf(EXPECTED_ARCHAIUS_PROPERTIES);
53+
}
54+
}

0 commit comments

Comments
 (0)