Coder Social home page Coder Social logo

training-material / gs-configuration-server Goto Github PK

View Code? Open in Web Editor NEW

This project forked from vmware-archive/gs-configuration-server

0.0 1.0 0.0 1.28 MB

Building a Configuration Server :: Learn how to create a configuration server and consume properties from within a client service.

Shell 11.58% Java 88.42%

gs-configuration-server's Introduction

tags

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.

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.

  1. Select the Config Server for Pivotal CF from the Marketplace:

    Marketplace
  2. Select the desired plan for the new service:

    Select Plan
  3. Provide a name for the new Config Server service (e.g. config-server) and click the Add button:

    Instance Name
  4. 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):

    Service Successfully Added
  5. 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:

    Manage Service
  6. The green confirmation box indicates the service is configured:

    Service Successfully 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[role=include]

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[role=include]
...
</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[role=include]

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[role=include]

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.

gs-configuration-server's People

Contributors

gregturn avatar royclarkson avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.