Java – How to get a list of files managed by Spring-Cloud-Config-Server

How to get a list of files managed by Spring-Cloud-Config-Server… here is a solution to the problem.

How to get a list of files managed by Spring-Cloud-Config-Server

I have 3 Spring-Boot applications running:

  1. Eureka :8761
  2. Spring-Cloud-Config:8080
  3. My microservices: 8181

For Spring-Cloud-Config, I use a local git URI to populate the data. The local repository is located on the master branch and the file structure is as follows:

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

According to documentation , I can access the text file like this:

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

Which works, but how do I get a complete list of available *.txt files provided by the Spring-Cloud-Config server?

I’ve tried this :

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

It returns only application.properties and its value.

Solution

Since there is no OOTB solution for this, I added a new request mapping to my configuration server:

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());
    }
  }
}

Get a list of files for myMicroService:

curl http://localhost:8888/myMicroService/default/master/listFiles

Result:

[
   "http://localhost:8888/myMicroService/default/master/application.properties",
   "http://localhost:8888/myMicroService/default/master/foo.txt",
   "http://localhost:8888/myMicroService/default/master/bar.txt"
]

Related Problems and Solutions