By the end of this article you will have learned

  • How to build a Spring Cloud Config server based on Git

1. An overview of the

Spring Cloud Config is a configuration management solution for distributed systems. It consists of a Client and a Server. The Server stores the configuration file and provides the content of the configuration file in the form of an interface. The Client obtains data through the interface and initializes its application based on the data.

This configuration management scheme can be modified at run time with Git version control. While it works well for Spring applications, using all of the supported configuration file formats as well as Environment, PropertySource, or @Value, etc., it can be used in any Environment running any programming language.

In this article, we’ll focus on an example of how to set up a Git-enabled configuration server and how to use it in a simple REST application server.

2. Project Settings and dependencies

Let’s start by creating two new Maven projects. The Server project has the following dependencies:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
Copy the code

The Client project has the following dependencies:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
Copy the code

3. Configure the server implementation

The main part of the application is a configuration class — @SpringBootApplication, to be more precise — that introduces all the required Settings via the automatic configuration annotation @EnableconFigServer.

@SpringBootApplication @EnableConfigServer public class ConfigServer { public static void main(String[] arguments) { SpringApplication. Run (ConfigServer. Class, the arguments). }}Copy the code

Now we need to configure the port on which the server listens and the Git-URL that provides the configuration content for version control. The latter can use a protocol such as HTTP, SSH, or a simple file on a local file system.

Tip: If you plan to use multiple configuration server instances pointing to the same repository, you can configure the server to clone your version to a local temporary folder. But be aware of private repositories with two-factor authentication, which can be difficult to handle. In that case, it’s easier to clone them to your local file system and work with that copy.

We also need to set a user name and a password for basic-authentication in application.properties to avoid automatic password generation each time the application is restarted.

server.port=8888
spring.cloud.config.server.git.uri=ssh://localhost/config-repo
spring.cloud.config.server.git.clone-on-start=true
spring.security.user.name=jayxu
spring.security.user.password=s3cr3t
Copy the code

4. Git repository as configuration store

To complete our server, we had to initialize a Git repository at the configured URL and create some new properties files.

The name of the configuration file is made up like normal application.properties, but with the configuration name instead of Application, such as the value of the client property Spring.Application.name, followed by a dash and the active configuration file.

Such as:

$> git init
$> echo 'user.role=Developer' > config-client-development.properties
$> echo 'user.role=User'      > config-client-production.properties
$> git add .
$> git commit -m 'Initial config-client properties'
Copy the code

Tip: If you are experiencing SSH related authentication problems, please carefully check the ~/.ssh/known_hosts and ~/.ssh/authorized_keys on your SSH server!

5. Query the configuration

Now we can start our server. The Git-based configuration API provided by our server can be queried through the following path.

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
Copy the code

The {label} placeholder refers to a Git branch, {application} refers to the client’s application name, and {profile} refers to the client’s current active application profile.

Finally, we can get the configuration for the client in the development configuration file in the Master branch by using the following command.

$> curl http://jayxu:s3cr3t@localhost:8888/config-client/development/master
Copy the code

6. Client implementation

Next, let’s deal with the client side. This will be a very simple client application.

To get our server, the configuration must be in the application.properties file. Spring Boot 2.4 introduced a new way to load configuration data using the spring.config.import property, which is now the default way to bind to the configuration server.

@SpringBootApplication
@RestController
public class ConfigClient {
    
    @Value("${user.role}")
    private String role;

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

    @GetMapping(
      value = "/test/{username}",  
      produces = MediaType.TEXT_PLAIN_VALUE)
    public String test(@PathVariable("username") String username) {
        return String.format("Hello!
          You're %s and you'll become a(n) %s...\n", username, role);
    }
}
Copy the code

In addition to the application name, we added the activity profile and connection details to application.properties.

spring.application.name=config-client
spring.profiles.active=development
spring.config.import=optional:configserver:http://jayxu:s3cr3t@localhost:8888
Copy the code

This will connect to the configuration server http://localhost:8888, which will also use HTTP when initiating the connection. We can also use the spring. Cloud. Config. The username and spring. Cloud. Config. Password attribute set the username and password.

In some cases, if a service cannot connect to the configuration server, we may want to let it fail to start. We can stop the client by removing the optional: prefix.

To test, if the configuration is received correctly from our server and the role is injected into our Controller method, we simply use the following command after starting the client:

$> curl http://localhost:8080/test/JayXu
Copy the code

If the response is as follows, our Spring Cloud configuration server and its client are currently working fine.

Hello! You're JayXu and you'll become a(n) Developer...
Copy the code

8. Conclusion

Now we can create a configuration server that provides a set of configuration files to the client application from the Git repository.