英文:
Updating code on production server when using Go
问题
当我使用PHP在生产服务器上开发和更新文件时,我只需在运行时复制文件,一切似乎都可以在不中断服务器的情况下正常工作。
但是,如果我要更新Go服务器和应用程序的代码,并且需要关闭服务器,将源代码文件复制到服务器上,运行go install,然后启动服务器,这将会中断服务。如果我经常这样做,对于我的用户来说会显得非常糟糕。
在使用Go的http服务器时,如何在不中断服务的情况下更新文件呢?
英文:
When I develop and update files on production server with PHP I just copy the files on the fly and everything seems to work without interrupting the server.
But if I am to update the code on the Go server and application and would need to kill the server, copy the src files to the server, run go install, and then start the server, this would interrupt the service, and if I do this quite often then it is going to look very bad for my users of the service.
How can I update files without the downtime when using Go with Go's http server?
答案1
得分: 2
PHP是一种解释型语言,这意味着你提供的代码是源代码格式,PHP解释器会读取并执行它(它可能会创建一个更紧凑的二进制形式,这样在需要时就不必再次分析源代码)。
Go是一种编译型语言,它编译成本机可执行二进制文件;更进一步,它是静态链接的,这意味着在创建可执行文件时,应用程序引用的每个代码和库都会被编译和链接。这意味着你不能只是将新的Go模块“插入”到正在运行的应用程序中。
你必须停止正在运行的应用程序并启动新版本。但是你可以将停机时间最小化:只有在新版本的可执行文件已经创建并准备好运行时才停止正在运行的应用程序。你可以选择在远程机器上编译它并上传二进制文件到服务器,或者上传源代码并在服务器上编译,这没有关系。
通过这种方式,你可以将停机时间减少到几秒钟,用户几乎察觉不到。此外,你不应该每小时更新一次,因为你无法在仅编写一个小时的代码中实现重大更新。你可以每天(甚至更少频繁)安排更新,并在流量较低的时间段进行。
如果即使几秒钟的停机时间也无法接受,那么你应该寻找可以自动处理此问题而无需停机的平台。例如,可以查看Google App Engine - Go。
英文:
PHP is an interpreted language, which means you provide your code in source format and the PHP interpreter will read it and execute it (it may create a more compact binary form so that it doesn't have to analyze the source again when needed).
Go is a compiled language, it compiles into a native executable binary; going further it is statically linked which means every code and library your app is referring to is compiled and linked when the executable is created. This implies you can't just "drop-in" new go modules into a running application.
You have to stop your running application and start the new version. You can however minimize the downtime: only stop the running application when the new version of the executable is already created and ready to be run. You may choose to compile it on a remote machine and upload the binary to the server, or upload the source and compile it on the server, it doesn't matter.
With this you could decrease the downtime to a maximum of few seconds, which your users won't notice. Also you shouldn't update in every hour, you can't really achieve significant updates in just an hour of coding. You could schedule updates daily (or even less frequently), and you could schedule them for hours when your traffic is low.
If even a few seconds downtime is not acceptable to you, then you should look for platforms which handle this for you automatically without any downtime. Check out Google App Engine - Go for example.
答案2
得分: 1
首先,不要按照那个顺序进行操作。先复制和安装,然后停止旧进程并运行新进程。
如果你运行多个应用实例,可以进行滚动更新,这样当你重启一个服务器时,其他服务器仍在提供服务。类似的方法是进行蓝绿部署,其优点是活动集群运行的代码始终是同质的(而在滚动部署期间,它们将是混合的),并且你还可以进行蓝绿部署,即使通常只有一个应用实例(而滚动部署需要多个实例)。然而,在蓝绿切换期间,你需要有双倍数量的实例。
你需要考虑的一件事是正在进行的请求,你可能希望确保正在进行的请求继续发送到旧代码服务器,直到它们完成。
你还可以研究平台即服务(PaaS)解决方案,它可以自动化很多这些工作,以及更多其他工作。这样,你就不需要手动登录到生产服务器并手动复制文件。在考虑运维时,12因素应用原则总是一个很好的起点。
英文:
First of all, don't do it in that order. Copy and install first. Then you could stop the old process and run the new one.
If you run multiple instances of your app, then you can do a rolling update, so that when you bounce one server, the other ones are still serving. A similar approach is to do blue-green deployments, which has the advantage that the code your active cluster is running is always homogeneous (whereas during a rolling deploy, you'll have a mixture until they've all rolled), and you can also do a blue-green deployment where you normally have only one instance of your app (whereas rolling requires more than one). It does however require you to have double the instances during the blue-green switch.
One thing you'll want to take into consideration is any in-flight requests -- you may want to make sure that in-flight requests continue to go to old-code servers until their finished.
You can also look into Platform-as-a-Service solutions, that can automate a lot of this stuff for you, plus a whole lot more. That way you're not ssh'ing into production servers and copying files around manually. The 12 Factor App principles are always a good place to start when thinking about ops.
答案3
得分: 1
优雅重启库(grace library)将允许您在不打扰用户的情况下进行优雅重启。您可以在以下链接找到该库:https://github.com/facebookgo/grace
然而,根据我的经验,重新启动Go应用程序非常快速,除非您拥有一个高流量的网站,否则不会引起任何麻烦。
英文:
The grace library will allow you to do graceful restarts without annoyance for your users: https://github.com/facebookgo/grace
Yet in my experience restarting Go applications is so quick, unless you have an high traffic website it won't cause any trouble.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论