Java 如何使用Vagrant连接两个VM(postgreSQL和tomee)?

Java 如何使用Vagrant连接两个VM(postgreSQL和tomee)?,java,linux,postgresql,vagrant,apache-tomee,Java,Linux,Postgresql,Vagrant,Apache Tomee,我正在尝试用Virtualbox和Vagrant构建一个虚拟环境。 目标是能够从pc浏览器访问运行在虚拟机“服务器”上的应用程序,该应用程序与虚拟机“db”交互 我设法创建了虚拟机 第一个是运行ApacheTomee,我可以通过浏览器访问它。它工作正常 第二个是运行Postgres,我可以通过浏览器访问它。它也能正常工作 当我尝试将VM“服务器”与“db”连接时,我收到一个http 500错误(仅当我尝试应用程序的某些db操作时),特别是: "Connection to localhost:22

我正在尝试用Virtualbox和Vagrant构建一个虚拟环境。 目标是能够从pc浏览器访问运行在虚拟机“服务器”上的应用程序,该应用程序与虚拟机“db”交互

我设法创建了虚拟机

第一个是运行ApacheTomee,我可以通过浏览器访问它。它工作正常

第二个是运行Postgres,我可以通过浏览器访问它。它也能正常工作

当我尝试将VM“服务器”与“db”连接时,我收到一个http 500错误(仅当我尝试应用程序的某些db操作时),特别是: "Connection to localhost:2222 refused. Check that the hostname and port 在apache tomee/conf中的tomee.xml文件中,我为测试中使用的db连接指定了相同的代码(在虚拟环境中正常工作)


JdbcDriver org.postgresql.Driver
JdbcUrl jdbc:postgresql://localhost:2222/music
用户名postgres
密码postgres
真的吗
默认自动提交错误
在db VM上,我修改了postgresql.conf和pg_hba.conf,添加了

侦听地址='*'

一开始

托管所有0.0.0.0/0 md5

在第二个。我不明白为什么当tomee在我的pc上运行时,我可以访问两个VM,但“服务器”不能与“db”通信

这是db VM上“sudo netstat-tulpn | grep postgres”的输出

tcp 0.0.0.0:5432 0.0.0.0:*收听5743/postgres

tcp6 0:::5432::*侦听5743/postgres

localhost
更改为托管VM的计算机的IP(您可以从主机为来宾进行端口转发)。或者使用并直接从客户端连接到服务器。

我尝试过这样做,但错误大致相同。(如果您参考tomee.xml文件)。>“到10.12.1.201:2222的连接被拒绝。请检查主机名和端口。如果您直接连接到来宾,请使用真实端口;转发端口用于连接到主机(运行您的虚拟箱的计算机)。 are correct and that the postmaster is accepting TCP/IP connections. javax.faces.webapp.FacesServlet.service(FacesServlet.java:229)"
config.vm.define "server" do |node|
    node.vm.hostname = "server"    
    node.vm.network "private_network", ip: "10.12.1.101",       virtualbox__intnet: true

  [...]

    node.vm.network "forwarded_port", guest: 22, host: 2211, id: 'ssh', auto_correct: true
    node.vm.network "forwarded_port", guest: 8080, host: 2212

    node.ssh.forward_agent = true

[...]
 end

config.vm.define "client" do |node|
    node.vm.hostname = "client"    
    node.vm.network "private_network", ip: "10.12.1.201", virtualbox__intnet: true

   [...]

    node.vm.network "forwarded_port", guest: 22, host: 2221, id: 'ssh', auto_correct: true
    node.vm.network "forwarded_port", guest: 5432, host: 2222

end
<tomee>
  <Resource id="PostgreSQL Database" type="DataSource">
          JdbcDriver   org.postgresql.Driver
      JdbcUrl  jdbc:postgresql://localhost:2222/music
      UserName     postgres
      Password     postgres
      JtaManaged      true
      DefaultAutoCommit   false
    </Resource>
</tomee>