This is the second day of my participation in Gwen Challenge

“Configuration center Spring Cloud Config in detail” series of articles updated, together in the road of technology progress! This series of articles will introduce Spring Cloud Config, which provides a distributed configuration center in Spring Cloud. In addition to the code to realize system functions, application services also need to connect resources and other applications. There are often a lot of data that need to be configured externally to adjust the behavior of applications, such as switching different databases and setting function switches. With the increase of microservices, the system needs to be scalable and extensible, in addition to managing the configuration data of a considerable number of service instances. In the application development stage, each service is autonomous, but in the production environment, it will bring great trouble to the operation and maintenance, especially the micro service scale is relatively large, configuration update is more troublesome. Therefore, the system needs to establish a unified configuration management center.

These reviews

In previous articles, we covered the basic features of Spring Cloud Config and the details of the client implementation. This article covers the Spring Cloud Config server implementation.

Configuring the Server

1. Jars in POM

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-consul-discovery</artifactId>
    </dependency>
</dependencies>
Copy the code

The jar package mainly introduces service discovery Consul, registers configuration service on Consul, and spring-cloud-config-server.

2. The entrance to the class

The server provides an HTTP API interface for external configuration (key-value pairs or yamL-like content formats). Spring Boot-based applications are easy to build in with the @enableconFigServer annotation.

@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigServerApplication {
    public static void main(String[] args) { SpringApplication.run(CloudApplication.class, args); }}Copy the code

3. Start the class configuration file

server:
  port: 8888
spring:
  application:
    name: config-server
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        ip-address: localhost
        port: ${server.port}
        instance-id: config-server-${server.port}
        service-name: config-server
---
spring:
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/keets/Config-Repo.git
          searchPaths: ${APP_LOCATE:dev}
          username: user
          password: password
Copy the code

The configuration information of the server side also includes the configuration information of the service discovery and configuration repository. Spring. Cloud. Config. Server. Git. The corresponding configuration git repository uri address; Spring. Cloud. Config. Server. Git. SearchPaths corresponding configuration path warehouse, here we specify the dev folders, distinguish between the different deployment environment; Spring. Cloud. Config. Server. Git. Username corresponding access git repository user name; Spring. Cloud. Config. Server. Git. Password corresponding access git repository user password; If the repository is private, you need to configure the user name and password and support the SECURE key mode of SSH. Otherwise, you do not need to add the repository. The above configuration uses private libraries and uses the username and password login mode.

Verify the server configuration

Once the configuration server is started, you can see the configuration information in the Git repository it pulls. Currently there is only config-client configuration in git repository, which corresponds to config-client-dev.yml.

Located property source: CompositePropertySource [name='configService', propertySources=[MapPropertySource {name='configClient'}, MapPropertySource {name='https://gitee.com/keets/Config-Repo.git/dev/config-client-dev.yml#dev'}]] 2018-01-10 20:01:35.367 INFO 25897 -- [NIO-8000-exec-1]Copy the code

The Spring Cloud Config server is responsible for publishing configuration files stored in Git as REST interfaces, so after building the configuration repository and configuration server, you can verify that the server can provide the interfaces properly. According to the rules of the corresponding endpoint above, we request to http://localhost:8888/config-client/dev, the following results are obtained.

{
    "name": "config-client",
    "profiles": [
        "dev"
    ],
    "label": "master",
    "version": "c5b6f3f78a0b3492a9ad01df212347839197e2e2",
    "state": null,
    "propertySources": [
        {
            "name": "https://gitee.com/keets/Config-Repo.git/dev/config-client-dev.yml#dev",
            "source": {
                "spring.profiles": "dev",
                "cloud.version": "Camden SR7"
            }
        }
    ]
}
Copy the code

The result displayed above shows the application name, profile, Git version, URL of the configuration file, and configuration content. According to the us, the interpretation of the above content can also be obtained through http://localhost:8888/config-client-dev.yml endpoint configuration.

Configure the authentication

Let’s verify that the client can get the configuration properly. This requires the client to add the API interface and submit the client configuration file to the configuration repository.

Add API interfaces for clients

On top of the above client, we add an API endpoint interface that is simple enough to request the value of cloud.version.

@RestController
@RequestMapping("/cloud")
public class TestController {

    @Value("${cloud.version}")
    private String version;

    @GetMapping("/version")
    public String version(a) {
        returnversion; }}Copy the code

Access to the configuration

After starting the configuration server and client, you can use tools such as POSTMAN or browser to verify that the client has correctly obtained the configuration information.

As shown in the preceding figure, config-client has successfully obtained the configuration information. After we commit the configuration file, the easiest way is to restart the service to get the configuration again. Clients are not actively aware of configuration changes and therefore actively obtain new configurations.

summary

This article focuses on the server-side implementation of the Spring Cloud Config example, which provides the configuration information necessary to start the client. Of course, the configuration center has much more functionality than this, and we’ll look at some advanced uses of Spring Cloud Config next.