[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:
parent
7b1f5c5724
commit
38027c56b4
|
@ -0,0 +1,47 @@
|
||||||
|
<?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>
|
||||||
|
<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>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>
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
<?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>jdbc-config</artifactId>
|
||||||
|
<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.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.h2database</groupId>
|
||||||
|
<artifactId>h2</artifactId>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
|
@ -0,0 +1,13 @@
|
||||||
|
package com.baeldung.spring.cloud.archaius.jdbconfig;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class JdbcSourcesApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(JdbcSourcesApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
package com.baeldung.spring.cloud.archaius.jdbconfig.config;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
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.netflix.config.DynamicConfiguration;
|
||||||
|
import com.netflix.config.FixedDelayPollingScheduler;
|
||||||
|
import com.netflix.config.PolledConfigurationSource;
|
||||||
|
import com.netflix.config.sources.JDBCConfigurationSource;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class ApplicationPropertiesConfigurations {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
DataSource h2DataSource;
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public AbstractConfiguration addApplicationPropertiesSource() {
|
||||||
|
PolledConfigurationSource source = new JDBCConfigurationSource(h2DataSource, "select distinct key, value from properties", "key", "value");
|
||||||
|
return new DynamicConfiguration(source, new FixedDelayPollingScheduler());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,32 @@
|
||||||
|
package com.baeldung.spring.cloud.archaius.jdbconfig.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,14 @@
|
||||||
|
package com.baeldung.spring.cloud.archaius.jdbconfig.jdbc;
|
||||||
|
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Properties {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
private String key;
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
private String value;
|
||||||
|
}
|
|
@ -0,0 +1,4 @@
|
||||||
|
server.port=8082
|
||||||
|
baeldung.archaius.properties.one=one FROM:application.properties
|
||||||
|
baeldung.archaius.properties.two=two FROM:application.properties
|
||||||
|
spring.h2.console.enabled=true
|
|
@ -0,0 +1,5 @@
|
||||||
|
insert into properties
|
||||||
|
values('baeldung.archaius.properties.one', 'one FROM:jdbc_source');
|
||||||
|
|
||||||
|
insert into properties
|
||||||
|
values('baeldung.archaius.properties.three', 'three FROM:jdbc_source');
|
|
@ -0,0 +1,53 @@
|
||||||
|
package com.baeldung.spring.cloud.archaius.jdbconfig;
|
||||||
|
|
||||||
|
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 ArchaiusJDBCSourceLiveTest {
|
||||||
|
|
||||||
|
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:jdbc_source");
|
||||||
|
map.put("baeldung.archaius.properties.two", "two FROM:application.properties");
|
||||||
|
map.put("baeldung.archaius.properties.three", "three FROM:jdbc_source");
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -21,6 +21,9 @@
|
||||||
<module>basic-config</module>
|
<module>basic-config</module>
|
||||||
<module>additional-sources-simple</module>
|
<module>additional-sources-simple</module>
|
||||||
<module>extra-configs</module>
|
<module>extra-configs</module>
|
||||||
|
<module>jdbc-config</module>
|
||||||
|
<module>dynamodb-config</module>
|
||||||
|
<module>zookeeper-config</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|
|
@ -0,0 +1,48 @@
|
||||||
|
<?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>
|
||||||
|
<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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue