Externalizing configurations (keeping env configs) out of source so that it follows build once deploy anywhere approach.
Motivation:
- How do we manage properties: application.properties, application.yml, system.properties,
- environment variables export SERVER_PORT=8080
- passing via command line: -Dserver.port=8080
- variables specific to env and why you should not have in local source. - server.port
What is Cloud Config server:
Its a way by which we can externalize the property values by moving out of the project. Primarily kept for properties that you normally override or keep secret values out of the project src.
Motivation:
- Externalize config data from the applications across all environments.
- Store crucial information such as password for DB in a centralized place.
- Share data between different services.
- See who changed which property and when and why.
What should you do to use Spring cloud config
- Application configuration:
- Manage app configuration in a git hub or a local file system.
- Config server
- Point the location of GIT or local file system where the app config is located (i.e., GIT URI)
- @EnableConfigServer
- Config client
- Use it normally as how you would read from the properties file
Features
- Many storage options including Git, Subversion.
- Pull model
- Traceability
- @RefreshScope for allowing properties to refresh. Calls the constructor of the beans and reinitializes it.
- @ConfigurationProperties alternate for @RefreshScope which does reinitialize the Bean that is annotated with @ConfigurationProperties.
- Encrypt and decrypt property values.
Usecases for Cloud config:
- Change log level in the properties.
- Environment config - containing passwords.
- Toggle a feature on or off.
- Using @RefreshScope
- @ConfigurationProperties.
Order in which the Cloud config looks into the properties files.
- Config server.
- Command line parameters
- System properties.
- Classpath: application.yml file
- Classpath: bootstrap.yml
Things that you can try:
- Simple example to demonstrate properties in Git and reading from config server and client
- Using maven profiles specific properties in Config server in Github.
- Managing log levels in the Config server
- Using @RefreshScope and @CofigurationProperties.
Best practices:
- Config server communication with the Git hub, pull every time when client requests for properties file. Use a Jenkins job which will push the properties to a Config server properties (clone).
- Single repository for all teams, subdirectories for different environments.
- Source code maintenance, having branch per release vs always using the master.
- Managing encrypted values for secure data, using JWT with Config server.
- Use Spring cloud bus to refresh all the services which listens to the rabbitmq.
Note:
- Store only things that need to be externalized, I.e., Resource bundles for internationalization should be at the project level.
Questions:
- How do I refresh all the instances of a service when a configuration is changed.
- Manage different repositories for dev and prod.
- What should I do so that I don’t have to say refresh in every services.
- What is encrypt and decrypt endpoints in Config server.
- Real time example of how the config properties look like, how do they maintain.
- What is the difference between Spring cloud an dSpring cloud services in maven.