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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

zhouandke   2020-05-26 我要评论

  上一篇文章 <C# HttpClient 使用 Consul 发现服务> 解决了内部服务之间的调用问题, 对外提供网关服务还没有解决, 最后我选择了 nginx-upsync-module 作为服务发现和转发的工具,

  现在 .net core 已经有很多包含权鉴、熔断的网关工具了, nginx-upsync-module 只提供了服务发现(支持Consul, 不需要重启nginx)与转发的功能, 功能少性能强, 如果不满意 ocelot 的性能, 可以试一试.

 

  一个WebApi最好只提供一个服务, 所以在这个示例中, 我准备了两个项目 SayHelloService 和 WeatherForecastService, 这两个服务均会注册到consul;

  nginx 通过 nginx-upsync-module 发现提供 WeatherForecastService 的实例

  WeatherForecastService 通过 ConsulDiscovery 发现提供 SayHelloService 的实例

  所以 nginx  收到请求后转发到 WeatherForecastService, WeatherForecastService 再调用 SayHelloService

 

  0. 环境

  CentOS Linux release 7.7.1908 (Core)   IP地址: 192.168.0.51 , 测试阶段建议关闭防火墙和SELinux

  consul_1.7.2_linux_amd64.zip(自行下载)

  nginx-1.17.10.tar.gz(自行下载)

  nginx-upsync-module-master.zip (示例代码中 或 https://github.com/weibocom/nginx-upsync-module )

  示例代码 (链接)

   开发机: .net core 3.1,  IP地址192.168.0.3

  推荐使用 MobaXterm 作为 Linux 的SSH工具, 可以很方便的上传文件

 

  1. 编译nginx

  为简单起见, 可以直接 进入su模式

  将nginx-1.17.10.tar.gz 和 nginx-upsync-module-master.zip 上传到 /usr/local/src   (哈哈, 这里多半会遇到权限问题), 然后解压

tar -zxvf nginx-1.17.10.tar.gz
unzip nginx-upsync-module-master.zip

  编译

yum -y install gcc gcc-c++ automake zlib zlib-devel openssl openssl--devel pcre pcre-devel
cd nginx-1.17.10/
./configure --add-module=../nginx-upsync-module-master
make && make install

  nginx 就编译到 /usr/local/nginx 了

 

  3. 运行consul

mkdir /usr/local/consul

  将 consul 执行程序上传到 /usr/local/consul  

chmod +x consul
./consul agent -server -data-dir=data -bind=192.168.0.51 -client=0.0.0.0 -bootstrap-expect 1 -ui

 

  4. 配置nginx.conf

  vi /usr/local/nginx/conf/nginx.conf

#user  nobody;
worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    keepalive_timeout  65;

    upstream weatherforecast{
        server 127.0.0.1:11111;  ## nginx 要求必须有这一行
        ## 连接consul server 获取动态 upstream 配置负载均很信息 间隔0.5秒获取consul配置信息
        upsync 127.0.0.1:8500/v1/catalog/service/WeatherForecastService upsync_timeout=6m upsync_interval=500ms upsync_type=consul_services strong_dependency=off;
        ## 拉取的服务列表保存起来, 这样的话, 即使consul失效了, 也可暂用这些信息
        upsync_dump_path /tmp/weatherforecast_consul.conf;
    }

    server {
        listen       80;
        server_name  localhost;

        location / {
            proxy_pass http://weatherforecast;
            index  index.html index.htm;
        }
    }
}

  启动nginx

cd /usr/local/nginx/sbin
./nginx

 

  5. 准备 SayHelloService 和 WeatherForecastService

  代码就不在这里展示了, 直接到 github 下载, 然后使用 "发布" 生成程序, 然后上传呢到 Linux

  需要注意的是 

  A. 因为是nginx作为网关对外提供服务, SayHelloService 和 WeatherForecastService 只需要绑定 127.0.0.1 即可

  B. 默认SayHelloService 的 appsettings.json 指明了 ServiceIP=127.0.0.1, ServicePort=5000

  WeatherForecastService的 appsettings.json 指明了 ServiceIP=127.0.0.1, ServicePort=5002

  启动的时候绑定的urls 要与其一致

cd /home/zhouke/SayHelloService
dotnet SayHelloService.dll --urls="http://localhost:5000"
cd /home/zhouke/WeatherForecastService
dotnet WeatherForecastService.dll --urls="http://localhost:5002"

 

 

 

 

 

 

End

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们