Java Spring云配置服务器是否也可以将配置更新推送到远程git repo?
在SpringCloudConfig服务器克隆GitRepo并正确提供文件后,它是否也可以对这些文件进行更改并将其推回到远程GitRepo?或者该功能必须写入运行配置服务器的应用程序中吗?不,它不能写入。Config Server是远程git存储库的只读客户端。对于其他感兴趣的人,我自动连接了Spring JgitenEnvironment存储库,并包装了一些附加功能。我使用FileUtils来处理文件操作,然后使用JGitBean编写了一个基本服务:Java Spring云配置服务器是否也可以将配置更新推送到远程git repo?,java,spring,git,spring-cloud,Java,Spring,Git,Spring Cloud,在SpringCloudConfig服务器克隆GitRepo并正确提供文件后,它是否也可以对这些文件进行更改并将其推回到远程GitRepo?或者该功能必须写入运行配置服务器的应用程序中吗?不,它不能写入。Config Server是远程git存储库的只读客户端。对于其他感兴趣的人,我自动连接了Spring JgitenEnvironment存储库,并包装了一些附加功能。我使用FileUtils来处理文件操作,然后使用JGitBean编写了一个基本服务: @Autowired private JG
@Autowired private JGitEnvironmentRepository jgit;
@Value("${spring.cloud.config.server.git.uri}.git")
private String remoteRepoURL;
@Value("${spring.cloud.config.server.git.username}")
private String username;
@Value("${spring.cloud.config.server.git.password}")
private String password;
private Git git = null;
private Repository localRepo ;
private CredentialsProvider creds;
public File getWorkingDirectory(){
return jgit.getBasedir();
}
public void updateLocalRepo() throws Exception {
log.info("Updating the local repository.");
init();
git.pull()
.setCredentialsProvider( creds )
.call();
}
public void commitAndPushRepository(String commitMessage) throws Exception {
init();
git.commit()
.setAll(true)
.setMessage(commitMessage)
.call();
git.push()
.setCredentialsProvider( creds )
.call();
log.info("Pushed local repository with message [" + commitMessage+"].");
}//end commitAndPush
private void init() throws IOException {
if(git!=null) return;
String repoPath = jgit.getBasedir().getPath();
localRepo = new FileRepository(repoPath + "/.git");
git = new Git(localRepo);
StoredConfig config = git.getRepository().getConfig();
config.setString("remote", "origin", "url", remoteRepoURL);
config.save();
creds = new UsernamePasswordCredentialsProvider(username, password);
log.info("Initialized local repository at path " + repoPath);
}
这是文件服务:
@Autowired private GitService gitService;
public void updateFile(String author, String fileName, String newContent) throws Exception{
Assert.hasLength(fileName, "File name must not be null.");
Assert.hasLength(newContent, "File must contain content.");
Assert.hasLength(author, "Unable to update file without author logging.");
gitService.updateLocalRepo();
File workingDirectory = gitService.getWorkingDirectory();
log.info("Updating file [" + fileName + "] in the working dir " + workingDirectory);
File matchingFile = findFileWithName(workingDirectory, fileName);
Assert.notNull(matchingFile, "No file with name " + fileName + " was found.");
FileUtils.write(matchingFile, newContent);
gitService.commitAndPushRepository( buildCommitMessage(author, fileName) );
}//end updateFile
public String getConfigFileContents(String fileName) throws Exception {
gitService.updateLocalRepo();
File file = findFileWithName(gitService.getWorkingDirectory(), fileName);
Assert.notNull(file, "No file with name " + fileName + " found.");
return FileUtils.readFileToString( file );
}
public Collection<String> getAllConfigFileNames() throws Exception{
gitService.updateLocalRepo();
Collection<String> fileNames = new ArrayList<>();
Collection<File> allFiles = FileUtils.listFiles(gitService.getWorkingDirectory(), TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
for(File file : allFiles) {
fileNames.add(file.getName());
}//end foreach file
return fileNames;
}
private String buildCommitMessage(String author, String fileName) {
return "Author of commit on [" + fileName + "]: " + author;
}
private File findFileWithName(File workingDirectory, String fileName) {
Collection<File> allFiles = FileUtils.listFiles(workingDirectory, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
for(File file : allFiles) {
if(fileName.equals(file.getName())){
return file;
}//endif found
}//end foreach file
return null;
}//end findFileWIthName
@Autowired-private-GitService-GitService;
public void updateFile(字符串作者、字符串文件名、字符串newContent)引发异常{
Assert.hasLength(文件名,“文件名不能为null”);
Assert.hasLength(newContent,“文件必须包含内容”);
Assert.hasLength(author,“在没有作者日志记录的情况下无法更新文件”);
gitService.updateLocalRepo();
File workingDirectory=gitService.getWorkingDirectory();
log.info(“正在更新工作目录“+工作目录”中的文件[“+文件名+”]);
文件匹配File=findFileWithName(工作目录,文件名);
Assert.notNull(matchingFile,“未找到名为“+fileName+”的文件”);
write(matchingFile,newContent);
提交PushRepository(buildCommitMessage(作者,文件名));
}//结束更新文件
公共字符串getConfigFileContents(字符串文件名)引发异常{
gitService.updateLocalRepo();
File File=findFileWithName(gitService.getWorkingDirectory(),文件名);
Assert.notNull(文件,“找不到名为“+fileName+”的文件”);
返回FileUtils.readFileToString(文件);
}
公共集合getAllConfigFileNames()引发异常{
gitService.updateLocalRepo();
集合文件名=新的ArrayList();
Collection allFiles=FileUtils.listFiles(gitService.getWorkingDirectory(),TrueFileFilter.INSTANCE,TrueFileFilter.INSTANCE);
用于(文件:所有文件){
add(file.getName());
}//结束foreach文件
返回文件名;
}
私有字符串buildCommitMessage(字符串作者、字符串文件名){
返回“提交时间[“+fileName+”]:“+Author;
}
私有文件findFileWithName(文件工作目录,字符串文件名){
Collection allFiles=FileUtils.listFiles(workingDirectory,TrueFileFilter.INSTANCE,TrueFileFilter.INSTANCE);
用于(文件:所有文件){
if(fileName.equals(file.getName())){
返回文件;
}//找到endif
}//结束foreach文件
返回null;
}//结束findFileWIthName
我正在考虑这样做,而不是切换到JDBC支持的环境存储库。我认为与使用JDBC相比,这提供了更多的可跟踪性和灵活性backend@user2153465这正是我这么做的原因。我希望提交历史随时可用,而无需构建记录事件的基础设施,并且能够分叉/拉取可用的repo。