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
Please enter at least three characters to search
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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How to Setup the Spring Cloud Config Server With Git
  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • Migration of Microservice Applications From WebLogic to Openshift
  • 7 Microservices Best Practices for Developers

Trending

  • Unlocking the Potential of Apache Iceberg: A Comprehensive Analysis
  • Measuring the Impact of AI on Software Engineering Productivity
  • Immutable Secrets Management: A Zero-Trust Approach to Sensitive Data in Containers
  • Agentic AI for Automated Application Security and Vulnerability Management
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. How to Use the Spring Config Server

How to Use the Spring Config Server

Learn more about how to create remote configurations in your applications.

By 
Jesus J. Puente user avatar
Jesus J. Puente
·
Dec. 21, 18 · Tutorial
Likes (27)
Comment
Save
Tweet
Share
173.8K Views

Join the DZone community and get the full member experience.

Join For Free

This time, we will use the package Spring Cloud Config to have remote configurations for our applications.

The idea is that our program or programs can move your settings to an external place so that our application is easily configurable and can even change their settings.

This is widely used in microservices. A same service or application can be launched many times in different containers, and it is interesting to have a central place where they can read the settings to these services.

To do this, we will create a configuration server and a client that reads your configuration of that server. The configurations server uses a GIT repository on GitHub, where we store the configuration files.

The application data are as follows:

Configuration Server

  • Project: config-server
  • Port: 8888
  • Spring Name: config-server
  • GIT server: https://github.com/chuchip/servercloudconfig.git

Configuration Client

  • Project: config-client
  • Port: 8080
  • Spring Name: config-client

The program sources can be found here.

  1. Configuration server

You must only include this dependency in your Maven project.

<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-config-server</artifactId>
</dependency>


The name of the Spring Starter is the Config Server:

Image title

The server configuration consists of a single class, which is detailed below:
package com.profesorp.configserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}


As seen below, the only thing to highlight here is the label @EnableConfigServer.

In the file application.properties, it should be the parameter: spring.cloud.config.server.git.uri:

spring.application.name=config-server
server.port=8888

spring.cloud.config.server.git.uri=https://github.com/chuchip/servercloudconfig.git


In this case, we use a server, Git, that is hosted on GitHub. We could also specify the use of a repository, GITlocally, as follows:

spring.cloud.config.server.git.uri=file://eclipe/spring-config/


Configuration servers that Spring Cloud supports are the following backends:

  • GIT
  • Vault
  • JDBC

These backends can even be mixed so that, depending on the chosen profile, they will use one or the other. But this is beyond the scope of this document.

In the case of the GIT server, which is used in the example, the important thing is to have a file with the name of the client that is requesting data. This file will have the suffix .properties.

 So, if we want to have the setting for a client application that is called config-client, which is to say the variable spring.application.name is equal to config-client, we must have a file named config-client.properties. The content of this file will be:

datosservidor.minimum=11
datosservidor.maximum=20
limites.minimum=-1
limites.maximum=2
valores.valor_fijo: VALORFIJO
valores.valor_funcion: "VALORDEFUNCION";


Note that the value can be assigned with : or = .

DO NOT use quotation marks to delimit the literal, unless you want our literal (Strings) to include those quotes.

To see past our client values, we will make a GET request specifying the client name and profile.

Image titleIn this case, we ask for the client configuration config-clientand the profile default,which is the profile used if you do not specify any.

To view the settings for the profile production, you would call the URL: HTTP:// localhost: 8888 / config-client/production, which displays the following output:

Image title

This shows the file contents config-client-production.propertiesand then the file contents
of  config-client.properties.

Thus, if a variable exists in the required profile, that value will be returned. Otherwise, it looks in the profile by default, returning the value assigned, if any.

Client Settings

Once we have our configuration server working, we will create the client.

We add this dependency to our Maven file:

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-config</artifactId>
</dependency>


Using the Spring Initializr would add the Config Client dependency. In addition, to refresh the hot configuration, add the starter Actuator.

Now, we configure the application to specify where to listen to the configuration server. First, we will change the name of the file config.properties by the bootstrap.properties. Thus, Spring Boot knows to look for a server configuration.

In this file, we add the property spring.cloud.config.uri, specifying the URL of our server configuration.

spring.application.name=config-client
spring.cloud.config.uri=http://localhost:8888
#spring.profiles.active=production
management.endpoints.web.exposure.include=refresh


We also establish the property management.endpoints.web.exposure.includeto refreshto configure the package actuator so that you can access the URL: http://localhost:8080/actuator/refresh that will force us to refresh the configuration.

Remember that the variable spring.application.nameset the application name and indicates the name of the file in the GIT repository where the settings were sought.

With the variable spring.profiles.active, we indicate which profile will be used. If we do not put any (as is the case for this commented line), the profile used will be the default.

In this example, I use several methods to read the configuration.

  1. Create a Component That Includes the Label   @ConfigurationProperties 

In this method, which is the simplest, we indicate which is the root of the properties to read and then define the variables that Spring should fill.

In the class configuration.java, we specify that we want to configure the variables that begin with limites.

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import lombok.Data;

@Data
@Component
@ConfigurationProperties("limites")
public class Configuration {
    private int minimum;
    private int maximum;    
}


Thus, the variable dato1will have the value specified inlimites.dato1.

If your limites.dato1has a value that can not be passed to an integer, our application would fail; however, if you do not find the value, simply do not fill it without giving any error.

This component will be injected through the label @Autowired. 

@Autowired
private Configuration configuration;


2. Create a Variable With the Annotation @Value

Thus, the value of server configurations will also be read. The biggest difference is that the value will be fixed as it will be assigned to run the application and never refreshed.

@Value("${valores.valor_fijo}")
String valorFijo;


The variable valorFijowill have the value assigned on the line:valores.valor_fijo.

3. Use the @Value Entry in the Parameter of a Function

Likewise, the value is read from the server configurations with the advantage that the value may be refreshed.

@GetMapping("/refrescado")
public BeanConfiguration getConfiguracionRefrescada(@Value("${valores.valor_funcion}") String valorFuncion)
    { .... }


Functioning

In our example, expose the URL /limites, refrescado,and datos.

The call limitesto return this output:

Image title

If we change the file config-client.properties and make a push and commit, such values in our GIT repository server, successive calls to this URL will show the old data because the client only reads the configuration at the star, unless you force him to refresh the data.

Imagine that we change the 'config-client.properties' file so that we now have these values:

datosservidor.minimum=10
datosservidor.maximum=20
limites.minimum=-101
limites.maximum=201
valores.valor_fijo: OTROVALORFIJO
valores.valor_funcion: "OTROVALORDEFUNCION"


Do the corresponding commit and push:

> git commit -a -m "cambiada configuracion";git push


When you call the URL http://localhost: 8080/actuator/refresh with a POST type request, Spring forces them to call the server configurations and refresh values.

Image title

As shown, the output of this request returns the refreshed variables.

Now, if we call http://localhost: 8080/limits, we would see that the value of minResuados and maxResultados  have changed. However, neither valorFijo or valorFuncion have changed.

If we call the URL http://localhost: 8080/refrescado and see that the variable valorFuncion has been updated, the call is placed on the label @Value so that the variable is read at that time. However, the variable valorFijo is not changed as established at the beginning of the program and its value remains unchanged.

One important concept to consider is that if we remove a variable in our configuration file, the value of the variable will not be made null. Instead, it keeps the previously established value. This is true whether the variable is read with @Value, like using a @ConfigurationProperties or a  @Bean.

Spring Framework application Git Spring Cloud Profile (engineering)

Opinions expressed by DZone contributors are their own.

Related

  • How to Setup the Spring Cloud Config Server With Git
  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • Migration of Microservice Applications From WebLogic to Openshift
  • 7 Microservices Best Practices for Developers

Partner Resources

×

Comments
Oops! Something Went Wrong

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
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!