Consul+upsync+Nginx实现动态负载均衡 摘自https://blog.csdn.net/qq_29247945/article/details/80787014

传统感念:每次修改完nginx配置文件,要重启nginx

动态感念:每次修改完nginx配置信息,不需要重启,nginx实时读取配置信息。

Nginx: 反向代理和负载均衡

Consul:是用go编写(谷歌),实现对动态负载均衡注册与发现功能

SpringCloud支持  Zookeeper、Eureka、Consul服务注册与发现。

服务注册:服务实现者可以通过HTTP API或DNS方式,将服务注册到Consul。

服务发现:服务消费者可以通过HTTP API或DNS方式,从Consul获取服务的IP和PORT。

故障检测:支持如TCP、HTTP等方式的健康检查机制,从而当服务有故障时自动摘除。

K/V存储:使用K/V存储实现动态配置中心,其使用HTTP长轮询实现变更触发和配置更改。

多数据中心:支持多数据中心,可以按照数据中心注册和发现服务,即支持只消费本地机房服务,使用多数据中心集群还可以避免单数据中心的单点故障。

Raft算法:Consul使用Raft算法实现集群数据一致性。

upsync:Nginx动态获取最新upStream(Upsync是新浪微博开源的基于Nginx实现动态配置的三方模块。Nginx-Upsync-Module的功能是拉取Consul的后端server的列表,并动态更新Nginx的路由信息。此模块不依赖于任何第三方模块。Consul作为Nginx的DB,利用Consul的KV服务,每个Nginx Work进程独立的去拉取各个upstream的配置,并更新各自的路由。)


实现动态负载均衡原理:

1、搭建ConsulServer专门存放负载均衡注册配置信息

2、Nginx间隔时间动态获取最新的ConsulServer配置信息

注意事项:Nginx1.9

Consul环境搭建:

1、下载consul_0.7.5_linux_amd64.zip

wgethttps://releases.hashicorp.com/consul/0.7.5/consul_0.7.5_linux_amd64.zip

2、解压consul_0.7.5_linux_amd64.zip

unzip consul_0.7.5_linux_amd64.zip

如果解压出现该错误

-bash: unzip: 未找到命令

解决办法

yum -yinstall unzip

3、

执行以下 ./consul 出现以下信息就说明安装成功

4、启动consul

我的linux ip地址为:10.11.3.161

./consul agent -dev -ui -node=consul-dev -client=10.11.3.161

如果出现

问题,解决方案:如果出现该问题说明你的ip(111.231.55.111)是公网ip,不能用公网ip,要用内网ip。

出现这些情况,则说明启动成功了。

5、关闭防火墙  

firewall-cmd --zone=public --add-port=8500/tcp --permanent

systemctl stop firewalld.service

6、浏览器访问111.22.55.111:8500

则说明consul安装完成。

二、安装Nginx

1、下载Nginx

wget http://nginx.org/download/nginx-1.9.10.tar.gz

2、解压Nginx

tar -zxvfnginx-1.9.10.tar.gz

3、配置Nginx

groupadd nginx

useradd -g nginx -s /sbin/nologin nginx

mkdir -p /var/tmp/nginx/client/

mkdir -p /usr/local/nginx

配置结束后,要先进行三、安装nginx-upsync-module,然后再进行第4、编译nginx

(三、安装nginx-upsync-module

        1、下载nginx-upsync-module

    wget https://github.com/weibocom/nginx-upsync-module/archive/master.zip

    下载下来的是master.zip

    2、解压nginx-upsync-module(master.zip)

    unzip master.zip

    如果解压出现该错误

    -bash: unzip: 未找到命令

    解决办法

    yum -yinstall unzip    

)

4、编译Nginx

./configure   --prefix=/usr/local/nginx   --user=nginx   --group=nginx   --with-http_ssl_module   --with-http_flv_module   --with-http_stub_status_module   --with-http_gzip_static_module   --with-http_realip_module   --http-client-body-temp-path=/var/tmp/nginx/client/   --http-proxy-temp-path=/var/tmp/nginx/proxy/  --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/  --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi   --http-scgi-temp-path=/var/tmp/nginx/scgi   --with-pcre--add-module=../nginx-upsync-module-master

出现

则说明编译成功。

make && make install

编译的是报错

./configure: error: SSL modules require the OpenSSL library.

解决办法

yum -y install opensslopenssl-devel

5、Upstream动态配置

##动态去consul 获取注册的真实反向代理地址

   upstream abing{

        server 127.0.0.1:11111;

upsync192.168.212.134:8500/v1/kv/upstreams/itmayieduupsync_timeout=6mupsync_interval=500ms upsync_type=consul strong_dependency=off;

upsync_dump_path  /usr/local/nginx/conf/servers/servers_test.conf;

    }

    server {

        listen       80;

server_name  localhost;

        location / {

proxy_passhttp://abing;

###nginx与上游服务器(真实访问的服务器)超时时间后端服务器连接的超时时间_发起握手等候响应超时时间

                                          proxy_connect_timeout1s;

                                          ###nginx发送给上游服务器(真实访问的服务器)超时时间

proxy_send_timeout1s;

                                          ###nginx接受上游服务器(真实访问的服务器)超时时间

proxy_read_timeout1s;

        index  index.html index.htm;

        }

    }
6、创建upsync_dump_path

mkdir /usr/local/nginx/conf/servers/

upsync_dump_path指定从consul拉取的上游服务器后持久化到的位置,这样即使consul服务器出问题了,本地还有一个备份。

7、启动consul
临时关闭防火墙systemctl stopfirewalld

我的linuxIp地址192.168.212.131

./consul agent -dev -ui -node=consul-dev -client=192.168.212.131

8、添加nginxUpstream服务

1.使用linux命令方式发送put请求

curl-X PUT http://192.168.212.134:8500/v1/kv/upstreams/itmayiedu/192.168.212.1:8081

curl-X PUT http://192.168.212.134:8500/v1/kv/upstreams/itmayiedu/192.168.212.1:8081

 2.使用postmen 发送put请求

http://192.168.212.134:8500/v1/kv/upstreams/itmayiedu/192.168.212.1:8081

http://192.168.212.134:8500/v1/kv/upstreams/itmayiedu/192.168.212.1:8081

 负载均衡信息参数

{"weight":1,"max_fails":2, "fail_timeout":10, "down":0}

9、启动Nginx

启动:
cd /usr/local/nginx/sbin
./nginx
nginx服务启动后默认的进程号会放在/usr/local/nginx/logs/nginx.pid文件
cat nginx.pid 查看进程号

关闭:
kill -TERM pid  快速停止服务
kill -QUIT pid  平缓停止服务
kill -9 pid     强制停止服务

重启:
cd /usr/local/nginx
./nginx -HUP pid

./nginx -s reload


/usr/local/webserver/nginx/sbin/nginx -s reload # 重新载入配置文件
/usr/local/webserver/nginx/sbin/nginx -s reopen # 重启 Nginx
/usr/local/webserver/nginx/sbin/nginx -s stop # 停止 Nginx

./nginx -v  显示nginx的版本号
./nginx -V  显示nginx的版本号和编译信息
./nginx -t  检查nginx配置文件的正确性
./nginx -t  检查nginx配置文件的正确定及配置文件的详细配置内容
./nginx -s  向主进程发送信号,如:./nginx -s reload 配置文件变化后重新加载配置文件并重启nginx服务
./nginx -p  设置nginx的安装路径
./nginx -c  设置nginx配置文件的路径
---------------------
作者:阿兵的小屋
来源:CSDN
原文:https://blog.csdn.net/qq_29247945/article/details/80787014
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自www.cnblogs.com/si812cn/p/9951497.html