Go服务器的无缝补丁部署,无需重启

I was curious if there are any solutions to deploying a go server without the need shut it down and relaunch it. I know popular solutions like ASP.Net and PHP and the like will do this seamlessly for user sessions.

Will stateless sessions take care of this issue?

This kind of seemless deployment can be achieved for even the most naive of application servers by introducing a request router such as nginx or haproxy. Both of these routers allow you to forward requests to different services (known as a reverse-proxy), and reload their configuration without dropping connections. By way of an example:

  • Configure your router to listen on 0.0.0.0.80 and forward these requests to 127.0.0.1:5001.
  • Run your application server v1 on 127.0.0.1:5001.
  • In this setup when your users make requests to your router, they will be forwarded it to your application v1 server.
  • In deploying a new version v2 of your application, configure it to listen on port 127.0.0.1:5002.
  • Change the router configuration such that it now forwards traffic to 127.0.0.1:5002. Then tell the router to reload its configuration.
  • Now when your users make new requests to your router, they will be forwarded to your application v2 server.
  • You can safely shut down v1 once it stops receiving traffic. If there are issues with v2 you can revert back to v1 by reverting your router configuration, so keeping v1 running in the background has its benefits.

This is a simplified, high-level overview. (You should prefer unix sockets over the loopback interface, for example.) This kind of deployment is typically referred to as a canary or blue-green deployment.