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 Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • Component Tests for Spring Cloud Microservices
  • A Robust Distributed Payment Network With Enchanted Audit Functionality - Part 2: Spring Boot, Axon, and Implementation
  • 7 Microservices Best Practices for Developers

Trending

  • Reducing Hallucinations Using Prompt Engineering and RAG
  • Continuous Quality Engineering: The Convergence of CI/CD, Chaos Testing, and AI-Powered Test Orchestration
  • Run Scalable Python Workloads With Modal
  • Why API-First Frontends Make Better Apps
  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.

By 
Hubert Klein Ikkink user avatar
Hubert Klein Ikkink
·
Jan. 18, 16 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
5.8K 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.

Related

  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • Component Tests for Spring Cloud Microservices
  • A Robust Distributed Payment Network With Enchanted Audit Functionality - Part 2: Spring Boot, Axon, and Implementation
  • 7 Microservices Best Practices for Developers

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: