天天看点

Nginx:Nginx基于TCP协议代理ZooKeeper集群

在上一篇博客中博主介绍了如何搭建​

​ZooKeeper​

​集群:

  • ​​ZooKeeper:搭建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​

​协议来实现。

  • ​​Spring Cloud Alibaba:搭建Nacos集群​​

Nginx添加TCP连接代理模块

​Nginx​

​​想要基于​

​TCP​

​​协议代理​

​ZooKeeper​

​​集群,需要​

​ngx_stream_core_module​

​​模块,​

​Nginx​

​​的方便之处就是可以添加各种模块,以便在​

​Nginx​

​​的基础上扩展各种想要的功能,比如添加​

​SSL​

​​实现​

​HTTPS​

​访问,

  • ​​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​

​集群了。

  • ​​Nginx:Nginx安装与运行​​

配置

./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      
Nginx:Nginx基于TCP协议代理ZooKeeper集群

修改配置

[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      
Nginx:Nginx基于TCP协议代理ZooKeeper集群
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基于TCP协议代理ZooKeeper集群

启动​

​Nginx​

​:

./nginx      

关闭防火墙(不同操作系统命令可能不同,自行百度):

systemctl stop firewalld      

​Nginx​

​​启动成功(​

​ZooKeeper​

​​集群节点​

​2​

​):

Nginx:Nginx基于TCP协议代理ZooKeeper集群

启动​

​ZooKeeper​

​​集群后,就可以通过​

​Nginx​

​​来请求​

​ZooKeeper​

​集群的服务了。

Nginx:Nginx基于TCP协议代理ZooKeeper集群

查询​

​ZooKeeper​

​​集群节点的状态有​

​Mode​

​​参数,就说明​

​ZooKeeper​

​集群启动成功了。

Nginx:Nginx基于TCP协议代理ZooKeeper集群

这里使用本地的客户端(​

​Windows​

​​版,需要提前准备好​

​ZooKeeper​

​​文件,当然也可以在​

​ZooKeeper​

​​集群的任意节点上使用客户端来进行测试)来连接​

​Nginx​

​:

zkCli.cmd -timeout 5000 -server 192.168.1.200:9999      
Nginx:Nginx基于TCP协议代理ZooKeeper集群
  • ​​ZooKeeper:重要概念​​