如何获取 Spring-Cloud-Config-Server 管理的文件列表
Posted
技术标签:
【中文标题】如何获取 Spring-Cloud-Config-Server 管理的文件列表【英文标题】:How to get list of files managed by Spring-Cloud-Config-Server 【发布时间】:2019-09-03 14:00:29 【问题描述】:我有 3 个 Spring-Boot 应用程序正在运行:
-
尤里卡:8761
Spring-Cloud-Config:8080
我的微服务:8181
对于 Spring-Cloud-Config,我使用本地 git URI 来填充数据。本地 repo 位于分支 master
上,文件结构如下:
./myMicroService
|-- application.properties
|-- foo.txt
|-- bar.txt
根据documentation,我可以像这样访问文本文件:
http://localhost:8080/myMicroService/default/master/foo.txt http://localhost:8080/myMicroService/default/master/bar.txt
哪种方法可行,但如何获取 Spring-Cloud-Config 服务器提供的可用 *.txt 文件的完整列表?
我试过了:
http://localhost:8080/myMicroService/default/master
仅返回 application.properties
及其值。
【问题讨论】:
现在没有办法做到这一点。 【参考方案1】:之前的解决方案假设您的 spring.cloud.config.server.git.uri 是 https 类型。下面的解决方案将引用包含已从 repo 中提取的文件的本地文件系统,并且适用于任何 repo 类型。端点还接受搜索路径并采用以下格式:
/name/profile/label/path/listFiles
/name/profile/path/listFiles?useDefaultLabel
@Configuration
@EnableAutoConfiguration
@EnableConfigServer
@RestController
public class CloudConfigApplication
private UrlPathHelper helper = new UrlPathHelper();
private SearchPathLocator service;
public static void main(String[] args)
SpringApplication.run(CloudConfigApplication.class, args);
public CloudConfigApplication(SearchPathLocator service)
this.service = service;
@RequestMapping("/name/profile/label/**/listFiles")
public List<String> retrieve(@PathVariable String name, @PathVariable String profile,
@PathVariable String label, ServletWebRequest request,
@RequestParam(defaultValue = "true") boolean resolvePlaceholders)
throws IOException
String path = getDirPath(request, name, profile, label);
return listAll(request, name, profile, label, path);
@RequestMapping(value = "/name/profile/**/listFiles", params = "useDefaultLabel")
public List<String> retrieve(@PathVariable String name, @PathVariable String profile,
ServletWebRequest request,
@RequestParam(defaultValue = "true") boolean resolvePlaceholders)
throws IOException
String path = getDirPath(request, name, profile, null);
return listAll(request, name, profile, null, path);
private String getDirPath(ServletWebRequest request, String name, String profile, String label)
String stem;
if (label != null)
stem = String.format("/%s/%s/%s/", name, profile, label);
else
stem = String.format("/%s/%s/", name, profile);
String path = this.helper.getPathWithinApplication(request.getRequest());
path = path.substring(path.indexOf(stem) + stem.length()).replace("listFiles", "");
return path;
public synchronized List<String> listAll(ServletWebRequest request, String application, String profile, String label, String path)
if (StringUtils.hasText(path))
String[] locations = this.service.getLocations(application, profile, label).getLocations();
List<String> fileURIs = new ArrayList<>();
try
int i = locations.length;
while (i-- > 0)
String location = String.format("%s%s", locations[i], path).replace("file:", "");
Path filePath = new File(location).toPath();
if(Files.exists(filePath))
fileURIs.addAll(Files.list(filePath).filter(file -> !Files.isDirectory(file)).map(file ->
String URL =
String.format(
"%s://%s:%d%s/%s/%s/%s%s?useDefaultLabel",
request.getRequest().getScheme(), request.getRequest().getServerName(),
request.getRequest().getServerPort(), request.getRequest().getContextPath(),
application, profile, path,
file.getFileName().toString());
if(label != null)
URL = String.format(
"%s://%s:%d%s/%s/%s/%s/%s%s",
request.getRequest().getScheme(), request.getRequest().getServerName(),
request.getRequest().getServerPort(), request.getRequest().getContextPath(),
application, profile, label, path,
file.getFileName().toString());
return URL;
).collect(Collectors.toList()));
catch (IOException var11)
throw new NoSuchResourceException("Error : " + path + ". (" + var11.getMessage() + ")");
return fileURIs;
throw new NoSuchResourceException("Not found: " + path);
获取列表:
curl http://localhost:8080/context/appName/profile/label/some/path/listFiles
示例结果:
[
"http://localhost:8080/context/appName/profile/label/some/path/robots.txt"
]
【讨论】:
【参考方案2】:鉴于没有针对此的 OOTB 解决方案,我向我的配置服务器添加了一个新的请求映射:
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
@RestController
public class ConfigServer
public static void main(String[] args)
SpringApplication.run(ConfigServer.class, args);
@Value("$spring.cloud.config.server.git.uri")
private String uri;
@Autowired private ResourceLoader resourceLoader;
@GetMapping("/name/profile/label/listFiles")
public Collection<String> retrieve(
@PathVariable String name,
@PathVariable String profile,
@PathVariable String label,
HttpServletRequest request)
throws IOException
Resource resource = resourceLoader.getResource(uri);
String uriPath = resource.getFile().getPath();
Path namePath = Paths.get(uriPath, name);
String baseUrl =
String.format(
"http://%s:%d/%s/%s/%s",
request.getServerName(), request.getServerPort(), name, profile, label);
try (Stream<Path> files = Files.walk(namePath))
return files
.map(Path::toFile)
.filter(File::isFile)
.map(File::getName)
.map(filename -> baseUrl + "/" + filename)
.collect(Collectors.toList());
获取 myMicroService 的文件列表:
curl http://localhost:8888/myMicroService/default/master/listFiles
结果:
[
"http://localhost:8888/myMicroService/default/master/application.properties",
"http://localhost:8888/myMicroService/default/master/foo.txt",
"http://localhost:8888/myMicroService/default/master/bar.txt"
]
【讨论】:
以上是关于如何获取 Spring-Cloud-Config-Server 管理的文件列表的主要内容,如果未能解决你的问题,请参考以下文章
在 Docker 中将 Spring-cloud-config 服务器托管为微服务
spring-cloud-config——Quick Start
问题 spring-cloud-config 和 spring-cloud-bus