Consul+upsync+Nginx实现动态负载均衡
上一篇文章 <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..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./
./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 -ui
4. 配置nginx.conf
vi /usr/local/nginx/conf/nginx.conf
#user nobody;
worker_processes ; events {
worker_connections ;
} http {
include mime.types;
default_type application/octet-stream; keepalive_timeout ; upstream weatherforecast{
server 127.0.0.1:; ## nginx 要求必须有这一行
## 连接consul server 获取动态 upstream 配置负载均很信息 间隔0.5秒获取consul配置信息
upsync 127.0.0.1:/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 ;
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
Consul+upsync+Nginx实现动态负载均衡的更多相关文章
- 基于Consul+Upsync+Nginx实现动态负载均衡
基于Consul+Upsync+Nginx实现动态负载均衡 1.Consul环境搭建 下载consul_0.7.5_linux_amd64.zip到/usr/local/src目录 cd /usr/l ...
- Consul+upsync+Nginx实现动态负载均衡 摘自https://blog.csdn.net/qq_29247945/article/details/80787014
传统感念:每次修改完nginx配置文件,要重启nginx 动态感念:每次修改完nginx配置信息,不需要重启,nginx实时读取配置信息. Nginx: 反向代理和负载均衡 Consul:是用go编写 ...
- 【Nginx】基于Consul+Upsync+Nginx实现动态负载均衡
一.Http动态负载均衡 什么是动态负载均衡 动态负载均衡实现方案 常用服务器注册与发现框架 二.Consul快速入门 Consul环境搭建 三.nginx-upsync-module nginx-u ...
- Nginx 实现动态负载均衡(Nginx-1.10.1 + Consul v0.6.4)
一直也没有找到合适的类似Socat + Haproxy 的组合能用在Nginx,后来发现了Nginx的几个模块,但是也存在各种不足. 而且Nginx 在大流量的情况下nginx -s reload 是 ...
- 《nginx 三》实现nginx的动态负载均衡——实战
Http动态负载均衡 什么是动态负载均衡 传统的负载均衡,如果Upstream参数发生变化,每次都需要重新加载nginx.conf文件, 因此扩展性不是很高,所以我们可以采用动态负载均衡,实现Upst ...
- 动态负载均衡(Nginx+Consul+UpSync)环境搭建
首先 安装好 Consul upsync 然后: 1.配置安装Nginx 需要做配置,包括分组之类的,创建目录,有些插件是需要存放在这些目录的 groupadd nginx useradd -g ng ...
- 动态负载均衡(Nginx+Consul+UpSync)
Http动态负载均衡 什么是动态负载均衡 传统的负载均衡,如果Upstream参数发生变化,每次都需要重新加载nginx.conf文件, 因此扩展性不是很高,所以我们可以采用动态负载均衡,实现Upst ...
- Consul+upsync+Nginx 动态负载均衡
1,动态负载均衡 传统的负载均衡,如果修改了nginx.conf 的配置,必须需要重启nginx 服务,效率不高.动态负载均衡,就是可配置化,动态化的去配置负载均衡. 2,实现方案 1. Consul ...
- 通过Nginx、Consul、Upsync实现动态负载均衡和服务平滑发布
前提 前段时间顺利地把整个服务集群和中间件全部从UCloud迁移到阿里云,笔者担任了架构和半个运维的角色.这里详细记录一下通过Nginx.Consul.Upsync实现动态负载均衡和服务平滑发布的核心 ...
随机推荐
- MySQL权限原理及删除MySQL的匿名账户
MySQL权限系统的工作原理 MySQL权限系统通过下面两个阶段进行认证: (1)对连接的用户进行身份认证,合法的用户通过认证,不合法的用户拒绝连接: (2)对通过认证的合法用户赋予相应的权限,用户可 ...
- ajax学习摘抄笔记
2019独角兽企业重金招聘Python工程师标准>>> AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). A ...
- 在sun jdk 8镜像基础上构建maven 3的docker镜像
2019独角兽企业重金招聘Python工程师标准>>> 在https://my.oschina.net/ytqvip/blog/1595054文章的sun jdk 8镜像基础上构建m ...
- Mysql 字符串拆分 OR 一行转多行
Mysql 字符串拆分 OR 一行转多行 需要了解的的几个mysql 函数: A.substring_index():字符串截取 substring_index(str,delim,count) ...
- CF思维联系– CodeForces - 991C Candies(二分)
ACM思维题训练集合 After passing a test, Vasya got himself a box of n candies. He decided to eat an equal am ...
- MySQL必知必会1-20章读书笔记
MySQL备忘 目录 目录 使用MySQL 检索数据 排序检索数据 过滤数据 数据过滤 用通配符进行过滤 用正则表达式进行搜索 创建计算字段 使用数据处理函数 数值处理函数 汇总数据 分组数据 使用子 ...
- redis文章汇总
方便集群管理时的查看操作 http://www.cnblogs.com/mushroom/p/4738170.html http://www.cnblogs.com/hjwublog/p/568170 ...
- Power BI Desktop心得
我是用钉钉邮箱做账号登录Power BI Desktop的.我用Power BI Desktop,做排版和统计. Power BI由Power Query和Power Pivot组成,前者有M语言,后 ...
- [hdu5387 Clock]时钟夹角问题
题意:给一个时刻,求时针.分钟.秒针三者之间的夹角 思路:确定参照点,求出三者的绝对夹角,然后用差来得到它们之间的夹角,钝角情况用360.减去就行了. #include <map> #in ...
- CentOS7 Installing Python3
最近开始学习python. python火了这么久,我终于还是跪舔它了,我是一个跟风的人,学过C.C#.JAVA.PHP,无一例外的浅尝即止,不知道我这双已经近视的眼,确认过的眼神还对不对,希望pyt ...