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

本文介绍如何配置Nginx以TCP模式代理ZooKeeper集群,通过添加ngx_stream_core_module模块并配置相关参数,使客户端能够通过Nginx访问到内部的ZooKeeper集群,提高安全性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在上一篇博客中博主介绍了如何搭建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集群成功了。如果博主有说错的地方或者大家有不同的见解,欢迎大家评论补充。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ITKaven

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值