This is the fifth 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 the Config Server configuration class in the Spring Cloud Config Configuration Server implementation. This article introduces the EnvironmentRepository interface and SearchPathLocator interface in the configuration server implementation.

EnvironmentRepository interface and SearchPathLocator interface

The various environmental repositories implement top-level interfaces, EnvironmentRepository and SearchPathLocator.

public interface EnvironmentRepository {
	Environment findOne(String application, String profile, String label);
}
Copy the code

This interface defines an Environment that retrieves the specified application service and returns an Environment object. The parameters that need to be passed in are also familiar, such as application, profile, and Label, which correspond to information about the client application. This method is used to obtain the configuration information of the application service based on the information about the client service. Here is an Environment object we got.

{
    "name": "config-client"."profiles": [
        "dev"]."label": null."version": "1e3e73d366cb37b99c918cd9b6f3ac471da17211"."propertySources": [{"name": "https://gitee.com/keets/Config-Repo.git/dev/config-client-dev.yml"."source": {
                "spring.profiles": "dev"."cloud.version": "Camden SR4"}}}]Copy the code

In addition to the information about the incoming client application, version corresponds to the CommitId submitted by Git, and propertySources corresponds to the source and specific value of the environment variable.

The other interface is SearchPathLocator, which obtains the location of the configuration environment file based on the incoming client application information.

public interface SearchPathLocator {

	Locations getLocations(String application, String profile, String label);

	class Locations {
		private final String application;
		private final String profile;
		private final String label;
		private final String[] locations;
		private final String version;
		/ /...}}Copy the code

The following figure shows a screenshot of obtaining a specified resource in a basic application:

Locations correspond to locations in the local repository, and the two locations returned here are the root directory and the dev directory because they are differentiated by profile. This interface is used to locate the resource search path. For example, the configuration information is stored in the file system or classpath. When obtaining the configuration information, you need to locate the configuration location. The internal Locations class defines the location information for the application service configuration. As with findOne, the getLocations method also requires information about the application service.

The class diagram for Interface EnvironmentRepository and SearchPathLocator is shown below:

Implement the configuration repository in JGit mode

MultipleJGitEnvironmentRepository inherited from JGitEnvironmentRepository, JGit storage way is also a kind of environment, we first need to understand what the JGit?

JGit is a relatively robust implementation of Git written in Java that is widely used in the Java community. The JGit project is maintained by Eclipse, whose home page is at www.eclipse.org/jgit.

Can be seen from the class diagram above, JGitEnvironmentRepository AbstractScmEnvironmentRepository inherited from the abstract class, and the abstract class inherits from AbstractScmAccessor, AbstractScmAccessor is a parent class of the SCM (Software Configuration Management, Source Control Management) implementation that defines the basic property composition to obtain the resources of the SCM. Let’s start with the base class AbstractScmAccessor, which defines the SCM configuration properties and basic methods.

public abstract class AbstractScmAccessor implements ResourceLoaderAware {

	private static final String[] DEFAULT_LOCATIONS = new String[] { "/" };

	private File basedir;

	private String uri;
	private ConfigurableEnvironment environment;

	private String username;

	private String password;

	private String passphrase;
 	// Deny remote access to hosts whose SSH keys are not in the known host list
	private boolean strictHostKeyChecking;
	// Search for the path to use in the local working copy. By default, only the root is searched
	private String[] searchPaths = DEFAULT_LOCATIONS.clone();
	/ /...
	public AbstractScmAccessor(ConfigurableEnvironment environment) {
		this.environment = environment;
		this.basedir = createBaseDir();
	}
	protected File createBaseDir(a) {
		/ /...
	}
	protected File getWorkingDirectory(a) {
		/ /...}}Copy the code

The address URI, user name, and password of the remote repository must be configured when using git repository. Other properties, such as basedir (locally copied workspace address) and passphrase (SSH private key), are also optional when setting up the environment repository.

summary

This article mainly introduces the implementation of the Config Server EnvironmentRepository interface and SearchPathLocator interface on the Spring Cloud Config Server. And the JGit way to implement the configuration repository server storage related definitions, will focus on the implementation of this way.