Windows+Nginx+Tomcat搭建負載均衡和集群環境同時實現session共享(一)

摘要:隨著網站的訪問量越來越多,所以就考慮給網站增加服務器了,現在比較流行的做法就是給網站做集群環境,下面我把我做的過程記錄一下,方便日后查看,同時也希望可以幫助到有需要的朋友!

一:首先是環境:

1.jdk 1.6.0_45

2.tomcat 6.0.44

3.nginx 1.8.0


二:jdk,tomcat,nginx的安裝:

1.jdk的安裝請參考:點擊打開鏈接

2.nginx的安裝步驟

(1).首先下載nginx的windows版本,我這里下載的nginx 1.8.0版本,下載地址:點擊打開鏈接

(2).下載完成后,直接解壓到一個沒有中文的路徑下,我這里是解壓到:D:\server\nginx-1.8.0

(3).通過dos命令進入nginx-1.8.0目錄下啟動nginx即可

(4)Windows下操作Nginx命令

1.啟動
nginx.exe
start nginx
2.停止
nginx -s stop
nginx -s quit

stop表示立即停止nginx,不保存相關信息

quit表示正常退出nginx,并保存相關信息

3.重啟
nginx -s reload
重啟(因為改變了配置,需要重啟)

3.tomcat的安裝(直接到tomcat的官網下載解壓縮版的即可)


三:把下載下載的tomcat分別復制3個,一共是個,其中三個做qdksDemo的集群環境用,另外三個做qdkyDemo的集群環境用,具體截圖如下:


復制完成后,要分別修改每個tomcat的端口和session共享的配置,具體的配置如下:

1.第一處要修改的端口號

<Server port="8005" shutdown="SHUTDOWN">
2.第二處要修改的端口號

<Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
3.第三處要修改的端口號

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
4.第四處要增加的session共享配置,這個可以查看tomcat的官方文檔,里面有配置直接復制到server.xml中就可以,地址: http://tomcat.apache.org/tomcat-6.0-doc/cluster-howto.html

<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"channelSendOptions="6"><Manager className="org.apache.catalina.ha.session.BackupManager"expireSessionsOnShutdown="false"notifyListenersOnReplication="true"mapSendOptions="6"/><!--<Manager className="org.apache.catalina.ha.session.DeltaManager"expireSessionsOnShutdown="false"notifyListenersOnReplication="true"/>--><Channel className="org.apache.catalina.tribes.group.GroupChannel"><Membership className="org.apache.catalina.tribes.membership.McastService"address="228.0.0.4"port="45564"frequency="500"dropTime="3000"/><Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"address="auto"port="5000"selectorTimeout="100"maxThreads="6"/><Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter"><Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/></Sender><Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/><Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/><Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/></Channel><Valve className="org.apache.catalina.ha.tcp.ReplicationValve"filter=".*\.gif|.*\.js|.*\.jpeg|.*\.jpg|.*\.png|.*\.htm|.*\.html|.*\.css|.*\.txt"/><Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"tempDir="/tmp/war-temp/"deployDir="/tmp/war-deploy/"watchDir="/tmp/war-listen/"watchEnabled="false"/><ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/></Cluster>

這樣一個tomcat的配置文件就修改完成了,其他5個tomcat的配置和這個流程一樣,只是對于的端口不能一樣


四:nginx的配置:

1.首先在nginx的目錄下新建一個新的文件夾來存放不同項目之間的集群配置文件,如下圖:


2.在manyvhost文件夾里面新建一個配置文件,分別對于兩個項目的集群配置,如下圖:


3.qdks.conf和qdky.conf的配置內容如下:

(1).qdks.conf

upstream  qdks  {  server   localhost:8081 weight=1;  server   localhost:8082 weight=1;server   localhost:8083 weight=1;
}
server {listen       8086;server_name 192.168.1.103;error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}root /data/projects/ycp/bill;# - rewrite: if( path ~ "^/assets/(.*)" ) goto "/public/assets/$1"#    location ~ ^/static/assets/(.*)$#    {#alias /data/projects/payment/web/public/assets/$1;#      access_log off;#      #expires 3d;#    }location / {index  index.html index.htm  index.jsp;}location ~ .* {# proxy_pass_header Server;proxy_set_header Host $http_host;# proxy_redirect off;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Scheme $scheme;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;add_header Pragma "no-cache";proxy_pass http://qdks;}rewrite ^/admin/?$ /admin/login redirect;# for rewriterewrite ^/(channel|admin|mobile|api|web)/(.*)$ /public/index.php/$2 last;#redirect to mobile wap#rewrite ^$ /m redirect;#rewrite ^/$ /mobile/user redirect;}

(2).qdky.conf

upstream  qdky  {  server localhost:7081 weight=1;  server localhost:7082 weight=1;server localhost:7083 weight=1;
}
server {listen       7086;server_name  192.168.1.103;error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}root /data/projects/ycp/bill;# - rewrite: if( path ~ "^/assets/(.*)" ) goto "/public/assets/$1"#    location ~ ^/static/assets/(.*)$#    {#alias /data/projects/payment/web/public/assets/$1;#      access_log off;#      #expires 3d;#    }location / {index  index.html index.htm  index.jsp;}location ~ .* {# proxy_pass_header Server;proxy_set_header Host $http_host;# proxy_redirect off;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Scheme $scheme;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;add_header Pragma "no-cache";proxy_pass http://qdky;}rewrite ^/admin/?$ /admin/login redirect;# for rewriterewrite ^/(channel|admin|mobile|api|web)/(.*)$ /public/index.php/$2 last;#redirect to mobile wap#rewrite ^$ /m redirect;#rewrite ^/$ /mobile/user redirect;}

這兩個配置文件就是我當前環境下的配置文件,其中qdks的項目的端口號8086,下面對應三個tomcat的端口為:8081,8082,8083;qdky的項目的端口號7086,下面對應三個tomcat的端口號為:7081,7082,7083


4.最后在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  5000;
}http {include       mime.types;default_type  application/octet-stream;#隱藏nginx的版本號server_tokens off;#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;#access日志存放路徑和格式access_log  logs/access.log;#提升文件傳輸性能sendfile        on;#tcp_nopush     on;#keepalive_timeout  0;#keepalive_timeout  65;#設置客戶端能夠請求的單個文件大小client_max_body_size 300m;keepalive_timeout  75;#proxy參數#跟后端服務器連接的超時時間,發起握手等候響應超時時間proxy_connect_timeout 5;#連接成功后,等候后端服務器的響應時間proxy_read_timeout 600;#后端服務器數據回傳時間proxy_send_timeout 600;#代理請求緩存區proxy_buffer_size 16k;#同上,告訴nginx保存單個用的幾個buffer、最大用多空間proxy_buffers 4 64k;#如果系統很忙時可以申請更大的proxy_buffers,官方推薦*2proxy_busy_buffers_size 128k;proxy_temp_file_write_size 128k;#gzip  on;#開啟壓縮功能gzip on;gzip_min_length 1k;gzip_buffers 4 16k;gzip_http_version 1.1; gzip_comp_level 2;#壓縮級別從低到高1-9gzip_types text/plain application/x-javascript text/css application/xml;gzip_vary on;#qdksDemo和qdkyDemo項目的虛擬目錄(用絕對路徑表示)include  D:/server/nginx-1.8.0/manyvhost/qdks.conf;include  D:/server/nginx-1.8.0/manyvhost/qdky.conf;# another virtual host using mix of IP-, name-, and port-based configuration##server {#    listen       80;#    listen       somename:8080;#    server_name  somename  alias  another.alias;#    location / {#        root   html;#        index  index.html index.htm;#    }#}# HTTPS server##server {#    listen       443;#    server_name  localhost;#    ssl                  on;#    ssl_certificate      cert.pem;#    ssl_certificate_key  cert.key;#    ssl_session_timeout  5m;#    ssl_protocols  SSLv2 SSLv3 TLSv1;#    ssl_ciphers  HIGH:!aNULL:!MD5;#    ssl_prefer_server_ciphers   on;#    location / {#        root   html;#        index  index.html index.htm;#    }#}}

五:分別部署對應的項目,來測試我們上面配置的集群環境是否正常

1.下載對應的項目源代碼,下載后解壓到你的本地workspace下面,我的路徑是:D:\workspace_qdexam\qdksDemo和D:\workspace_qdexam\qdkyDemo

qdksDemo的下載地址:http://download.csdn.net/detail/sxdtzhaoxinguo/9187453

qdkyDemo的下載地址:http://download.csdn.net/detail/sxdtzhaoxinguo/9187455


2.分別部署到對應的tomcat下面,這里我采用的是直接映射路徑的方法

(1).qdksDemo的部署方法是,在對應的三個tomcat的conf\Catalina\localhost目錄下新建一個配置文件qdksDemo.xml,該配置文件的內容如下:

<Context    path="/qdksDemo"   docBase="D:\workspace_qdexam\qdksDemo\WebContent"     debug="0"    privileged="true">      </Context>

(2).qdkyDemo的部署方法是,在對應的三個tomat的conf\Catalina\localhost目錄下新建一個配置文件qdkyDemo.xml,該配置文件的內容如下:

<Context path="/qdkyDemo"   docBase="D:\workspace_qdexam\qdkyDemo\WebContent" debug="0" privileged="true"></Context>


3.分別啟動這6個tomcat,如下圖:


4.啟動nginx,如下圖:


5.最后通過瀏覽器訪問這兩個項目,如下圖:



分別點擊刷新,你會發現上面的實際訪問端口會變,但是下面的Session ID是不變的,這就說明基于nginx和tocmat的集群搭建成功了!


最后我把我的tomcat和nginx都打包上傳,供大家參考!

tomcat的配置壓縮包下載地址:http://download.csdn.net/detail/sxdtzhaoxinguo/9187521

nginx的配置壓縮包下載地址:http://download.csdn.net/detail/sxdtzhaoxinguo/9187527




本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/443420.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/443420.shtml
英文地址,請注明出處:http://en.pswp.cn/news/443420.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Android開發中調用Spring CXF整合發布的WebService接口為什么拋出異常錯誤?

摘要&#xff1a;最近在協助同事搞Android調用WebService接口&#xff0c;再測試的過程中發現老師報錯&#xff0c;經過baidu&#xff0c;google&#xff0c;終于解決了&#xff0c;現在記錄一下&#xff1a; 一&#xff1a;錯誤信息&#xff1a; 2015-10-28 18:50:39 org.apac…

Eclipse,MyEclipse 安裝SVN插件

摘要&#xff1a;MyEclipse10.5安裝SVN插件最簡單的方式&#xff1a; 一&#xff1a;先到SVN官網下載對應版本的SVN插件包&#xff0c;我這里下載的site-1.10.10版本 下載地址&#xff1a;http://subclipse.tigris.org/servlets/ProjectDocumentList?folderID2240 二&#xf…

MyEclipse 10.5 安裝SVN插件

摘要&#xff1a;MyEclipse10.5安裝SVN插件最簡單的方式&#xff1a; 一&#xff1a;先到SVN官網下載對應版本的SVN插件包&#xff0c;我這里下載的site-1.10.10版本 下載地址&#xff1a;http://subclipse.tigris.org/servlets/ProjectDocumentList?folderID2240 二&#xf…

PHP獲取服務器端的相關信息

摘要&#xff1a;PHP獲取服務端端的相關信息 一&#xff1a;代碼&#xff1a; <!DOCTYPE html> <html> <head><title>第一個PHP程序(獲取服務器信息)</title><meta http-equiv"content-type" content"text/html"; charse…

log4j 標準配置模板:

摘要&#xff1a;log4j在開發中是少不了的&#xff0c;下面貼上我的log4j的配置&#xff0c;該配置文件實現的結果是&#xff0c;每天會生成一個日子文件到指定的目錄下&#xff0c;這樣方便我們隨時查看日志信息&#xff01; log4j.propertieslog4j.rootLoggerINFO,CONSOLE,A …

Sql Server 2005 分頁

摘要&#xff1a;Sql Server 分頁查詢sql! Sql Server 2005分頁查詢數據&#xff0c;適用于手機端和服務器端交互 一&#xff1a;Dao代碼&#xff1a; /*** 分頁查詢研訊分類下的新聞列表*/Overridepublic List<TblNews> getListByYunXunNewId(int cId, int start, int…

Spring+Hibernate+SpringMVC+MySql實現配置多個數據源!

摘要&#xff1a;在日常的工作中&#xff0c;很多時候我們進行項目開發的時候&#xff0c;一個項目有可能不止用到一個數據源&#xff0c;為了提高數據庫的水平伸縮性&#xff0c;需要對多個數據庫實例進行管理&#xff0c;需要配置多數據源! 一&#xff1a;代碼截圖&#xff…

Java之Base64實現文件和字符串之間的轉換

摘要&#xff1a;Java通過Base64加密解密實現文件和字符串之間的轉換&#xff01; Base64.java package com.qdexam.util;import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputS…

app中使用用戶名/郵箱/手機號登錄的思路分析

摘要&#xff1a;app中使用用戶名/郵箱/手機號登錄的思路分析 1.客戶端 客戶端需要傳遞2個參數 account 代表&#xff1a;用戶名/郵箱/手機號 password 代表&#xff1a;密碼這里問題來了&#xff0c;那么如何判斷識別用戶輸入的是用戶名還是郵箱&#xff0c;還是手機號就需要…

java實現ListObject轉List實體類,java實現Object轉對象,java實現Object轉實體類

摘要&#xff1a;在java開發中&#xff0c;我們常常會遇到Object轉對象的情況&#xff0c;最近我就遇到了這個問題&#xff0c;現在記錄一下&#xff0c;方便日后自己查看復習&#xff01; 一&#xff1a;查詢Object類型的集合對象的方法如下&#xff1a; List topicList top…

MySql實現分頁查詢的SQL,mysql實現分頁查詢的sql語句

摘要&#xff1a;MySql數據庫實現分頁查詢的SQL語句寫法&#xff01; 一&#xff1a;分頁需求&#xff1a; 客戶端通過傳遞start(頁碼)&#xff0c;limit(每頁顯示的條數)兩個參數去分頁查詢數據庫表中的數據&#xff0c;那我們知道MySql數據庫提供了分頁的函數limit m,n&…

CentOS下安裝Memcached,Linux下安裝Memcached,centos下安裝memcached,linux下安裝memcached...

摘要&#xff1a;最近在看《大型分布式網站架構設計與實踐》這本書&#xff0c;把其中的學習過程記錄一下&#xff0c;以便日后復習。 一&#xff1a;Memcached簡單介紹和描述&#xff1a; Memcached 是一個高性能的分布式內存對象緩存系統&#xff0c;用于動態Web應用以減輕數…

Spring,ehcache整合報錯

摘要&#xff1a;在做Spring整合ehcache配置的時候出現了下面的錯誤&#xff0c;提示如下&#xff1a;java.lang.ClassNotFoundException: org.springframework.cache.ehcache.EhCacheManagerFactoryBean&#xff0c;經過檢查發現是因為org.springframework.cache.ehcache.EhCa…

Caused by: java.lang.NoClassDefFoundError: org/apache/commons/pool/BasePoolableObjectFactory

摘要&#xff1a;異常信息解決過程記錄 一&#xff1a;異常信息&#xff1a; Caused by: java.lang.NoClassDefFoundError: org/apache/commons/pool/BasePoolableObjectFactoryat java.lang.ClassLoader.defineClass1(Native Method)at java.lang.ClassLoader.defineClass(Cla…

一個微信公眾號接入另一個微信公眾號的內容

摘要&#xff1a;近日來了這么一個需求&#xff0c;就是客戶想在他們的微信公眾號里面直接接入我們微信公眾號的文章&#xff0c;于是我便開始baidu&#xff0c;google&#xff0c;最后發現了一個新的搜索引擎&#xff1a;“搜狗微信搜索”&#xff0c;該搜索引擎是最近才上線的…

SQL Server將數據庫中的表復制到另一個數據庫

在使用SqlServer的過程中&#xff0c;我們可能需要將表從一個數據庫復制到另一個數據庫中&#xff0c;今天&#xff0c;小編為大家介紹這種操作的具體方法及步驟。 一&#xff1a;復制表結構&#xff1a; 1.首先&#xff0c;打開并連接Sql Server&#xff0c;在源數據庫Sourc…

SQL Server cast() 批量更新列內容

摘要&#xff1a;有這樣一個需求&#xff0c;需要把數據表中的列批量更新一下&#xff0c;給后面的數字都增加20&#xff0c;如下圖&#xff1a; 正確的sql如下&#xff1a; update tbl_papers_test set name 執業護士考試兒科護理學cast((cast(substring(name,12,datalength(…

SQL Server 批量更新字段值為ROW_NUMBER()+列名稱

摘要&#xff1a;有這樣一個需求&#xff0c;需要把數據表中的列名稱的數據按行號重新排序并更新一下&#xff0c;這里用到了ROW_NUMBER()函數。 一&#xff1a;需求如下圖&#xff1a; 二&#xff1a;通過執行如下sql語句實現了上面的需求&#xff0c;如下&#xff1a; update…

Maven+Tomcat的熱部署方案

摘要&#xff1a;在日常的開發中&#xff0c;熱部署是個非常重要的事&#xff0c;下面就介紹下Maven&#xff0c;Tomcat如何實現熱部署! 第一步&#xff1a;配置Tomcat的登陸的用戶名與密碼 E:\apache-tomcat-7.0.68\conf\tomcat-users.xml 從第35行開始配置 <role rolenam…

readonly與disabled屬性在css中區別

論readonly與disabled在css中區別 之前在項目中遇到的一個問題&#xff0c;現在想把它記錄下來。 當項目需求上需要在文本框中禁用輸入某個值的時候&#xff0c;我第一個反應是用了disabled&#xff0c;因為之前一直接觸的都是disabled。但當我需要提交FROM表單的時候&#xf…