• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

windowsnginx配置负载均衡

武飞扬头像
EvenBoy
帮助1

1、官网下载nginx稳定版

nginx官网

学新通

2、下载之后,解压到指定的目录

学新通

3、在nginx目录下打开cmd控制台,输入start nginx ,然后在浏览器页面输入localhost,出现如下界面则表示安装成功。默认监听80端口号。

学新通

学新通

 4、负载均衡配置(打开nginx.conf)

学新通

weight代表权重越大则访问该服务的次数占比就越多

学新通

设置负载均衡配置(lezu可以任意命令)

  1.  
     
  2.  
    #user nobody;
  3.  
    worker_processes 1;
  4.  
     
  5.  
    #error_log logs/error.log;
  6.  
    #error_log logs/error.log notice;
  7.  
    #error_log logs/error.log info;
  8.  
     
  9.  
    #pid logs/nginx.pid;
  10.  
     
  11.  
     
  12.  
    events {
  13.  
    worker_connections 1024;
  14.  
    }
  15.  
     
  16.  
     
  17.  
    http {
  18.  
    include mime.types;
  19.  
    default_type application/octet-stream;
  20.  
     
  21.  
    #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  22.  
    # '$status $body_bytes_sent "$http_referer" '
  23.  
    # '"$http_user_agent" "$http_x_forwarded_for"';
  24.  
     
  25.  
    #access_log logs/access.log main;
  26.  
     
  27.  
    sendfile on;
  28.  
    #tcp_nopush on;
  29.  
     
  30.  
    #keepalive_timeout 0;
  31.  
    keepalive_timeout 65;
  32.  
     
  33.  
    #gzip on;
  34.  
     
  35.  
    #负载均衡配置
  36.  
    upstream lezu{
  37.  
    server 127.0.0.1:8080 weight=1;
  38.  
    server 127.0.0.1:8081 weight=1;
  39.  
    }
  40.  
     
  41.  
    server {
  42.  
    listen 80;
  43.  
    server_name localhost;
  44.  
     
  45.  
    #charset koi8-r;
  46.  
     
  47.  
    #access_log logs/host.access.log main;
  48.  
     
  49.  
     
  50.  
    location / {
  51.  
    root html;
  52.  
    index index.html index.htm;
  53.  
    proxy_pass http://lezu;
  54.  
    }
  55.  
     
  56.  
    #error_page 404 /404.html;
  57.  
     
  58.  
    # redirect server error pages to the static page /50x.html
  59.  
    #
  60.  
    error_page 500 502 503 504 /50x.html;
  61.  
    location = /50x.html {
  62.  
    root html;
  63.  
    }
  64.  
     
  65.  
    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  66.  
    #
  67.  
    #location ~ \.php$ {
  68.  
    # proxy_pass http://127.0.0.1;
  69.  
    #}
  70.  
     
  71.  
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  72.  
    #
  73.  
    #location ~ \.php$ {
  74.  
    # root html;
  75.  
    # fastcgi_pass 127.0.0.1:9000;
  76.  
    # fastcgi_index index.php;
  77.  
    # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  78.  
    # include fastcgi_params;
  79.  
    #}
  80.  
     
  81.  
    # deny access to .htaccess files, if Apache's document root
  82.  
    # concurs with nginx's one
  83.  
    #
  84.  
    #location ~ /\.ht {
  85.  
    # deny all;
  86.  
    #}
  87.  
    }
  88.  
     
  89.  
     
  90.  
    # another virtual host using mix of IP-, name-, and port-based configuration
  91.  
    #
  92.  
    #server {
  93.  
    # listen 8000;
  94.  
    # listen somename:8080;
  95.  
    # server_name somename alias another.alias;
  96.  
     
  97.  
    # location / {
  98.  
    # root html;
  99.  
    # index index.html index.htm;
  100.  
    # }
  101.  
    #}
  102.  
     
  103.  
     
  104.  
    # HTTPS server
  105.  
    #
  106.  
    #server {
  107.  
    # listen 443 ssl;
  108.  
    # server_name localhost;
  109.  
     
  110.  
    # ssl_certificate cert.pem;
  111.  
    # ssl_certificate_key cert.key;
  112.  
     
  113.  
    # ssl_session_cache shared:SSL:1m;
  114.  
    # ssl_session_timeout 5m;
  115.  
     
  116.  
    # ssl_ciphers HIGH:!aNULL:!MD5;
  117.  
    # ssl_prefer_server_ciphers on;
  118.  
     
  119.  
    # location / {
  120.  
    # root html;
  121.  
    # index index.html index.htm;
  122.  
    # }
  123.  
    #}
  124.  
     
  125.  
    }

配置完成之后通过nginx -s reload命令来刷新配置文件

学新通

5、springboot启动两个项目设置端口

--server.port=8080

--server.port=8081

学新通

6、通过接口测试工具进行测试访问

http://localhost/deductStock1

学新通

  1.  
    /**
  2.  
    * 单机业务
  3.  
    *
  4.  
    * @return
  5.  
    */
  6.  
    @RequestMapping("/deductStock1")
  7.  
    public String deductStock1() throws InterruptedException {
  8.  
    /**
  9.  
    * 单机下单操作
  10.  
    * 存在的问题:
  11.  
    * 并发量过大会存在超卖
  12.  
    */
  13.  
    //商品数量
  14.  
    // int stock = Integer.parseInt(redisTemplate.opsForValue().get("stock"));
  15.  
    // if (stock > 0) {
  16.  
    // int resultStock = stock - 1;
  17.  
    // redisTemplate.opsForValue().set("stock", resultStock "");
  18.  
    // System.out.println("扣减成功,剩余库存:" resultStock);
  19.  
    // } else {
  20.  
    // System.out.println("扣减失败,库存不足!");
  21.  
    // }
  22.  
     
  23.  
    /**
  24.  
    * 使用synchronized来进行加锁操作
  25.  
    * 存在的问题:
  26.  
    * 分布式多台机器应用下会存在超卖
  27.  
    */
  28.  
    synchronized (this) {
  29.  
    int stock = Integer.parseInt(redisTemplate.opsForValue().get("stock"));
  30.  
    if (stock > 0) {
  31.  
    int resultStock = stock - 1;
  32.  
    redisTemplate.opsForValue().set("stock", resultStock "");
  33.  
    System.out.println("扣减成功,剩余库存:" resultStock);
  34.  
    } else {
  35.  
    System.out.println("扣减失败,库存不足!");
  36.  
    }
  37.  
    }
  38.  
     
  39.  
    /**
  40.  
    * 使用ReentrantLock加锁
  41.  
    * 存在的问题:
  42.  
    * 分布式多台机器应用下会存在超卖
  43.  
    */
  44.  
    // lock.lock();
  45.  
    // try {
  46.  
    // int stock = Integer.parseInt(redisTemplate.opsForValue().get("stock"));
  47.  
    // if (stock > 0) {
  48.  
    // int resultStock = stock - 1;
  49.  
    // redisTemplate.opsForValue().set("stock", resultStock "");
  50.  
    // System.out.println("扣减成功,剩余库存:" resultStock);
  51.  
    // } else {
  52.  
    // System.out.println("扣减失败,库存不足!");
  53.  
    // }
  54.  
    // } catch (Exception e) {
  55.  
    // e.printStackTrace();
  56.  
    // return "error";
  57.  
    // } finally {
  58.  
    // lock.unlock();
  59.  
    // }
  60.  
     
  61.  
     
  62.  
    return "success";
  63.  
    }

 成功实现负载均衡效果

 学新通

 学新通

nginx命令

查看Nginx的版本号:nginx -V

启动Nginx:start nginx

快速停止或关闭Nginx:nginx -s stop

正常停止或关闭Nginx:nginx -s quit

配置文件修改重装载命令:nginx -s reload

查看windows任务管理器下Nginx的进程命令:tasklist /fi "imagename eq nginx.exe"

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfjbach
系列文章
更多 icon
同类精品
更多 icon
继续加载