[BAEL-2115] spring-cloud | Configuring Netflix Archaius (#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
This commit is contained in:
rozagerardo
2018-09-22 18:10:33 -03:00
committed by maibin
parent 7b1f5c5724
commit 38027c56b4
24 changed files with 685 additions and 0 deletions
@@ -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);
}
}
@@ -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));
}
}
@@ -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());
}
}
@@ -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;
}
}
@@ -0,0 +1,23 @@
package com.baeldung.spring.cloud.archaius.dynamosources.dynamodb;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
@DynamoDBTable(tableName = "archaiusProperties")
public class ArchaiusProperties {
@DynamoDBHashKey
@DynamoDBAttribute
private String key;
@DynamoDBAttribute
private String value;
}
@@ -0,0 +1,7 @@
package com.baeldung.spring.cloud.archaius.dynamosources.dynamodb;
import org.springframework.data.repository.CrudRepository;
public interface ArchaiusPropertiesRepository extends CrudRepository<ArchaiusProperties, String> {
}
@@ -0,0 +1,6 @@
server.port=8082
baeldung.archaius.properties.one=one FROM:application.properties
baeldung.archaius.properties.two=two FROM:application.properties
amazon.dynamodb.endpoint=http://localhost:8000/
aws.accessKeyId=key
aws.secretKey=key2
@@ -0,0 +1,53 @@
package com.baeldung.spring.cloud.archaius.dynamosources;
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 ArchaiusDynamoDbLiveTest {
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:dynamoDB");
map.put("baeldung.archaius.properties.two", "two FROM:application.properties");
map.put("baeldung.archaius.properties.three", "three FROM:dynamoDB");
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);
}
}