在我的 Spring Boot 应用程序中,我在 application.properties 文件中维护了 spring.datasource.url 。我需要保护 URL/用户和密码,并且这些值可在 AWS 参数存储中找到。我已经引用了这个doc只需添加对 (spring-cloud-starter-aws-parameter-store-config) 启动模块的依赖即可激活支持。我实际上不知道如何维护配置参数以及如何将参数存储值检索到我的项目中。
如何在不使用以下 (application.properties) 属性中的硬编码值的情况下实现此目的?
spring.datasource.url= url
spring.datasource.username = root
spring.datasource.password = xxxxxx
最佳答案
要在 Spring Boot 应用程序中使用 AWS System Manager Parameter Store 作为 PropertySource
,请执行以下步骤。
将以下依赖项添加到build.gradle
:
dependencyManagement {
imports {
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Greenwich.SR3'
}
}
dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-aws-parameter-store-config'
}
将指定应用程序名称的属性添加到src/main/resources/bootstrap.properties
:
spring.application.name=my-app
默认情况下 Spring Cloud AWS 依赖 DefaultAWSCredentialsProviderChain和 DefaultAwsRegionProviderChain .
如果应用程序不在 EC2/Fargate 实例上运行,请通过设置以下环境变量来配置 AWS 凭证和区域:
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_REGION=us-west-1
或者确保您拥有有效的 AWS 配置文件 ~/.aws/credentials
和 ~/.aws/config
。
在 AWS System Manager Parameter Store 中定义以下属性:
/config/my-app/spring.datasource.url
/config/my-app/spring.datasource.username
/config/my-app/spring.datasource.password
关于java - 如何从AWS参数存储获取Spring数据源属性值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58949030/