This is the fourth 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.

In previous articles, we introduced Spring Cloud Config as a whole. This article introduces the Config Server configuration class in the Config Server implementation.

Configuring the Server

Based on the basic applications in the previous article, the main functions of the configuration server are as follows: connecting to the configuration repository, pulling remote configurations and caching them locally, and providing configuration information externally through the REST interface.

Config Server Configuration class

The @enableconFigServer annotation enables application service support for the configuration center.

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(ConfigServerConfiguration.class)
public @interface EnableConfigServer {

}
Copy the code

The annotations as configuration server switch, introduces ConfigServerConfiguration configuration class. The following is a concrete implementation of this configuration class.

@Configuration
public class ConfigServerConfiguration {
	class Marker {}

	@Bean
	public Marker enableConfigServerMarker(a) {
		return newMarker(); }}Copy the code

This configuration class does not bear the specific implementation, but internally defines an internal class Marker as the Marker class and registers this object with the Bean factory, which controls whether Config Server is enabled.

When you enable Config Server, configure the Server at startup requires to automatically configure Config Server in ConfigServerAutoConfiguration introduces multiple configuration class is as follows:

  • EnvironmentRepositoryConfiguration: environment variables to store related configuration
  • Configuration of a CompositeConfiguration environment repository
  • ResourceRepositoryConfiguration: resource storage related configuration
  • ConfigServerEncryptionConfiguration: encryption endpoint configuration
  • ConfigServerMvcConfiguration: external exposure of MVC endpoint configuration of the controller
  • TransportConfiguration: Configures the callback of the Clone or FETCH transfer command

The @import annotation imports these configuration classes, which are also the main functionality implementations of Config Server. This section focuses on the core configuration class, which introduces the core functions of Config Server.

Gets the environment configuration for the specified service

EnvironmentRepositoryConfiguration

EnvironmentRepositoryConfiguration is the environment configuration of the warehouse, warehouse environment support form the configuration of the warehouse as follows: JDBC, Vault (HashiCorp’s private information management tool), Svn, local file system, Git library, etc. These configuration classes are certainly needed here.

  • CompositeRepositoryConfiguration: combination of the configuration storage configuration class, corresponding profie for “composite”
  • JdbcRepositoryConfiguration: based on JDBC database to store the configuration of the class, the corresponding profie for “JDBC”
  • VaultRepositoryConfiguration: Vault configuration, corresponding profie for “Vault”
  • SvnRepositoryConfiguration: SVN mode configuration class, corresponding profie for “subversion”
  • NativeRepositoryConfiguration: local file configuration, corresponding profie as the “native”
  • GitRepositoryConfiguration: Git way configuration, corresponding profie for “Git”
  • DefaultRepositoryConfiguration: the default configuration class that do not specify a profile, with profie “git” at the same way

There are several ways to configure your repository, each of which is implemented on the same principle. Here, I will configure your repository using Git as the most common default.

Since the environment repository supports multiple modes, how do you specify which mode to use when configuring the server to start? Memory configuration section based on application for spring. Cloud. Config. Server. Git, the configuration seems to see no way to specify which configuration warehouse, we take a look at the specific implementation of the configuration class.

@Configuration
@ConditionalOnMissingBean(EnvironmentRepository.class)
class DefaultRepositoryConfiguration { / / 1.@Bean
	public MultipleJGitEnvironmentRepository defaultEnvironmentRepository(a) {
		MultipleJGitEnvironmentRepository repository = new MultipleJGitEnvironmentRepository(
				this.environment); / / 2
		repository.setTransportConfigCallback(this.transportConfigCallback); / / 3
		if (this.server.getDefaultLabel() ! =null) {
			repository.setDefaultLabel(this.server.getDefaultLabel()); / / 4
		}
		returnrepository; }}@Configuration
@Profile("jdbc")
class JdbcRepositoryConfiguration { / / 5
	@Bean
	public JdbcEnvironmentRepository jdbcEnvironmentRepository(JdbcTemplate jdbc) {
		return newJdbcEnvironmentRepository(jdbc); }}/ /... Other configuration methods are omitted
Copy the code
  1. The default environment repository, not specified in Config Serverspring.profiles.activeIs initialized by defaultDefaultRepositoryConfigurationConfiguration in;
  2. In the default initialization configuration class, Git repository is used to construct a new oneMultipleJGitEnvironmentRepositoryObject registered to the Spring container;
  3. Set the JGit transfer information. When running the clone command, you need to set the sSH-based callback.
  4. Label corresponds to the label of the configuration warehouse. If the parameter is missing, set the default label to master.
  5. If the profile of the Config Server service isjdbc, activate the JDBC configuration repository, and the configuration classes corresponding to multiple profiles are omitted here.

EnvironmentRepositoryConfiguration also declares in a number of other configuration, capture only a part of the code above, it can be seen that the implementation of each configuration warehouse are corresponding to the configuration class of statement with @ Profile annotations to activate the corresponding configuration class, And specify spring.profiles. Active = JDBC in the application. Properties or application. When we are not set, USES the default way of environment warehouse for Git, so see DefaultRepository configuration annotations @ ConditionalOnMissingBean (EnvironmentRepository. Class), The default EnvironmentRepository configuration class is instantiated only when the EnvironmentRepository object is not in context.

summary

This paper mainly introduces the specific code implementation of the Config Server configuration class on the Spring Cloud Config Server. Next, we will cover the details of the EnvironmentRepository interface implementation for server-side storage.