2017-02-22 1008 views
0

我有一個AWS Elastic Beanstalk實例,其中Tomcat運行安裝的Java RESTful服務。java.net.ConnectException:連接被拒絕(連接被拒絕)

enter image description here

然後我也有一個MySQL數據庫實例設立AWS-RDS

enter image description here

我有以下活動安全小組,讓所有入站​​和出站流量。

enter image description here

我能夠連接到與MySQL Workbench中的數據庫:

enter image description here

這表明數據庫是好的。所以我認爲問題出在我的Java代碼上。

讀完this後,我在Hibernate Configuration(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) 
+0

看起來像昨天你問了[同樣的事情](http://stackoverflow.com/questions/42371803/aws-rds-how-to-set-up-a-mysql-database) – inquisitive

回答

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服務現在連接到數據庫。

+0

你可以使用'系統.getenv'提供的這些值被設置爲系統的環境變量。看着你的代碼的人不知道你是否將數據庫設置保存在屬性文件或環境變量中,以便代碼對我們來說很好。它不是一個錯誤。 –