Spring Cloud AWS配置中心使用

前提

假设,已经拥有了AWS账户,并且这个账户已经开通了AWS Parameter Store和AWS Secrets Manager这两个服务;假设已经存在一个SpringBoot项目。假设开发运行的IDEA已经配置好了,AWS相关权限认证,即开发者的aws_access_key_id和aws_secret_access_key在本地已经配置好了。

目标

在SpringBoot项目中使用AWS的配置中心。这两个AWS配置中心,要求的应用程序必须能够访问互联网访问,才能够使用;不过,也可用通过endpoint在私有vpc通过内网访问到。

步骤

pom.xml

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-aws</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-aws-parameter-store-config</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-aws-secrets-manager-config</artifactId>
</dependency>

配置Spring Cloud AWS需要的相关依赖。

bootstrap.yml

cloud:
  aws:
    region:
      static: cn-north-1
      auto: false
    stack:
      auto: false
    instance:
      data:
        enabled: false
aws:
  paramstore:
    enabled: true
    defaultContext: application
    region: cn-north-1
    prefix: /config
    profileSeparator: '_'
  secretsmanager:
    enabled: true
    defaultContext: application
    region: cn-north-1
    prefix: /secret
    profileSeparator: '_'

这里禁用基于EC2元数据的自动区域发现功能,因为我们就一个区域,故将cloud.aws.region.auto设置为false。还禁用了stack和instance。
defaultContext配置paramstore和secretsmanager默认读取的配置;prefix设置了配置中心需要使用的前缀;profileSeparator设置了Spring Boot应用名与Spring Boot Profile使用下划线进行分割。

AWS Paramstore

AWS Parameter Store
application就是默认配置,api_test就是api这个应用在test这个profile下面需要读取的配置。AWS Secrets Manager也是类似,这里就不再展示了。

Spring中使用

在Spring中就可以正常使用value注解对配置进行读取了,类似如下代码:

    @Value("${omain}")
    private String omain;

总结

这就是Spring Cloud AWS配置中心的使用。注意这里idea需要使用到AWS Toolkit插件,对本地开发环境进行AWS认证Key管理。

参考:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值