我要外部配置文件使用spring-boot应用程序。我保存在Amazon S3中的配置文件。如何编写代码以在项目中加载配置?谢谢
答案 0 :(得分:0)
这里https://github.com/spring-cloud/spring-cloud-config/issues/654处有一个开放的请求,要求将S3用作Spring Cloud Config的后端存储。 https://github.com/mwnorman/spring-cloud-config-server-s3
尝试通过样板代码来实现此目的在此处复制相关部分
它取决于spring-cloud-config-server和aws-java-sdk-s3并具有 META-INF / spring.factories中的自动配置类: org.springframework.boot.autoconfigure.EnableAutoConfiguration = \ org.springframework.cloud.config.s3.CloudConfigS3AutoConfiguration 看起来像
@Configuration @ConditionalOnBean(CloudConfigS3Configuration.Marker.class) @EnableConfigurationProperties(CloudConfigS3Properties.class) @Import({S3EnvironmentRepositoryConfiguration.class}) public class CloudConfigS3AutoConfiguration {}
S3EnvironmentRepositoryConfiguration类看起来像
@Configuration public class S3EnvironmentRepositoryConfiguration { @Autowired protected ConfigurableEnvironment environment; @Autowired protected CloudConfigS3Properties s3Properties; @Bean @Primary public EnvironmentRepository environmentRepository() { return new S3EnvironmentRepository(environment, s3Properties); } @Bean public CloudConfigS3Properties buildCloudConfigS3Properties() { return new CloudConfigS3Properties(); } }
一个简单的客户端:
@SpringBootApplication( //Hmm I think I shouldn't have to do this ... haven't figured it out yet exclude = { org.springframework.cloud.config.server.config.ConfigServerAutoConfiguration.class } ) @EnableS3CloudConfig public class Client { public static void main(String[] args) { ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder(Client.class) .web(false) .run(args) ; applicationContext.getEnvironment().getProperty("foo.bar"); } }
更多内容:https://github.com/spring-cloud/spring-cloud-config/issues/654#issuecomment-321004867