Spring cloud config - 共享文件或他的位置

问题描述 投票:0回答:5

我有两个应用程序:

  • 一个spriig启动配置服务器
  • 另一个是spring boot配置客户端

客户端必须使用名为certificate.json的文件。

我想将此文件存储在服务器端,因此需要它的另一个微程序和我的客户端程序可以从服务器端检索它。

我试着这样:

  • 将文件certificate.json复制到classpath:/ config
  • 将此行添加到application.properties: certificate.location:classpath:config / certificate.json
  • 通过以下方式调用客户端程序的值: @Value(“$ {certificate.location}”)私有字符串证书位置;

但certificateLocation的值是classpath:config / certificate.json。我想要的值是文件位置,如:/home/user/project/scr/main/resources/config/certificate.json。

或者,有没有办法通过URI直接检索我的文件,例如localhost:8889 / ...(8889是我的配置服务器端口)。

编辑1:我不能使用服务器的绝对路径,因为我不是运行它的人。

先感谢您。

java spring-cloud spring-cloud-stream spring-cloud-config
5个回答
2
投票

我会做

@Value("${certificate.location}") private Resource certificateLocation;

这样,你的位置已经是你可以加载的Resource,调用getURI()getFile().getAbsolutePath()等。由于你的值以classpath:为前缀,你将拥有一个ClassPathResource实例,你可以使用它来做很多事情。


1
投票

classpath只是URL的协议部分。您可以使用file或任何其他支持的协议。例如,file://home/user/project/scr/main/resources/config/certificate.json


1
投票

Try this url

spring.cloud.config.server.native.searchLocations
=file://${user.home}/CentralRepo/
SPRING_PROFILES_ACTIVE=native
I am also get this way using microservice

1
投票

OP您需要创建一个Web服务并返回certificate.json作为响应。然后,您可以通过向https://12.12.12.12:8080/getCertificate等URI发送GET或POST请求来发送和接收数据。请参阅:

https://spring.io/guides/gs/rest-service/

作为旁注,您不应该将您的内部文件暴露给互联网,因为它非常不安全,并且会让您受到盗版和黑客攻击。


1
投票

在这种情况下我会做什么

  1. 添加静态文件夹的配置 @SpringBootApplication public class DemoStaticresourceApplication extends WebMvcConfigurerAdapter { public static void main(String[] args) { SpringApplication.run(DemoStaticresourceApplication.class, args); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/files/**").addResourceLocations("file:/fileFolder/") .setCachePeriod(0); } }
  2. 将certificate.json放在文件夹中并尝试使用qazxsw poop,它将直接从文件系统中为您提供。

现在,只有在使用/ files / path调用时,才会提供在此文件夹中添加的每个文件

© www.soinside.com 2019 - 2024. All rights reserved.