意图:我正在开发一个 POC,打算使用 AWS 参数存储作为属性存储。这会将 secret 应用程序属性存储在 AWS SSM 的参数存储中。我正在使用 Java 8 和 spring启动/云版本2。
资源:我关注了 this ref guide来自 Spring 文档 和
也是一篇综合文章Integrating the AWS Parameter Store with Spring Cloud .因此试图利用
spring-cloud-starter-aws-parameter-store-config.jar
因此在构建文件中添加了所需的依赖项。
预期输出:
实际输出:
这是来自 AWS 控制台的快照我正在尝试从 AWS 参数存储访问下面显示的参数
下面是我的 spring 属性文件:
application.yml
bootstrap.yml
我在 POM.xml 中使用具有以下依赖项的 Maven
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-aws-parameter-store-config</artifactId>
</dependency>
</dependencies>
我在这里错过了什么吗?如果有人已经遇到并解决了这个问题,请告诉我。
我可以从命令行输入和获取参数,但它只是无法让这个 java 库工作。
我正在尝试的示例的 GitHub 存储库 -
最佳答案
我检查了您的应用程序,它没有按预期工作,因为我有 ~.aws/config 文件,这导致 AWS 凭证配置错误(由 DefaultAWSCredentialsProviderChain
导致,了解更多 here ),所以我删除了它,然后再次尝试,但它失败了,说 spring 无法在 env 中找到 aws 区域,因此显然在 spring 从 AWS 参数存储加载属性之前,不会使用 application.yml 中指定的那些属性。
我是如何让它发挥作用的
我添加:
System.setProperty("aws.accessKeyId","My_Key");
System.setProperty("aws.secretKey","Secret");
System.setProperty("aws.region","us-east-1");//same region where all your params exist
在 SpringApplication.run(DemoApplication.class, args);
之前,然后它就起作用了。
将 aws.region
更改为另一个未定义参数值的区域时,我得到了与您完全相同的结果(空值)。
确保您的计算机或 EC2 实例上没有任何 aws 配置会覆盖您的应用中提供的配置。
关于java - 为什么将 Spring Cloud 应用程序与 AWS Parameter Store 集成不会从参数存储中返回任何属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54050702/