1

Nginx与LUA(2)

 1 year ago
source link: https://blog.51cto.com/u_15817148/6004585
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Nginx与LUA(2)

精选 原创

湘王爱娟娟 2023-01-12 17:53:34 博主文章分类:技术 ©著作权

文章标签 nginx lua 负载均衡 文章分类 Java 编程语言 yyds干货盘点 阅读数140

您好,我是湘王,这是我的51CTO博客,欢迎您来,欢迎您再来~


除了反向代理,Nginx另一个主要的功能就是「负载均衡」。

所谓负载均衡,就是将请求分摊到多个服务器上执行,从而减轻单台服务器的访问压力。负载均衡一般都需要同时配置反向代理,通过反向代理跳转到指定的服务器上。

Nginx与LUA(2)_负载均衡

Nginx目前支持自带三种负载均衡策略,还有两种常用的第三方策略。

先准备好环境:

1、先安装三台Linux虚拟机,每台虚拟机上安装好JDK环境(不想装虚拟机,docker也可以)

2、开发一个最简单的SpringBoot应用

3、分别部署到其中两台服务器上,一台叫server01,一台叫server02

4、另一台服务器安装Nginx,做负载均衡,叫做server03

Nginx与LUA(2)_负载均衡_02
Nginx与LUA(2)_lua_03
Nginx与LUA(2)_nginx_04

在application.properties文件中增加:server.port=8080

修改启动类,排除数据源的自动加载:

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)

增加Controller类:

Nginx与LUA(2)_nginx_05

打包应用,并上传到server02,启动应用

java -jar nginx-0.0.1-SNAPSHOT.jar

修改Controller类

Nginx与LUA(2)_nginx_06

打包应用,并上传到server03,启动应用

java -jar nginx-0.0.1-SNAPSHOT.jar

访问server01和server02(或者docker)的地址:

http://172.16.185.130:8080/test?username=test1

http://172.16.185.131:8080/test?username=test1

使用Nginx的默认方式,也就是轮询:每个请求按顺序轮流地分配到不同的后端服务器。如果某些后端服务器宕机或离线,也能自动剔除。

修改配置:

Nginx与LUA(2)_lua_07

重新加载Nginx:

cd /usr/local/nginx/sbin/

./nginx -s reload

访问server03的地址:http://172.16.185.136/test?username=test1

第n次访问会返回「server02」

第n+1次访问会返回「server03」

或者相反。

说明配置已生效。

现在给轮询增加一下权重,避免平均主义:每个请求指定轮询几率,weight和访问比率成正比。这可以用于后端服务器性能不均的情况,如果某些后端服务器宕机或离线,也能自动剔除。

修改配置,增加weight关键字:

Nginx与LUA(2)_负载均衡_08

重新加载Nginx:

cd /usr/local/nginx/sbin/

./nginx -s reload

再访问server03:​ ​http://172.16.185.136/test?username=test1​

连续访问多次的结果:

多数访问会返回「server02」

少数访问会返回「server03」

大概就是三七开,说明配置已生效。

轮询和权重的方式只能满足无状态的或者幂等的业务应用。但很多时候业务需要满足一个客户只能访问一个服务器的条件因此,这种情况就需要采用iphash方式来分配后端服务器。

修改配置,增加ip_hash关键字:

Nginx与LUA(2)_负载均衡_09

重新加载Nginx:

cd /usr/local/nginx/sbin/

./nginx -s reload

再次访问server03:http://172.16.185.136/test?username=test1

连续访问多次的结果:如果每次访问都只返回了「server01」或者「server02」,则说明配置已生效。

除了轮询、权重和iphash,另外还可以通过第三方插件来设置负载均衡的方式:

如果需要按后端服务器的响应时间来分配请求的话,可以使用第三方插件fair。

1、下载地址:https://github.com/gnosek/nginx-upstream-fair

(或者https://www.nginx.com/resources/wiki/modules/index.html)

2、下载后解压到/home/work/upstream-fair/

3、安装时增加参数:./configure --add-module=/home/work/upstream-fair/

4、错误「'ngx_http_upstream_srv_conf_t'没有名为'default_port'的成员」的解决办法:

cd /home/work/upstream-fair/

Linux输入:sed -i 's/default_port/no_port/g' ngx_http_upstream_fair_module.c

mac输入:sed -i '' 's/default_port/no_port/g' ngx_http_upstream_fair_module.c

修改配置,将关键字ip_hash改为fair:

Nginx与LUA(2)_lua_10

重新加载Nginx:

cd /usr/local/nginx/sbin/

./nginx -s reload

访问server03:http://172.16.185.136/test?username=test1

连续访问多次的结果:

有时返回「server02」

有时返回「server03」

既不是轮询、也不是权重和ip,这说明配置已生效。

另外一种第三方插件是「一致性hash」:它可以根据参数采取不同的方式,将请求均匀映射到后端服务器:

1、根据客户端ip映射;

2、根据客户端uri映射;

3、根据客户端参数映射。

安装插件:

1、下载:https://github.com/replay/ngx_http_consistent_hash

(或者https://www.nginx.com/resources/wiki/modules/index.html)

2、下载后解压到/home/work/upstream-hash/

3、安装时增加参数:./configure --add-module=/home/work/upstream-hash/

4、修改配置,将关键字fair改为consistent_hash $request_uri

Nginx与LUA(2)_lua_11

重新加载Nginx:

cd /usr/local/nginx/sbin/

./nginx -s reload

访问server03:http://172.16.185.136/test?username=test1

连续访问多次的结果:每次访问都只返回了「server01」或者「server02」

说明配置已生效。


感谢您的大驾光临!咨询技术、产品、运营和管理相关问题,请关注后留言。欢迎骚扰,不胜荣幸~


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK