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实现动态负载均衡和服务平滑发布的核心 ...
随机推荐
- linux和windows互传文件、用户配置文件和密码配置文件、用户组管理、用户管理...
linux和windows互传文件 第一种:在linux主机上下载lrzsz软件包 1.yum install lrzsz 2.通过rz命令上传window的文件到linux主机上 用过sz 文件名下 ...
- iOS9.2.1 App从AppStore上下载闪退问题
首先这是小编的第一篇文章,我是一名做iOS开发的小白,出于爱好会更新发表些相关的技术文章,偶尔也会发些视频.恳请大家不要去嘲笑一个努力的人,要是做的不好请多多评论,反正我也不改. 好了!敲黑板!!说正 ...
- 数学--数论--Hdu 1452 Happy 2004(积性函数性质+和函数公式+快速模幂+乘法逆元)
Consider a positive integer X,and let S be the sum of all positive integer divisors of 2004^X. Your ...
- 图论--LCA--树上倍增法(在线)
/* * LCA在线算法(倍增法) */ const int MAXN = 10010; const int DEG = 20; struct Edge { int to, next; } edge[ ...
- mock-server 之 mock 接口测试
1.mock 介绍 mock 除了用在单元测试过程中,还有一个用途,当前端开发在开发页面的时候,需要服务端提供 API 接口,此时服务端没开发完成,或者说没搭建测试环境,这个时候前端开发会自己 moc ...
- python requests 接口测试
1.get方法请求接口 url:显而易见,就是接口的地址url啦 headers:请求头,例如:content-type = application/x-www-form-urlencoded par ...
- python的unittest框架中的assert断言
unittest框架自带断言,如果想用assert断言,一定要引入unittest.TestCase框架才行,不然不会自动识别assert断言
- session与cookie的浅谈
cookie的用途: 当你浏览网页时,会有一些推送消息,大多数是你最近留意过的同类东西,比如你想买桌子,上淘宝搜了一下,结果连着几天会有各种各样的桌子的链接.这是因为你浏览某个网页的时候,WEB 服务 ...
- OpenCV 4下darknet修改
darknet的安装使用直接在官网上获取.https://pjreddie.com/darknet/ 但我用的是OpenCV4.1.1,make时会在image_opencv.cpp中有两个错误. 1 ...
- tp5中使用ueditor编辑器保存文本到数据库后回显后显示html标签问题解决办法
在编辑器ueditor中获取文本,保存到到数据库后为 当在数据库中提取出来,在显示回ueditor编辑器时候,出了问题, html标签都显示出来了 百度了下别人的解决办法是,使用官方提供的api 可是 ...