负载均衡实现

1.安装nginx

    sudo apt insta11 libgd-dev

    wget http://nginx.org/download/nginx-1.22.1.tar.gz

    tar -zvxf nginx-1.22.1.tar.g2

    cd nginx-1.22.1

2.指定安装位置,执行安装之后会创建指定文件夹/www/env/nginx

./configure--prefix=/www/env/nginx--with-pcre
--with-http_ssl_module--with-http_v2_module
--with-http_realip_module--with-http_addition_module--with-http_sub_module
--with-http_dav_module
--with-http_fiv_module
--with-http_mp4_module
--with-http_gunzip_module
--with-http_gzip_static_module--with-http_random_index_module--with-http_secure_ink_module--with-http_stub_status_module--with-http_auth_request_module--with-http image filter module

./configure --prefix=/home/nginx--with-pcre
--with-http_ss1_module
--with-http_v2_module
--with-http_realip_module--with-http_addition_module--with-http_sub_module
--with-http_dav_module
--with-http_fiv_module
--with-http_mp4_module
--with-http_gunzip_module
--with-http_gzip_static_module--with-http_random_index_module--with-http_secure_link_module--with-http_stub_status_module--with-http_auth_request_module--with-http image filter_module--with-http_slice_module
--with-mai]
--with-threads
--with-file-aio
--with-stream
--with-mail_ss1_module
--with-stream_ssl module

启动nginx: ps -ef | grep nginx

首先通过SpringBoot+Freemarker快速搭建一个WEB项目:springboot-web-nginx然后在该项目中,创建一个IndexNginxController.java文件,逻辑如下:

    public class IndexNginxController {
        @Value("${server.port}")
        private String port;
    ​
        @RequestMapping("/")
        public ModelAndView index(){
            ModelAndView model = new ModelAndView();
            model.addObject("port", port);
            model.setViewName("index");
            return model;
        }
    }

在该Controller类中,存在一个成员变量:port,它的值即是从application.properties配置文件中获取server.port值。当出现访问/资源的请求时,跳转前端index页面,并将该值携带返回。

前端的index.ftl文件代码如下:

    <html>
        <head>
            <title>Nginx演示页面</title>
            <link href="nginx_style.css" rel="stylesheet" type="text/css"/>
        </head>
        <body>
            <div style="border: 2px solid red;margin: auto;width: 800px;text-align: center">
                <div  id="nginx_title">
                    <h1>$hello world 172.16.100.10{port}号!</h1>
                </div>
            </div>
        </body>
    </html>

复制代码

从上可以看出其逻辑并不复杂,仅是从响应中获取了port输出。

前提工作准备就绪后,再简单修改一下nginx.conf的配置:

    upstream nginx_boot{
       # 30s内检查心跳发送两次包,未回复就代表该机器宕机,请求分发权重比为1:2
       server 192.168.100.25:8080 weight=100 max_fails=2 fail_timeout=30s;
       server 192.168.100.10:8090 weight=200 max_fails=2 fail_timeout=30s;
       # 这里的IP请配置成你WEB服务所在的机器IP
    }
    ​
    server {
        location / {
            root   html;
            # 配置index的地址,加上index.ftl。
            index  index.html index.htm index.jsp index.ftl;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            # 请求交给名为nginx_boot的upstream上
            proxy_pass http://nginx_boot;
        }
    }

再启动Nginx,然后再启动两个web服务,第一个WEB服务启动时,在application.properties配置文件中,将端口号改为8080,第二个WEB服务启动时,将其端口号改为8090

  • 22
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值