-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add Custom Environment Repositories and update config sharing (#…
…2389) * docs: add Custom Environment Repositories and update config sharing Introduce a new section for "Custom Environment Repositories" to guide users in creating and integrating custom `EnvironmentRepository` implementations. Additionally, update the "Sharing Configuration With All Applications" section. * add @Profile("custom")
- Loading branch information
Showing
3 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...ules/ROOT/pages/server/environment-repository/custom-enviroment-repository.adoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
[[custom-environment-repositories]] | ||
= Custom Environment Repositories | ||
|
||
Spring Cloud Config supports enhancing its configuration management by allowing you to create and integrate custom EnvironmentRepository implementations. This enables the addition of unique configuration sources to your application. Implementing the Ordered interface and specifying the getOrder method also lets you set the priority of your custom repository within a composite configuration setup. Without this, custom repositories are considered with the lowest priority by default. | ||
|
||
Below is an example of how to create and configure a custom `EnvironmentRepository`: | ||
|
||
[source,java] | ||
---- | ||
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(); | ||
} | ||
} | ||
---- | ||
|
||
With this setup, if you activate the `custom` profile within your Spring application's configuration, your custom environment repository will be integrated into the configuration server. For instance, specifying the `custom` profile in your `application.properties` or `application.yml` as follows: | ||
|
||
[source,yaml] | ||
---- | ||
spring: | ||
application: | ||
name: configserver | ||
profiles: | ||
active: custom | ||
---- | ||
|
||
Now, accessing the configuration server at: | ||
---- | ||
http://localhost:8080/any-client/dev/latest | ||
---- | ||
will return default values from the custom repository, as shown below: | ||
[source,json] | ||
---- | ||
{ | ||
"name": "any-client", | ||
"profiles": ["dev"], | ||
"label": "latest", | ||
"propertySources": [ | ||
{ | ||
"name": "customPropertySource", | ||
"source": { | ||
"key1": "value1", | ||
"key2": "value2", | ||
"key3": "value3" | ||
} | ||
} | ||
] | ||
} | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters