当前位置: 首页 > article >正文

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

问题

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

思路

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

Spring Cloud引入

Spring Cloud版本选择
从Spring官网可知,我这个老版本的Spring2.5.15,只能选择Spring Cloud 2020.0.x的Cloud版本。具体依赖如下:

<properties><spring-cloud.version>2020.0.6</spring-cloud.version>
</properties>
<dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies>
</dependencyManagement>

等待一段时间后,然后在引入Spring Cloud AWS集成依赖。

Spring Cloud AWS 依赖引入

<properties><spring-cloud-aws.version>2.3.5</spring-cloud-aws.version>
</properties>
<dependencyManagement><dependencies><dependency><groupId>io.awspring.cloud</groupId><artifactId>spring-cloud-starter-aws-parameter-store-config</artifactId><version>${spring-cloud-aws.version}</version></dependency><dependency><groupId>io.awspring.cloud</groupId><artifactId>spring-cloud-starter-aws-secrets-manager-config</artifactId><version>${spring-cloud-aws.version}</version></dependency></dependencies>
</dependencyManagement>

注意这里也有版本对应要求的,可以查看Spring Cloud AWS在github上面的开源项目介绍,具体如下图:
Spring Cloud AWS版本选择

<dependency><groupId>io.awspring.cloud</groupId><artifactId>spring-cloud-starter-aws-parameter-store-config</artifactId>
</dependency><dependency><groupId>io.awspring.cloud</groupId><artifactId>spring-cloud-starter-aws-secrets-manager-config</artifactId>
</dependency>

Parameter Store创建参数

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

Secrets Manager创建参数

下面设置数据库中用户名和密码,如下图:
secrets设置
配置密钥,如下图:
设置secrets名字

Spring配置

spring:application:name: apiconfig:import:- aws-parameterstore:/config/${spring.application.name}_${spring.profiles.active}- aws-secretsmanager:/secret/${spring.application.name}_${spring.profiles.active}
aws:paramstore:region: cn-north-1defaultContext: ${spring.application.name}secretsmanager:region: cn-north-1defaultContext: ${spring.application.name}

注意,这里用到了spring.config.import,这种方式Spring配置是对原来的boot环境多配置文件的替代。

配置调试日志

# 日志配置
logging:level:io:awspring:cloud:paramstore:AwsParamStorePropertySource: debug

测试验证

重启Spring工程,出现如下内容,说明spring读到了配置:

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
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
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工程没有启用boot方式。所以在Spring源代码里面使用@Value("${xxx.xxx}")方式配置无法读到Parameter Store的配置,如果想要读到Parameter Store里面的数据需要使用@ConfigurationProperties("xxx")这种JavaBean方式配置。@Value("${xxx.xxx}")方式配置在 Secrets Manager仍旧生效。@ConfigurationProperties("xxx")配置如下:

@ConfigurationProperties("bc")
public class MyProperties {private boolean name;
...
}

按Spring文档的意思,就是@Value方式,我们得少用,我们多用@ConfigurationProperties(“xxx”)方式。
这里简单的说就是Parameter Store不支持@Value注解。

总结

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配置中心使用
http://www.lryc.cn/news/2387541.html

相关文章:

  • 打破云平台壁垒支持多层级JSON生成的MQTT网关技术解析
  • Modbus通信中的延迟和时间间隔详解
  • maven 最短路径依赖优先
  • SAAS架构设计2-流程图-用户与租户之间对应关系图
  • TypeScript入门到精通
  • 三、Docker目录挂载、卷映射、网络
  • 迪米特法则 (Law of Demeter, LoD)
  • 【R语言编程绘图-函数篇】
  • 训练自己的yolo模型,并部署到rk3588上
  • Leetcode 3556. Sum of Largest Prime Substrings
  • 以少学习:通过无标签数据从大型语言模型进行知识蒸馏
  • 鸿蒙OSUniApp 实现带有滑动删除的列表#三方框架 #Uniapp
  • Qt qml Network error问题
  • Prompt工程:解锁大语言模型的终极密钥
  • Spring Boot微服务架构(六):伪装的微服务有哪些问题?
  • 恶意npm与VS Code包窃取数据及加密货币资产
  • Matlab快速上手五十六:详解符号运算里假设的用法,通过假设可以设置符号变量的取值范围,也可以通过假设设置变量属于集合:整数、正数和实数等
  • 机器学习笔记【Week1】
  • 什么是3D全景视角?3D全景有什么魅力?
  • 【Mini-F5265-OB开发板试用测评】按键控制测试
  • Debian重装系统后
  • 每日Prompt:古花卷
  • [学习]C语言指针函数与函数指针详解(代码示例)
  • 夏季用电高峰如何防患于未“燃”?电力测温技术守护城市生命线
  • 浙大版《Python 程序设计》题目集6-3,6-4,6-5,6-6列表或元组的数字元素求和及其变式(递归解法)
  • Leetcode 3563. Lexicographically Smallest String After Adjacent Removals
  • 【创造型模式】抽象工厂方法模式
  • 一台手机怎样实现多IP上网?方法有多种
  • 【FFmpeg+SDL】播放音频时,声音正常但是有杂音问题(已解决)
  • Linux 527 重定向 2>1 rsync定时同步(未完)