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…

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

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效