• Stars
    star
    137
  • Rank 266,121 (Top 6 %)
  • Language
    Lua
  • License
    Apache License 2.0
  • Created over 6 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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

More Repositories

1

spring-cloud-code

🔥《重新定义Spring Cloud实战》实体书对应源码,欢迎大家Star点赞收藏
Java
1,893
star
2

Moss

Moss(莫斯)-Spring Cloud体系的服务治理平台,让Spring Cloud应用不再流浪!欢迎Star!
Java
1,361
star
3

spring-cloud-document

Spring Cloud中国社区线下沙龙文档
772
star
4

spring-cloud-gray

Spring Cloud版本控制和灰度starter
Java
575
star
5

spring-cloud-dubbo

spring-cloud-dubbo
Java
442
star
6

venus-cloud-feign

venus-cloud-feign-对Spring Cloud Feign的增强
Java
165
star
7

eureka-admin

Eureka Server管控平台
JavaScript
160
star
8

spring-cloud-admin

基于Spring Cloud的微服务治理平台
Java
45
star
9

spring-cloud-zuul-nacos

zuul集成Nacos实现动态路由
Java
45
star
10

codegen

专注于Spring Cloud的代码生成器,http://start.springcloud.cn
Java
44
star
11

spring-cloud-sharding-jdbc-demo

Spring Cloud与sharding-jdbc的整合demo
Java
40
star
12

sample-zuul-swagger2

🌴 A sample for zuul-swagger2 to test original services
Java
38
star
13

spring-cloud-docs

docs.springcloud.cn文档
30
star
14

spring-cloud-catalog

☀️《重新定义Spring Cloud实战》实体书目录
29
star
15

spring-cloud-cm

The cm communication component of Spring Cloud for kubernetes.
Java
17
star
16

spring-cloud-zuul2

spring-cloud-zuul2
13
star
17

spring-cloud-eureka

http://eureka.springcloud.cn/
CSS
13
star
18

spring-cloud-codegen-ui

Spring Cloud代码生成器界面
Vue
13
star
19

spring-cloud-gateway-admin

spring-cloud-gateway的管控平台,简称SCGA
12
star
20

spring-cloud-feignplus

Spring Cloud OpenFeign增强
Java
11
star
21

spring-cloud-fusion

Spring Cloud 跨服务数据聚合小框架,解决服务拆分的静态数据聚合之痛
Java
9
star
22

spring-cloud-discovery

对Spring Cloud的服务注册与发现进行增强和管控处理
Java
9
star
23

cms

Spring Cloud中国社区新官网
HTML
9
star
24

spring-cloud-book

《重新定义Spring Cloud实战》实体书
8
star
25

spring-cloud-ddd

Spring Cloud与DDD框架的结合-https://xujin.org/projects/halo/overview/
7
star
26

spring-cloud-muses

记录社区成员的实战记录和思考
7
star
27

Spring-Cloud-QA

中国最专业的Spring Cloud QA问答记录
6
star
28

springcloud.github.io

http://docs.springcloud.cn
HTML
5
star
29

spring-cloud-nacos

增强Spring Cloud生态圈-适配支持Nacos,替换Spring Cloud Config和Eureka,zk,consul等.http://nacos.net
3
star
30

spring-cloud-radar

Java
3
star
31

springcloud-bbs

http:/bbs.springcloud.cn
JavaScript
3
star
32

spring-cloud-translation

Spring Cloud中国社区文档翻译
3
star
33

spring-cloud-ribbon-extension

spring-cloud-ribbon的延伸和扩展
2
star
34

spring-cloud-grpc

Spring Cloud与GRPC的无缝集成
2
star
35

spring-cloud-sentinel

spring-cloud-sentinel
1
star
36

spring-cloud-etcd

Spring Cloud中国社区维护的etcd
1
star