2017-02-22 343 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'提供的这些值被设置为系统的环境变量。看着你的代码的人不知道你是否将数据库设置保存在属性文件或环境变量中,以便代码对我们来说很好。它不是一个错误。 –

相关问题