We learned how to use file system back end to configure spring cloud configuration server in previous post.

We will learn how to use git repository back end to configure configuration server in this post.

Create configuration server

Let us set up configuration server.

This involves adding required maven dependency, creating git repository with configuration files, setting up required configuration to link to git repository and setting up configuration properties.

Add required dependency

Create a spring boot application with spring-cloud-config-server dependency.

Our pom.xml file should should look like below.

<?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 http://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>2.1.6.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.asb.example</groupId>
	<artifactId>spring-boot-config-server</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>spring-boot-config-server</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
		<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
	</properties>

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

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</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>

Create local git repository with configuration files

We can skip this step if we already have a git repository with configuration files.

Open git bash, and create a git repository. I have created one in my windows system’s desktop location as shown below.

Spring cloud configuration git integration example

We have created a simple-service directory to keep all simple-service configuration client service related configuration properties in one place.

Inside simple-service directory, we have added two configuration property files one for dev profile and another for prod spring profile.

Notice the name simple-service-{profile}.yml. Here, simple-service also should be the spring application name of the configuration client service, which we are going to create later.

Setup server with required configuration

Now we need add required configuration properties to set up our configuration server with git repository.

Update the application.properties under src/main/resources/ directory file with below content.

spring.cloud.config.server.git.uri=C:/Users/ASB/Desktop/config
#scans the directories available inside git URI location:
spring.cloud.config.server.git.search-paths=simple-service
server.port=8888
  • spring.cloud.config.server.git.uri : Git URL, where configuration files are present.
  • spring.cloud.config.server.git.search-paths : Comma separated directories containing configuration property files.
  • server.port : Server port used by the spring cloud configuration server, once application is started.

Enable spring configuration server

Finally, we need enable spring cloud configuration server by adding @EnableConfigServer annotation to our spring boot application class.

This annotation indicates that the application is used as a spring cloud configuration server.

@SpringBootApplication
@EnableConfigServer
public class SpringBootConfigServerApplication {

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

Checking the configuration server

Run the spring boot application now.

Our configuration properties should be accessible under http://localhost:8888/simple-service/dev and http://localhost:8888/simple-service/prod as shown below.

cloud config server dev profile
spring cloud config server prod profile

We have configured our spring cloud config server with git repository as back end. Good job!! ๐Ÿ™‚

Setting up config client server

We have successfully created spring cloud configuration server and started the server in previous section. Now it is time to integrate with configuration client service.

Add required dependencies

Create a spring boot application with name simple-servie and spring-boot-starter-web and spring-cloud-config-client dependencies.

pom.xml of created application should is given below.

<?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 http://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>2.1.6.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.asb.example</groupId>
	<artifactId>simple-service</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>simple-service</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
		<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
		<spring-cloud.version>Greenwich.SR2</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-config-client</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</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>

Configuring the config client service

Next, we have to setup our service to connect to the configuration server.

Create a bootstrap.properties file under src/main/resources/ directory of simple-service and add the following configuration properties.

spring.profiles.active=dev,prod
spring.application.name=simple-service
spring.cloud.config.url=http://localhost:8888
  • spring.profiles.active : List of spring profiles available for the application.
  • spring.application.name : Spring application name. This should match with the configuration file available on config server.
  • spring.cloud.config.url : URL of spring cloud config server, we have created in earlier steps.

Update the spring boot application class file by adding a simple end point, which reads the configuration property.

Here we have used @Value annotation, which reads the my.prop configuration property from git configuration file and set it to myProp string variable.

We have created a http get end point which is available at http://localhost:{port}/, which returns string value containing the configuration property.

@SpringBootApplication
@RestController
public class SimpleServiceApplication {

	@Value(value = "${my.prop}")
	private String myProp;
	
	@GetMapping("/")
	public String getMyProp() {
		return "Prop value: " + myProp;
	}
	
	public static void main(String[] args) {
		SpringApplication.run(SimpleServiceApplication.class, args);
	}
}

Testing the config client

Start the configuration client service in two different ports for dev and prod profile.

With STS IDE, we can use run configuration option to set vm arguments. It is available at Right click on application > Run As > Run configuration > (create new configuration if not available). Add arguments under arguments tab as shown below.

command line arguments spring boot

We have started service with 8080 port for dev profile and 8090 for prod profile.

We should be able to get the configuration properties by accessing http://localhost:8080/ and http://localhost:8080/.

spring cloud dev profile config
Spring cloud prod profile config client

We have created a simple service and integrated with spring cloud configuration server successfully!! Good job! ๐Ÿ™‚

Detect configuration property changes with @RefreshScope

In case configuration property values are changed when services are running, spring cloud automatically exposes changed property values without requiring server restart.

But client servers read configuration properties during application startup. So it will not get updated configuration values without server restart.

we can use @RefreshScope annotation which enables actuator refresh/ end point to refresh the loaded configuration property values.

Add @RefreshScope annotation

Add @RefreshScope annotation to the configuration client’s spring boot application starter class.

@SpringBootApplication
@RestController
@RefreshScope
public class SimpleServiceApplication {
	//
}

Add actuator dependency to the client dependency list

Add spring-boot-starter-actuator dependency to the configuration client application’s pom.xml file.

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

Expose refresh/ actuator end point by adding following property to application.properties file. This is a HTTP POST end point.

management.endpoints.web.exposure.include=refresh

Update the configuration property value on git

Open the simple-service-dev.yml using vi editor and update the property value as shown below.

update config server properties
update config server properties

You can also update prod properties too.

commit the updated file. Without commit, values will not be reflected in configuration server.

update config server properties

Refresh the configuration properties using postman

Since the actuator refresh/ end point supports only HTTP POST access, we can use postman.

actuator refresh postman

Check the browser for updated configurations.

config updated

Conclusion

In this article, we learned how to set up spring cloud configuration server with git repository as configuration source.

We also created different configuration files for different spring profiles.

we created a service and invoked property values from configuration server.

We also enabled @RefreshScope to refresh configuration property changes during application runtime.

Sample source code is available on Github. Happy Coding ๐Ÿ™‚ ๐Ÿ™‚

You may also interested in