Accessing Properties in Spring Cloud Config Server
Accessing Properties in Spring Cloud Config Server
Spring Cloud Config Server selects its properties based on the incoming REST endpoint path, in this format:
http://config-server-host:port/application-name/profile/label
Application name
OK, so the first part of the REST endpoint path specifies the application name you’re requesting the configuration for.
This name should match the spring.application.name property in the client’s configuration.
For example, if you have a client application with spring.application.name=client-app, the REST endpoint for fetching its configuration would be:
http://config-server-host:port/client-app/
Profile
The second part of the REST endpoint path specifies the active profile(s) for which the configuration is requested. Profiles allow you to maintain separate configurations for different environments, such as dev, test, and prod.
If your client application is running with the dev profile, the REST endpoint for fetching its configuration would be:
http://config-server-host:port/client-app/dev
Label
The last (optional) part of the REST endpoint path specifies the label, which represents a version or a branch in the configuration source. By default, the label is main for Git-based repositories.
If you want to fetch the configuration from a specific branch, say feature-branch, the REST endpoint would be:
http://config-server-host:port/client-app/dev/feature-branch
When you make a request to the config server with the specified REST endpoint path, the server looks for the configuration files that match the given application name and profile.
If you provide a label, the server fetches the configuration from that specific version or branch.
Properties are Merged across All Sources
The server then merges the properties from all matching files and serves them as a single response.
Example
Assume the following configuration files are available in the Git repository:
| Filename | Purpose |
|---|---|
application.yml | global properties |
application-dev.yml | global properties for the dev profile |
client-app.yml | application properties |
client-app-dev.yml | application properties for the dev profile |
You then make a request to the REST endpoint:
The config server will merge the properties from application.yml, application-dev.yml, client-app.yml, and client-app-dev.yml.
It will give precedence to properties in more specific files. For example, properties in client-app-dev.yml will override those in application-dev.yml.