Grails Goodness: Using Spring Cloud Config Server
The Spring Cloud project has several sub projects. One of them is the Spring Cloud Config Server. With the Config Server we have a central place to manage external properties for applications with support for different environments. Configuration files in several formats like YAML or properties are added to a Git repository. The server provides an REST API to get configuration values. But there is also a good integration for client applications written with Spring Boot. And because Grails (3) depends on Spring Boot we can leverage the same integration support. Because of the Spring Boot auto configuration we only have to add a dependency to our build file and add some configuration.
Before we look at how to use a Spring Cloud Config server in our Grails application we start our own server for testing. We use a local Git repository as backend for the configuration. And we use the Spring Boot CLI to start the server. We have the following Groovy source file to enable the configuration server:
// File: server.groovy
@DependencyManagementBom('org.springframework.cloud:spring-cloud-starter-parent:Brixton.M4')
@Grab('spring-cloud-config-server')
import org.springframework.cloud.config.server.EnableConfigServer
@EnableConfigServer
class ConfigServer {}
Next we create a new local Git repository with $ git init /Users/mrhaki/config-repo
. We use the Spring Boot CLI and our Groovy script to start a sample Spring Cloud Config Server:
$ spring run server.groovy -- --server.port=9000 --spring.cloud.config.server.git.uri=file:///Users/mrhaki/config-repo --spring.config.name=configserver
...
2016-01-13 14:35:19.679 INFO 69933 --- [ runner-0] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 9000 (http)
2016-01-13 14:35:19.682 INFO 69933 --- [ runner-0] o.s.boot.SpringApplication : Started application in 4.393 seconds (JVM running for 7.722)
Next we create a YAML configuration file with a configuration property app.message
. The name of the configuration file must start with the application name that want to use the configuration. It is best to not use hyphens in the name. Optionally we can use a Spring profile name to override configuration properties for a specific profile. The profile maps to the Grails environment names so we can use the pattern also for our Grails configuration. To learn about even more possibilities we must read the Spring Cloud Config documentation.
Here are two configuration files with a default value and a override property for the development environment:
# grails_cloud_config.yml
app:
message: Default message
# grails_cloud_config-development.yml
app:
message: Running in development mode
We add and commit both files in our local Git repository.
Let's configure our Grails application so it uses the configuration from our Spring Cloud Config Server. First we change build.gradle
and add a dependency on spring-cloud-starter-config
. We also add an extra BOM for the Spring Cloud dependencies so the correct version is automatically used.
// File: build.gradle
...
dependencyManagement {
imports {
mavenBom "org.grails:grails-bom:$grailsVersion"
mavenBom 'org.springframework.cloud:spring-cloud-starter-parent:Angel.SR4'
}
applyMavenExclusions false
}
...
dependencies {
// Adding this dependency is enough to use
// Spring Cloud Server.
compile "org.springframework.cloud:spring-cloud-starter-config"
}
Next we need to define the URL for our configuration server. We can set the system property spring.cloud.config.uri
when we start our Grails application or we can add the file grails-app/conf/bootstrap.yml
with the following contents:
# File: grails-app/conf/bootstrap.yml
spring:
cloud:
config:
uri: http://localhost:9000
Finally we set our application name to grails_cloud_config
which is used to get the configuration properties from the Config server:
# File: grails-app/conf/application.yml
...
spring:
application:
name: grails_cloud_config
...
That is it, we can now use properties defined in the configuration server in our Grails application. Let's add a controller which reads the configuration property app.message
:
// File: grails-app/controllers/sample/MessageController.groovy
package sample
import org.springframework.beans.factory.annotation.Value
class MessageController {
@Value('${app.message}')
private String message
def index() {
render message
}
}
When we start our application with the development environment we get the following message:
$ http localhost:8080/message
Content-Type: text/html;charset=utf-8
Date: Wed, 13 Jan 2016 14:08:37 GMT
Server: Apache-Coyote/1.1
Transfer-Encoding: chunked
X-Application-Context: grails_cloud_config:development
Running in development mode
$
And when in production mode we get:
$ http localhost:8080/message
Content-Type: text/html;charset=utf-8
Date: Wed, 13 Jan 2016 14:08:37 GMT
Server: Apache-Coyote/1.1
Transfer-Encoding: chunked
X-Application-Context: grails_cloud_config:production
Default message
$
Written with Grails 3.0.11