openresty 负载均衡


openresty 负载均衡

         

                 

                              

负载均衡

      

轮询

server {
    listen       80;
    server_name  localhost;
 
    location / {
        proxy_pass http://web-server;
 
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    } 
}
 
upstream web-server {
    server 192.168.57.10:8080;
    server 192.168.57.11:8080;
}

        

加权轮询

server {
    listen       80;
    server_name  localhost;
 
    location / {
        proxy_pass http://web-server;
 
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    } 
}
 
upstream web-server {
    server 192.168.57.10:8080 weight=1;
    server 192.168.57.11:8080 weight=2;
}

         

最少连接数

server {
    listen       80;
    server_name  localhost;
 
    location / {
        proxy_pass http://web-server;
 
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    } 
}
 
upstream web-server {
	least_conn;

    server 192.168.57.10:8080;
    server 192.168.57.11:8080;
}

        

最短响应时间:需安装nginx-upstream-fair

server {
    listen       80;
    server_name  localhost;
 
    location / {
        proxy_pass http://web-server;
 
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    } 
}
 
upstream web-server {
    fair;
 
    server 192.168.57.10:8080;
    server 192.168.57.11:8080;
}

           

sticky:根据cookie将请求分布到后端服务器上,同一cookie尽可能发送到同一后端服务器

server {
    listen       80;
    server_name  localhost;
 
    location / {
        proxy_pass http://web-server;
 
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    } 
}
 
upstream web-server {
    sticky;
 
    server 192.168.57.10:8080;
    server 192.168.57.11:8080;
}

                 

hash:根据指定的key进行hash,如:$request_uri、$uri等

server {
    listen       80;
    server_name  localhost;
 
    location / {
        proxy_pass http://web-server;
 
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    } 
}
 
upstream web-server {
    hash $request_uri;
 
    server 192.168.57.10:8080;
    server 192.168.57.11:8080;
}

          

ip hash:根据请求的ip地址hash

server {
    listen       80;
    server_name  localhost;
 
    location / {
        proxy_pass http://web-server;
 
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    } 
}
 
upstream web-server {
    ip_hash;
 
    server 192.168.57.10:8080;
    server 192.168.57.11:8080;
}

         

                

                              

应用 1

      

                         

       

HelloController

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello(){
        return "hello 瓜田李下";
    }
}

          

Dockerfile

from java:8

workdir /usr/local/jar
copy hello.jar app.jar

expose 8080
entrypoint ["java", "-jar", "app.jar"]

        

edit configuration ==> docker

               

            

配置完成后,启动应用

               

             


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.7.1)

2022-07-02 04:46:23.750  INFO 1 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication v0.0.1-SNAPSHOT using Java 1.8.0_111 on ea15715ee141 with PID 1 (/usr/local/jar/app.jar started by root in /usr/local/jar)
2022-07-02 04:46:23.758  INFO 1 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to 1 default profile: "default"
2022-07-02 04:46:25.959  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2022-07-02 04:46:25.991  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-07-02 04:46:25.992  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.64]
2022-07-02 04:46:26.180  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-07-02 04:46:26.180  INFO 1 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2258 ms
2022-07-02 04:46:27.086  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-07-02 04:46:27.109  INFO 1 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 4.334 seconds (JVM running for 5.342)

               

             

                              

应用 2

      

                         

         

HelloController

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String hello(){
        return "hello 瓜田李下 2";
    }
}

         

Dockerfile

from java:8

workdir /usr/local/jar
copy hello.jar app.jar

expose 8080
entrypoint ["java", "-jar", "app.jar"]

        

edit configuration ==> docker

               

             

配置完成后,启动应用

               

              


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.7.1)

2022-07-02 04:47:21.717  INFO 1 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication v0.0.1-SNAPSHOT using Java 1.8.0_111 on e27c691f3622 with PID 1 (/usr/local/jar/app.jar started by root in /usr/local/jar)
2022-07-02 04:47:21.729  INFO 1 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to 1 default profile: "default"
2022-07-02 04:47:24.071  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2022-07-02 04:47:24.098  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-07-02 04:47:24.099  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.64]
2022-07-02 04:47:24.277  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-07-02 04:47:24.278  INFO 1 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2402 ms
2022-07-02 04:47:25.292  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-07-02 04:47:25.312  INFO 1 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 4.587 seconds (JVM running for 5.603)

        

              

                              

openresty 配置

      

default.conf

server {
    listen       80;
    server_name  localhost;
 
    location / {
        proxy_pass http://web-server;
 
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    } 
}
 
upstream web-server {
    server 172.18.0.3:8080;
    server 172.18.0.4:8080;
}

          

创建容器

docker run -it -d --net fixed --ip 172.18.0.2 -p 9000:80 \
-v /Users/huli/lua/openresty/conf4/default.conf:/etc/nginx/conf.d/default.conf \
--name openresty4 lihu12344/openresty

          

使用测试

huli@hudeMacBook-Pro ~ % curl localhost:9000/hello
hello 瓜田李下                                                                 huli@hudeMacBook-Pro ~ % curl localhost:9000/hello
hello 瓜田李下                                                                 huli@hudeMacBook-Pro ~ % curl localhost:9000/hello
hello 瓜田李下 2                                                               huli@hudeMacBook-Pro ~ % curl localhost:9000/hello
hello 瓜田李下                                                                 huli@hudeMacBook-Pro ~ % curl localhost:9000/hello
hello 瓜田李下 2                                                               huli@hudeMacBook-Pro ~ % curl localhost:9000/hello
hello 瓜田李下                                                                 huli@hudeMacBook-Pro ~ % curl localhost:9000/hello
hello 瓜田李下 2                                                              

        

               

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OpenResty是一个基于Nginx的Web应用服务器,它使用Lua语言扩展了Nginx的功能。OpenResty可以实现负载平衡的功能,具体实现原理如下: 1. 配置多个后端服务器 在OpenResty中,我们可以通过配置多个upstream来实现负载平衡。upstream是OpenResty中的一个指令,用于配置一个或多个后端服务器,例如: ``` upstream backend { server 192.168.1.2:80; server 192.168.1.3:80; server 192.168.1.4:80; } ``` 上述配置定义了一个名为backend的upstream,包含了三个后端服务器。 2. 配置负载均衡策略 OpenResty支持多种负载均衡策略,例如轮询、IP哈希、最小连接数等。我们可以通过proxy_pass指令的参数来选择负载均衡策略。例如: ``` location / { proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } ``` 上述配置将请求转发给名为backend的upstream,OpenResty会根据负载均衡策略选择一个后端服务器进行处理。 3. 监测后端服务器状态 在实际应用中,后端服务器可能会发生宕机或者故障,为了避免将请求发送到不可用的服务器上,我们需要监测后端服务器的状态。OpenResty提供了health_check指令,可以用于监测后端服务器的状态。例如: ``` upstream backend { server 192.168.1.2:80; server 192.168.1.3:80; server 192.168.1.4:80; health_check interval=5s; } ``` 上述配置将每隔5秒钟检测一次后端服务器的状态,如果某个服务器不能正常响应请求,OpenResty会将其从负载均衡池中移除。 综上所述,OpenResty实现负载平衡的原理是通过配置多个后端服务器和负载均衡策略,同时监测后端服务器的状态,从而实现请求的分发和负载均衡

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值