【nginx】Consul+upsync+Nginx实现动态负载均衡

环境安装

安装Consul

1.下载consul_0.7.5_linux_amd64.zip

mkdir -p /usr/local/consul

cd /usr/local/consul

wget https://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

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

4.启动consul(后面写上IP地址)

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

5.临时关闭防火墙

systemctl stop firewalld 

6.浏览器访问192.168.202.132:8500

consul安装成功!

7.使用PostMan 注册Http服务

http://192.168.202.132:8500/v1/catalog/register
{"Datacenter": "dc1", "Node":"tomcat", "Address":"192.168.202.1","Service": {"Id" :"192.168.202.1:8080", "Service": "jeecg-boot","tags": ["dev"], "Port": 8080}}
{"Datacenter": "dc1", "Node":"tomcat", "Address":"192.168.202.1","Service": {"Id" :"192.168.202.1:8081", "Service": "jeecg-boot","tags": ["dev"], "Port": 8081}}

Datacenter指定数据中心,Address指定服务IP,Service.Id指定服务唯一标识,Service.Service指定服务分组,Service.tags指定服务标签(如测试环境、预发环境等),Service.Port指定服务端口。

安装upsync

作用:nginx动态获取最新upstream信息

cd /usr/local

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

下载不下来的话用浏览器下载,上传到/usr/local,然后使用unzip解压即可.

安装nginx

1.在安装nginx前首先要确认系统中安装了gcc、pcre-devel、zlib-devel、openssl-devel,没有则安装

//一键安装上面四个依赖

yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel


2.下载并解压安装包 

//创建一个文件夹
cd /usr/local

mkdir nginx

cd nginx

wget https://nginx.org/download/nginx-1.9.9.tar.gz


3.解压

tar -zxvf nginx-1.9.9.tar.gz


4.进入nginx目录并配置安装目录
 

groupadd nginx

useradd -g nginx -s /sbin/nologin nginx

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

cd nginx-1.9.9/

5.编译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

6.Upstream 动态配置

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

mkdir /usr/local/nginx/conf/servers/
upstream jeecg-boot{
    server 127.0.0.1:11111;

    upsync 192.168.202.132:8500/v1/kv/upstreams/test 
    upsync_timeout=6m                 
    upsync_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_pass http://jeecg-boot;
    index  index.html index.htm;

    }
}

upsync指令指定从consul哪个路径拉取上游服务器配置;upsync_timeout配置从consul拉取上游服务器配置的超时时间;upsync_interval配置从consul拉取上游服务器配置的间隔时间;upsync_type指定使用consul配置服务器;strong_dependency配置nginx在启动时是否强制依赖配置服务器,如果配置为on,则拉取配置失败时nginx启动同样失败。upsync_dump_path指定从consul拉取的上游服务器后持久化到的位置,这样即使consul服务器出问题了,本地还有一个备份。

 

发布了110 篇原创文章 · 获赞 36 · 访问量 13万+

猜你喜欢

转载自blog.csdn.net/wjx_jasin/article/details/100640831