カスタム環境リポジトリ
Spring Cloud Config は、カスタム EnvironmentRepository 実装の作成と統合を可能にすることで、構成管理の強化をサポートしています。これにより、アプリケーションに固有の構成ソースを追加できます。また、Ordered インターフェースを実装し、getOrder メソッドを指定すると、複合構成セットアップ内でカスタムリポジトリの優先順位を設定することもできます。これを行わないと、カスタムリポジトリはデフォルトで最も低い優先順位と見なされます。
以下は、カスタム EnvironmentRepository
を作成して構成する方法の例です。
public class CustomConfigurationRepository implements EnvironmentRepository, Ordered {
@Override
public Environment findOne(String application, String profile, String label) {
// Simulate fetching configuration from a custom source
final Map<String, String> properties = Map.of(
"key1", "value1",
"key2", "value2",
"key3", "value3"
);
Environment environment = new Environment(application, profile);
environment.add(new PropertySource("customPropertySource", properties));
return environment;
}
@Override
public int getOrder() {
return 0;
}
}
@Configuration
@Profile("custom")
public class AppConfig {
@Bean
public CustomConfigurationRepository customConfigurationRepository() {
return new CustomConfigurationRepository();
}
}
この設定では、Spring アプリケーションの構成内で custom
プロファイルを有効にすると、カスタム環境リポジトリが構成サーバーに統合されます。たとえば、application.properties
または application.yml
で custom
プロファイルを次のように指定します。
spring:
application:
name: configserver
profiles:
active: custom
次に、次の構成サーバーにアクセスします。
http://localhost:8080/any-client/dev/latest
以下に示すように、カスタムリポジトリからデフォルト値が返されます。
{
"name": "any-client",
"profiles": ["dev"],
"label": "latest",
"propertySources": [
{
"name": "customPropertySource",
"source": {
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
}
]
}