gpt4 book ai didi

amazon-web-services - RDS 环境变量对 AWS 上的 Elastic Beanstalk 实例不可用?

转载 作者:行者123 更新时间:2023-12-04 13:03:27 33 4
gpt4 key购买 nike

使用 RDS 创建 Java 8 Elastic Beanstalk 实例后,RDS 连接详细信息作为环境变量不可见(它们在其他正在运行的实例上可见)。

运行后printenv命令,期望这些值可用,但它们不是。

RDS_HOSTNAME=foo.com

RDS_USERNAME=foo

RDS_PASS=bar



这些是服务器配置所必需的
database:
driverClass: com.mysql.jdbc.Driver
user: ${RDS_USERNAME}
password: ${RDS_PASSWORD}
url: jdbc:mysql://${RDS_HOSTNAME}/${RDS_DB_NAME}

在应用程序触发期间,它们不可用,日志显示 Java 异常,无法找到环境变量。

io.dropwizard.configuration.UndefinedEnvironmentVariableException: The environment variable 'RDS_USERNAME' is not defined; could not substitute the expression '${RDS_USERNAME}'. at io.dropwizard.configuration.EnvironmentVariableLookup.lookup(EnvironmentVariableLookup.java:41) at org.apache.commons.lang3.text.StrSubstitutor.resolveVariable(StrSubstitutor.java:934) at org.apache.commons.lang3.text.StrSubstitutor.substitute(StrSubstitutor.java:855) at org.apache.commons.lang3.text.StrSubstitutor.substitute(StrSubstitutor.java:743) at org.apache.commons.lang3.text.StrSubstitutor.replace(StrSubstitutor.java:403) at io.dropwizard.configuration.SubstitutingSourceProvider.open(SubstitutingSourceProvider.java:39) at io.dropwizard.configuration.BaseConfigurationFactory.build(BaseConfigurationFactory.java:83) at io.dropwizard.cli.ConfiguredCommand.parseConfiguration(ConfiguredCommand.java:124) at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:72) at io.dropwizard.cli.Cli.run(Cli.java:75) at io.dropwizard.Application.run(Application.java:93)



但是,如果我在 ec2 实例上运行以下命令
sudo /opt/elasticbeanstalk/bin/get-config environment

它以 JSON 格式打印值:
{"CONFIG":"dev.yml","RDS_HOSTNAME":"foo.com","RDS_PASSWORD":"foo","M2":"/usr/local/apache-maven/bin","M2_HOME":"/usr/local/apache-maven","RDS_DB_NAME":"foo","JAVA_HOME":"/usr/lib/jvm/java","RDS_USERNAME":"foo","GRADLE_HOME":"/usr/local/gradle","RDS_PORT":"3306"}

任何想法如何为 ec2-user 恢复这些值?

我试过了:
  • 重启EB实例
  • 重建实例
  • cat将值放入脚本中,将它们设置在 eb deploy 之后

  • 任何想法,为什么它们在这个特定实例上不可见?

    实例详情
      Environment details foo: foo-service
    Application name: foo-service
    Region: eu-west-2
    Platform: arn:aws:elasticbeanstalk:eu-west-2::platform/Java 8 running on 64bit Amazon Linux/2.6.0
    Tier: WebServer-Standard

    最佳答案

    我会运行 EB 环境更新和/或替换实例。或者您可以移至 "Storing the Connection String in Amazon S3"

    When the environment update is complete, the DB instance's hostname and other connection information are available to your application through the following environment properties:

    RDS_HOSTNAME – The hostname of the DB instance. Amazon RDS console label – Endpoint (this is the hostname)

    RDS_PORT – The port on which the DB instance accepts connections. The default value varies between DB engines. Amazon RDS console label – Port

    RDS_DB_NAME – The database name, ebdb. Amazon RDS console label – DB Name

    RDS_USERNAME – The user name that you configured for your database. Amazon RDS console label – Username

    RDS_PASSWORD – The password that you configured for your database.


    private static Connection getRemoteConnection() {
    if (System.getenv("RDS_HOSTNAME") != null) {
    try {
    Class.forName("org.postgresql.Driver");
    String dbName = System.getenv("RDS_DB_NAME");
    String userName = System.getenv("RDS_USERNAME");
    String password = System.getenv("RDS_PASSWORD");
    String hostname = System.getenv("RDS_HOSTNAME");
    String port = System.getenv("RDS_PORT");
    String jdbcUrl = "jdbc:postgresql://" + hostname + ":" + port + "/" + dbName + "?user=" + userName + "&password=" + password;
    logger.trace("Getting remote connection with connection string from environment variables.");
    Connection con = DriverManager.getConnection(jdbcUrl);
    logger.info("Remote connection successful.");
    return con;
    }
    catch (ClassNotFoundException e) { logger.warn(e.toString());}
    catch (SQLException e) { logger.warn(e.toString());}
    }
    return null;
    }

    https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/java-rds.html#java-rds-javase

    关于amazon-web-services - RDS 环境变量对 AWS 上的 Elastic Beanstalk 实例不可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47537508/

    33 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com