使用balancer_by_lua_block做应用层负载均衡
来源:互联网 发布:php执行exec权限不足 编辑:程序博客网 时间:2024/06/01 07:38
首先感谢章义春大神的openresty,解决了web开发的一些痛点并简化了web开发的复杂度。
需求:
根据url的一个参数,做负载均衡,使得某一个用户总是被分配到固定的业务服务器上处理,方便后续的业务处理,做缓存或单元化架构部署
假设这个参数为dvid,一共有两个业务服务器, 8088端口和8089端口,分别返回hello和world
- server{
- listen 8088;
- location /hello {
- content_by_lua ’
- ngx.say(“hello”)
- ’;
- }
- }
- server{
- listen 8089;
- location /hello {
- content_by_lua ’
- ngx.say(“world”)
- ’;
- }
- }
server{ listen 8088; location /hello { content_by_lua ' ngx.say("hello") '; } } server{ listen 8089; location /hello { content_by_lua ' ngx.say("world") '; } }
upstream balancer_by_lua_block的配置:
- upstream backend{
- server 0.0.0.0;
- balancer_by_lua_block {
- local balancer = require “ngx.balancer”
- local port = {8088, 8089}
- local backend = “”
- local dvid = ngx.req.get_uri_args()[“dvid”] or 0
- ngx.log(ngx.ERR, ”dvid=”, dvid)
- local hash = (dvid % 2) + 1
- ngx.log(ngx.ERR, ”hash=”, hash)
- backend = port[hash]
- ngx.log(ngx.ERR, ”backend=”, backend)
- ngx.log(ngx.ERR, ”dvid=“, dvid, ” hash=“, hash, ” up=”, backend)
- local ok, err = balancer.set_current_peer(“127.0.0.1”, backend)
- if not ok then
- ngx.log(ngx.ERR, “failed to set the current peer: ”, err)
- return ngx.exit(500)
- end
- ngx.log(ngx.DEBUG, “current peer ”, backend)
- }
- }
upstream backend{ server 0.0.0.0; balancer_by_lua_block { local balancer = require "ngx.balancer" local port = {8088, 8089} local backend = "" local dvid = ngx.req.get_uri_args()["dvid"] or 0 ngx.log(ngx.ERR, "dvid=", dvid) local hash = (dvid % 2) + 1 ngx.log(ngx.ERR, "hash=", hash) backend = port[hash] ngx.log(ngx.ERR, "backend=", backend) ngx.log(ngx.ERR, "dvid=", dvid, " hash=", hash, " up=", backend) local ok, err = balancer.set_current_peer("127.0.0.1", backend) if not ok then ngx.log(ngx.ERR, "failed to set the current peer: ", err) return ngx.exit(500) end ngx.log(ngx.DEBUG, "current peer ", backend) } }
接收业务请求的server:
- server {
- listen 80;
- server_name localhost;
- #charset koi8-r;
- #access_log logs/host.access.log main;
- location / {
- root html;
- index index.html index.htm;
- }
- location /hello {
- proxy_pass http://backend;
- }
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- }
server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } location /hello { proxy_pass http://backend; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }测试:
http://localhost/hello?dvid=1 返回world
http://localhost/hello?dvid=2 返回hello
测试结果OK
openresty安装:
https://openresty.org/en/download.html
wget https://openresty.org/download/openresty-1.11.2.2.tar.gz
tar zxf openresty-1.11.2.2.tar.gz
cd openresty-1.11.2.2/
./configure –with-luajit&& make && make install
参考:
http://weibo.com/1834459124/DaEmgBhlD?from=singleweibo&mod=recommand_weibo&type=comment#_rnd1479734607640
http://blog.csdn.net/force_eagle/article/details/52224660
http://www.wtoutiao.com/p/177SJDc.html
- 使用balancer_by_lua_block做应用层负载均衡
- 使用balancer_by_lua_block做应用层负载均衡
- ubuntu下使用haproxy做tcp 层的负载均衡
- ubuntu下使用haproxy做tcp 层的负载均衡
- 使用haproxy做tcp 层的负载均衡
- haproxy做TCP层的负载均衡
- 使用Apache做负载均衡
- 使用Apache做负载均衡
- 使用Apache做负载均衡
- 使用Apache做负载均衡
- 使用nginx做负载均衡
- 服务器负载均衡体系结构,II:应用层负载均衡
- 如何使用Apache做负载均衡
- 使用DNSPOD做DNS负载均衡
- 使用Haproxy对Sphinx做负载均衡
- 【Nginx】使用Nginx做负载均衡
- ubuntu下使用nginx做负载均衡
- IP层负载均衡
- INADDR_ANY的确切含义
- oracle同义词测试
- Scrapy 爬虫 个人学习经验
- numpy 报错
- [Thinking in Java]
- 使用balancer_by_lua_block做应用层负载均衡
- spring-boot集成swagger
- UIApplication openUrl too slow (十分慢 10秒)
- POI生成WORD文档
- JSP页面乱码的所有情况及解决办法
- Machine Learning 2014 by Andrew NG (part 1)
- 源码解析文章
- IDEA Struts学习项目搭建[初学者一目了然]
- python相关函数