Springboot引入Spring Cloud for AWS的设置中心(Parameter Store和Secrets ...

打印 上一主题 下一主题

主题 1898|帖子 1898|积分 5694

题目

现在手上有一个老Spring2.5.15项目,需要使用AWS Parameter Store作为设置中心服务。
思绪

引入这个Spring版本对应的Spring Cloud,然后再引入Spring Cloud AWS相关组件。然后,在AWS云上面准备好设置,然后,启动我当地Spring工程,验证有没有使用云端设置。
Spring Cloud引入


从Spring官网可知,我这个老版本的Spring2.5.15,只能选择Spring Cloud 2020.0.x的Cloud版本。详细依赖如下:
  1. <properties>
  2.     <spring-cloud.version>2020.0.6</spring-cloud.version>
  3. </properties>
  4. <dependencyManagement>
  5.     <dependencies>
  6.         <dependency>
  7.             <groupId>org.springframework.cloud</groupId>
  8.             <artifactId>spring-cloud-dependencies</artifactId>
  9.             <version>${spring-cloud.version}</version>
  10.             <type>pom</type>
  11.             <scope>import</scope>
  12.         </dependency>
  13.     </dependencies>
  14. </dependencyManagement>
复制代码
等候一段时间后,然后在引入Spring Cloud AWS集成依赖。
Spring Cloud AWS 依赖引入

  1. <properties>
  2.     <spring-cloud-aws.version>2.3.5</spring-cloud-aws.version>
  3. </properties>
  4. <dependencyManagement>
  5.     <dependencies>
  6.             <dependency>
  7.                 <groupId>io.awspring.cloud</groupId>
  8.                 <artifactId>spring-cloud-starter-aws-parameter-store-config</artifactId>
  9.                 <version>${spring-cloud-aws.version}</version>
  10.             </dependency>
  11.             <dependency>
  12.                 <groupId>io.awspring.cloud</groupId>
  13.                 <artifactId>spring-cloud-starter-aws-secrets-manager-config</artifactId>
  14.                 <version>${spring-cloud-aws.version}</version>
  15.             </dependency>
  16.                
  17.     </dependencies>
  18. </dependencyManagement>
复制代码
留意这里也有版本对应要求的,可以检察Spring Cloud AWS在github上面的开源项目介绍,详细如下图:

  1. <dependency>
  2.     <groupId>io.awspring.cloud</groupId>
  3.     <artifactId>spring-cloud-starter-aws-parameter-store-config</artifactId>
  4. </dependency>
  5. <dependency>
  6.     <groupId>io.awspring.cloud</groupId>
  7.     <artifactId>spring-cloud-starter-aws-secrets-manager-config</artifactId>
  8. </dependency>
复制代码
Parameter Store创建参数


这里以jdbc连接参数为例子。创建了一个连接参数。
Secrets Manager创建参数

下面设置数据库中用户名和暗码,如下图:

设置密钥,如下图:

Spring设置

  1. spring:
  2.   application:
  3.     name: api
  4.   config:
  5.     import:
  6.       - aws-parameterstore:/config/${spring.application.name}_${spring.profiles.active}
  7.       - aws-secretsmanager:/secret/${spring.application.name}_${spring.profiles.active}
  8. aws:
  9.   paramstore:
  10.           region: cn-north-1
  11.     defaultContext: ${spring.application.name}
  12.   secretsmanager:
  13.     region: cn-north-1
  14.     defaultContext: ${spring.application.name}
复制代码
留意,这里用到了spring.config.import,这种方式Spring设置是对原来的boot情况多设置文件的更换。
设置调试日记

  1. # 日志配置
  2. logging:
  3.   level:
  4.     io:
  5.       awspring:
  6.         cloud:
  7.           paramstore:
  8.             AwsParamStorePropertySource: debug
复制代码
测试验证

重启Spring工程,出现如下内容,阐明spring读到了设置:
  1. 11:02:13.555 [restartedMain] INFO  i.a.c.s.AwsSecretsManagerPropertySources - [logTo,255] - Loading secrets from AWS Secret Manager secret with name: /secret/api_local, optional: false
  2. 11:02:13.555 [restartedMain] INFO  i.a.c.p.AwsParamStorePropertySources - [logTo,255] - Loading property from AWS Parameter Store with name: /config/api_local, optional: false
  3. 11:02:13.555 [restartedMain] DEBUG i.a.c.p.AwsParamStorePropertySource - [logTo,252] - Populating property retrieved from AWS Parameter Store: .spring.datasource.druid.master.url
复制代码
类似如下效果:

总结

spring.config.import方式设置,是Spring为了支持云原生设置中心,反面参加新的设置方式,是对boot设置方式的更换。之前写过一篇文章(《Spring Cloud AWS设置中心使用》)就是使用Spring老的boot方式进行的设置。
参考



  • Spring Cloud
  • Spring Cloud2020最新版本
  • spring-cloud-aws
  • Externalized Configuration
  • Leveraging AWS Systems Manager Parameter Store for Spring Boot Configuration
  • Spring Cloud AWS设置中心使用

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

瑞星

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表