Spring Boot无法解析AWS参数存储变量

时间:2020-03-16 19:13:19

标签: spring amazon-web-services spring-boot spring-cloud aws-ssm

我正在尝试将秘密保存到Spring Cloud中。我有从Spring Cloud读取微服务的配置,但无法解析参数存储中的值。例如,安全的db用户并通过。

password = $ {/ config / password},但路径未解析

我添加了Maven依赖项

spring-cloud-starter-aws-parameter-store-config

欢迎任何想法

1 个答案:

答案 0 :(得分:0)

也许您缺少boostrap.yaml文件?它应该在您的/src/main/resources/文件夹中,看起来像这样:

aws:
  paramstore:
    name: your-application-name
    default-context: application
    profile-separator: _

此外,我认为默认查找路径为/config/application,因此您可能希望将参数放在/config/application/password处。由于它可能特定于某个应用程序,因此您可能想要像/config/your-application-name_${environmentName}

您还需要正确设置Spring Cloud依赖关系。我在Gradle文件中有我的文件。这是我的整个Gradle文件,供您参考。

buildscript {
    ext {
        springBootVersion = '2.0.6.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'idea'
apply plugin: 'groovy'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

springBoot {
    mainClassName 'com.myapi.Application'
}

group = 'com.myapi'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

jar {
    baseName = 'my-api'
    version = 1.0
}

repositories {
    mavenCentral()
}

ext {
    springCloudVersion = 'Finchley.RELEASE'
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

dependencies {
    implementation "org.springframework.boot:spring-boot-starter-security:${springBootVersion}"
    implementation "org.springframework.boot:spring-boot-starter-web:${springBootVersion}"

    //AWS Param store configuration
    implementation 'com.amazonaws:aws-java-sdk-core:1.11.477'
    implementation 'org.springframework.cloud:spring-cloud-starter-aws-parameter-store-config'
    implementation 'com.amazonaws:aws-java-sdk-ssm:1.11.475'
    implementation 'org.springframework.boot:spring-boot-starter-actuator'

    implementation 'org.springframework.cloud:spring-cloud-aws-messaging:2.0.1.RELEASE'        //AWS SNS configuration
    implementation 'org.springframework.boot:spring-boot-configuration-processor'              //Necessary for the @ConfigurationProperties tag
    implementation 'org.springframework.cloud:spring-cloud-aws-autoconfigure:2.0.1.RELEASE'    //Necessary for auto wiring AmazonSNS
    
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.codehaus.groovy:groovy-all:2.4.15'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-rest'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'com.github.derjust:spring-data-dynamodb:5.0.4'

    //Swagger
    implementation 'io.springfox:springfox-swagger2:2.9.2'
    implementation 'io.springfox:springfox-swagger-ui:2.9.2'

    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.springframework.security:spring-security-test'
    testImplementation 'org.spockframework:spock-core:1.1-groovy-2.4'
    testImplementation 'org.jsoup:jsoup:1.12.1'
}