栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

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

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

ZooKeeper :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添加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安装与运行

配置

./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


  • ZooKeeper :重要概念 & 客户端命令介绍

很显然Nginx基于TCP协议代理ZooKeeper集群成功了。如果博主有说错的地方或者大家有不同的见解,欢迎大家评论补充。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/583435.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号