您好,登錄后才能下訂單哦!
這篇文章主要講解了“nginx+tomcat怎么使用redis session共享”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“nginx+tomcat怎么使用redis session共享”吧!
環境準備
1、準備一臺nginx服務器 ip192.168.1.133 端口81
安裝過程:
#首先安裝依賴: yum -y install gcc-c++ yum -y install pcre pcre-devel yum -y install zlib zlib-devel yum -y install openssl openssl—devel #注意 : 安裝nginx必須使用 root 用戶安裝 #創建一個nginx目錄 mkdir /usr/local/src/nginx #進入到nginx目錄 cd /usr/local/src/nginx #下載或上傳安裝包 wget http://nginx.org/download/nginx.tar.gz 或 rz上傳 #解壓安裝包 tar -xvf nginx.tar.gz #進入到解壓后的目錄 cd nginx # 下面 才開始正式安裝 #把nginx安裝到指定用戶的目錄 mkdir -p /ucenter/soft/nginx #安裝配置 prefix為安裝目錄 user為用戶 group為 組 ./configure --prefix=/ucenter/soft/nginx --user=ucenter --group=ucenter #編譯 make #安裝 make install #在linux系統中由于非root用戶不能占用80端口,所以需要使普通用戶以root身份啟動nginx。 cd /ucenter/soft/nginx/sbin #把soft文件下所有的文件所屬者修改為ucener -r 表示遞歸 chown ucenter:ucenter ./soft/ -r #修改 ./nginx 的所屬為root chown root nginx #讓普通用戶可以使用80端口,可以使用root權限啟用nginx chmod u+s nginx #修改配置文件 在修改配置文件之前 ,要備份該文件 cd conf/ # 要注意nginx 的工作進程,一般根據cpu的核數去修改 vim nginx.conf #關閉防火墻,打開80端口 service iptables stop #啟動nginx ./nginx #重啟nginx ./nginx -s reload #關閉nginx ./nginx -s stop
準備一臺tomcat服務器,先準備java環境,安裝jdk步驟省略
然后分別安裝3個tomcat 服務器ip地址:192.168.1.143,tomcat1 8080端口,tomcat2 8081端口,tomcat3 8082端口。
apache-tomcat-7.0.64/conf/server.xml配置文件修改這三個地方,這樣端口就不會沖突
<server port="8005" shutdown="shutdown"> <connector port="8080" protocol="http/1.1" connectiontimeout="20000" redirectport="8443" /> <connector port="8009" protocol="ajp/1.3" redirectport="8443" />
修改tomcat root目錄下index.jsp,分別增加每個tomcat的標識,以及在頁面上顯示session id
<%-- licensed to the apache software foundation (asf) under one or more contributor license agreements. see the notice file distributed with this work for additional information regarding copyright ownership. the asf licenses this file to you under the apache license, version 2.0 (the "license"); you may not use this file except in compliance with the license. you may obtain a copy of the license at http://www.apache.org/licenses/license-2.0 unless required by applicable law or agreed to in writing, software distributed under the license is distributed on an "as is" basis, without warranties or conditions of any kind, either express or implied. see the license for the specific language governing permissions and limitations under the license. --%> <!doctype html> <%@ page session="true" %> <% java.text.simpledateformat sdf = new java.text.simpledateformat("yyyy"); request.setattribute("year", sdf.format(new java.util.date())); request.setattribute("tomcaturl", "http://tomcat.apache.org/"); request.setattribute("tomcatdocurl", "/docs/"); request.setattribute("tomcatexamplesurl", "/examples/"); %> <html> <head> <title><%=request.getservletcontext().getserverinfo() %></title> <link href="favicon.ico" rel="icon" type="image/x-icon" /> <link href="favicon.ico" rel="shortcut icon" type="image/x-icon" /> <link href="tomcat.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="wrapper"> <div id="navigation" class="curved container"> <span id="nav-home"><a href="${tomcaturl}">home--<%=request.getsession().getid() %></a></span> <span id="nav-hosts"><a href="${tomcatdocurl}">documentation</a></span> <span id="nav-config"><a href="${tomcatdocurl}config/">configuration</a></span> <span id="nav-examples"><a href="${tomcatexamplesurl}">examples</a></span> <span id="nav-wiki"><a href="http://wiki.apache.org/tomcat/frontpage">wiki</a></span> <span id="nav-lists"><a href="${tomcaturl}lists.html">mailing lists</a></span> <span id="nav-help"><a href="${tomcaturl}findhelp.html">find help</a></span> <br /> </div> <div id="asf-box"> <h1>${pagecontext.servletcontext.serverinfo}--8080</h1> </div> <div id="upper" class="curved container"> <div id="congrats" class="curved container"> <h2>if you're seeing this, you've successfully installed tomcat. congratulations!</h2> </div> <div id="notice"> <img src="tomcat.png" alt="[tomcat logo]" /> <div id="tasks"> <h3>recommended reading:</h3> <h4><a href="${tomcatdocurl}security-howto.html">security considerations how-to</a></h4> <h4><a href="${tomcatdocurl}manager-howto.html">manager application how-to</a></h4> <h4><a href="${tomcatdocurl}cluster-howto.html">clustering/session replication how-to</a></h4> </div> </div> <div id="actions"> <div> <a class="container shadow" href="/manager/status"><span>server status</span></a> </div> <div> <a class="container shadow" href="/manager/html"><span>manager app</span></a> </div> <div> <a class="container shadow" href="/host-manager/html"><span>host manager</span></a> </div> </div> <!-- <br /> --> <br /> </div> <div id="middle" class="curved container"> <h3>developer quick start</h3> <div> <div> <p><a href="${tomcatdocurl}setup.html">tomcat setup</a></p> <p><a href="${tomcatdocurl}appdev/">first web application</a></p> </div> </div> <div> <div> <p><a href="${tomcatdocurl}realm-howto.html">realms & aaa</a></p> <p><a href="${tomcatdocurl}jndi-datasource-examples-howto.html">jdbc datasources</a></p> </div> </div> <div> <div> <p><a href="${tomcatexamplesurl}">examples</a></p> </div> </div> <div> <div> <p><a href="http://wiki.apache.org/tomcat/specifications">servlet specifications</a></p> <p><a href="http://wiki.apache.org/tomcat/tomcatversions">tomcat versions</a></p> </div> </div> <br /> </div> <div id="lower"> <div id="low-manage" class=""> <div class="curved container"> <h3>managing tomcat</h3> <p>for security, access to the <a href="/manager/html">manager webapp</a> is restricted. users are defined in:</p> <pre>$catalina_home/conf/tomcat-users.xml</pre> <p>in tomcat 7.0 access to the manager application is split between different users. <a href="${tomcatdocurl}manager-howto.html">read more...</a></p> <br /> <h4><a href="${tomcatdocurl}release-notes.txt">release notes</a></h4> <h4><a href="${tomcatdocurl}changelog.html">changelog</a></h4> <h4><a href="${tomcaturl}migration.html">migration guide</a></h4> <h4><a href="${tomcaturl}security.html">security notices</a></h4> </div> </div> <div id="low-docs" class=""> <div class="curved container"> <h3>documentation</h3> <h4><a href="${tomcatdocurl}">tomcat 7.0 documentation</a></h4> <h4><a href="${tomcatdocurl}config/">tomcat 7.0 configuration</a></h4> <h4><a href="http://wiki.apache.org/tomcat/frontpage">tomcat wiki</a></h4> <p>find additional important configuration information in:</p> <pre>$catalina_home/running.txt</pre> <p>developers may be interested in:</p> <ul> <li><a href="http://tomcat.apache.org/bugreport.html">tomcat 7.0 bug database</a></li> <li><a href="${tomcatdocurl}api/index.html">tomcat 7.0 javadocs</a></li> <li><a href="http://svn.apache.org/repos/asf/tomcat/tc7.0.x/">tomcat 7.0 svn repository</a></li> </ul> </div> </div> <div id="low-help" class=""> <div class="curved container"> <h3>getting help</h3> <h4><a href="${tomcaturl}faq/">faq</a> and <a href="${tomcaturl}lists.html">mailing lists</a></h4> <p>the following mailing lists are available:</p> <ul> <li id="list-announce"><strong><a href="${tomcaturl}lists.html#tomcat-announce">tomcat-announce</a><br /> important announcements, releases, security vulnerability notifications. (low volume).</strong> </li> <li><a href="${tomcaturl}lists.html#tomcat-users">tomcat-users</a><br /> user support and discussion </li> <li><a href="${tomcaturl}lists.html#taglibs-user">taglibs-user</a><br /> user support and discussion for <a href="${tomcaturl}taglibs/">apache taglibs</a> </li> <li><a href="${tomcaturl}lists.html#tomcat-dev">tomcat-dev</a><br /> development mailing list, including commit messages </li> </ul> </div> </div> <br /> </div> <div id="footer" class="curved container"> <div> <div> <h4>other downloads</h4> <ul> <li><a href="${tomcaturl}download-connectors.cgi">tomcat connectors</a></li> <li><a href="${tomcaturl}download-native.cgi">tomcat native</a></li> <li><a href="${tomcaturl}taglibs/">taglibs</a></li> <li><a href="${tomcatdocurl}deployer-howto.html">deployer</a></li> </ul> </div> </div> <div> <div> <h4>other documentation</h4> <ul> <li><a href="${tomcaturl}connectors-doc/">tomcat connectors</a></li> <li><a href="${tomcaturl}connectors-doc/">mod_jk documentation</a></li> <li><a href="${tomcaturl}native-doc/">tomcat native</a></li> <li><a href="${tomcatdocurl}deployer-howto.html">deployer</a></li> </ul> </div> </div> <div> <div> <h4>get involved</h4> <ul> <li><a href="${tomcaturl}getinvolved.html">overview</a></li> <li><a href="${tomcaturl}svn.html">svn repositories</a></li> <li><a href="${tomcaturl}lists.html">mailing lists</a></li> <li><a href="http://wiki.apache.org/tomcat/frontpage">wiki</a></li> </ul> </div> </div> <div> <div> <h4>miscellaneous</h4> <ul> <li><a href="${tomcaturl}contact.html">contact</a></li> <li><a href="${tomcaturl}legal.html">legal</a></li> <li><a href="http://www.apache.org/foundation/sponsorship.html">sponsorship</a></li> <li><a href="http://www.apache.org/foundation/thanks.html">thanks</a></li> </ul> </div> </div> <div> <div> <h4>apache software foundation</h4> <ul> <li><a href="${tomcaturl}whoweare.html">who we are</a></li> <li><a href="${tomcaturl}heritage.html">heritage</a></li> <li><a href="http://www.apache.org">apache home</a></li> <li><a href="${tomcaturl}resources.html">resources</a></li> </ul> </div> </div> <br /> </div> <p>copyright ©1999-${year} apache software foundation. all rights reserved</p> </div> </body> </html>
這時候 修改nginx配置文件nginx.conf,把三臺tomcat的ip地址以及端口號加入進去,使用nginx做代理
#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 localhost1 { #ip_hash; server 192.168.1.143:8080; server 192.168.1.143:8081; server 192.168.1.143:8082; } server { listen 81; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_connect_timeout 3; proxy_send_timeout 30; proxy_read_timeout 30; proxy_pass http://localhost1; } #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; # 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; # } #} }
這時候,分別啟動三臺tomcat以及nginx,訪問,這時候每次刷新頁面,都會隨機訪問8080或者8081或者8082,而且頁面上出現的session id也都是不一樣的,我們應該如何讓這三臺tomcat共享session呢,我們使用redis來做。
這時候,在已經按照三臺tomcat的服務器192.168.1.143上,安裝redis,安裝步驟如下:
$ wget http://download.redis.io/releases/redis-3.2.3.tar.gz $ tar xzf redis-3.2.3.tar.gz $ cd redis-3.2.3 $ make malloc=libc #啟動redis src前面是安裝的路徑 $ src/redis-server & #關閉redis src/redis-cli shutdown #使用redis 放入鍵值對 key value $ src/redis-cli 127.0.0.1:6379> set foo bar ok 127.0.0.1:6379> get foo "bar" $
安裝完redis之后,在三個tomcat的lib文件夾內分別上傳這五個所依賴的jar,分別是
commons-pool2-2.0.jar
jedis-2.5.2.jar
tomcat-redis-session-manager1.2.jar
tomcat-juli.jar
tomcat-juli-adapters.jar
所有jar在這里了,
然后分別修個三個tomcat的context.xml文件,增加如下的配置
<?xml version='1.0' encoding='utf-8'?> <context> <!-- default set of monitored resources --> <watchedresource>web-inf/web.xml</watchedresource> <!-- 這里增加redis session共享的配置 6379是redis的端口--> <valve classname="com.orangefunction.tomcat.redissessions.redissessionhandlervalve" /> <manager classname="com.orangefunction.tomcat.redissessions.redissessionmanager" host="127.0.0.1" port="6379" database="0" maxinactiveinterval="60" /> </context>
這時候,分別重啟三個tomcat以及nginx,查看tomcat日志之后,發現沒有任何異常報錯,說明我們成功了,接下來開始測試。
我們訪問nginx服務器地址:
得到的是8080端口的 tomcat1 ,session id為1a0625767f27ba95ef4d5f061fe0568d
這時候按f5刷新頁面,得到的是8081端口的 tomcat2 ,session id依舊是 1a0625767f27ba95ef4d5f061fe0568d
再次刷新頁面,得到的是8082端口的 tomcat3,session id依舊是 1a0625767f27ba95ef4d5f061fe0568d。 、
這時候,說明我們搭建tomcat + nginx負載均衡 + redis session同步成功啦!
nginx幫助把我們的請求均勻的分發給三個tomcat --》tomcat1 、tomcat2以及tomcat3
redis幫助我們同步session,這樣一來,我們的服務器性能就會提高許多,任何一臺tomcat發生故障后,對整體的服務都不會有影響了。
感謝各位的閱讀,以上就是“nginx+tomcat怎么使用redis session共享”的內容了,經過本文的學習后,相信大家對nginx+tomcat怎么使用redis session共享這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。