Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

🍁 基于Lua的Spring Cloud网关高可用通用Ngnix插件

License

Notifications You must be signed in to change notification settings

fansys/nginx-eureka-dynamic-lb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

14 Commits

Repository files navigation

image

nginx-zuul-dynamic-lb

🍁 基于Lua的Spring Cloud网关高可用通用Ngnix插件


场景痛点

image

在Spring Cloud微服务架构体系中,我们往往会部署一个Zuul集群来横向扩展我们的微服务应用,集群的上层是Nginx软负载,在实际情况中,往往会遇到Zuul宕机的尴尬事情,这时候从Nginx到这台机器的请求就会全部失效。此项目针对此痛点,用lua脚本实现定时拉取特定服务地址,动态无感知增减Zuul在Nginx中的负载节点。

如果您希望实现从Nginx直接到普通服务的动态节点负载,在下文配置服务名与Eureka注册中心地址即可。

OpenResty安装与配置

1、环境
yum -y install readline-devel pcre-devel openssl-devel gcc
2、下载解压OpenResty包
wget https://openresty.org/download/openresty-1.13.6.1.tar.gz
tar -zxvf openresty-1.13.6.1.tar.gz
3、下载ngx_cache_purge模块,该模块用于清理nginx缓存
cd openresty-1.13.6.1/bundle
wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
tar -zxvf ngx_cache_purge-2.3.tar.gz
4、下载nginx_upstream_check_module模块,该模块用于upstream健康检查
wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/v0.3.0.tar.gz
tar -zxvf v0.3.0.tar.gz
5、OpenResty配置增加
cd openresty-1.13.6.1
./configure --prefix=/usr/servers --with-http_realip_module --with-pcre --with-luajit --add-module=./bundle/ngx_cache_purge-2.3/ --add-module=./bundle/nginx_upstream_check_module-0.3.0/ -j2 
6、编译安装
make
make install
7、OpenResty没有http模块,需要单独安装
cd /usr/servers/lualib/resty
wget https://raw.githubusercontent.com/pintsized/lua-resty-http/master/lib/resty/http_headers.lua 
wget https://raw.githubusercontent.com/pintsized/lua-resty-http/master/lib/resty/http.lua
8、项目脚本拷贝到这里
copy dynamic_eureka_balancer.lua into this dir
9、Nginx配置文件
vim /usr/servers/nginx/conf/nginx.conf

Nginx配置


http {
	#sharing cache area
	lua_shared_dict dynamic_eureka_balancer 128m;
	init_worker_by_lua_block {
		-- init eureka balancer
		local file = require "resty.dynamic_eureka_balancer"
		local balancer = file:new({dict_name="dynamic_eureka_balancer"})
		
		--eureka server list
		balancer.set_eureka_service_url({"127.0.0.1:8888", "127.0.0.1:9999"})
		
		--eureka basic authentication
		--use this setting if eureka has enabled basic authentication. 
		--note: basic authentication must use BASE64 encryption in `user:password` format
 --balancer.set_eureka_service_basic_authentication("")
		
		--The service name that needs to be monitored
		balancer.watch_service({"zuul", "client"})
	}
	
	upstream springcloud_cn {
		server 127.0.0.1:666; # Required, because empty upstream block is rejected by nginx (nginx+ can use 'zone' instead)
		
		balancer_by_lua_block { 
		
			--The zuul name that needs to be monitored
			local service_name = "zuul"
			
			local file = require "resty.dynamic_eureka_balancer"
			local balancer = file:new({dict_name="dynamic_eureka_balancer"}) 
			
			--balancer.ip_hash(service_name) --IP Hash LB
			balancer.round_robin(service_name) --Round Robin LB
		}
	}
 server {
 listen 80;
 server_name localhost;
		
		location / {
			proxy_pass http://springcloud_cn/;
			proxy_set_header Host $http_host;
			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_set_header X-Forwarded-Proto $scheme;
		}
 error_page 500 502 503 504 /50x.html;
 location = /50x.html {
 root html;
 }
	}
}

支持多zone

增加 zone 参数,支持多区域的负载均衡


http {
	#sharing cache area
	lua_shared_dict dynamic_eureka_balancer 128m;
	init_worker_by_lua_block {
		-- init eureka balancer
		local file = require "resty.dynamic_eureka_balancer"
		local balancer = file:new({dict_name="dynamic_eureka_balancer"})
		
		--eureka server list, zone: DEV,UAT
		balancer.set_eureka_service_url({"127.0.0.1:6666", "127.0.0.1:7777"}, 'DEV')
		balancer.set_eureka_service_url({"127.0.0.1:8888", "127.0.0.1:9999"}, 'UAT')
		
		--eureka basic authentication, zone: DEV,UAT
		--use this setting if eureka has enabled basic authentication. 
		--note: basic authentication must use BASE64 encryption in `user:password` format
 --balancer.set_eureka_service_basic_authentication("", 'DEV')
		--balancer.set_eureka_service_basic_authentication("", 'UAT')
		
		--The service name that needs to be monitored, zone: DEV,UAT
		balancer.watch_service({"zuul", "client"}, 'DEV')
		balancer.watch_service({"zuul", "client"}, 'UAT')
	}
	
	upstream springcloud_cn_dev {
		server 127.0.0.1:666; # Required, because empty upstream block is rejected by nginx (nginx+ can use 'zone' instead)
		
		balancer_by_lua_block { 
		
			--The zuul name that needs to be monitored
			local service_name = "zuul"
			
			local file = require "resty.dynamic_eureka_balancer"
			local balancer = file:new({dict_name="dynamic_eureka_balancer"}) 
			
			--balancer.ip_hash(service_name, 'DEV') --IP Hash LB, zone: DEV,UAT
			balancer.round_robin(service_name, 'DEV') --Round Robin LB, zone: DEV,UAT
		}
	}
	
	upstream springcloud_cn_uayt {
		server 127.0.0.1:666; # Required, because empty upstream block is rejected by nginx (nginx+ can use 'zone' instead)
		
		balancer_by_lua_block { 
		
			--The zuul name that needs to be monitored
			local service_name = "zuul"
			
			local file = require "resty.dynamic_eureka_balancer"
			local balancer = file:new({dict_name="dynamic_eureka_balancer"}) 
			
			--balancer.ip_hash(service_name, 'UAT') --IP Hash LB
			balancer.round_robin(service_name, 'UAT') --Round Robin LB
		}
	}
 server {
 listen 80;
 server_name dev.springcloud.cn;
		
		location / {
			proxy_pass http://springcloud_cn_dev/;
			proxy_set_header Host $http_host;
			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_set_header X-Forwarded-Proto $scheme;
		}
 error_page 500 502 503 504 /50x.html;
 location = /50x.html {
 root html;
 }
	}
 server {
 listen 80;
 server_name uat.springcloud.cn;
		
		location / {
			proxy_pass http://springcloud_cn_uat/;
			proxy_set_header Host $http_host;
			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_set_header X-Forwarded-Proto $scheme;
		}
 error_page 500 502 503 504 /50x.html;
 location = /50x.html {
 root html;
 }
	}
}

About

🍁 基于Lua的Spring Cloud网关高可用通用Ngnix插件

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Lua 100.0%

AltStyle によって変換されたページ (->オリジナル) /