Giter Club home page Giter Club logo

nginx-zuul-dynamic-lb's Introduction

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"})
		
		--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;
        }
	}
}

nginx-zuul-dynamic-lb's People

Contributors

lovnx avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.