-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs: add Custom Environment Repositories and update config sharing #2389
Merged
ryanjbaxter
merged 2 commits into
spring-cloud:main
from
apappascs:doc/add-custom-repo-update-config-sharing
Mar 7, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this bean only need to be activated when the custom profile is active?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes @ryanjbaxter you are correct.
added
@Profile("custom")