Как получить список файлов, управляемых Spring-Cloud-Config-Server - PullRequest
1 голос
/ 12 апреля 2019

У меня запущено 3 приложения Spring-Boot:

  1. Эврика: 8761
  2. Spring-Cloud-Config: 8080
  3. myMicroService: 8181

Для Spring-Cloud-Config я использую локальный git URI для заполнения данных. Локальное хранилище находится на ветке master и имеет такую ​​файловую структуру:

./myMicroService
  |-- application.properties
  |-- foo.txt
  |-- bar.txt

Согласно документации , я могу получить доступ к текстовым файлам следующим образом:

http://localhost:8080/myMicroService/default/master/foo.txt http://localhost:8080/myMicroService/default/master/bar.txt

Что работает, но как мне получить полный список доступных * .txt файлов, обслуживаемых сервером Spring-Cloud-Config?

Я пробовал это:

http://localhost:8080/myMicroService/default/master

, который возвращает только application.properties и его значения.

1 Ответ

1 голос
/ 15 апреля 2019

Учитывая, что для этого нет решения OOTB, я добавил новое сопоставление запросов на мой config-сервер:

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"
]
...