在上一篇博客中博主介绍了如何搭建ZooKeeper
集群:
搭建ZooKeeper
集群是为了使用它,之前博主也介绍过如何搭建Nacos
集群,并且使用了Nginx
作为Nacos
集群的代理,当客户端想要请求Nacos
集群的服务时,就只需要与该Nginx
进行交互即可(不需要考虑Nacos
集群的复杂性,Nginx
会将请求转发给Nacos
集群,而Nacos
集群的响应,Nginx
也会响应给客户端),而真正的Nacos
集群可以不暴露在外网下,处于内网下的Nacos
集群会更加安全,但Nginx
是基于HTTP
协议代理的Nacos
集群,因为客户端与Nacos
集群是使用RESTful API
进行交互的,而ZooKeeper
客户端与服务端建立的是TCP
长连接,显然是基于TCP
协议,因此想要使用Nginx
代理ZooKeeper
集群,需要Nginx
基于TCP
协议来实现。
Nginx添加TCP连接代理模块
Nginx
想要基于TCP
协议代理ZooKeeper
集群,需要ngx_stream_core_module
模块,Nginx
的方便之处就是可以添加各种模块,以便在Nginx
的基础上扩展各种想要的功能,比如添加SSL
实现HTTPS
访问,
ngx_stream_core_module
模块从1.9.0
版本开始可用,但这个模块不是默认构建的,在配置Nginx
时通过--with-stream
参数启用。
安装Nginx
在之前已经介绍过了,这里不再赘述,但在执行./configure
命令时需要加--with-stream
参数,这样Nginx
就会构建ngx_stream_core_module
模块,之后Nginx
就可以基于TCP
协议代理ZooKeeper
集群了。
配置
./configure --with-stream
[root@localhost nginx-1.20.2]# ./configure --with-stream
checking for OS
+ Linux 3.10.0-1160.el7.x86_64 x86_64
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
checking for gcc -pipe switch ... found
...
checking for gcc builtin 64 bit byteswap ... found
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
编译、安装
make && make install
修改配置
[root@localhost /]# cd /usr/local/nginx/conf
[root@localhost conf]# ll
总用量 68
-rw-r--r--. 1 root root 1077 11月 19 17:19 fastcgi.conf
-rw-r--r--. 1 root root 1077 11月 19 17:19 fastcgi.conf.default
-rw-r--r--. 1 root root 1007 11月 19 17:19 fastcgi_params
-rw-r--r--. 1 root root 1007 11月 19 17:19 fastcgi_params.default
-rw-r--r--. 1 root root 2837 11月 19 17:19 koi-utf
-rw-r--r--. 1 root root 2223 11月 19 17:19 koi-win
-rw-r--r--. 1 root root 5231 11月 19 17:19 mime.types
-rw-r--r--. 1 root root 5231 11月 19 17:19 mime.types.default
-rw-r--r--. 1 root root 2656 11月 19 17:19 nginx.conf
-rw-r--r--. 1 root root 2656 11月 19 17:19 nginx.conf.default
-rw-r--r--. 1 root root 636 11月 19 17:19 scgi_params
-rw-r--r--. 1 root root 636 11月 19 17:19 scgi_params.default
-rw-r--r--. 1 root root 664 11月 19 17:19 uwsgi_params
-rw-r--r--. 1 root root 664 11月 19 17:19 uwsgi_params.default
-rw-r--r--. 1 root root 3610 11月 19 17:19 win-utf
[root@localhost conf]# vim nginx.conf
stream {
server {
listen 9999;
proxy_pass zookeeper;
}
upstream zookeeper {
server 192.168.1.199:9000;
server 192.168.1.200:9000;
server 192.168.1.201:9000;
}
}
./nginx 启动nginx。
./nginx -t 检查nginx的配置文件是否符合要求
./nginx -s stop 此方式相当于先查出nginx进程id,再使用kill命令强制杀掉进程。
./nginx -s quit 此方式是待nginx进程处理完任务后,再停止nginx。
./nginx -s reload 重启nginx。
检查配置是否有问题:
./nginx -t
启动Nginx
:
./nginx
关闭防火墙(不同操作系统命令可能不同,自行百度):
systemctl stop firewalld
Nginx
启动成功(ZooKeeper
集群节点2
):
启动ZooKeeper
集群后,就可以通过Nginx
来请求ZooKeeper
集群的服务了。
查询ZooKeeper
集群节点的状态有Mode
参数,就说明ZooKeeper
集群启动成功了。
这里使用本地的客户端(Windows
版,需要提前准备好ZooKeeper
文件,当然也可以在ZooKeeper
集群的任意节点上使用客户端来进行测试)来连接Nginx
:
zkCli.cmd -timeout 5000 -server 192.168.1.200:9999
很显然Nginx
基于TCP
协议代理ZooKeeper
集群成功了。如果博主有说错的地方或者大家有不同的见解,欢迎大家评论补充。