我遇到了同样令人讨厌的情况。
我将首先说明我是如何解决这种情况的,然后会强调我在以前的方法中所犯的错误。在我的上下文中,我正在使用application.properties
我的应用程序概述如下所示:我有一个集中的配置服务器并将相应的配置数据提供给相应的微服务。
例如,一个微服务“limits-service”需要一些配置数据,它从中央配置服务器(“spring-cloud-config-server”)获取。因此,为了实现这一点,“limits-service”查询中央配置服务器,该服务器又从远程 git 分支(“spring-cloud-samples”)获取请求的数据。
┌---------------------- <--> [currency-exchange-service]
[git] <--> [spring-cloud-config-server] ------- <--> [limits-service]
└---------------------- <--> [currency-conversion-service]
解决方案:
我只是为所有配置文件创建了一个新的 Git 存储库(spring-cloud-samples),这些配置文件将由多个微服务通过中央配置服务器使用。
在application.properties
中央配置服务器('spring-cloud-config-server')的文件中,我提供了一个属性:
spring.cloud.config.server.git.uri=https://github.com/{username}/{git-reponame.git}
这可能看起来像
spring.cloud.config.server.git.uri= https://github.com/alice123/spring-cloud-samples.git
就是这样!
在启动我的中央配置服务器并观察日志如下:
2020-02-17 05:25:07.867 INFO 15000 --- [nio-8888-exec-9] o.s.c.c.s.e.NativeEnvironmentRepository : Adding property source: file:/D:/Users/{userName}/AppData/Local/Temp/config-repo-3453413414/limits-service.properties
此外,我还通过我的“限制服务”消耗了配置数据,它就像一个魅力!
我尝试了以下导致失败和大量尖叫和大喊大叫的事情。XD
我发布它,只是为了让尝试相同概念的人可以节省一个晚上的工作,或者更少:p
1)最初,我在 central-config-server 的 application.properties 文件中使用我的 Git Repositories SSH URL,如下所示:
spring.cloud.config.server.git.uri=git@github.com:alice123/spring-cloud-samples.git
spring.cloud.config.server.git.username=alice123
spring.cloud.config.server.git.password=alice123Pwd
这导致了以下错误:
2020-02-17 05:22:45.091 WARN 15000 --- [nio-8888-exec-1] .c.s.e.MultipleJGitEnvironmentRepository : Error occured cloning to base directory.
org.eclipse.jgit.api.errors.TransportException: git@github.com:aniketrb-github/spring-cloud-samples.git: Auth fail
2)后来,我尝试从本机/本地 git 目录中读取配置数据,该目录指向我的 Git 远程分支(spring-cloud-samples)然后我在我的 application.properties 文件中提供了以下内容:
spring.cloud.config.server.git.uri=file://D:\aniket-workspace\github-ws\microservices\udemy-microservices\git-localconfig-repo
这崩溃了:
java.lang.IllegalStateException: Failed to load property source from location 'classpath:/application.properties'
3)后来经过足够的谷歌搜索,我将上面的属性更改为下面的工作!!:
spring.cloud.config.server.git.uri=D:\\\\alice-workspace\\\\github-ws\\\\microservices\\\\git-localconfig-repo
'limits-service' 在尝试从'spring.cloud.config.server' 获取配置数据时最终失败,并出现以下错误:
404: Not Found - "org.springframework.cloud.config.server.environment.NoSuchLabelException: No such label: master"
由于我上面提到的所有失败,我所说的解决方案对我有用。如果我错了,请纠正我并在必要时即兴创作。我希望这会有所帮助并节省一些关键时间。
参考:spring-cloud-config,https-or-ssh,git-ssh-windows,git-ssh-windows-troubleshooting