简体   繁体   English

如何从 AWS 参数存储中获取数据库 url、凭据到我的 spring-boot 服务中,以测试数据库连接性?

[英]How to fetch Database url, credentials from AWS parameter store, into my spring-boot service, to test database connectivity?

Database URL and database password are stored in the AWS parameter store.数据库 URL 和数据库密码存储在 AWS 参数存储中。 I have a service in spring-boot, and I need to write a function to test database connectivity.我在spring-boot中有一个服务,我需要写一个函数来测试数据库的连通性。

For this function, I need to fetch the database URL, and credentials from the AWS parameter store.对于此功能,我需要从 AWS 参数存储中获取数据库 URL 和凭证。 How do I fetch these parameters from the AWS parameter store and pass them as arguments to my function?如何从 AWS 参数存储中获取这些参数并将它们作为参数传递给我的函数?

I've written my function which will test the database connectivity.我已经编写了我的函数来测试数据库连接。 Here, the URL, Username, and password for the database I need to test connectivity, are present in the AWS parameter store.在这里,我需要测试连接性的数据库的 URL、用户名和密码存在于 AWS 参数存储中。 I'm unable to fetch those and pass them to my function.我无法获取这些并将它们传递给我的函数。

HikariConfig config = new HikariConfig();
config.setJdbcUrl(url);
config.setUsername(username);
config.setPassword(password);

HikariDataSource ds = new HikariDataSource(config);

try (Connection conn = ds.getConnection()) {
  // Execute a simple SQL query to test the connection
  Statement stmt = conn.createStatement();
  ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM users");
  if (rs.next()) {
    int count = rs.getInt(1);
    System.out.println("Number of users: " + count);
  }
}

You can use AWS Spring Cloud to get values from SSM params.您可以使用 AWS Spring Cloud 从 SSM 参数中获取值。 For that, use Spring Cloud framework-based configuration management.为此,请使用基于 Spring Cloud 框架的配置管理。 bootstrap.properties file has the default configuration required for Spring Cloud. bootstrap.properties 文件具有 Spring Cloud 所需的默认配置。

cloud.aws.credentials.instanceProfile=false
cloud.aws.credentials.useDefaultAwsCredentialsChain=true
cloud.aws.stack.auto=false
cloud.aws.region.auto=false
cloud.aws.region.static=us-east-1

aws.paramstore.prefix=?
aws.paramstore.defaultContext=application
aws.paramstore.profileSeparator=?
aws.paramstore.failFast=true
aws.paramstore.name=?
aws.paramstore.enabled=true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM