平滑重启是应用持续发布上线不断服务的重要保障,其原理和网络模型是有紧密联系的,这里讨论在epoll网络模型下面是如何实现平滑重启的(实现方式不唯一)。
后台服务首先会绑定某个特定端口(bind)比如web服务就是绑定80或443端口,然后进行监听(listen)。进程在监听端口时就是等待请求,请求来了就处理。但是为了更高效的处理请或在同时处理更多的请求,这时就需要epoll这种网络模型。由epoll进行统一管理,进程通过注册事件的方式来监听请求,当有请求来时epoll会通知进程去处理请求。
实现上面的功能是通过下面三个函数:epoll_create() ,epoll_ctl和epoll_wait,通过这三个函数把特定进程注册到等待队列中,在有请求事件来时就会通知队列中的进程,然后进程被唤醒去处理请求(在没有事件来时 是会阻塞在epoll_wait这个方法,等待分配)。这里有个关键的点:就是进程是主动去通知说想接受请求的,然后被自身生成的描述符加入后备队列中,所以我们只要通知系统把描述符移除掉就不会再接收新的请求,这样只要处理完已经接收的请求就可以重启了。所以平滑重启的核心就在此。为了更好的理解,下面以c语言为例用代码实践下(代码分为服务端和客户端,直接拷贝就可以允许)(代码中的注释同时还解释了epoll事件模型,惊群问题等,值得细看,如果有问题 可以后面评论):
服务端代码:
#include#include #include #include #include #include #include #include #include #include #include #include #include //次是为了验证 获取连接后 然后吧监听去掉,然后在返回处理结果 是否ok 主要是用于平滑重启char* getDateTime(); #define MAX_FD_NUM 3 void setnonblock(int fd) { int flag = fcntl(fd, F_GETFL, 0); if (flag == -1) { printf("get fcntl flag %s\n", strerror(errno)); return; } int ret = fcntl(fd, F_SETFL, flag | O_NONBLOCK); if (ret == -1) { printf("set fcntl non-blocking %s\n", strerror(errno)); return; }} //服务端进行端口的绑定和监听操作,并返回描述符int socket_create(int port) { int fd = socket(AF_INET, SOCK_STREAM, 0); if (fd == -1) { printf("socket create %s\n", strerror(errno)); return -1; } setnonblock(fd); struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(port); addr.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) { printf("socket bind %s\n", strerror(errno)); return -1; } if (listen(fd, 20) == -1) { printf("socket listen %s\n", strerror(errno)); return -1; } return fd;} //使用epoll模型进行事件的添加和 移除已经获取等操作void socket_accept(int fd) { //串讲描述符 用于标识--会加入后备队列 int epfd = epoll_create(MAX_FD_NUM); if (epfd == -1) { printf("epoll create %s\n", strerror(errno)); return; } struct epoll_event event, events[MAX_FD_NUM]; memset(&event, 0, sizeof(event)); event.data.fd = fd; event.events = EPOLLIN | EPOLLERR | EPOLLHUP; //增加事件,当有对当前端口请求时会当做事件。 if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &event) == -1) { printf("epoll ctl %s\n", strerror(errno)); return; } printf("epoll_ctl is ok"); char* nowtime = getDateTime(); printf("%s\n", nowtime); int client_fd; while (1) { printf("epoll_wait is ok"); nowtime = getDateTime(); printf("%s\n", nowtime); //会停留在此 进行时间的等待,等待事件触发,然后进行处理请求 //-1表示是一直等待。 int num = epoll_wait(epfd, events, MAX_FD_NUM, -1); printf("epoll_wait is ok%d",num); nowtime = getDateTime(); printf("%s\n", nowtime); if (num == -1) { printf("epoll wait %s\n", strerror(errno)); break; } else { int i = 0; for (; i
客户端代码:
#include#include #include #include #include #include #include #include #include #include #include int socket_connect(const char *ip, int port) { int fd = socket(AF_INET, SOCK_STREAM, 0); if (fd == -1) { printf("socket create %s\n", strerror(errno)); return -1; } struct sockaddr_in addr; memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(port); addr.sin_addr.s_addr = inet_addr(ip); //与服务端创建连接 if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) { printf("socket connect %s\n", strerror(errno)); return -1; } return fd;} void socket_send(int fd) { char buf[64]; memset(buf, 0, sizeof(buf)); read(STDIN_FILENO, buf, sizeof(buf)); //给已创建的连接发送数据 send(fd, buf, strlen(buf), 0); printf("%%%%%%%%%%%%%%\n"); close(fd);} void client(const char *ip, int port) { int fd = socket_connect(ip, port); if (fd == -1) { printf("client socket create failed\n"); return; } socket_send(fd);} int main(int argc, char *argv[]) { if (argc < 3) { printf("give the error parameters\n"); return 0; } const char *ip = argv[1]; int port = atoi(argv[2]); client(ip, port); return 0;}复制代码
运行代码:
gcc server.c -o sv
./sv 8080 //在本机的8080端口进行监听,下图就是在接收client请求时的打印日志
gcc client.c -o ct
./ct 127.0.0.1 8080 //向127.0.0.1的8080端口发起请求。