一.首先安装nginx(源码编译安装)
① 下载安装包(本人使用的是1.20.2版本) :https://nginx.org/download/nginx-1.20.2.tar.gz
解压nginx-1.20.2.tar.gz 命令:
tar -zxvf nginx-1.20.2.tar.gz
②下载openssl :
下载地址:https://www.openssl.org/source/openssl-1.1.1t.tar.gz
然后解压到指定的目录:
我的目录是(在接下来的编译nginx中增加这个模块, 注意不是编译后的openssl是源码的地址!): /home/topf/software/nginx-1.20.2/openssl-1.1.1t
到nginx的解压包目录下:
cd nginx-1.20.2
配置make信息
./configure --prefix=/usr/local/nginx --with-openssl=/home/topf/software/nginx-1.20.2/openssl-1.1.1t --with-stream --with-stream_ssl_module
make -j3
等待make结束后执行安装命令:
sudo make install
至此nginx安装完成, 接下来验证是否ok (可以创建快捷启动,本案例不在这些细节,主要在于流程思路):
本机源码编译安装的nginx默认安装在:/usr/local/nginx下,[
1.启动: nginx: sudo /usr/local/nginx/sbin/nginx ###//启动nginx的命令。
2.停止: nginx: sudo /usr/local/nginx/sbin/nginx -s stop ###//此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。
3.重启nginx: sudo /usr/local/nginx/sbin/nginx -s reload ###//重新加载配置文件:当 nginx的配置文件 nginx.conf 修改后,要想让配置生效需要重启 nginx,使用-s reload不用先停止 nginx再启动 nginx 即可将配置信息在 nginx 中生效
4.sudo /usr/local/nginx/sbin/nginx -s quit ###//此方式停止步骤是待nginx进程处理任务完毕进行停止。]
执行命令:
sudo /usr/local/nginx/sbin/nginx
启动nginx, 在浏览器地址栏输入:http://127.0.0.1/
接着出现一个欢迎界面即ok
Welcome to nginx!
-------------------------------------------------
二. 接着安装zmq
主机上安装zmq支持
sudo apt-get install libzmq3-dev
由于本机使用的是python版本的zmq接口和opencv接口:pyzmq因此 在python环境安装(没有zmq和opencv环境的请各位提前安装)
pip install pyzmq python-opencv
至此软件环境安装完毕!
三.接下来要搭建tcp的负载均衡,
第一步骤:
配置nginx.conf文本,配置之前养成备份的习惯,先备份后更改:
sudo cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
更改nginx配置:
vim /usr/local/nginx/conf/nginx.conf
下面我把自己的所有配置项给出来:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
stream {
# 代理服务:
upstream Myserver {
# simple round-robin 转发IP和端口 weight权重的意思 后面和超时有关的
server 192.168.1.70:13265 weight=1 max_fails=3 fail_timeout=10s;#max_fails=3 fail_timeout=30s;
server 192.168.1.70:13266 weight=1 max_fails=3 fail_timeout=10s;#max_fails=3 fail_timeout=30s;
server 192.168.1.70:13267 weight=1 max_fails=3 fail_timeout=10s;#max_fails=3 fail_timeout=30s;
}
server {
proxy_connect_timeout 30s;
proxy_timeout 30s;
listen 8000 sndbuf=0; #监听端口8000,即代理服务器的端口,不是真正提供服务的端口。
proxy_pass Myserver; #转发请求
tcp_nodelay on;
}
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
ok 保存退出命令一顿执行:
摁Esc 接下来保存命令: !wq
保存配置后,接来下重启nginx:
sudo /usr/local/nginx/sbin/nginx -s reload
下来验证环节 (如果各位缺少python包自行pip install 安装下,很简单的),本案例依传输图片为例(自行准备图片):
首先server.py 源代码
import zmq
import io
import PIL
import cv2
import numpy as np
import PIL.Image as Image
import sys
context = zmq.Context()
print("Connecting to server...")
socket = context.socket(zmq.REP)
port = 13267
addr = 'tcp://*:{}'.format(port)
socket.bind(addr)
indx = 0
while True:
indx+=1
msg = socket.recv()
image1 = np.array(Image.open(io.BytesIO(msg)))
image1 = Image.fromarray(image1)
frame_rawhead = cv2.cvtColor(np.asarray(image1), cv2.COLOR_RGB2BGR)
backMsg = 'recvok'
socket.send(backMsg.encode('utf-8'))
if indx%100==0:
print('port:', port, ' , exec', indx)
# print(type(frame_rawhead))
# cv2.imshow("yyyy",frame_rawhead)
# cv2.waitKey(1)
其次贴上client.py 源码(各位测试可以改下client的端口号开启多个服务,端口分别按照nginx配置的13267, 13266, 13265, 当然一个也可以的):
import zmq
import io
import cv2
import sys
import time
context = zmq.Context()
listen_port = 8000
addr = 'tcp://192.168.1.70:{}'.format(listen_port)
print("Connecting to server...")
socket = context.socket(zmq.REQ)
# 发送超时时长3s
socket.setsockopt(zmq.SNDTIMEO, 3000)
# 接收消息时长超时3s
socket.setsockopt(zmq.RCVTIMEO, 3000)
socket.connect(addr)
while True:
try:
fram = cv2.imread('/home/topf/test/testZmq/python_zmq/t1.png')
img_encode= cv2.imencode('.jpg',fram)
str_encode = img_encode[1].tostring()
buf_str = io.BytesIO(str_encode).getvalue()
socket.send(buf_str)
print("Server send image ok!")
message = socket.recv()
print("Received reply: ", message.decode('utf-8'))
except zmq.ZMQError as e:
message = "Exception({0}): {1}".format(e.errno, e.strerror)
print('error :', message)
# 超时关闭socket
socket.close()
# 1秒后重新链接
time.sleep(1)
socket = context.socket(zmq.REQ)
socket.setsockopt(zmq.SNDTIMEO, 3000)
socket.setsockopt(zmq.RCVTIMEO, 3000)
socket.connect(addr)
ok ,实验结论:
1. 请求端的监听8000端口,发出请求由nginx代理分发到
192.168.1.70:13267 ,
192.168.1.70:13266,
192.168.1.70:13265
这三个相同的服务去执行(这三个服务功能相同,也可以理解为集群):
2.zmq添加的超时链接机制,确保其中一个服务断后达到高可用性;
3.此实验可以应用到使用zmq 中间件tcp传输协议的负载, 快捷方便