Spring Cloud Config Server – Git Back End

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

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

Create configuration server

Let us set up a configuration server.

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

Add required dependency

Create a spring boot application and also add the spring-cloud-config-server dependency.

Our pom.xml file 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 configuration properties in one place.

Inside the simple-service directory, we have added two configuration property files one for the dev profile and another for the 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 to add the required configuration properties to set up our configuration server with git repository.

Update the application.properties under src/main/resources/ directory file with the 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 to enable the 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 the back end.

Setting up config client server

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

Add required dependencies

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

The pom.xml of created application 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 set up 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 endpoint, which reads the configuration property.

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

We have also created an http get endpoint which is available at http://localhost:{port}/, which returns a 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 profiles.

With STS IDE, we can use the 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). Also, add arguments under the 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.

Also, 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

Finally, we have created a simple service and integrated it with the spring cloud configuration server.

Detect configuration property changes with @RefreshScope

In case of configuration property values are changed when services are running, spring cloud automatically exposes changed property values without requiring a 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 the @RefreshScope annotation which enables the actuator endpoint refresh/ to refresh the loaded configuration property values.

Add the @RefreshScope annotation

Now, add the @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 the 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 endpoint by adding the following property to the application.properties file. This is an HTTP POST endpoint.

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 the configuration server.

update config server properties

Refresh the configuration properties using postman

Since the actuator refresh/ endpoint 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 a spring cloud configuration server with the git repository as a configuration source.

We also created different configuration files for different spring profiles.

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

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

Finally, the sample source code is available on Github.

Spring Cloud Config Server – Git Back End
Scroll to top

Discover more from ASB Notebook

Subscribe now to keep reading and get access to the full archive.

Continue reading