windowsnginx配置负载均衡
1、官网下载nginx稳定版
2、下载之后,解压到指定的目录
3、在nginx目录下打开cmd控制台,输入start nginx ,然后在浏览器页面输入localhost,出现如下界面则表示安装成功。默认监听80端口号。
4、负载均衡配置(打开nginx.conf)
weight代表权重越大则访问该服务的次数占比就越多
设置负载均衡配置(lezu可以任意命令)
-
-
#user nobody;
-
worker_processes 1;
-
-
#error_log logs/error.log;
-
#error_log logs/error.log notice;
-
#error_log logs/error.log info;
-
-
#pid logs/nginx.pid;
-
-
-
events {
-
worker_connections 1024;
-
}
-
-
-
http {
-
include mime.types;
-
default_type application/octet-stream;
-
-
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
-
# '$status $body_bytes_sent "$http_referer" '
-
# '"$http_user_agent" "$http_x_forwarded_for"';
-
-
#access_log logs/access.log main;
-
-
sendfile on;
-
#tcp_nopush on;
-
-
#keepalive_timeout 0;
-
keepalive_timeout 65;
-
-
#gzip on;
-
-
#负载均衡配置
-
upstream lezu{
-
server 127.0.0.1:8080 weight=1;
-
server 127.0.0.1:8081 weight=1;
-
}
-
-
server {
-
listen 80;
-
server_name localhost;
-
-
#charset koi8-r;
-
-
#access_log logs/host.access.log main;
-
-
-
location / {
-
root html;
-
index index.html index.htm;
-
proxy_pass http://lezu;
-
}
-
-
#error_page 404 /404.html;
-
-
# redirect server error pages to the static page /50x.html
-
#
-
error_page 500 502 503 504 /50x.html;
-
location = /50x.html {
-
root html;
-
}
-
-
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
-
#
-
#location ~ \.php$ {
-
# proxy_pass http://127.0.0.1;
-
#}
-
-
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
-
#
-
#location ~ \.php$ {
-
# root html;
-
# fastcgi_pass 127.0.0.1:9000;
-
# fastcgi_index index.php;
-
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
-
# include fastcgi_params;
-
#}
-
-
# deny access to .htaccess files, if Apache's document root
-
# concurs with nginx's one
-
#
-
#location ~ /\.ht {
-
# deny all;
-
#}
-
}
-
-
-
# another virtual host using mix of IP-, name-, and port-based configuration
-
#
-
#server {
-
# listen 8000;
-
# listen somename:8080;
-
# server_name somename alias another.alias;
-
-
# location / {
-
# root html;
-
# index index.html index.htm;
-
# }
-
#}
-
-
-
# HTTPS server
-
#
-
#server {
-
# listen 443 ssl;
-
# server_name localhost;
-
-
# ssl_certificate cert.pem;
-
# ssl_certificate_key cert.key;
-
-
# ssl_session_cache shared:SSL:1m;
-
# ssl_session_timeout 5m;
-
-
# ssl_ciphers HIGH:!aNULL:!MD5;
-
# ssl_prefer_server_ciphers on;
-
-
# location / {
-
# root html;
-
# index index.html index.htm;
-
# }
-
#}
-
-
}
配置完成之后通过nginx -s reload命令来刷新配置文件
5、springboot启动两个项目设置端口
--server.port=8080
--server.port=8081
6、通过接口测试工具进行测试访问
-
/**
-
* 单机业务
-
*
-
* @return
-
*/
-
-
public String deductStock1() throws InterruptedException {
-
/**
-
* 单机下单操作
-
* 存在的问题:
-
* 并发量过大会存在超卖
-
*/
-
//商品数量
-
// int stock = Integer.parseInt(redisTemplate.opsForValue().get("stock"));
-
// if (stock > 0) {
-
// int resultStock = stock - 1;
-
// redisTemplate.opsForValue().set("stock", resultStock "");
-
// System.out.println("扣减成功,剩余库存:" resultStock);
-
// } else {
-
// System.out.println("扣减失败,库存不足!");
-
// }
-
-
/**
-
* 使用synchronized来进行加锁操作
-
* 存在的问题:
-
* 分布式多台机器应用下会存在超卖
-
*/
-
synchronized (this) {
-
int stock = Integer.parseInt(redisTemplate.opsForValue().get("stock"));
-
if (stock > 0) {
-
int resultStock = stock - 1;
-
redisTemplate.opsForValue().set("stock", resultStock "");
-
System.out.println("扣减成功,剩余库存:" resultStock);
-
} else {
-
System.out.println("扣减失败,库存不足!");
-
}
-
}
-
-
/**
-
* 使用ReentrantLock加锁
-
* 存在的问题:
-
* 分布式多台机器应用下会存在超卖
-
*/
-
// lock.lock();
-
// try {
-
// int stock = Integer.parseInt(redisTemplate.opsForValue().get("stock"));
-
// if (stock > 0) {
-
// int resultStock = stock - 1;
-
// redisTemplate.opsForValue().set("stock", resultStock "");
-
// System.out.println("扣减成功,剩余库存:" resultStock);
-
// } else {
-
// System.out.println("扣减失败,库存不足!");
-
// }
-
// } catch (Exception e) {
-
// e.printStackTrace();
-
// return "error";
-
// } finally {
-
// lock.unlock();
-
// }
-
-
-
return "success";
-
}
成功实现负载均衡效果
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
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
photoshop蒙版画笔没反应怎么办
PHP中文网 06-24