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

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.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.xdnf.cn/news/672499.html

相关文章:

  • RK3568DAYU开发板-平台驱动开发:ADC驱动
  • 火柴INIBOX专业矿机登场,碾压现有Initverse挖矿设备
  • Java构建Tree并实现节点名称模糊查询
  • C 语言学习笔记(结构体1)
  • STM32的DMA入门指南:让单片机学会“自动搬运“数据
  • 【Day38】
  • C语言_文件操作
  • 【深度学习-Day 18】从SGD到Adam:深度学习优化器进阶指南与实战选择
  • 【JavaSE】枚举和注解学习笔记
  • 高考加油(Python+HTML)
  • 青少年编程与数学 02-020 C#程序设计基础 07课题、控制结构
  • Android设置界面层级为最上层实现
  • 【数据集】EarthExplore下载Landsat LST 数据
  • Java网络编程性能优化
  • 【Vue Vapor Mode :技术突破与性能优化的可能性】
  • 嵌入式学习--江协stm32day1
  • 第9章:网络协议栈与 IoT 通信
  • React 第四十七节 Router 中useLinkClickHandler使用详解及开发注意事项案例
  • React 第四十八节 Router中 useMatch 的使用详细介绍及案例分析
  • React---day2
  • 微服务及容器化设计--可扩展的架构设计
  • Python 中的 for 循环:从基础到高级应用的全面解析
  • WPF【09】WPF基础入门 (三层架构与MVC架构)
  • 沈阳城市空气质量综合评价系统/答辩以及详细讲解
  • 基于cornerstone3D的dicom影像浏览器 第二十四章 显示方位、坐标系、vr轮廓线
  • Python requests
  • App Runner和Task Pipeline中的数据库连接管理指南
  • 【数据结构】树形结构--二叉树
  • U-Boot ARMv8 平台异常处理机制解析
  • Android studio 查看aar源码出现/* compiled code */