有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

AmazonWeb服务java。网ConnectException:连接被拒绝(连接被拒绝)

我安装了一个AWS Elastic Beanstalk实例,其中Tomcat运行Java RESTful服务

enter image description here

然后,我还在AWS-RDS上设置了一个MySQL数据库实例

enter image description here

我有以下活动安全组,允许所有入站和出站流量

enter image description here

我可以使用MySQL Workbench连接到数据库:

enter image description here

这表明数据库还可以。所以我认为问题在于我的Java代码

在阅读this之后,我在我的Hibernate配置中设置了以下数据源(注释的代码在OpenShift服务器上工作)

    @Bean
    public DataSource dataSource() {
        // Openshift
//      String host = System.getenv("OPENSHIFT_MYSQL_DB_HOST");
//      String port = System.getenv("OPENSHIFT_MYSQL_DB_PORT");
//      String username = System.getenv("OPENSHIFT_MYSQL_DB_USERNAME");
//      String password = System.getenv("OPENSHIFT_MYSQL_DB_PASSWORD");

        // AWS
        String host = System.getenv("RDS_HOSTNAME");
        String port = System.getenv("RDS_PORT");
        String username = System.getenv("RDS_USERNAME");
        String password = System.getenv("RDS_PASSWORD");
        String dbname = System.getenv("RDS_DB_NAME");

        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName")); // jdbc.driverClassName=com.mysql.jdbc.Driver
        if (host == null) {
            dataSource.setUrl(environment.getRequiredProperty("jdbc.url")); 
            dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
            dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
        } else {
            //dataSource.setUrl("jdbc:mysql://" + host + ":" + port + "/");
            // Openshift
            //dataSource.setUrl("jdbc:mysql://" + host + ":" + port + "/" + environment.getRequiredProperty("jdbc.dbname"));
            // AWS
            dataSource.setUrl("jdbc:mysql://" + host + ":" + port + "/" + dbname);
            dataSource.setUsername(username);
            dataSource.setPassword(password);
            System.out.println("jdbc:mysql://" + host + ":" + port + "/" + dbname+", username: "+username+", password: "+password);
        }

        return dataSource;
    }

但是,当我尝试访问RESTful服务时,会得到以下结果:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection
    org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:431)
org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
java.net.ConnectException: Connection refused (Connection refused)

共 (1) 个答案

  1. # 1 楼答案

    解决方案:

    需要使用System.getProperty而不是System.getenv

        String host = System.getProperty("RDS_HOSTNAME");
        String port = System.getProperty("RDS_PORT");
        String username = System.getProperty("RDS_USERNAME");
        String password = System.getProperty("RDS_PASSWORD");
        String dbname = System.getProperty("RDS_DB_NAME");
    

    Java服务现在连接到数据库