配置存储
springCloudConfig是使用EnvironmentRepository
来存储服务器的配置数据的,返回Environment对象
1 2 3 4 5 6 7 8 9 10 11
| public Environment(@JsonProperty("name") String name, @JsonProperty("profiles") String[] profiles, @JsonProperty("label") String label, @JsonProperty("version") String version, @JsonProperty("state") String state) { super(); this.name = name; this.profiles = profiles; this.label = label; this.version = version; this.state = state; }
|
基于本地文件
如果存储是基于文件的,即配置了spring.cloud.config.server.native.searchLocation
,此时使用的是NativeEnvironmentRepository来查找配置
使用本地文件的前提是要配置spring.prifiles.active=native
1 2 3 4 5 6 7 8 9 10 11 12
| @Configuration(proxyBeanMethods = false) @Profile("native") class NativeRepositoryConfiguration {
@Bean public NativeEnvironmentRepository nativeEnvironmentRepository( NativeEnvironmentRepositoryFactory factory, NativeEnvironmentProperties environmentProperties) { return factory.build(environmentProperties); }
}
|
只有配置了spring.prifiles.active=native
才会创建NativeEnvironmentRepository这个bean
基于Git
如果存储是基于git的,即配置了spring.cloud.config.server.git.uri
,此时使用的是JGitEnvironmentRepository来查找配置
1 2 3 4 5 6
| if (getUri().startsWith(FILE_URI_PREFIX)) { return copyFromLocalRepository(); } else { return cloneToBasedir(); }
|
如果使用file:前缀来进行设置的,此时是从本地Git仓库获取的,也就不需要克隆
1 2 3 4 5 6 7 8 9 10
| private Git copyFromLocalRepository() throws IOException { Git git; File remote = new UrlResource(StringUtils.cleanPath(getUri())).getFile(); Assert.state(remote.isDirectory(), "No directory at " + getUri()); File gitDir = new File(remote, ".git"); Assert.state(gitDir.exists(), "No .git at " + getUri()); Assert.state(gitDir.isDirectory(), "No .git directory at " + getUri()); git = this.gitFactory.getGitByOpen(remote); return git; }
|
如果不是用file前缀来进行设置的,就需要从git库去进行clone
1 2 3 4 5 6 7 8 9 10 11 12 13
| private Git cloneToBasedir() throws GitAPIException { CloneCommand clone = this.gitFactory.getCloneCommandByCloneRepository() .setURI(getUri()).setDirectory(getBasedir()); configureCommand(clone); try { return clone.call(); } catch (GitAPIException e) { this.logger.warn("Error occured cloning to base directory.", e); deleteBaseDirIfExists(); throw e; } }
|