This is a creation in Article, where the information may have evolved or changed.
Article Source: http://gf.johng.cn/625833
Basic introduction
Smooth restart (hot restart) means that Web server does not interrupt the execution of an existing request when it restarts. This feature is especially useful when different project versions are released, for example, when a two version is required: A, B, then in the process of a execution, we can publish the program of B directly to the program covering a, and seamlessly transition the request to the new version of the service using the smooth restart feature.
It is important to note that this feature is limited to the *nix
system (LINUX/UNIX/FREEBSD, and so on) and only supports the full restart function under Windows (the request cannot smooth the transition).
Management functions
GF Framework supports very convenient web management functions, that is, we can directly through the Web page/interface to the Web server restart/shutdown and other management operations. At the same time, the GF framework supports the management of Web server restart/shutdown in the form of command line terminal instructions ( *nix
system only).
Web Management
Let's take a look at some of the ways in which administrative actions are involved in Web server:
func (s *Server) Reload() errorfunc (s *Server) Restart() errorfunc (s *Server) Shutdown() errorfunc (s *Server) EnableAdmin(pattern ...string)
Where, for a Reload
smooth restart, for a Restart
full restart, Shutdown
use to shut down web Server. We can call these 3 methods at any time in the Registered service processing interface to implement Web server management operations.
In addition, the GF Framework provides an easy-to-manage method EnableAdmin
for registering the Administration page with the specified routing rule, which is the default address /debug/admin
.
Example 1: Basic use
package mainimport ( "time" "gitee.com/johng/gf/g" "gitee.com/johng/gf/g/os/gproc" "gitee.com/johng/gf/g/net/ghttp")func main() { s := g.Server() s.BindHandler("/", func(r *ghttp.Request){ r.Response.Writeln("哈喽!") }) s.BindHandler("/pid", func(r *ghttp.Request){ r.Response.Writeln(gproc.Pid()) }) s.BindHandler("/sleep", func(r *ghttp.Request){ r.Response.Writeln(gproc.Pid()) time.Sleep(10*time.Second) r.Response.Writeln(gproc.Pid()) }) s.EnableAdmin() s.SetPort(8199) s.Run()}
We test the smooth restart by following several steps:
- Access Http://127.0.0.1:8199/pid View PID of the current process
- Access to Http://127.0.0.1:8199/sleep, this page will be executed for 10 seconds to test whether the page request execution will be broken when the restart
- Access Http://127.0.0.1:8199/debug/admin, which is
s.EnableAdmin
a Web server Administration page after the default registration
Then we click on reload
the admin link and the WEB server will immediately smooth the restart
The following information is also output at the terminal:
2018-05-18 11:02:04.812 11511: http server started listening on [:8199]2018-05-18 11:02:09.172 11511: server reloading2018-05-18 11:02:09.176 16358: http server reloaded listening on [:8199]
- We can find that throughout the operation, the
sleep
execution of the page has not been interrupted and continues to wait for a few seconds, and when sleep
execution completes, the page output is:
- It can be found that the process PID of the page output is different from
sleep
the previous one, the execution of the request is smoothly taken over by the new process, and the old service process is destroyed;
Example 2:HTTPS support
package mainimport ( "gitee.com/johng/gf/g" "gitee.com/johng/gf/g/net/ghttp")func main() { s := g.Server() s.BindHandler("/", func(r *ghttp.Request){ r.Response.Writeln("哈罗!") }) s.EnableHTTPS("/home/john/temp/server.crt", "/home/john/temp/server.key") s.EnableAdmin() s.SetPort(8200) s.Run()}
The smooth restart feature of the GF framework is also very friendly and easy to support for HTTPS, with the following steps:
- Access Https://127.0.0.1:8200/debug/admin/reload smooth restart HTTPS service;
- Access Https://127.0.0.1:8200/debug/admin/shutdown smooth shutdown of the Web Server service;
The following output information can be seen at the command line terminal:
2018-05-18 11:13:05.554 17278: https server started listening on [:8200]2018-05-18 11:13:21.270 17278: server reloading2018-05-18 11:13:21.278 17319: https server reloaded listening on [:8200]2018-05-18 11:13:34.895 17319: server shutting down2018-05-18 11:13:34.895 17269: all servers shutdown
Example 3: Multi-service and Multiport
The smooth restart feature of GF Framework is quite powerful and stable, not only supports single-service single port monitoring management, but also supports the monitoring management of complex scenarios such as multi-service multiport.
package mainimport ( "gitee.com/johng/gf/g")func main() { s1 := g.Server("s1") s1.EnableAdmin() s1.SetPort(8100, 8200) s1.Start() s2 := g.Server("s2") s2.EnableAdmin() s2.SetPort(8300, 8400) s2.Start() g.Wait()}
The above example shows two Web servers and, s1
s2
respectively, listening 8100
, 8200
and 8300
, 8400
. We then visit the Http://127.0.0.1:8100/debug/admin/reload Smooth Restart service, and then smoothly shut down the service through Http://127.0.0.1:8100/debug/admin/shutdown, Finally, the information printed at the terminal is as follows:
2018-05-18 11:26:54.729 18111: http server started listening on [:8400]2018-05-18 11:26:54.729 18111: http server started listening on [:8100]2018-05-18 11:26:54.729 18111: http server started listening on [:8300]2018-05-18 11:26:54.729 18111: http server started listening on [:8200]2018-05-18 11:27:08.203 18111: server reloading2018-05-18 11:27:08.207 18124: http server reloaded listening on [:8300]2018-05-18 11:27:08.207 18124: http server reloaded listening on [:8400]2018-05-18 11:27:08.207 18124: http server reloaded listening on [:8200]2018-05-18 11:27:08.207 18124: http server reloaded listening on [:8100]2018-05-18 11:27:19.379 18124: server shutting down2018-05-18 11:27:19.380 18102: all servers shutdown
Command line Management
GF Framework, in addition to providing web-based management capabilities, also supports command line management, because the command line is managed with semaphores, so only the system is supported *nix
.
Master and Child processes
The smooth restart feature of the GF framework is achieved by relying on multi-process management and interprocess communication, so there will be two processes after the Web server program executes, with the points of the master/child process. If our Web server program name is called demo
, after execution, we see the ps
process through the command:
$ ps aux | grep demojohn 19557 0.1 0.1 282408 10676 ? Sl 11:53 0:00 /tmp/demojohn 19566 0.0 0.1 356140 10688 ? Sl 11:53 0:00 /tmp/demo --gproc-child
Where the command line with --gproc-child
parameters identifies the child process, the Web server is the real service delivery process, and the other is the main process, for the child process status monitoring and behavior management. At any time, there will be only one program's main process in the system, and a corresponding child process, and there will be no case of multiple main or child processes.
Smooth restart
Use SIGUSR1
the semaphore to achieve, use the way:kill -SIGUSR1 主进程ID
Full restart
Use SIGUSR2
the semaphore to achieve, use the way:kill -SIGUSR2 主进程ID
Close Service
Use SIGINT/SIGQUIT/SIGKILL/SIGHUP/SIGTERM
any one of the semaphore to achieve, using:kill -SIGTERM 主进程ID
Other management methods
Because the GF Framework Web Server has a singleton design, you can use the g.server (name)
or ghttp anywhere. Getserver (name)
to obtain a singleton object for the corresponding Web server, followed by Reload
, Restart
, and Shutdown
method to implement management of the Web server.