This is the first day of my participation in Gwen Challenge

Next challenge, start the Spring CLoud series of updates, together on the road to 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.

Common configuration center implementation methods are as follows:

  • Hard coding, the disadvantage is the need to modify the code, large risk;
  • They are packaged together with applications in configuration files such as XML. However, they need to be repackaged and restarted.
  • In the file system, the disadvantages are dependent on the operating system.
  • The disadvantage of reading system environment variables is that a lot of configuration needs to be manually set into the environment variables, which is not easy to manage and depends on the platform;
  • Cloud storage has the disadvantage of being coupled with other applications.

In distributed system, there are many open source components about distributed configuration center in the industry, such as Apollo of Ctrip, Disconf of Baidu, Diamond of Taobao and so on. Spring Cloud provides a distributed configuration center, Spring Cloud Config, which provides client and server support for external configuration.

Based on the Config server, you can centrally manage the configuration information of various applications in various environments. The client and server match the concepts of Environment and PropertySource in Spring, so this works well not only for all Spring applications, but for any language. An application can have multiple environments, from dev to test to production, and you can manage the configuration of these different environments and ensure that the application is fully configured to work after the migration.

The default storage implementation on the Config server is Git, which makes it easy to support the tag version of the configuration environment, and there are various tools to manage these configurations easily. The Config service also supports various warehouse implementation modes, such as local file system and SVN.

The base application

In the basic application of configuration center, there will be two parts: configuration server and configuration client. The Config Server provides configuration information for the configuration client from the configuration repository. The configuration information needs to be pulled from the repository and cached in the local repository during startup. The Config Client is a configuration Client that configures only necessary information in the configuration file. For example, the Client obtains configuration information from the Config Server and dynamically refreshes property values in the configuration repository.

Let’s start by familiarizing ourselves with this component through a basic application of Spring Cloud Config.

1. Jars in POM

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

The JAR package introduces the starter of config. To obtain configuration information from the configuration server, you need to discover the starter of Consul.

2. The entrance to the class

@SpringBootApplication
@EnableDiscoveryClient
public class ConfigClientApplication {

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

3. Start the class configuration file

server:
  port: 8000
spring:
  application:
    name: config-client
  cloud:
    config:
      label: master
      discovery:
        enabled: true
        service-id: config-server
      enabled: true
      fail-fast: true
      profile: dev
    consul:
      host: localhost
      port: 8500
      discovery:
        ip-address: localhost
        port: ${server.port}
        instance-id: config-client-${server.port}
        service-name: config-client
Copy the code

The configuration information for the startup class is relatively simple, and you can put everything except service discovery in the configuration center. In the boot class profile, you first need to specify the consul registry configuration; Secondly, the related configuration of Spring.cloud. config includes label, profile, and Discovery service-ID, that is, the service name of the configuration server.

summary

This paper mainly introduces the concepts and basic practices of distributed configuration center. In distributed system, there are many open source components about distributed configuration center in the industry, such as Apollo of Ctrip, Disconf of Baidu, Diamond of Taobao and so on. Spring Cloud provides a distributed configuration center, Spring Cloud Config, which provides client and server support for external configuration.

The following articles will continue with the subsequent implementation code of Spring Cloud Config.