我使用以下 Gradle 依赖项配置了一个非常简单的 Spring Boot 应用程序:
implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap'
想法是embedSpring Cloud Config Server到应用程序中。
bootstrap.yml
文件内容如下:
spring:
cloud:
config:
server:
bootstrap: true
git:
username: git
password: <personal-access-token>
uri: <github-uri>
远程 GitHub 存储库包含此
application.yml
文件:
server:
port: 8280
当我启动 Spring Boot 应用程序时,它会正确检索远程配置,因为日志包含以下内容:
Tomcat initialized with port(s): 8280 (http)
...
Tomcat started on port(s): 8280 (http) with context path ''
我的问题是:如果我在远程 GitHub 存储库中放入一个名为
dummy.txt
的文件,与 application.yml
处于同一级别,那么从应用程序本身检索其内容的编程方式是什么?
我知道使用分离的配置客户端和配置服务器,从配置客户端我可以依靠this从配置服务器检索纯文本文件。如何在应用程序中嵌入配置服务器时获得相同的结果?
到目前为止我得到的最好的解决方案是基于
org.springframework.cloud.config.server.resource.ResourceRepository
在课堂上注释为
@Service
/@Component
,..只需包括以下内容:
@Autowired private ResourceRepository resourceRepo;
接下来,假设默认配置文件名为
default
,并且配置存储在名为main
的分支中,以下代码行可以检索文件内容:
resourceRepo.findOne(null, "default", "main", "dummy.txt").getContentAsString(StandardCharsets.UTF_8)