作者个人研发在高并发场景下提供了一个简单、稳定、可扩展的延迟消息队列框架,具有精准的定时任务和延迟队列处理功能。开源半年多以来,已成功为十几家中小企业提供精准定时调度解决方案,经受住了生产环境的考验。为了造福更多的童鞋,这里给出开源框架的地址:https://github.com/sunshinelyz/mykit-delay这次写的比较早,被问到Nginx的四层负载均衡。别着急,让我们来仔细看看这个看似简单的问题。负载均衡可以分为静态负载均衡和动态负载均衡。接下来我们分析一下Nginx是如何实现四层静态负载均衡和四层动态负载均衡的。静态负载均衡Nginx的四层静态负载均衡需要开启ngx_stream_core_module模块。默认情况下,ngx_stream_core_module未启用。安装Nginx时,添加--with-stream配置参数启用,如下图。./configure--prefix=/usr/local/nginx-1.17.2--with-openssl=/usr/local/src/openssl-1.0.2s--with-pcre=/usr/local/src/pcre-8.43--with-zlib=/usr/local/src/zlib-1.2.11--with-http_realip_module--with-http_stub_status_module--with-http_ssl_module--with-http_flv_module--with-http_gzip_static_module--with-cc-opt=-O3--with-stream--with-http_ssl_module配置四层负载均衡配置HTTP负载均衡时,在http命令下配置,四层负载均衡在stream命令下配置。结构如下.stream{upstreammysql_backend{...}server{...}}配置upstreamupstreammysql_backend{server192.168.175.201:3306max_fails=2fail_timeout=10sweight=1;server192.168.175.202:3306max_fails=2fail_timeout=10sweight=1;least_conn;}配置serverserver{#监听端口,默认为tcp协议,如果需要UDP协议,配置为listen3307udp;listen3307;#失败重试proxy_next_upstream_timeout0;proxy_next_upstream_tries0;#timeout配置#配置与上游服务器连接超时,默认为60sproxy_connect_timeout1s;#配置客户端连接上游服务器两次成功读写操作的超时时间,超时则自动断开连接#是连接存活时间,可以通过它释放不活跃的连接,默认是10分钟proxy_timeout1m;#限速配置#从客户端读取数据的速率,以字节/秒为单位,默认为0,不限速proxy_upload_rate0;#从上游服务器读取数据的速率,单位是每秒字节数,默认为0,不限速proxy_download_rate0;#上游服务器proxy_passmysql_backend;}配置完成后,可以连接到Nginx的3307端口并访问数据库Nginx完整配置Nginx完整配置如下:worker_connections1024;}stream{upstreammysql_backend{server192.168.175.100:3306max_fails=2fail_timeout=10sweight=1;least_conn;}server{#监听端口默认使用tcp协议。如果需要UDP协议,配置为listen3307udp;listen3307;#Failuretoretryproxy_next_upstreamon;proxy_next_upstream_timeout0;proxy_next_upstream_tries0;#超时配置#配置连接上游服务器的超时时间,默认为60sproxy_connect_timeout1s;#配置客户端两次成功连接上游服务器的读写超时时间,超时则automaticallydisconnect#也就是连接存活时间,通过它可以释放不活跃的连接。默认值为10分钟。datarate,单位是字节数/秒,默认0,不限速进程不退出。会报错如下图。nginx:工作进程正在关闭;这是因为Worker进程维护的长连接一直在使用,所以无法退出,只能kill进程。可以使用Nginx的四层动态负载均衡来解决这个问题。使用Nginx的四层动态负载均衡有两种方案:使用商业版Nginx和使用开源的nginx-stream-upsync-module模块。注意:nginx-stream-upsync-module模块可用于四层动态负载均衡,nginx-upsync-module模块可用于七层动态负载均衡。使用以下命令将nginx-stream-upsync-module和nginx-upsync-module模块添加到Nginx。此时Nginx将同时支持四层动态负载均衡和HTTP七层动态负载均衡。gitclonehttps://github.com/xiaokai-wang/nginx-stream-upsync-module.gitgitclonehttps://github.com/weibocom/nginx-upsync-module.gitgitclonehttps://github.com/CallMeFoxie/nginx-upsync。gitcp-rnginx-stream-upsync-module/*nginx-upsync/nginx-stream-upsync-module/cp-rnginx-upsync-module/*nginx-upsync/nginx-upsync-module/./configure--prefix=/usr/local/nginx-1.17.2--with-openssl=/usr/local/src/openssl-1.0.2s--with-pcre=/usr/local/src/pcre-8.43--with-zlib=/usr/local/src/zlib-1.2.11--with-http_realip_module--with-http_stub_status_module--with-http_ssl_module--with-http_flv_module--with-http_gzip_static_module--with-cc-opt=-O3--with-stream--add-module=/usr/local/src/nginx-upsync--with-http_ssl_module在配置四层负载均衡时在配置HTTP负载均衡时,在http命令下配置,配置四层负载均衡在stream命令下,结构如下,stream{upstreammysql_backend{...}server{...}}配置upstreamupstreammysql_backend{server127.0.0.1:1111;#occupancyserverupsync192.168.175.100:8500/v1/kv/upstreams/mysql_backendupsync_timeout=6mupsync_interval=500msupsync_type=consulstrong_dependency=off;upsync_dump_path/usr/local/nginx-1.17.2/conf/mysql_backend.conf;}upsync命令指定从consul拉取上游服务器配置的路径;upsync_timeout配置从consul拉取上游服务器配置的超时时间;upsync_interval配置从consul拉取上游服务器配置的间隔时间;upsync_type指定使用consul配置服务器;strong_dependency配置nginx启动时是否强制依赖配置服务器。从consul拉取的upstreamserver所在的位置是持久化的,这样即使consulserver出问题了,本地还是有备份的。配置serverserver{#监听端口默认使用tcp协议。如果需要UDP协议,配置为listen3307udp;listen3307;#失败重试proxy_next_upstream_timeout0;proxy_next_upstream_tries0;#超时配置#配置与上游服务器的连接超时时间,默认为60sproxy_connect_timeout1s;#配置两次成功读取的超时时间/连接到客户端上游服务器的写操作。如果超时,连接会自动断开#也就是连接存活时间,通过它可以释放不活跃的连接。默认10分钟proxy_timeout1m;#限速配置#从客户端读取数据的速率,单位是字节/秒,默认为0,不限速proxy_upload_rate0;#从上游服务器读取数据的速率,单位是字节/秒,默认为0,不限速proxy_download_rate0;#上游服务器proxy_passmysql_backend;}从Consul添加上游服务器curl-XPUT-d"{\"weight\":1,\"max_fails\":2,\"fail_timeout\":10}"http://192.168.175.100:8500/v1/kv/upstreams/mysql_backend/192.168.175.201:3306curl-XPUT-d"{\"weight\":1,\"max_fails\":2,\"fail_timeout\":10}"http://192.168.175.100:8500/v1/kv/upstreams/mysql_backend/192.168.175.202:3306删除上游服务器来自Consulcurl-XDELETEhttp://192.168.175.100:8500/v1/kv/upstreams/mysql_backend/192.168.175.202:3306配置upstream_showserver{listen13307;upstream_show;}配置你pstream_show命令后,可以通过curlhttp://192.168.175.100:13307/upstream_show查看当前动态负载均衡upstream服务器列表Nginx完整配置Nginx完整配置如下:{server127.0.0.1:1111;#occupancyserverupsync192.168.175.100:8500/v1/kv/upstreams/mysql_backendupsync_timeout=6mupsync_interval=500msupsync_type=consulstrong_dependency=off;upsync_dump_path/usr/local1/7.2/conx-1。mysql_backend.conf;}server{#监听端口默认使用tcp协议。如果需要UDP协议,配置为listen3307udp;listen3307;#失败重试proxy_next_upstreamon;proxy_next_upstream_timeout0;proxy_next_upstream_tries0;#超时配置#配置与上游服务器连接超时时间,默认60sproxy_connect_timeout1s;#配置客户端连接上游服务器两次成功读写操作的超时时间。如果超时,连接会自动断开#也就是连接存活时间,通过它可以释放不活跃的连接,默认是10分钟proxy_timeout1m;#限速配置#从客户端读取数据的速率,单位是字节每秒,默认为0,不限速proxy_upload_rate0;#从上游服务器读取数据的速率,单位是字节每秒,默认为0,不限速proxy_download_rate0;#upstreamserverproxy_passmysql_backend;}服务器{listen13307;upstream_show;}}
