欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

nginx+tomcat實現(xiàn)負載均衡,使用redis session共享

 更新時間:2016年12月02日 08:33:31   作者:程序人生0407  
這篇文章主要介紹了nginx tomcat負載均衡 使用redis session共享,有興趣的同學可以了解一下。

環(huán)境準備

1、準備一臺nginx服務(wù)器 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 用戶安裝
#創(chuàng)建一個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系統(tǒng)中由于非root用戶不能占用80端口,所以需要使普通用戶以root身份啟動nginx。 
cd /ucenter/soft/nginx/sbin
#把soft文件下所有的文件所屬者修改為ucener -R 表示遞歸
chown ucenter:ucenter ./soft/ -R
#修改 ./nginx 的所屬為root
chown root nginx
#讓普通用戶可以使用80端口,可以使用root權(quán)限啟用nginx
chmod u+s nginx
#修改配置文件 在修改配置文件之前 ,要備份該文件
cd conf/
# 要注意nginx 的工作進程,一般根據(jù)cpu的核數(shù)去修改
vim nginx.conf
#關(guān)閉防火墻,打開80端口
service iptables stop
#啟動nginx
./nginx
#重啟nginx
./nginx -s reload
#關(guān)閉nginx
./nginx -s stop

準備一臺tomcat服務(wù)器,先準備java環(huán)境,安裝jdk步驟省略

然后分別安裝3個tomcat 服務(wù)器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 lang="en">
 <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 >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 class="separator" />
   </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 class="button">
      <a class="container shadow" href="/manager/status"><span>Server Status</span></a>
     </div>
     <div class="button">
      <a class="container shadow" href="/manager/html"><span>Manager App</span></a>
     </div>
     <div class="button">
      <a class="container shadow" href="/host-manager/html"><span>Host Manager</span></a>
     </div>
    </div>
    <!--
    <br class="separator" />
    -->
    <br class="separator" />
   </div>
   <div id="middle" class="curved container">
    <h3>Developer Quick Start</h3>
    <div class="col25">
     <div class="container">
      <p><a href="${tomcatDocUrl}setup.html">Tomcat Setup</a></p>
      <p><a href="${tomcatDocUrl}appdev/">First Web Application</a></p>
     </div>
    </div>
    <div class="col25">
     <div class="container">
      <p><a href="${tomcatDocUrl}realm-howto.html">Realms &amp; AAA</a></p>
      <p><a href="${tomcatDocUrl}jndi-datasource-examples-howto.html">JDBC DataSources</a></p>
     </div>
    </div>
    <div class="col25">
     <div class="container">
      <p><a href="${tomcatExamplesUrl}">Examples</a></p>
     </div>
    </div>
    <div class="col25">
     <div class="container">
      <p><a >Servlet Specifications</a></p>
      <p><a >Tomcat Versions</a></p>
     </div>
    </div>
    <br class="separator" />
   </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. &nbsp; <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 >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 >Tomcat 7.0 Bug Database</a></li>
       <li><a href="${tomcatDocUrl}api/index.html">Tomcat 7.0 JavaDocs</a></li>
       <li><a >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 class="separator" />
   </div>
   <div id="footer" class="curved container">
    <div class="col20">
     <div class="container">
      <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 class="col20">
     <div class="container">
      <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 class="col20">
     <div class="container">
      <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 >Wiki</a></li>
      </ul>
     </div>
    </div>
    <div class="col20">
     <div class="container">
      <h4>Miscellaneous</h4>
      <ul>
       <li><a href="${tomcatUrl}contact.html">Contact</a></li>
       <li><a href="${tomcatUrl}legal.html">Legal</a></li>
       <li><a >Sponsorship</a></li>
       <li><a >Thanks</a></li>
      </ul>
     </div>
    </div>
    <div class="col20">
     <div class="container">
      <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 >Apache Home</a></li>
       <li><a href="${tomcatUrl}resources.html">Resources</a></li>
      </ul>
     </div>
    </div>
    <br class="separator" />
   </div>
   <p class="copyright">Copyright &copy;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,訪問http://192.168.1.133:81,這時候每次刷新頁面,都會隨機訪問8080或者8081或者8082,而且頁面上出現(xiàn)的session id也都是不一樣的,我們應(yīng)該如何讓這三臺tomcat共享session呢,我們使用redis來做。

這時候,在已經(jīng)按照三臺tomcat的服務(wù)器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 &

#關(guān)閉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文件夾內(nèi)分別上傳這五個所依賴的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日志之后,發(fā)現(xiàn)沒有任何異常報錯,說明我們成功了,接下來開始測試。

我們訪問nginx服務(wù)器地址:http://192.168.1.133:81/

得到的是8080端口的 tomcat1 ,session id為1A0625767F27BA95EF4D5F061FE0568D 

這時候按F5刷新頁面,得到的是8081端口的 tomcat2 ,session id依舊是 1A0625767F27BA95EF4D5F061FE0568D

再次刷新頁面,得到的是8082端口的 tomcat3,session id依舊是 1A0625767F27BA95EF4D5F061FE0568D。 、

這時候,說明我們搭建tomcat + nginx負載均衡 + redis session同步成功啦!

nginx幫助把我們的請求均勻的分發(fā)給三個tomcat --》tomcat1 、tomcat2以及tomcat3

 redis幫助我們同步session,這樣一來,我們的服務(wù)器性能就會提高許多,任何一臺tomcat發(fā)生故障后,對整體的服務(wù)都不會有影響了。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 利用nginx解決跨域問題的方法(以flask為例)

    利用nginx解決跨域問題的方法(以flask為例)

    這篇文章主要介紹了利用nginx解決跨域問題的方法,文中以flask為例給大家介紹的很詳細,需要的朋友可以參考學習,下面來一起看看吧。
    2017-02-02
  • Nginx/Openresty中啟用http2支持的方法教程

    Nginx/Openresty中啟用http2支持的方法教程

    Openresty/Nginx默認是不支持http2的,需要將http2模塊編譯進應(yīng)用中。這篇文章主要給大家介紹了關(guān)于在Nginx/Openresty中啟用http2支持的方法教程,文中介紹的非常詳細,對大家具有一的參考學習價值,需要的朋友們下面來一起看看吧。
    2017-07-07
  • windows下nginx如何操作命令

    windows下nginx如何操作命令

    這篇文章主要介紹了windows下nginx如何操作命令,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • 國內(nèi)一些常用PHP的CMS的Nginx服務(wù)器的偽靜態(tài)規(guī)則整理

    國內(nèi)一些常用PHP的CMS的Nginx服務(wù)器的偽靜態(tài)規(guī)則整理

    當我們從apache服務(wù)器轉(zhuǎn)向Nginx服務(wù)器的時候,它們的偽靜態(tài)規(guī)則就不一樣了,所以你熟悉Nginx服務(wù)器的偽靜態(tài)規(guī)則,自己寫當然也好
    2011-03-03
  • Nginx+Tomcat負載均衡集群安裝配置案例詳解

    Nginx+Tomcat負載均衡集群安裝配置案例詳解

    Nginx是一款非常優(yōu)秀的http服務(wù)器軟件,它能夠支持高達50000個并發(fā)連接數(shù)的相應(yīng),Nginx+Tomcat負載均衡集案列是應(yīng)用于生產(chǎn)環(huán)境的一套可靠的Web站點解決方案,對Nginx Tomcat負載均衡集群相關(guān)知識感興趣的朋友一起看看吧
    2021-10-10
  • nginx封空user_agent實現(xiàn)封禁迅雷的方法

    nginx封空user_agent實現(xiàn)封禁迅雷的方法

    nginx封空user_agent實現(xiàn)封禁迅雷的方法,需要的朋友可以參考下。
    2010-11-11
  • Nginx實現(xiàn)負載均衡和反向代理的方法

    Nginx實現(xiàn)負載均衡和反向代理的方法

    Nginx是由俄羅斯人研發(fā)的,應(yīng)對Rambler的網(wǎng)站,并且2004年發(fā)布的第一個版本,Nginx功能豐富,可作為HTTP服務(wù)器,也可作為反向代理服務(wù)器,郵件服務(wù)器,本文給大家介紹了Nginx實現(xiàn)負載均衡和反向代理的方法,需要的朋友可以參考下
    2024-02-02
  • Nginx多ip部署多站點的實現(xiàn)步驟

    Nginx多ip部署多站點的實現(xiàn)步驟

    使用Nginx在具有多個IP地址的服務(wù)器上部署多個站點,從而實現(xiàn)高效、安全的網(wǎng)站托管,本文主要介紹了Nginx多ip部署多站點的實現(xiàn)步驟,感興趣的可以了解一下
    2024-01-01
  • angular6+springboot實現(xiàn)前后分離nginx配置

    angular6+springboot實現(xiàn)前后分離nginx配置

    這篇文章主要介紹了angular6+springboot實現(xiàn)前后分離nginx配置詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-06-06
  • Nginx日志實現(xiàn)訪問異常報警詳解

    Nginx日志實現(xiàn)訪問異常報警詳解

    Nginx把遇到的不同級別的問題信息寫到錯誤日志。error_log 指令配置記錄到特定的文件,stderr,或者syslog,配置寫到日志的最低級別信息。下面這篇文章主要介紹了利用Nginx日志實現(xiàn)訪問異常報警的相關(guān)資料,需要的朋友可以參考下。
    2017-03-03

最新評論