Spring Cloud Config provides server and client-side support for externalised configuration in a distributed system. With the Config Server we have a central place to manage external properties for applications across all environments. The concepts on both client and server map identically to the Spring Environment and PropertySource abstractions, so they fit very well with Spring applications, but can be used with any application running in any language. As an application moves through the deployment pipeline from dev to test and into production we can manage the configuration between those environments and be certain that applications have everything they need to run when they migrate.
In the example below we will see how we can externalise and manage configuration from github repository and file system.
Spring Cloud Config Server
The Server provides an HTTP, resource-based API for external configuration. We can create/start a config server with @EnableConfigServer annotation at the Spring Boot application startup class.
Code:
@EnableConfigServer
@SpringBootApplication
public class ConfigServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServiceApplication.class, args);
}
}
application.properties file:
server.port=8888
# if you want to manage config files from local file system uncomment the property bellow
#spring.cloud.config.server.git.uri=file://<your_local_system_directory>
# if you want to manage config files from github repository then uncomment the property below
# alongwith user name and password
#spring.cloud.config.server.git.uri=<github_url>
spring.cloud.config.server.git.searchPaths=<some-name>
#spring.cloud.config.server.git.username=
#spring.cloud.config.server.git.password=
finally in maven dependencies include
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>
Lets assume that spring.cloud.config.server.git.searchPaths attribute value is sample-config and we are supporting default, dev and production environment, so your github repo/local file system will have three different files like
sample-config.properties
sample-config-dev.properties
sample-config-production.properties
Now start the application and access
http://localhost:8888/sample-config/defaut
output: {"name":"sample-config","profiles":["defaut"],"label":"master","version":"f57f675e23c02a9e2f8422b01f07302d41d9774f","propertySources":[{"name":"<github-default-url>","source":{"message":"some message for default"}}]}
http://localhost:8888/sample-config/dev
{"name":"sample-config","profiles":["dev"],"label":"master","version":"f57f675e23c02a9e2f8422b01f07302d41d9774f","propertySources":[{"name":"<github-default-url>","source":{"message":"some message for dev"}}, ........... ]}
http://localhost:8888/sample-config/production
{"name":"sample-config","profiles":["production"],"label":"master","version":"f57f675e23c02a9e2f8422b01f07302d41d9774f","propertySources":[{"name":"<github-default-url>","source":{"message":"some message for production"}}, ........ ]}
we are assuming here that we have only one property with key message in the property files.
So our spring cloud config server is now ready to support environment specific configuration properties for the client.
we can also health check with http://localhost:8888/sample-config/health
Spring Cloud Config Client
A Spring Boot application can take immediate advantage of the Spring Config Server. Lets see how -
@SpringBootApplication
@RefreshScope
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
@RestController
class MessageRestController {
@Value("${message:Hello default}")
private String message;
@RequestMapping("/message")
String getMessage() {
return this.message;
}
}
So we have a REST endpoint /message which returns the value of the key message from property files.
application.properties file
server.port=8080
spring.application.name=sample-config # this is the name we used in config server
#URL for config server
spring.cloud.config.uri=http://localhost:8888
# Active profile
spring.profiles.active=dev
finally add the dependency below in pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>
Lets start the server and access http://localhost:8080/message [the output will be some message for dev]
If we change the spring.profiles.active value the output will be changed accordingly.
@RefreshScope
If we change the value of any of the key (value of the property files attribute key for any environment) we don't even need to restart the Config Server and Config Client, Just send HTTP POST request to http://localhost:8080/refresh the updated values will be reflected automatically.
So we can use Spring Cloud config server to -
1. Externalise the configuration properties (property files for applications) either to file system or github repository.
2. We can get the support of different profiles like dev, production seamlessly.
3. If at runtime we need to change any configuration attribute value we don't even need to restart the server.
Source code is available at [https://github.com/badalb/spring-cloud-microservices-security]
Check out how Spring Microservices Config Server helps you to have remove configuration for your applications. Watch the complete video to know the basics of config server.
ReplyDelete