Spring Cloud config 配置git 实现配置中心但是一直报错,官方github的demo也报错,但是配置文件在本地就没问题,很奇怪


APPLICATION FAILED TO START


Description:

Parameter 0 of method defaultEnvironmentRepository in org.springframework.cloud.config.server.config.DefaultRepositoryConfiguration required a bean of type 'org.springframework.cloud.config.server.environment.MultipleJGitEnvironmentRepositoryFactory' that could not be found.

Action:

Consider defining a bean of type 'org.springframework.cloud.config.server.environment.MultipleJGitEnvironmentRepositoryFactory' in your configuration.

Process finished with exit code 1

上面是提示的错误,我自己写的demo和官方的github都报错
https://github.com/spring-cloud-samples/configserver

Spring Cloud是2020.0.1

Spring Boot是2.4.3

jdk1.8

我真是醉了,没想到官方的demo也会报错,有人知道啥原因吗?

后续解决过程:
过程中我发现Linux、Windows打包运行没问题,我的是Mac
后来我对比里面内容后,发现少了一个依赖,pom文件增加上就行,如下:

       <dependency>
            <groupId>org.eclipse.jgit</groupId>
            <artifactId>org.eclipse.jgit</artifactId>
            <version>5.1.7.201904200442-r</version>
        </dependency>

版本号还有要求,一开始我用最新的还不行。
至于深层次原因就不知道为啥了。

上边没给出具体的报错信息,只能简单排查一下

排查方法
1、这个demo已经很长时间没更新,
可能是demo的库版本不匹配或者jdk的版本和一些配置项早就进行了修改导致的,
详情可以看下2019年一个人提的issure:
https://github.com/spring-cloud-samples/configserver/issues/29

2、如果用的是远程拉取git仓库的方法来进行配置中心的更新的话,
那么需要确认几点

  • git的远程配置,是否配置正确,分支,秘钥之类的。
  • 如果部署在是在服务器,确认服务器可以访问远程git地址url,因为有时候防火墙会把这个端口关闭,记得开放端口,记得添加拉取秘钥,
    本地有时候因为有之前存储的秘钥导致可以直接拉取,而服务器却不可以

解决方法
对于前者,找个最新的demo和库的版本试一下
对于后者,请注意排查下git的仓库配置和服务器配置以及端口号是否开放

连接:
这个是spring cloud官方的配置文档,你可以看下里面的配置步骤和配置格式


如有问题及时沟通

原因是服务器/tmp目录下的缓存仓库被定期清理了,该问题在SpringCloud的官方文档也有说明

With VCS based backends (git, svn) files are checked out or cloned to the local filesystem.
By default they are put in the system temporary directory with a prefix of config-repo-.
On linux, for example it could be /tmp/config-repo-.
Some operating systems routinely clean out temporary directories.
This can lead to unexpected behaviour such as missing properties.
To avoid this problem, change the directory Config Server uses,
by setting spring.cloud.config.server.git.basedir or
spring.cloud.config.server.svn.basedir to a directory that does not reside in the system temp structure.

解决方法:在配置文件中修改缓存目录

在config的application.properties里添加配置:

spring.cloud.config.server.git.basedir=config-repo

把如图所示方框中的代码删掉试试

img