dongren1011 2016-03-09 14:51
浏览 34
已采纳

在单个VM中为用Google Go编写的两个网站提供服务

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条回答 默认 最新

  • donglu4159 2016-03-09 15:10
    关注

    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…

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法