Giter Site home page Giter Site logo

javacspring / gs-centralized-configuration Goto Github PK

View Code? Open in Web Editor NEW

This project forked from spring-guides/gs-centralized-configuration

0.0 2.0 0.0 380 KB

Centralized Configuration :: Learn how to manage application settings from an external, centralized source

Home Page: https://spring.io/guides/gs/centralized-configuration

Java 14.97% Shell 50.29% Batchfile 34.74%

gs-centralized-configuration's Introduction

tags projects
spring-cloud
config server
spring-cloud
spring-cloud-config

This guide walks you through the process of standing up, and consuming configuration from, the Spring Cloud Config Server

What you’ll build

You’ll setup a Config Server and then build a client that consumes the configuration on startup and then refreshes the configuration without restarting the client.

Build with Gradle

Build with Gradle

configuration-service/build.gradle

link:https://raw.githubusercontent.com/spring-guides/gs-centralized-configuration/master/initial/configuration-service/build.gradle[role=include]

configuration-client/build.gradle

link:https://raw.githubusercontent.com/spring-guides/gs-centralized-configuration/master/initial/configuration-client/build.gradle[role=include]

Build with Maven

Build with Maven

To get you started quickly, here are the complete configurations for the server and client applications:

configuration-service/pom.xml

link:https://raw.githubusercontent.com/spring-guides/gs-centralized-configuration/master/initial/configuration-service/pom.xml[role=include]

configuration-client/pom.xml

link:https://raw.githubusercontent.com/spring-guides/gs-centralized-configuration/master/initial/configuration-client/pom.xml[role=include]

Stand up a Config Server

You’ll first need a Config Service to act as a sort of intermediary between your Spring applications and a typically version-controlled repository of configuration files. You can use Spring Cloud’s @EnableConfigServer to standup a config server that other applications can talk to. This is a regular Spring Boot application with one annotation added to enable the config server.

configuration-service/src/main/java/hello/ConfigServiceApplication.java

link:complete/configuration-service/src/main/java/hello/ConfigServiceApplication.java[role=include]

The Config Server needs to know which repository to manage. There are several choices here, but we’ll use a Git-based filesystem repository. You could as easily point the Config Server to a Github or GitLab repository, as well. On the file system, create a new directory and git init it. Then add a file called a-bootiful-client.properties to the Git repository. Make sure to also git commit it, as well. Later, you will connect to the Config Server with a Spring Boot application whose spring.application.name property identifies it as a-bootiful-client to the Config Server. This is how the Config Server will know which set of configuration to send to a specific client. It will also send all the values from any file named application.properties or application.yml in the Git repository. Property keys in more specifically named files (like a-bootiful-client.properties) override those in application.properties or application.yml.

Add a simple property and value, message = Hello world, to the newly created a-bootiful-client.properties file and then git commit the change.

Specify the path to the Git repository by specifying the spring.cloud.config.server.git.uri property in configuration-service/src/main/resources/application.properties. Make sure to also specify a different server.port value to avoid port conflicts when you run both this server and another Spring Boot application on the same machine.

configuration-service/src/main/resources/application.properties

link:complete/configuration-service/src/main/resources/application.properties[role=include]

Reading Configuration from the Config Server using the Config Client

Now that we’ve stood up a Config Server, let’s stand up a new Spring Boot application that uses the Config Server to load its own configuration and that refreshes its configuration to reflect changes to the Config Server on-demand, without restarting the JVM. Add the org.springframework.cloud:spring-cloud-starter-config dependency in order to connect to the Config Server. Spring will see the configuration property files just like it would any property file loaded from application.properties or application.yml or any other PropertySource.

The properties to configure the Config Client must necessarily be read in before the rest of the application’s configuration is read from the Config Server, during the bootstrap phase. Specify the client’s spring.application.name as a-bootiful-client and the location of the Config Server spring.cloud.config.uri in configuration-client/src/main/resources/bootstrap.properties, where it will be loaded earlier than any other configuration.

configuration-client/src/main/resources/bootstrap.properties

link:complete/configuration-client/src/main/resources/bootstrap.properties[role=include]

The client may access any value in the Config Server using the traditional mechanisms (e.g. @ConfigurationProperties, @Value("${…​}") or through the Environment abstraction). Create a Spring MVC REST controller that returns the resolved message property’s value. Consult the Building a RESTful Web Service guide to learn more about building REST services with Spring MVC and Spring Boot.

By default, the configuration values are read on the client’s startup, and not again. You can force a bean to refresh its configuration - to pull updated values from the Config Server - by annotating the MessageRestController with the Spring Cloud Config @RefreshScope and then by triggering a refresh event.

configuration-client/src/main/java/hello/ConfigClientApplication.java

link:complete/configuration-client/src/main/java/hello/ConfigClientApplication.java[role=include]

Test the application

Test the end-to-end result by starting the Config Service first and then, once loaded, starting the client. Visit the client app in the browser, http://localhost:8080/message. There, you should see the String Hello world reflected in the response.

Change the message key in the a-bootiful-client.properties file in the Git repository to something different (Hello Spring!, perhaps?). You can confirm that the Config Server sees the change by visiting http://localhost:8888/a-bootiful-client/default. You need to invoke the refresh Spring Boot Actuator endpoint in order to force the client to refresh itself and draw the new value in. Spring Boot’s Actuator exposes operational endpoints, like health checks and environment information, about an application. In order to use it you must add org.springframework.boot:spring-boot-starter-actuator to the client app’s CLASSPATH. You can invoke the refresh Actuator endpoint by sending an empty HTTP POST to the client’s refresh endpoint, http://localhost:8080/refresh, and then confirm it worked by reviewing the http://localhost:8080/message endpoint.

Note
we set management.security.enabled=false in the client app to make this easy to test (by default since Spring Boot 1.5 the Actuator endpoints are secure by default). By default you can still access them over JMX if you don’t set the flag.

Summary

Congratulations! You’ve just used Spring to centralize configuration for all your services by first standing up a and to then dynamically update configuration.

gs-centralized-configuration's People

Contributors

gregturn avatar cbeams avatar joshlong avatar royclarkson avatar btalbott avatar habuma avatar dsyer avatar buzzardo avatar shadowmanos avatar odrotbohm avatar

Watchers

James Cloos avatar  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.