The configuration files (e.g. application.yaml
, application.properties
, bootstrap.yaml
) will be maintained in the resources
directory of your service application.
my-app
|_ src
|_ main
|_ java
|_ resources
|_ application.yaml
|_ application-local.yaml
|_ application-dev.yaml
|_ application-uat.yaml
|_ application-prod.yaml
|_ test
|_ pom.xml
application.yaml:
spring:
application:
name: my-app
server:
port: 8080
application-dev.yaml:
server:
port: 8081
application-uat.yaml:
server:
port: 8082
application-prod.yaml:
server:
port: 8083
Running the application with profiles:
$ mvn spring-boot:run -Dpring.profiles.active=dev
Using a config service, we will place our configuration properties in environment specific directories (e.g. dev, uat, prod, local). Notice that the file name is now renamed to the actual application name (i.e. dev/my-app.yaml
instead of previously resource/application-dev.yaml
):
config-service
|_ src
|_ main
|_ java
|_ demo
|_ ConfigServerApplication.yaml
|_ resources
|_ application.yaml
|_ dev
|_ my-app.yaml
|_ uat
|_ my-app.yaml
|_ prod
|_ my-app.yaml
|_ local
|_ my-app.yaml
|_ test
|_ pom.xml
application.yaml
spring:
application:
name: configserver
server:
port: 8888
Add the @EnableConfigServer
annotation in your main application class:
package demo;
import org.springframework.cloud.config.server.EnableConfigServer;
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String args[]) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
Run the config service app:
$ mvn spring-boot:run
You can then retrieve the configuration by app and profile using the config service endpoint:
http://localhost:8888/{app}/{profile}
Example:
http://localhost:8888/my-app/local
http://localhost:8888/my-app/dev
http://localhost:8888/my-app/uat
http://localhost:8888/my-app/prod
Then in our my-app
application, we simply specify the spring cloud config uri in resources/application.yaml
:
my-app
|_ src
|_ main
|_ java
|_ resources
|_ application.yaml
|_ test
|_ pom.xml
spring:
cloud:
config:
uri: ${CONFIG_SERVER_URI:${vcap.services.${PREFIX:}configserver.credentials.uri:http://user:password@localhost:8888}}
Running the application is the same, we simply specify the active profiles to use:
$ mvn spring-boot:run -Dpring.profiles.active=dev
Pros of using a config service:
https://spring.io/projects/spring-cloud-config#overview