Vagrant Machines - Errno :: ECONNREFUSED(无法打开到localhost的TCP连接:3001

时间:2018-01-26 15:53:35

标签: vagrant

我目前正在建立一个开发环境,其中有两台流浪汉机器。第一个托管网站等,而第二个托管网站调用的API。目前我的vagrantfiles如下:

API

Vagrant.configure("2") do |config|
  config.vm.box = "hashicorp/precise64"
  config.vm.network "forwarded_port", guest: 3001, host: 3001
end

网站

Vagrant.configure("2") do |config|
  config.vm.box = "hashicorp/precise64"
  config.vm.network "forwarded_port", guest: 3000, host: 3000
end

当我尝试加载调用API的网站时,我得到:

Errno::ECONNREFUSED (Failed to open TCP connection to localhost:3001 (Connection refused - connect(2) for "localhost" port 3001)):

我知道有多机器方法,但由于我已经为每个项目设置了单独的环境,我想知道是否有任何方法让我允许两者之间的连接。我已经考虑过更改托管网站的流浪汉机器上的hosts文件,但不确定我应该做什么。

1 个答案:

答案 0 :(得分:2)

您需要将两个虚拟机置于同一个专用网络中,Website虚拟机将能够通过它访问API虚拟机:

config.vm.network "private_network", ip: "<ip_address>"

例如,对于您的情况:

API VM:

Vagrant.configure("2") do |config|
  config.vm.box = "hashicorp/precise64"
  config.vm.network "forwarded_port", guest: 3001, host: 3001
  config.vm.network "private_network", ip: "192.168.200.3"
end

网站虚拟机:

Vagrant.configure("2") do |config|
  config.vm.box = "hashicorp/precise64"
  config.vm.network "forwarded_port", guest: 3000, host: 3000
  config.vm.network "private_network", ip: "192.168.200.2"
end

现在,Website虚拟机可以API上的192.168.200.3:3001虚拟机。当然,您可以通过主机上的Website127.0.0.1:3000 API上的127.0.0.1:3001 VM访问private void Validate() { var query = SubModules.GroupBy(s => s.Name) .Select(s => new { Key = s.Key, Count = s.Count() }); if(query.Any(a => a.Count > 1)) { /* raise notification with some error message or use RasiePropertyChanged("InValid") * where InValid property in VM is bing to your XAML for indicating error*/ } }