高性能Nginx服务器(4)----基于Consul+Upsync+Nginx实现动态负载均衡

动态负载均衡就相当于不去修改nginx的配置,就可以改变nginx的上游服务器。

动态负载均衡的实现方案

  1. Consul+Consul-template
  • 发现这种方案,每次发现配置之后需要reload nginx,重启nginx
  1. Consul+OpenResty:该方案实现无需reload动态负载均衡
  2. Consul+upsync+nginx:推荐该方案,实现无需reload nginx,可以实现动态的负载均衡

Consul+upsync+nginx

  • nginx:用来做反向代理和负载均衡
  • consul:使用go语言编写,consul专门实现分布式服务的注册与发现的功能
  • UpSync:其作用是定时的让nginx从Consul中获取最新的配置信息,相当于是nginx的插件

zookeeper,Eureka,Consul

  • 上面的三个都可以作为服务的注册和发现
  • 通常我们会有多个微服务,微服务之间存在相互的调用,如果我们没有注册中心(没有服务的注册与发现的时候)我们的调用方式是用HttpClient等,直接进行IP+端口号的调用。
    在这里插入图片描述
    但是,当我们有了注册中心的时候,我们会将各个微服务都注册到注册中心中,简单来说,注册中心就是存放各个IP地址+端口号的。注册到注册中心之后,我们会在服务间通信的时候,先访问注册中心,发现服务,看上去我们访问的是注册中心,实际访问的是我们在注册中心发现的调用服务的服务IP+端口号。注意:这里我们注册到注册中心的服务,其实类似于存放在一个map中,key是服务的项目名称,value是各自的项目名称+端口号,其中key是只有一个的,但是value是允许有多个的,即一个项目可以对应多个IP,类似集群等,避免一个服务不可用导致down机
    在这里插入图片描述
  • 我们的Consul,Zookeeper,Eureka其实就是服务注册中心。

动态负载均衡的原理

  1. 搭建ConsulServer,专门存放负载均衡的上游服务器的注册配置信息
  2. nginx间隔一定时间(并不是实时的),自动动态获取最新的ConsulServer配置信息(就是自动取注册到上面的信息为上游服务器)
    在这里插入图片描述
  3. UpSync的作用是定时的让nginx从Consul中获取最新的配置信息

Consul快速入门

  • Consul是一款开源的分布式服务注册与发现系统,通过HTTP API可以使得服务注册,发现实现起来非常简单,他支持如下的特性
    (1)服务注册:服务实现者可以通过HTTP API或者DNS的方式,将服务注册到Consul
    (2)服务发现:服务消费者可以通过HTTP API或者DNS的方式,从Consul获取服务的IP和PORT
    (3)服务检测:支持如TCP,HTTP等方式的健康检查机制,从而服务有故障的时候自动摘除
    (4)key/value存储:使用K/V存储实现动态配置中心,其使用HTTP长轮询实现变更触发和配置更改
    (5)多数据中心:支持多数据中心,可以按照数据中心注册和发现服务,即支持只消费本地机房的服务,使用多数据中心集群还可以避免单数据中心的单点故障

Consul环境搭建

  1. 下载consul_0.7.5_linux_amd64.zip
    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

  • 如果解压出现该错误
    -bash: unzip: 未找到命令
  • 解决办法
    yum -y install unzip
  1. 执行以下 ./consul 出现以下信息就说明安装成功
    [root@localhost soft] ./consul
    usage: consul [ --version ] [ --help ] < command > [ < args>]
    Available commands are:
    agent Runs a Consul agent
    configtest Validate config file
    event Fire a new event
    exec Executes a command on Consul nodes
    force-leave Forces a member of the cluster to enter the “left” state
    info Provides debugging information for operators
    join Tell Consul agent to join cluster
    keygen Generates a new encryption key
    keyring Manages gossip layer encryption keys
    kv Interact with the key-value store
    leave Gracefully leaves the Consul cluster and shuts down
    lock Execute a command holding a lock
    maint Controls node or service maintenance mode
    members Lists the members of a Consul cluster
    monitor Stream logs from a Consul agent
    operator Provides cluster-level tools for Consul operators
    reload Triggers the agent to reload configuration files
    rtt Estimates network round trip time between nodes
    snapshot Saves, restores and inspects snapshots of Consul server state
    version Prints the Consul version
    watch Watch for changes in Consul

  2. 启动consul

  • 我的linux Ip地址172.17.0.6(内网地址)
    ./consul agent -dev -ui -node=consul-dev -client=172.17.0.6
  1. 临时关闭防火墙systemctl stop firewalld

  2. 浏览器访问122.51.3.134:8500

  • 访问的时候访问的是外网地址,配置配置的是内网

在这里插入图片描述

  1. 使用PostMan 注册Http服务
    http://122.51.3.134:8500/v1/catalog/register
    参数1
    {“Datacenter”: “dc1”,
    “Node”:“tomcat”, “Address”:“192.168.5.165”,“Service”: {“Id” :“192.168.5.165:8080”, “Service”: “itmayiedu”,“tags”: [“dev”], “Port”: 8080}}
    参数2
    {“Datacenter”: “dc1”, “Node”:“tomcat”, “Address”:“192.168.5.165”,“Service”: {“Id” :“192.168.5.165:8081”, “Service”: “itmayeidu”,“tags”: [“dev”], “Port”: 8081}}
  • Datacenter指定数据中心,Address指定服务IP,Service.Id指定服务唯一标识,Service.Service指定服务分组,Service.tags指定服务标签(如测试环境、预发环境等),Service.Port指定服务端口。

在这里插入图片描述

  1. 发现Http服务
    http://122.51.3.134:8500/v1/catalog/service/item_jd_tomcat

  2. 添加nginx Upstream的服务

  • 使用linux命令发送put请求

curl -X PUT http://122.51.3.134:8500/v1/kv/upstreams/itmayiedu/117.34.137.156:8080
curl -X PUT http://122.51.3.134:8500/v1/kv/upstreams/itmayiedu/117.34.137.156:8081

  • 使用postman发送put请求

http://122.51.3.134:8500/v1/kv/upstreams/itmayiedu/117.34.137.156:8080 http://122.51.3.134:8500/v1/kv/upstreams/itmayiedu/117.34.137.156:8081

  • 负载均衡信息参数

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

nginx-upsync-module

  • 注意:安装这个模块之前,需要清除之前的Nginx环境,然后重新安装(因为要使用该模块,nginx一定要在1.9以上)
  • Upsync是新浪微博开源的基于Nginx实现动态配置的第三方模块。Nginx-Upsync-Module的功能是拉取Consul的后端Server列表,并且动态更新Nginx的路由信息。此模块不依赖于任何的第三方模块。Consul作为Nginx的DB,利用Consul得到KV服务(key是服务名,value是服务的IP+端口),每个Nginx Work进程独立的去拉取各个upstream的配置,并更新各自的路由。

nginx-upsync-module的安装

  1. 下载安装包

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

  • 作用是:nginx动态获取最新的upstream
  1. 解压安装

unzip master.zip

  1. nginx的安装

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

  1. 配置nginx

groupadd nginx
useradd -g nginx -s /sbin/nologin nginx
mkdir -p /var/tmp/nginx/client/
mkdir -p /usr/local/nginx

  1. 编译nginx

cd nginx-1.9.0
./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

Upstream的动态配置

  ##动态去consul 获取注册的真实反向代理地址
   upstream itmayiedu{
        server 127.0.0.1:11111;
        upsync 192.168.212.134:8500/v1/kv/upstreams/itmayiedu 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://itmayiedu;
            index  index.html index.htm;
        }
    }

  • upsync指令指定了从consul哪个路径拉取上游服务器的配置
  • upsync_timeout:该配置从consil拉取上游服务器的配置的超时时间
  • upsync_interval:改配置从consul拉取上游服务器的间隔时间
  • upsync_type:指定使用consul配置服务器
  • strong_dependency配置nginx在启动的时候是否强制依赖配置服务器,如果配置为on,则拉取配置失败的时候nginx启动同样失败。
  • upsync_dump_path:指定从consul拉取的上游服务器后持久化到的位置,这样即使consul服务器出问题了,本地还有一个备份。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值