Skip to content

Commit 38027c5

Browse files
rozagerardomaibin
authored andcommitted
[BAEL-2115] spring-cloud | Configuring Netflix Archaius (eugenp#5289)
* added examples for archaius: * dynamodb * jdbc configurations * added archaius zookeeper config * small fixes, removed dependency that was already provided by another library, and changed property value to match project name
1 parent 7b1f5c5 commit 38027c5

24 files changed

Lines changed: 685 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<artifactId>dynamodb-config</artifactId>
6+
<packaging>jar</packaging>
7+
8+
<parent>
9+
<groupId>com.baeldung.spring.cloud</groupId>
10+
<artifactId>spring-cloud-archaius</artifactId>
11+
<version>1.0.0-SNAPSHOT</version>
12+
<relativePath>..</relativePath>
13+
</parent>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.springframework.boot</groupId>
18+
<artifactId>spring-boot-starter-web</artifactId>
19+
</dependency>
20+
<dependency>
21+
<groupId>com.amazonaws</groupId>
22+
<artifactId>aws-java-sdk-dynamodb</artifactId>
23+
<version>${aws.sdk.dynamo.version}</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>com.github.derjust</groupId>
27+
<artifactId>spring-data-dynamodb</artifactId>
28+
<version>${spring.dynamo.version}</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>com.netflix.archaius</groupId>
32+
<artifactId>archaius-aws</artifactId>
33+
<version>${archaius.version}</version>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.projectlombok</groupId>
37+
<artifactId>lombok</artifactId>
38+
<scope>provided</scope>
39+
</dependency>
40+
</dependencies>
41+
42+
<properties>
43+
<aws.sdk.dynamo.version>1.11.407</aws.sdk.dynamo.version>
44+
<spring.dynamo.version>5.0.3</spring.dynamo.version>
45+
<archaius.version>0.7.6</archaius.version>
46+
</properties>
47+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.spring.cloud.archaius.dynamosources;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class DynamoSourcesApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(DynamoSourcesApplication.class, args);
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.spring.cloud.archaius.dynamosources.config;
2+
3+
import java.util.Arrays;
4+
5+
import org.apache.commons.configuration.AbstractConfiguration;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.Configuration;
9+
10+
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
11+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
12+
import com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
13+
import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
14+
import com.amazonaws.services.dynamodbv2.util.TableUtils;
15+
import com.baeldung.spring.cloud.archaius.dynamosources.dynamodb.ArchaiusProperties;
16+
import com.baeldung.spring.cloud.archaius.dynamosources.dynamodb.ArchaiusPropertiesRepository;
17+
import com.netflix.config.DynamicConfiguration;
18+
import com.netflix.config.FixedDelayPollingScheduler;
19+
import com.netflix.config.PolledConfigurationSource;
20+
import com.netflix.config.sources.DynamoDbConfigurationSource;
21+
22+
@Configuration
23+
public class ApplicationPropertiesConfigurations {
24+
25+
@Autowired
26+
AmazonDynamoDB amazonDynamoDb;
27+
28+
@Autowired
29+
private ArchaiusPropertiesRepository repository;
30+
31+
@Bean
32+
public AbstractConfiguration addApplicationPropertiesSource() {
33+
// Normally, the DB Table would be already created and populated.
34+
// In this case, we'll do it just before creating the archaius config source that uses it
35+
initDatabase();
36+
PolledConfigurationSource source = new DynamoDbConfigurationSource(amazonDynamoDb);
37+
return new DynamicConfiguration(source, new FixedDelayPollingScheduler());
38+
}
39+
40+
private void initDatabase() {
41+
// Create the table
42+
DynamoDBMapper mapper = new DynamoDBMapper(amazonDynamoDb);
43+
CreateTableRequest tableRequest = mapper.generateCreateTableRequest(ArchaiusProperties.class);
44+
tableRequest.setProvisionedThroughput(new ProvisionedThroughput(1L, 1L));
45+
TableUtils.createTableIfNotExists(amazonDynamoDb, tableRequest);
46+
47+
// Populate the table
48+
ArchaiusProperties property = new ArchaiusProperties("baeldung.archaius.properties.one", "one FROM:dynamoDB");
49+
ArchaiusProperties property3 = new ArchaiusProperties("baeldung.archaius.properties.three", "three FROM:dynamoDB");
50+
repository.saveAll(Arrays.asList(property, property3));
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.spring.cloud.archaius.dynamosources.config;
2+
3+
import org.socialsignin.spring.data.dynamodb.repository.config.EnableDynamoDBRepositories;
4+
import org.springframework.beans.factory.annotation.Value;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
8+
import com.amazonaws.auth.AWSCredentialsProvider;
9+
import com.amazonaws.auth.AWSStaticCredentialsProvider;
10+
import com.amazonaws.auth.BasicAWSCredentials;
11+
import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration;
12+
import com.amazonaws.regions.Regions;
13+
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
14+
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
15+
16+
@Configuration
17+
@EnableDynamoDBRepositories(basePackages = "com.baeldung.spring.cloud.archaius.dynamosources.dynamodb")
18+
public class DynamoDbConfiguration {
19+
20+
@Value("${amazon.dynamodb.endpoint}")
21+
private String amazonDynamoDBEndpoint;
22+
23+
@Value("${aws.accessKeyId}")
24+
private String amazonDynamoDBAccessKeyId;
25+
26+
@Value("${aws.secretKey}")
27+
private String amazonDynamoDBSecretKey;
28+
29+
@Bean
30+
public AmazonDynamoDB amazonDynamoDB() {
31+
AmazonDynamoDB amazonDynamoDB = AmazonDynamoDBClientBuilder.standard()
32+
.withCredentials(amazonAWSCredentials())
33+
.withEndpointConfiguration(setupEndpointConfiguration())
34+
.build();
35+
36+
return amazonDynamoDB;
37+
}
38+
39+
private AWSCredentialsProvider amazonAWSCredentials() {
40+
return new AWSStaticCredentialsProvider(new BasicAWSCredentials(amazonDynamoDBAccessKeyId, amazonDynamoDBSecretKey));
41+
}
42+
43+
private EndpointConfiguration setupEndpointConfiguration() {
44+
return new EndpointConfiguration(amazonDynamoDBEndpoint, Regions.DEFAULT_REGION.getName());
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.spring.cloud.archaius.dynamosources.controller;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
import com.netflix.config.DynamicPropertyFactory;
10+
import com.netflix.config.DynamicStringProperty;
11+
12+
@RestController
13+
public class ConfigPropertiesController {
14+
15+
private DynamicStringProperty propertyOneWithDynamic = DynamicPropertyFactory.getInstance()
16+
.getStringProperty("baeldung.archaius.properties.one", "not found!");
17+
18+
private DynamicStringProperty propertyTwoWithDynamic = DynamicPropertyFactory.getInstance()
19+
.getStringProperty("baeldung.archaius.properties.two", "not found!");
20+
21+
private DynamicStringProperty propertyThreeWithDynamic = DynamicPropertyFactory.getInstance()
22+
.getStringProperty("baeldung.archaius.properties.three", "not found!");
23+
24+
@GetMapping("/properties-from-dynamic")
25+
public Map<String, String> getPropertiesFromDynamic() {
26+
Map<String, String> properties = new HashMap<>();
27+
properties.put(propertyOneWithDynamic.getName(), propertyOneWithDynamic.get());
28+
properties.put(propertyTwoWithDynamic.getName(), propertyTwoWithDynamic.get());
29+
properties.put(propertyThreeWithDynamic.getName(), propertyThreeWithDynamic.get());
30+
return properties;
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.spring.cloud.archaius.dynamosources.dynamodb;
2+
3+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
4+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
5+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
6+
7+
import lombok.AllArgsConstructor;
8+
import lombok.Getter;
9+
import lombok.Setter;
10+
11+
@Getter
12+
@Setter
13+
@AllArgsConstructor
14+
@DynamoDBTable(tableName = "archaiusProperties")
15+
public class ArchaiusProperties {
16+
17+
@DynamoDBHashKey
18+
@DynamoDBAttribute
19+
private String key;
20+
21+
@DynamoDBAttribute
22+
private String value;
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.spring.cloud.archaius.dynamosources.dynamodb;
2+
3+
import org.springframework.data.repository.CrudRepository;
4+
5+
public interface ArchaiusPropertiesRepository extends CrudRepository<ArchaiusProperties, String> {
6+
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
server.port=8082
2+
baeldung.archaius.properties.one=one FROM:application.properties
3+
baeldung.archaius.properties.two=two FROM:application.properties
4+
amazon.dynamodb.endpoint=http://localhost:8000/
5+
aws.accessKeyId=key
6+
aws.secretKey=key2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.baeldung.spring.cloud.archaius.dynamosources;
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 ArchaiusDynamoDbLiveTest {
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:dynamoDB");
30+
map.put("baeldung.archaius.properties.two", "two FROM:application.properties");
31+
map.put("baeldung.archaius.properties.three", "three FROM:dynamoDB");
32+
return map;
33+
}
34+
35+
@Autowired
36+
ConfigurableApplicationContext context;
37+
38+
@Autowired
39+
private TestRestTemplate template;
40+
41+
private <T> Map<T, T> exchangeAsMap(String uri, ParameterizedTypeReference<Map<T, T>> responseType) {
42+
return template.exchange(uri, HttpMethod.GET, null, responseType)
43+
.getBody();
44+
}
45+
46+
@Test
47+
public void givenNonDefaultConfigurationFilesSetup_whenRequestProperties_thenEndpointRetrievesValuesInFiles() {
48+
Map<String, String> initialResponse = this.exchangeAsMap(BASE_URL + DYNAMIC_PROPERTIES_URL, new ParameterizedTypeReference<Map<String, String>>() {
49+
});
50+
51+
assertThat(initialResponse).containsAllEntriesOf(EXPECTED_ARCHAIUS_PROPERTIES);
52+
}
53+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<artifactId>jdbc-config</artifactId>
6+
<packaging>jar</packaging>
7+
8+
<parent>
9+
<groupId>com.baeldung.spring.cloud</groupId>
10+
<artifactId>spring-cloud-archaius</artifactId>
11+
<version>1.0.0-SNAPSHOT</version>
12+
<relativePath>..</relativePath>
13+
</parent>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.springframework.boot</groupId>
18+
<artifactId>spring-boot-starter-web</artifactId>
19+
</dependency>
20+
<dependency>
21+
<groupId>org.springframework.boot</groupId>
22+
<artifactId>spring-boot-starter-data-jpa</artifactId>
23+
</dependency>
24+
<dependency>
25+
<groupId>com.h2database</groupId>
26+
<artifactId>h2</artifactId>
27+
<scope>runtime</scope>
28+
</dependency>
29+
</dependencies>
30+
</project>

0 commit comments

Comments
 (0)