在spring cloud配置服务器克隆git repo并正确提供文件之后,它是否也可以对这些文件进行更改并将它们推回到远程git repo?或者是否必须在运行配置服务器的应用程序中编写该功能?
答案 0 :(得分:1)
不,它不能写。 Config Server是远程git存储库的只读客户端。
答案 1 :(得分:1)
对于其他感兴趣的人,我自动安装了Spring JGitEnvironmentRepository并包含了一些额外的功能。我使用FileUtils来处理文件操作,然后使用该JGit bean编写了一个基本服务:
@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