Learnitweb

Read properties from Spring Cloud Config Server Using Spring Cloud Config Client

1. Introduction to Spring Boot Config Client

In this tutorial, we’ll discuss the Spring Cloud Config Client and will explore how to read properties from Spring Cloud Config Server. In the previous tutorial, we created a Spring Cloud Config Server. Once the Spring Cloud Config Server is created, the Spring Boot application can access these properties using the Spring Cloud Config Client.

The default way to bind to the Config Server is using the spring.config.import property. If you want the Spring Cloud Config Client to fail, if for any reason it is unable to connect to the config server, add the optional:.

spring.config.import=optional:configserver:

By default, the client connects to the Config Server at the default location of http://localhost:8888. You can change the location of Config server either by setting the spring.cloud.config.uri or adding the url to the spring.config.import.

spring.config.import=optional:configserver:http://localhost:8887

The location in the import property has precedence over the uri property.

spring:
  application:
    name: service1
  profiles:
    active: prod
  config:
    import: "optional:configserver:http://127.0.0.1:8085/"

Spring Boot Config Data resolves configuration in a two step process:

  1. Load all configuration using the default profile. This allows Spring Boot to gather all configuration which may activate additional profiles.
  2. Load configuration for the active profiles.

Due to this two step process, you may see multiple requests being made to the Spring Cloud Config Server to fetch configuration. This two step process allows to activate profiles from configuration coming from the Config Server.

If you check the logs, the logs look like the following:

Fetching config from server at : http://127.0.0.1:8085/
Located environment: name=service1, profiles=[default], label=null, version=null, state=null
Fetching config from server at : http://127.0.0.1:8085/
Located environment: name=service1, profiles=[prod], label=null, version=null, state=null

2. Config Client Fail Fast

If you don’t want the service to start up if it fails to connect to the config server, remove the optional: from the spring.config.import property value.

3. Config Client Retry

If you believe that the config server may occassionally be unavailable when your application starts, you can use the retry after a failure. You can specify the max-attempts, max-interval, initial backoff interval and an exponential multiplier for subsequent backoffs.

For the retry to work, you need to add spring-retry and spring-boot-starter-aop to your classpath.

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-aop</artifactId>
</dependency>

<dependency>
	<groupId>org.springframework.retry</groupId>
	<artifactId>spring-retry</artifactId>
	<version>2.0.3</version>
</dependency>

You can specify the max-attempts, max-interval, initial backoff interval and an exponential multiplier for subsequent backoffs in the URL.

spring:
	config:
		import: "optional:configserver:http://localhost:8085?fail-fast=true&max-attempts=10&max-interval=1500&multiplier=1.2&initial-interval=1100"

4. Locating Remote Configuration Resources

The Config Service serves property sources from /{application}/{profile}/{label}, where the default bindings in the client app are as follows:

  • “application” = ${spring.application.name}
  • “profile” = ${spring.profiles.active} (actually Environment.getActiveProfiles())
  • “label” = “master”

You can override all of them by setting spring.cloud.config.name, spring.cloud.config.profile and spring.cloud.config.label.

5. Configuring Timeouts

Read timeouts can be configured by using the property spring.cloud.config.request-read-timeout.

Connection timeouts can be configured by using the property spring.cloud.config.request-connect-timeout.

6. Example – Read properties from Spring Cloud Config Server

For our example, the pom.xml of aour Spring Boot application looks like the following:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.2.1</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.learnitweb</groupId>
	<artifactId>configclient</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>configclient</name>
	<description>Demo project for Spring Cloud Config Client</description>
	<properties>
		<java.version>17</java.version>
		<spring-cloud.version>2023.0.0</spring-cloud.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-aop</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.retry</groupId>
			<artifactId>spring-retry</artifactId>
			<version>2.0.3</version>
		</dependency>
	</dependencies>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

The next step to do in Spring Boot application is to provide configuration values. Following is the application.yml.

spring:
  application:
    name: service1
  profiles:
    active: prod
  config:
    import: "optional:configserver:http://localhost:8085?fail-fast=true&max-attempts=10&max-interval=1500&multiplier=1.2&initial-interval=1100"

And that’s it done. When you start the application and assuming the Config server is running, the Spring Boot application will be able to fetch the configuration from the Config Server.

7. Conclusion

In conclusion, this tutorial has provided a comprehensive overview of Spring Cloud Config Client, offering a deep dive into its capabilities and demonstrating how it seamlessly integrates with the Spring Cloud Config Server. By leveraging the power of centralized configuration management, developers can efficiently manage and update application settings across distributed microservices.