gs-configuration-server is no longer actively maintained by VMware, Inc.
tags: [] --- :toc: :icons: font :source-highlighter: prettify :project_id: gs-configuration-server This guide walks you through the process of creating a "hello world" Configuration Server and client service.
What you’ll build
You will enable support for Spring Cloud Config Server and build a client app that will read configuration properties from the server.
What you’ll need
Create a Config Server service
The Pivotal Cloud Foundry Marketplace offers the Config Server for Pivotal CF as a service for providing externalized configuration for your various applications and services running on PCF.
-
Select the Config Server for Pivotal CF from the Marketplace:
-
Select the desired plan for the new service:
-
Provide a name for the new Config Server service (e.g. config-server) and click the Add button:
-
The new service is created and will be available for use after a few moments (a new Config Server is being deployed and launched just for this service instance, so this takes some time):
-
Select the Manage link on the new service instance and enter the following URI for the configuration Git repository:
https://github.com/pcf-guides/configuration-server-config-repo.git
A Git repo is the recommended configuration source. This demonstration Git repository contains an
application.properties
file with the following contents:name=Spring Cloud Config Server
Enter the Git repository URI and click Submit:
-
The green confirmation box indicates the service is configured:
You have completed the setup for a Config Server service. This service can now be used to provide centralized configuration to a client application running on Cloud Foundry.
Create a client app
In this section you will create a simple Spring Boot REST service which accepts external configuration from the Config Server service.
Run the Spring Boot REST service
Before proceeding, take a moment to familiarize yourself with code in the initial
folder. This code represents a working starting point prior to the addition of any specific Spring Cloud features. The initial
folder located in this repository contains a simple Spring Boot REST service that returns "Hello World" when the root resource is requested. Execute the following commands to run the service.
cd initial mvn spring-boot:run
While the Spring Boot app is running, view the output of the REST request by executing the following command in a different terminal window. If all goes well, this command will respond with "Hello World".
curl http://localhost:8080
Now that you have reviewed the initial state of the code, you will modify the app to use Spring Cloud features.
Create a Maven POM
Modify the initial code to enable support for external configuration through the use of the Spring Cloud Services starter. Modify the Maven POM to replace the Spring Boot parent with the Spring Cloud Services parent.
pom.xml
link:complete/pom.xml[]
Add the Spring Cloud Services dependencies. The spring-cloud-services-starter-config-client
specifically enables support for automatically discovering a running Spring Cloud Configuration Server service on Pivotal Cloud Foundry.
pom.xml
<dependencies>
...
link:complete/pom.xml[]
...
</dependencies>
The Spring Cloud dependencies are pre-releases and not available in Maven Central. Add the Spring Repository to pull in the required dependencies.
pom.xml
link:complete/pom.xml[]
These POM changes are all that is required to enable external configuration support in a Spring application. When deployed to Cloud Foundry, this app will import the external configuration and make it available within the Spring context.
Create a controller class
Modify the HomeController
to utilize the external configuration. Add the name
member variable and annotate it with @Value("${name:World}")
. Then modify the home()
method to return the value of the name variable.
src/main/java/demo/HomeController.java
link:complete/src/main/java/demo/HomeController.java[]
The @Value
annotation is used to inject the value of the name
property provided by the external configuration. If the property is not found, then it defaults to "World".
Build an executable JAR
Now that you have built the app, you can deploy it to Cloud Foundry.
Deploy the client to Cloud Foundry
Using the Cloud Foundry CLI in a terminal window, push the JAR file to Cloud Foundry and name the new app rest-client
.
cf push rest-client --random-route -p target/gs-configuration-server-client-0.1.0.jar
By default, the domain name for the application is determined by the application name. By using the --random-route
parameter, you’re asking Cloud Foundry to assign a random route. This will prevent domain name collisions with others who are working through this guide. Note the following line from the ouput. It contains the URI of the app with the randomly generated route.
Binding rest-client-brothellike-parallelogram.lion.wild.cf-app.com to rest-client...
The next step is to bind the config-server
service to the newly published rest-client
app.
cf bind-service rest-client config-server
Finally, restage the rest-client
to ensure the configuration changes take effect
cf restage rest-client
Test
Using the URI from the previous app deploy, make a request to the rest-client
app:
curl http://rest-client-brothellike-parallelogram.lion.wild.cf-app.com
If everything works correctly, you will see:
Hello Config Server for Pivotal CF!
Summary
Congratulations! You’ve just enabled centralized configuration and utilized that configuration from within a client application.