• windows中nginx配置负载均衡


    1、官网下载nginx稳定版

    nginx官网

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

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

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

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

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

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

    配置完成之后通过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. * 使用synchronized来进行加锁操作
    24. * 存在的问题:
    25. * 分布式多台机器应用下会存在超卖
    26. */
    27. synchronized (this) {
    28. int stock = Integer.parseInt(redisTemplate.opsForValue().get("stock"));
    29. if (stock > 0) {
    30. int resultStock = stock - 1;
    31. redisTemplate.opsForValue().set("stock", resultStock + "");
    32. System.out.println("扣减成功,剩余库存:" + resultStock);
    33. } else {
    34. System.out.println("扣减失败,库存不足!");
    35. }
    36. }
    37. /**
    38. * 使用ReentrantLock加锁
    39. * 存在的问题:
    40. * 分布式多台机器应用下会存在超卖
    41. */
    42. // lock.lock();
    43. // try {
    44. // int stock = Integer.parseInt(redisTemplate.opsForValue().get("stock"));
    45. // if (stock > 0) {
    46. // int resultStock = stock - 1;
    47. // redisTemplate.opsForValue().set("stock", resultStock + "");
    48. // System.out.println("扣减成功,剩余库存:" + resultStock);
    49. // } else {
    50. // System.out.println("扣减失败,库存不足!");
    51. // }
    52. // } catch (Exception e) {
    53. // e.printStackTrace();
    54. // return "error";
    55. // } finally {
    56. // lock.unlock();
    57. // }
    58. return "success";
    59. }

     成功实现负载均衡效果

     

     

    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"

  • 相关阅读:
    LINUX入门篇【6】----第一个LINUX小程序---进度条及相关知识讲解
    183. 从不订购的客户—not in()、左连接
    java8 (jdk 1.8) 新特性——Lambda
    机器学习笔记 - 构建自己的视频分类模型的分步教程
    为什么我的文章就是审核不通过或者不推荐呢?
    【AGC】SDK未经用户同意获取AndroidID问题
    运动装备品牌排行榜,运动爱好者必备好物分享
    使用 PHP WorkerMan 构建 WebSocket 全双工群聊通信(二)
    浅谈无脚本自动化测试
    Java的封装
  • 原文地址:https://blog.csdn.net/weixin_42169734/article/details/124953186