DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
What's in store for DevOps in 2023? Hear from the experts in our "DZone 2023 Preview: DevOps Edition" on Fri, Jan 27!
Save your seat
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Grails Goodness: Using Spring Cloud Config Server

Grails Goodness: Using Spring Cloud Config Server

Learn more about the Spring Cloud Config server, where you can centrally manage external properties for applications with support for different environments.

Hubert Klein Ikkink user avatar by
Hubert Klein Ikkink
·
Jan. 18, 16 · Tutorial
Like (2)
Save
Tweet
Share
4.98K Views

Join the DZone community and get the full member experience.

Join For Free

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'll start our own server for testing. We'll use a local Git repository as backend for the configuration, and we'll 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
HTTP/1.1 200 OK
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
HTTP/1.1 200 OK
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

Spring Framework Spring Cloud Grail (web browser)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 5 Data Mesh Best Practices From 4 Data Leaders
  • Required Knowledge To Pass AWS Certified Data Analytics Specialty Exam
  • Insight Into Developing Quarkus-Based Microservices
  • Core Machine Learning Metrics

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: