如何让两个流浪盒子互相交谈?

编程入门 行业动态 更新时间:2024-10-27 08:35:29
本文介绍了如何让两个流浪盒子互相交谈?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

假设我制作了两个流浪盒子,foobar:

$ mkdir -p foo bar$推foo;流浪者初始化 hashicorp/precise32;流浪起来;弹出$推杆;流浪者初始化 hashicorp/precise32;流浪起来;弹出

现在假设我在 foo 上启动了一个 HTTP 服务器:

$ cd foo$ vagrant ssh -c 'python -m SimpleHTTPServer 8080

我的问题是,如何让 barfoo 进行通信(例如通过 curl)?

$ cd bar$ vagrant ssh -c 'curl http://????'

解决方案

虽然问题没有说清楚,但我觉得这个老问题是在问:如果*相同*的开发机器正在运行两个 vagrant 实例,foo上运行的应用程序如何从bar`上的网址获取数据.

如果是这样,我最近在我的两个 Rails 应用程序中遇到了这个问题(每个应用程序都在同一台开发机器上的一个单独的 vagrant 实例中运行).

如果 foo 想要从 bar 获取数据,那么两部分的技巧"是:

1) 每个 Vagrant 文件都需要一行:

config.vmwork :private_network, ip: PVT_NETWORK

其中 PVT_NETWORK 是本地 IP,每个 Vagrant 文件都不同,并且可能需要在同一子网中.例如 PVT_NETWORK 可能是 192.168.50.50 (foo) 和 192.168.50.51 (bar)

2) foo 通过 PVT_NETWORK IP 地址访问 bar不是您将在网络浏览器中使用的真实"IP.

在我的 Rails 示例中,我还让每个应用程序运行在不同的端口上,所以 foo 在 localhost:3000 上,bar 在 localhost:3001 上,所以 foo 将通过

访问 bar 上的 url

http://192.168.50.51:3001/some_url

Let's say I make two vagrant boxes, foo and bar:

$ mkdir -p foo bar
$ pushd foo; vagrant init hashicorp/precise32; vagrant up; popd
$ pushd bar; vagrant init hashicorp/precise32; vagrant up; popd

Now let's say I start an HTTP server on foo:

$ cd foo
$ vagrant ssh -c 'python -m SimpleHTTPServer 8080

My question is, how can I get bar to communicate (e.g. via curl) with foo?

$ cd bar
$ vagrant ssh -c 'curl http://????'

解决方案

Although the question does not make it clear, I think this older question is asking:if the *same* development machine is running two vagrant instances, how can an app running onfoofetch data from a url onbar`.

If so, I ran into this recently for my two Rails apps(each running in a separate vagrant instance on the same development machine).

The two-part 'trick' if foo wants to fetch data from bar, is:

1) each Vagrant files needs a line:

config.vmwork :private_network, ip: PVT_NETWORK

where PVT_NETWORK is a local IP, is different for each Vagrant file, and probably needs to be in the same subnet. For example PVT_NETWORK might be 192.168.50.50 (foo) and 192.168.50.51 (bar)

2) foo accesses bar via the PVT_NETWORK IP address not the "real" IP you would use with a web browser.

In my Rails example, I also have each app running on a different port, so foo is on localhost:3000 and bar is on localhost:3001, so foo would access a url on bar via

http://192.168.50.51:3001/some_url

这篇关于如何让两个流浪盒子互相交谈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-28 07:45:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1169645.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:盒子   两个

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!