在单个虚拟机中为两个使用Google Go编写的网站提供服务

huangapple go评论97阅读模式
英文:

Serving two websites written in Google Go within a single VM

问题

我有一个来自Digital Ocean的虚拟机。目前有两个域名与该虚拟机关联。
我没有使用其他的Web服务器,只使用了Golang内置的http模块。从性能方面来说,我很喜欢它,并且感觉我对它有完全的控制。

目前,我正在使用一个单独的Go程序,其中内置了多个网站。

http.HandleFunc("test.com/", serveTest)
http.HandleFunc("123.com/", serve123)
http.HandleFunc("/", serve123)

由于它们是网站,Go程序正在使用端口80。
问题是,当我尝试仅更新一个网站时,我必须重新编译整个程序,因为它们是在同一段代码中编写的。

1)有没有办法只使用Golang(不使用Nginx或Apache)实现热插拔?
2)什么是标准的最佳实践?

非常感谢!

英文:

I have a VM from Digital Ocean. It currently has two domains linked to the VM.
I do not use any other web server but Golang's built in http module. Performance-wise I like it, and I feel like I have a full control over it.

Currently I am using a single Go program that has multiple websites built in.

http.HandleFunc("test.com/", serveTest)
http.HandleFunc("123.com/", serve123)
http.HandleFunc("/", serve123)

As they are websites, Go program is using port 80 for that.
And the problem is when I am trying to update only 1 website, I have to recompile whole thing as they are written in the same code.

  1. Is there a way to make it hot-swappable only with Golang (without Nginx or Apache)
  2. What would be a standard best practice?

Thank you so much!

答案1

得分: 1

嗯,你可以在Go语言中进行热插拔,但我真的不想这样做,除非真的必要,因为增加的复杂性是不可忽略的(我指的不仅仅是代码)。

你可以通过一种代理的方式实现类似的效果,代理程序会位于程序前面,当你的二进制文件发生变化时,它会进行优雅的切换:原理是将二进制文件放在一个端口上,代理放在另一个端口上。当一个新的二进制文件准备好时,你在另一个端口上运行它,并让代理重定向到新的端口,然后优雅地关闭旧的端口。

Go语言中有一个工具可以实现这个功能,但我记不起来它的名字了……

编辑:不是我想到的那个工具,但很接近 https://github.com/rcrowley/goagain

个人建议:使用反向代理更简单。我个人的设置是使用h2o来终止SSL、HTTP2等,然后将请求发送到后台运行的各个网站。不仅仅是Go语言,还有PHP语言、Gitlab实例等。它更加灵活,而且代理的性能损耗很小……

英文:

Well, you can do hotswapping in go, but I really wouldn't want to do that unless really ncecessary as the complexity added isn't negligible (and I'm not talking about code).

You can have something close with a kind of proxy that would sit in front of the program and do a graceful swap whenever your binary change : the principle is to have the binary on one port, the proxy on another. When a new binary is ready, you run it on another port, and make the proxy redirect to the new port, then gracefully shutdown the old one.

There was a tool for that in Go that I can't remember the name of…

EDIT: not the one I had in mind, but close call https://github.com/rcrowley/goagain

Personnal advice: use a reverse proxy for that, its much more simple to do. My personnal setup is to use h2o to terminate SSL, HTTP2, etc, and send the requests to the various websites running on the background. Not only Go ones, though, but also PHP ones, a Gitlab instance, etc. Its much more flexible, and the performance penalty of the proxy is small…

huangapple
  • 本文由 发表于 2016年3月9日 22:51:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/35894894.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定