記錄一下若依項目利用nginx實現負載均衡及保持會話的步驟。
此次作為試驗性的測試,為了方便在本地window的環境上實現。
具體步驟:
1、安裝兩個tomcat8,可以下載一個后,另一個復制即可,下載地址:
https://tomcat.apache.org/download-80.cgi
注意為了避免端口沖突的問題,需要進入D:\tomcat-9080\conf\目錄,打開server.xml文件,修改下面兩個地方:
(1)<Server port="8006" shutdown="SHUTDOWN">
修改這個port=”8006”,使得它的關閉端口和另一個關閉端口不發生沖突。
(2)<Connector port="9080" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" />
修改port=”9080”,使得它的連接端口和另一個不沖突。
(3)<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
修改這個port=”8010”,使得它的AJP端口和另一個不沖突。
2、安裝nginx,我下載的是穩定版本,地址:http://nginx.org/download/nginx-1.12.2.zip。
解壓即可使用,在啟動前,必須要對nginx進行一下配置才可實現負載均衡的功能,打開conf文件夾,下面有一個nginx.conf文件,配置如下:
#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;
#以下四行是新添加的,兩個IP是兩個tomcat的訪問地址,weight表示分給該服務器的請求比重,兩個都是1,則按照1:1來分配,
upstream netitcast.com{
server 127.0.0.1:8080 weight=1;
server 127.0.0.1:9080 weight=1;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#一下兩行是進行修改的,http://netitcast.com和上面添加的要保持一致
location / {
proxy_pass http://netitcast.com;
proxy_connect_timeout 1; #表示是1秒后超時會連接到另外一台服務器
proxy_read_timeout 1;
proxy_send_timeout 1;
}
#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;
# }
#}
}
3、安裝redis,redis官方沒有64位的Windows下的可執行程序,目前有個開源的托管在github上, 地址:https://github.com/ServiceStack/redis-windows,
下載文件並解壓到自己的電腦目錄下,我這邊選擇的是redis-64.3.0.503版本。
4、選擇若依項目,此處選擇RuoYi-duzunwu512,該版本集成了Redis實現Session共享多模塊(支持Cacheable緩存),地址:https://gitee.com/duzunwu512/RuoYi。
將該版本的若依在本地環境中搭建起來,修改數據庫配置,將ruoyi-admin項目的pom.xml中改成<packaging>war</packaging>,即將其打成war包。
5、將war包復制到對應兩個tomcat的webapp目錄下,分別啟動兩個tomcat,在cmd中cd到對應tomcat的bin目錄下,運行startup.bat即可。
此處分別修改一下ruoyi-admin\WEB-INF\classes\templates下的login.html文件,更改此處,主要目的是為了區分后續的訪問到底訪問到了那個tomcat。
<h4>歡迎使用 <strong>若依 后台管理系統【Master】</strong></h4>
6、啟動nginx,運行start nginx即可。詳細可參考
https://www.cnblogs.com/qianzf/p/6809427.html
啟動redis,運行redis-server redis.windows.conf,詳細可參考
https://blog.csdn.net/qq_33450681/article/details/78250664
7、本地訪問http://localhost/ruoyi-admin/index,不斷刷新,可看到會不斷切換至不同的tomcat項目中,登錄后,故意停止一個tomcat,仍然可以正常訪問,自此基本搭建成功。