zoukankan      html  css  js  c++  java
  • Nginx+Tomcat+Memcached部署

    环境清单列表:(因为只有三台电脑,所有把Nginx和memcached放到一起)

      应用服务器1:192.168.51.10;

      应用服务器2:192.168.55.110;

      memcached服务器:192.168.51.75;

      Nginx地址:192.168.51.75

    一、安装tomcate

      1、下载tomcate7.0,下载地址:链接: https://pan.baidu.com/s/1dxiE9w 密码: nba7,把压缩包放在/home/ump/tomcat目录下。

      先按照55.110上的tomcate

      2、解压压缩包到/usr/local目录下,并进入到安装目录,命令:

    unzip apache-tomcat-7.0.77-windows-x64.zip -d /usr/local
    cd /usr/local/apache-tomcat-7.0.77

      3、修改conf/server.xml文件,因为192.168.55.110上面已经装有一个应用服务器,所以需要修改对应的端口,修改server端口,修改连接器的端口,修改后的内容如下:

    复制代码
    <?xml version='1.0' encoding='utf-8'?>
    <!--
      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.
    -->
    <!-- Note: A "Server" is not itself a "Container", so you may not
    define subcomponents such as "Valves" at this level.
    Documentation at /docs/config/server.html
    -->
    <Server port="9005" shutdown="SHUTDOWN">
    <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
    <!-- Security listener. Documentation at /docs/config/listeners.html
    <Listener className="org.apache.catalina.security.SecurityListener" />
    -->
    <!--APR library loader. Documentation at /docs/apr.html -->
    <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
    <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
    <Listener className="org.apache.catalina.core.JasperListener" />
    <!-- Prevent memory leaks due to use of particular java/javax APIs-->
    <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
    <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
    <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

    <!-- Global JNDI resources
    Documentation at /docs/jndi-resources-howto.html
    -->
    <GlobalNamingResources>
    <!-- Editable user database that can also be used by
    UserDatabaseRealm to authenticate users
    -->
    <Resource name="UserDatabase" auth="Container"
    type
    ="org.apache.catalina.UserDatabase"
    description
    ="User database that can be updated and saved"
    factory
    ="org.apache.catalina.users.MemoryUserDatabaseFactory"
    pathname
    ="conf/tomcat-users.xml" />
    </GlobalNamingResources>

    <!-- A "Service" is a collection of one or more "Connectors" that share
    a single "Container" Note: A "Service" is not itself a "Container",
    so you may not define subcomponents such as "Valves" at this level.
    Documentation at /docs/config/service.html
    -->
    <Service name="Catalina">

    <span style="color: #008000">&lt;!--</span><span style="color: #008000">The connectors can use a shared executor, you can define one or more named thread pools</span><span style="color: #008000">--&gt;</span>
    <span style="color: #008000">&lt;!--</span><span style="color: #008000">
    &lt;Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="150" minSpareThreads="4"/&gt;
    </span><span style="color: #008000">--&gt;</span>
    
    
    <span style="color: #008000">&lt;!--</span><span style="color: #008000"> A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking &amp; non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL HTTP/1.1 Connector on port 8080
    </span><span style="color: #008000">--&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">Connector </span><span style="color: #ff0000">port</span><span style="color: #0000ff">="<span style="background-color: #00ff00">9090</span>"</span><span style="color: #ff0000"> protocol</span><span style="color: #0000ff">="HTTP/1.1"</span><span style="color: #ff0000">
               connectionTimeout</span><span style="color: #0000ff">="20000"</span><span style="color: #ff0000">
               redirectPort</span><span style="color: #0000ff">="<span style="background-color: #00ff00">9443</span>"</span> <span style="color: #0000ff">/&gt;</span>
    <span style="color: #008000">&lt;!--</span><span style="color: #008000"> A "Connector" using the shared thread pool</span><span style="color: #008000">--&gt;</span>
    <span style="color: #008000">&lt;!--</span><span style="color: #008000">
    &lt;Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" /&gt;
    </span><span style="color: #008000">--&gt;</span>
    <span style="color: #008000">&lt;!--</span><span style="color: #008000"> Define a SSL HTTP/1.1 Connector on port 8443
         This connector uses the BIO implementation that requires the JSSE
         style configuration. When using the APR/native implementation, the
         OpenSSL style configuration is required as described in the APR/native
         documentation </span><span style="color: #008000">--&gt;</span>
    <span style="color: #008000">&lt;!--</span><span style="color: #008000">
    &lt;Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" /&gt;
    </span><span style="color: #008000">--&gt;</span>
    
    <span style="color: #008000">&lt;!--</span><span style="color: #008000"> Define an AJP 1.3 Connector on port 8009 </span><span style="color: #008000">--&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">Connector </span><span style="color: #ff0000">port</span><span style="color: #0000ff">="<span style="background-color: #00ff00">9009</span>"</span><span style="color: #ff0000"> protocol</span><span style="color: #0000ff">="AJP/1.3"</span><span style="color: #ff0000"> redirectPort</span><span style="color: #0000ff">="<span style="background-color: #00ff00">9443</span>"</span> <span style="color: #0000ff">/&gt;</span>
    
    
    <span style="color: #008000">&lt;!--</span><span style="color: #008000"> An Engine represents the entry point (within Catalina) that processes
         every request.  The Engine implementation for Tomcat stand alone
         analyzes the HTTP headers included with the request, and passes them
         on to the appropriate Host (virtual host).
         Documentation at /docs/config/engine.html </span><span style="color: #008000">--&gt;</span>
    
    <span style="color: #008000">&lt;!--</span><span style="color: #008000"> You should set jvmRoute to support load-balancing via AJP ie :
    &lt;Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1"&gt;
    </span><span style="color: #008000">--&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">Engine </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="Catalina"</span><span style="color: #ff0000"> defaultHost</span><span style="color: #0000ff">="localhost"</span><span style="color: #0000ff">&gt;</span>
    
      <span style="color: #008000">&lt;!--</span><span style="color: #008000">For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) </span><span style="color: #008000">--&gt;</span>
      <span style="color: #008000">&lt;!--</span><span style="color: #008000">
      &lt;Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/&gt;
      </span><span style="color: #008000">--&gt;</span>
    
      <span style="color: #008000">&lt;!--</span><span style="color: #008000"> Use the LockOutRealm to prevent attempts to guess user passwords
           via a brute-force attack </span><span style="color: #008000">--&gt;</span>
      <span style="color: #0000ff">&lt;</span><span style="color: #800000">Realm </span><span style="color: #ff0000">className</span><span style="color: #0000ff">="org.apache.catalina.realm.LockOutRealm"</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #008000">&lt;!--</span><span style="color: #008000"> This Realm uses the UserDatabase configured in the global JNDI
             resources under the key "UserDatabase".  Any edits
             that are performed against this UserDatabase are immediately
             available for use by the Realm.  </span><span style="color: #008000">--&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">Realm </span><span style="color: #ff0000">className</span><span style="color: #0000ff">="org.apache.catalina.realm.UserDatabaseRealm"</span><span style="color: #ff0000">
               resourceName</span><span style="color: #0000ff">="UserDatabase"</span><span style="color: #0000ff">/&gt;</span>
      <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Realm</span><span style="color: #0000ff">&gt;</span>
    
      <span style="color: #0000ff">&lt;</span><span style="color: #800000">Host </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="localhost"</span><span style="color: #ff0000">  appBase</span><span style="color: #0000ff">="webapps"</span><span style="color: #ff0000">
            unpackWARs</span><span style="color: #0000ff">="true"</span><span style="color: #ff0000"> autoDeploy</span><span style="color: #0000ff">="true"</span><span style="color: #0000ff">&gt;</span>
    
        <span style="color: #008000">&lt;!--</span><span style="color: #008000"> SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html </span><span style="color: #008000">--&gt;</span>
        <span style="color: #008000">&lt;!--</span><span style="color: #008000">
        &lt;Valve className="org.apache.catalina.authenticator.SingleSignOn" /&gt;
        </span><span style="color: #008000">--&gt;</span>
    
        <span style="color: #008000">&lt;!--</span><span style="color: #008000"> Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" </span><span style="color: #008000">--&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">Valve </span><span style="color: #ff0000">className</span><span style="color: #0000ff">="org.apache.catalina.valves.AccessLogValve"</span><span style="color: #ff0000"> directory</span><span style="color: #0000ff">="logs"</span><span style="color: #ff0000">
               prefix</span><span style="color: #0000ff">="localhost_access_log."</span><span style="color: #ff0000"> suffix</span><span style="color: #0000ff">=".txt"</span><span style="color: #ff0000">
               pattern</span><span style="color: #0000ff">="%h %l %u %t &amp;quot;%r&amp;quot; %s %b"</span> <span style="color: #0000ff">/&gt;</span>
    
      <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Host</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Engine</span><span style="color: #0000ff">&gt;</span>
    

    </Service>
    </Server>

    复制代码

      4、启动tomcate,访问:http://192.168.55.110:9090,如果出现tomcate管理界面,说明tamcate安装成功。

      5、用同样的方法,部署另一个tomcate到192.168.51.10,因为192.168.51.10上不存在端口冲突,所以server.xml不需要修改,解压后,启动,可以通过:http://192.168.51.10:8080/访问到tomcate管理控制台。

    二、创建一个动态web工程,并部署到tomcate上

      1、eclipse创建一个动态web工程,并新建一个index.jsp,内容如下:

    复制代码
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Cluster Web Test</title>
    </head>
    <body>
        <h1>sessionID:<%=session.getId() %></h1>  
        <h1>长度:<%=session.getId().length() %></h1>  
        <p>this is Web 1</p>
    </body>
    </html>
    复制代码

      2、把创建的动态工程导出为war包。下载地址:链接: https://pan.baidu.com/s/1c2Waok4 密码: 2jaf

      3、创建存放工程的目录,命令:

    mkdir /home/ump/www

      4、把ClusterProject.war解压到ClusterProject文件夹,把ClusterProject文件夹放入道/home/ump/www目录下,修改server.xml,添加ClusterProject工程,内容如下:

    复制代码
    <?xml version='1.0' encoding='utf-8'?>
    <!--
      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.
    -->
    <!-- Note: A "Server" is not itself a "Container", so you may not
    define subcomponents such as "Valves" at this level.
    Documentation at /docs/config/server.html
    -->
    <Server port="9005" shutdown="SHUTDOWN">
    <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
    <!-- Security listener. Documentation at /docs/config/listeners.html
    <Listener className="org.apache.catalina.security.SecurityListener" />
    -->
    <!--APR library loader. Documentation at /docs/apr.html -->
    <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
    <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
    <Listener className="org.apache.catalina.core.JasperListener" />
    <!-- Prevent memory leaks due to use of particular java/javax APIs-->
    <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
    <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
    <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

    <!-- Global JNDI resources
    Documentation at /docs/jndi-resources-howto.html
    -->
    <GlobalNamingResources>
    <!-- Editable user database that can also be used by
    UserDatabaseRealm to authenticate users
    -->
    <Resource name="UserDatabase" auth="Container"
    type
    ="org.apache.catalina.UserDatabase"
    description
    ="User database that can be updated and saved"
    factory
    ="org.apache.catalina.users.MemoryUserDatabaseFactory"
    pathname
    ="conf/tomcat-users.xml" />
    </GlobalNamingResources>

    <!-- A "Service" is a collection of one or more "Connectors" that share
    a single "Container" Note: A "Service" is not itself a "Container",
    so you may not define subcomponents such as "Valves" at this level.
    Documentation at /docs/config/service.html
    -->
    <Service name="Catalina">

    <span style="color: #008000">&lt;!--</span><span style="color: #008000">The connectors can use a shared executor, you can define one or more named thread pools</span><span style="color: #008000">--&gt;</span>
    <span style="color: #008000">&lt;!--</span><span style="color: #008000">
    &lt;Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="150" minSpareThreads="4"/&gt;
    </span><span style="color: #008000">--&gt;</span>
    
    
    <span style="color: #008000">&lt;!--</span><span style="color: #008000"> A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking &amp; non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL HTTP/1.1 Connector on port 8080
    </span><span style="color: #008000">--&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">Connector </span><span style="color: #ff0000">port</span><span style="color: #0000ff">="9090"</span><span style="color: #ff0000"> protocol</span><span style="color: #0000ff">="HTTP/1.1"</span><span style="color: #ff0000">
               connectionTimeout</span><span style="color: #0000ff">="20000"</span><span style="color: #ff0000">
               redirectPort</span><span style="color: #0000ff">="9443"</span> <span style="color: #0000ff">/&gt;</span>
    <span style="color: #008000">&lt;!--</span><span style="color: #008000"> A "Connector" using the shared thread pool</span><span style="color: #008000">--&gt;</span>
    <span style="color: #008000">&lt;!--</span><span style="color: #008000">
    &lt;Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" /&gt;
    </span><span style="color: #008000">--&gt;</span>
    <span style="color: #008000">&lt;!--</span><span style="color: #008000"> Define a SSL HTTP/1.1 Connector on port 8443
         This connector uses the BIO implementation that requires the JSSE
         style configuration. When using the APR/native implementation, the
         OpenSSL style configuration is required as described in the APR/native
         documentation </span><span style="color: #008000">--&gt;</span>
    <span style="color: #008000">&lt;!--</span><span style="color: #008000">
    &lt;Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" /&gt;
    </span><span style="color: #008000">--&gt;</span>
    
    <span style="color: #008000">&lt;!--</span><span style="color: #008000"> Define an AJP 1.3 Connector on port 8009 </span><span style="color: #008000">--&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">Connector </span><span style="color: #ff0000">port</span><span style="color: #0000ff">="9009"</span><span style="color: #ff0000"> protocol</span><span style="color: #0000ff">="AJP/1.3"</span><span style="color: #ff0000"> redirectPort</span><span style="color: #0000ff">="9443"</span> <span style="color: #0000ff">/&gt;</span>
    
    
    <span style="color: #008000">&lt;!--</span><span style="color: #008000"> An Engine represents the entry point (within Catalina) that processes
         every request.  The Engine implementation for Tomcat stand alone
         analyzes the HTTP headers included with the request, and passes them
         on to the appropriate Host (virtual host).
         Documentation at /docs/config/engine.html </span><span style="color: #008000">--&gt;</span>
    
    <span style="color: #008000">&lt;!--</span><span style="color: #008000"> You should set jvmRoute to support load-balancing via AJP ie :
    &lt;Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1"&gt;
    </span><span style="color: #008000">--&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">Engine </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="Catalina"</span><span style="color: #ff0000"> defaultHost</span><span style="color: #0000ff">="localhost"</span><span style="color: #0000ff">&gt;</span>
    
      <span style="color: #008000">&lt;!--</span><span style="color: #008000">For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) </span><span style="color: #008000">--&gt;</span>
      <span style="color: #008000">&lt;!--</span><span style="color: #008000">
      &lt;Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/&gt;
      </span><span style="color: #008000">--&gt;</span>
    
      <span style="color: #008000">&lt;!--</span><span style="color: #008000"> Use the LockOutRealm to prevent attempts to guess user passwords
           via a brute-force attack </span><span style="color: #008000">--&gt;</span>
      <span style="color: #0000ff">&lt;</span><span style="color: #800000">Realm </span><span style="color: #ff0000">className</span><span style="color: #0000ff">="org.apache.catalina.realm.LockOutRealm"</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #008000">&lt;!--</span><span style="color: #008000"> This Realm uses the UserDatabase configured in the global JNDI
             resources under the key "UserDatabase".  Any edits
             that are performed against this UserDatabase are immediately
             available for use by the Realm.  </span><span style="color: #008000">--&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">Realm </span><span style="color: #ff0000">className</span><span style="color: #0000ff">="org.apache.catalina.realm.UserDatabaseRealm"</span><span style="color: #ff0000">
               resourceName</span><span style="color: #0000ff">="UserDatabase"</span><span style="color: #0000ff">/&gt;</span>
      <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Realm</span><span style="color: #0000ff">&gt;</span>
    
      <span style="color: #0000ff">&lt;</span><span style="color: #800000">Host </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="localhost"</span><span style="color: #ff0000">  appBase</span><span style="color: #0000ff">="webapps"</span><span style="color: #ff0000">
            unpackWARs</span><span style="color: #0000ff">="true"</span><span style="color: #ff0000"> autoDeploy</span><span style="color: #0000ff">="true"</span><span style="color: #0000ff">&gt;</span>
    

    <Context path="ClusterProject" docBase="/home/ump/www/ClusterProject" reloadable="true">

    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Context</span><span style="color: #0000ff">&gt;</span></span>
    
        <span style="color: #008000">&lt;!--</span><span style="color: #008000"> SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html </span><span style="color: #008000">--&gt;</span>
        <span style="color: #008000">&lt;!--</span><span style="color: #008000">
        &lt;Valve className="org.apache.catalina.authenticator.SingleSignOn" /&gt;
        </span><span style="color: #008000">--&gt;</span>
    
        <span style="color: #008000">&lt;!--</span><span style="color: #008000"> Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" </span><span style="color: #008000">--&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">Valve </span><span style="color: #ff0000">className</span><span style="color: #0000ff">="org.apache.catalina.valves.AccessLogValve"</span><span style="color: #ff0000"> directory</span><span style="color: #0000ff">="logs"</span><span style="color: #ff0000">
               prefix</span><span style="color: #0000ff">="localhost_access_log."</span><span style="color: #ff0000"> suffix</span><span style="color: #0000ff">=".txt"</span><span style="color: #ff0000">
               pattern</span><span style="color: #0000ff">="%h %l %u %t &amp;quot;%r&amp;quot; %s %b"</span> <span style="color: #0000ff">/&gt;</span>
    
      <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Host</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Engine</span><span style="color: #0000ff">&gt;</span>
    

    </Service>
    </Server>

    复制代码

    主要是添加绿色背景的语句。

    5、重启Tomcat,并访问:http://192.168.55.110:9090/ClusterProject/index.jsp,如果出现下图,说明应用安装成功。

      4、把ClusterProject.war安装同样的方法,部署到192.168.51.10上。为了与192.168.55.110上的应用区分,修改index.jsp中

     <p>this is Web 1</p>为<p>this is Web 2</p>

      5、访问:http://192.168.51.10:8080/ClusterProject/index.jsp,如下图所示

    至此,应用部署完毕。

    三、安装Nginx,并配置负载均衡。(192.168.51.75)

      1、Nginx的安装过程参考其他文章,不在赘述。

      2、修改Nginx的配置文件,/etc/nginx/nginx.conf,修改内容如下:

    复制代码
    # For more information on configuration, see:
    #   * Official English Documentation: http://nginx.org/en/docs/
    #   * Official Russian Documentation: http://nginx.org/ru/docs/
    

    user nginx;
    worker_processes 1;

    error_log /var/log/nginx/error.log;

    error_log /var/log/nginx/error.log notice;

    error_log /var/log/nginx/error.log info;

    pid /run/nginx.pid;

    events {
    worker_connections 1024;
    }

    http {
    include /etc/nginx/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  /var/log/nginx/access.log  main;
    
    sendfile        on;
    #tcp_nopush     on;
    
    #keepalive_timeout  0;
    keepalive_timeout  65;
    
    #gzip  on;
    
    index   index.html index.htm;
    
    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
    

    upstream myweb{
    server 192.168.51.10:8080;
    server 192.168.55.110:9090;
    }

    server {
    listen 80 default_server;
    server_name localhost;
    root /usr/share/nginx/html;

        #charset koi8-r;
    
        #access_log  /var/log/nginx/host.access.log  main;
    
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
    
        location / {
        <span style="color: #ff0000">proxy_pass http://myweb;</span>
        }
    
        # redirect server error pages to the static page /40x.html
        #
        error_page  404              /404.html;
        location = /40x.html {
        }
    
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
        }
    }
    

    }

    复制代码

    只需要添加红色字体部分。

      3、访问Nginx:http://192.168.51.75/ClusterProject/index.jsp,第一次访问显示图一,刷新显示图二,说明第一次访问192.168.51.10,第二次访问192.168.55.110

    至此负载均衡搭建完毕。

    四、安装memcached,安装过程参考其他文档,不在赘述。

      memcached配置信息如下:

    当前项目中,linux下memcached的启动/结束的方式

    默认情况下memcached安装到/usr/local/bin下。

    进入安装目录,启动memcached:/usr/local/memcached/bin/memcached -d -c 10240 -m 1024 -u root
    获取运行状态:echo stats | nc localhost 11211(可以查看出pid) 或使用ps -ef|grep memcached 
    停止memcached:kill -9 pid  (-9表示强制杀死,pid 为进程的进程标识符)
    -d 选项是启动一个守护进程,
    -m 是分配给Memcache使用的内存数量,单位是MB,这里是1024MB,默认是64MB
    -u 是运行Memcache的用户,这里是root
    -l 是监听的服务器IP地址,默认应该是本机
    -p 是设置Memcache监听的端口,默认是11211,最好是1024以上的端口
    -c 选项是最大运行的并发连接数,默认是1024,这里设置了10240,按照你服务器的负载量来设定
    -P 是设置保存Memcache的pid文件位置
    -h 打印帮助信息
    -v 输出警告和错误信息
    -vv 打印客户端的请求和返回信息

    五、基于memcached配置tomcate,共享session

      1、启动memcached,memcached -d -c 10240 -m 1024 -u root

      2、下载memcached相关jar包和序列化对应的jar包,这里session序列化采用kryo,所有的jar包下载地址:链接: https://pan.baidu.com/s/1htV2vnm 密码: hkiy

      3、把下载的所有jar,放入到tomcate安装目录的lib下面。

      4、修改tomcate,server.xml,添加session管理。

    复制代码
    <?xml version='1.0' encoding='utf-8'?>
    <!--
      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.
    -->
    <!-- Note: A "Server" is not itself a "Container", so you may not
    define subcomponents such as "Valves" at this level.
    Documentation at /docs/config/server.html
    -->
    <Server port="9005" shutdown="SHUTDOWN">
    <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
    <!-- Security listener. Documentation at /docs/config/listeners.html
    <Listener className="org.apache.catalina.security.SecurityListener" />
    -->
    <!--APR library loader. Documentation at /docs/apr.html -->
    <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
    <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
    <Listener className="org.apache.catalina.core.JasperListener" />
    <!-- Prevent memory leaks due to use of particular java/javax APIs-->
    <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
    <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
    <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

    <!-- Global JNDI resources
    Documentation at /docs/jndi-resources-howto.html
    -->
    <GlobalNamingResources>
    <!-- Editable user database that can also be used by
    UserDatabaseRealm to authenticate users
    -->
    <Resource name="UserDatabase" auth="Container"
    type
    ="org.apache.catalina.UserDatabase"
    description
    ="User database that can be updated and saved"
    factory
    ="org.apache.catalina.users.MemoryUserDatabaseFactory"
    pathname
    ="conf/tomcat-users.xml" />
    </GlobalNamingResources>

    <!-- A "Service" is a collection of one or more "Connectors" that share
    a single "Container" Note: A "Service" is not itself a "Container",
    so you may not define subcomponents such as "Valves" at this level.
    Documentation at /docs/config/service.html
    -->
    <Service name="Catalina">

    <span style="color: #008000">&lt;!--</span><span style="color: #008000">The connectors can use a shared executor, you can define one or more named thread pools</span><span style="color: #008000">--&gt;</span>
    <span style="color: #008000">&lt;!--</span><span style="color: #008000">
    &lt;Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="150" minSpareThreads="4"/&gt;
    </span><span style="color: #008000">--&gt;</span>
    
    
    <span style="color: #008000">&lt;!--</span><span style="color: #008000"> A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking &amp; non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL HTTP/1.1 Connector on port 8080
    </span><span style="color: #008000">--&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">Connector </span><span style="color: #ff0000">port</span><span style="color: #0000ff">="9090"</span><span style="color: #ff0000"> protocol</span><span style="color: #0000ff">="HTTP/1.1"</span><span style="color: #ff0000">
               connectionTimeout</span><span style="color: #0000ff">="20000"</span><span style="color: #ff0000">
               redirectPort</span><span style="color: #0000ff">="9443"</span> <span style="color: #0000ff">/&gt;</span>
    <span style="color: #008000">&lt;!--</span><span style="color: #008000"> A "Connector" using the shared thread pool</span><span style="color: #008000">--&gt;</span>
    <span style="color: #008000">&lt;!--</span><span style="color: #008000">
    &lt;Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" /&gt;
    </span><span style="color: #008000">--&gt;</span>
    <span style="color: #008000">&lt;!--</span><span style="color: #008000"> Define a SSL HTTP/1.1 Connector on port 8443
         This connector uses the BIO implementation that requires the JSSE
         style configuration. When using the APR/native implementation, the
         OpenSSL style configuration is required as described in the APR/native
         documentation </span><span style="color: #008000">--&gt;</span>
    <span style="color: #008000">&lt;!--</span><span style="color: #008000">
    &lt;Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" /&gt;
    </span><span style="color: #008000">--&gt;</span>
    
    <span style="color: #008000">&lt;!--</span><span style="color: #008000"> Define an AJP 1.3 Connector on port 8009 </span><span style="color: #008000">--&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">Connector </span><span style="color: #ff0000">port</span><span style="color: #0000ff">="9009"</span><span style="color: #ff0000"> protocol</span><span style="color: #0000ff">="AJP/1.3"</span><span style="color: #ff0000"> redirectPort</span><span style="color: #0000ff">="9443"</span> <span style="color: #0000ff">/&gt;</span>
    
    
    <span style="color: #008000">&lt;!--</span><span style="color: #008000"> An Engine represents the entry point (within Catalina) that processes
         every request.  The Engine implementation for Tomcat stand alone
         analyzes the HTTP headers included with the request, and passes them
         on to the appropriate Host (virtual host).
         Documentation at /docs/config/engine.html </span><span style="color: #008000">--&gt;</span>
    
    <span style="color: #008000">&lt;!--</span><span style="color: #008000"> You should set jvmRoute to support load-balancing via AJP ie :
    &lt;Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1"&gt;
    </span><span style="color: #008000">--&gt;</span>
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">Engine </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="Catalina"</span><span style="color: #ff0000"> defaultHost</span><span style="color: #0000ff">="localhost"</span><span style="color: #0000ff">&gt;</span>
    
      <span style="color: #008000">&lt;!--</span><span style="color: #008000">For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) </span><span style="color: #008000">--&gt;</span>
      <span style="color: #008000">&lt;!--</span><span style="color: #008000">
      &lt;Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/&gt;
      </span><span style="color: #008000">--&gt;</span>
    
      <span style="color: #008000">&lt;!--</span><span style="color: #008000"> Use the LockOutRealm to prevent attempts to guess user passwords
           via a brute-force attack </span><span style="color: #008000">--&gt;</span>
      <span style="color: #0000ff">&lt;</span><span style="color: #800000">Realm </span><span style="color: #ff0000">className</span><span style="color: #0000ff">="org.apache.catalina.realm.LockOutRealm"</span><span style="color: #0000ff">&gt;</span>
        <span style="color: #008000">&lt;!--</span><span style="color: #008000"> This Realm uses the UserDatabase configured in the global JNDI
             resources under the key "UserDatabase".  Any edits
             that are performed against this UserDatabase are immediately
             available for use by the Realm.  </span><span style="color: #008000">--&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">Realm </span><span style="color: #ff0000">className</span><span style="color: #0000ff">="org.apache.catalina.realm.UserDatabaseRealm"</span><span style="color: #ff0000">
               resourceName</span><span style="color: #0000ff">="UserDatabase"</span><span style="color: #0000ff">/&gt;</span>
      <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Realm</span><span style="color: #0000ff">&gt;</span>
    
      <span style="color: #0000ff">&lt;</span><span style="color: #800000">Host </span><span style="color: #ff0000">name</span><span style="color: #0000ff">="localhost"</span><span style="color: #ff0000">  appBase</span><span style="color: #0000ff">="webapps"</span><span style="color: #ff0000">
            unpackWARs</span><span style="color: #0000ff">="true"</span><span style="color: #ff0000"> autoDeploy</span><span style="color: #0000ff">="true"</span><span style="color: #0000ff">&gt;</span>
    
    <span style="color: #0000ff">&lt;</span><span style="color: #800000">Context </span><span style="color: #ff0000">path</span><span style="color: #0000ff">="ClusterProject"</span><span style="color: #ff0000"> docBase</span><span style="color: #0000ff">="/home/ump/www/ClusterProject"</span><span style="color: #ff0000"> reloadable</span><span style="color: #0000ff">="true"</span><span style="color: #0000ff">&gt;</span>
       <span style="background-color: #00ff00"> <span style="color: #0000ff">&lt;</span><span style="color: #800000">Manager  
        </span><span style="color: #ff0000">className</span><span style="color: #0000ff">="de.javakaffee.web.msm.MemcachedBackupSessionManager"</span><span style="color: #ff0000">  
        memcachedNodes</span><span style="color: #0000ff">="n1:192.168.51.75:11211"</span><span style="color: #ff0000">  
        sticky</span><span style="color: #0000ff">="false"</span><span style="color: #ff0000">  
        sessionBackupAsync</span><span style="color: #0000ff">="false"</span><span style="color: #ff0000">  
        lockingMode</span><span style="color: #0000ff">="uriPattern:/path1|/path2"</span><span style="color: #ff0000">  
        requestUriIgnorePattern</span><span style="color: #0000ff">=".*.(ico|png|gif|jpg|css|js)$"</span><span style="color: #ff0000">  
        transcoderFactoryClass</span><span style="color: #0000ff">="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"</span><span style="color: #0000ff">/&gt;</span> </span>
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Context</span><span style="color: #0000ff">&gt;</span>
    
        <span style="color: #008000">&lt;!--</span><span style="color: #008000"> SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html </span><span style="color: #008000">--&gt;</span>
        <span style="color: #008000">&lt;!--</span><span style="color: #008000">
        &lt;Valve className="org.apache.catalina.authenticator.SingleSignOn" /&gt;
        </span><span style="color: #008000">--&gt;</span>
    
        <span style="color: #008000">&lt;!--</span><span style="color: #008000"> Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" </span><span style="color: #008000">--&gt;</span>
        <span style="color: #0000ff">&lt;</span><span style="color: #800000">Valve </span><span style="color: #ff0000">className</span><span style="color: #0000ff">="org.apache.catalina.valves.AccessLogValve"</span><span style="color: #ff0000"> directory</span><span style="color: #0000ff">="logs"</span><span style="color: #ff0000">
               prefix</span><span style="color: #0000ff">="localhost_access_log."</span><span style="color: #ff0000"> suffix</span><span style="color: #0000ff">=".txt"</span><span style="color: #ff0000">
               pattern</span><span style="color: #0000ff">="%h %l %u %t &amp;quot;%r&amp;quot; %s %b"</span> <span style="color: #0000ff">/&gt;</span>
    
      <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Host</span><span style="color: #0000ff">&gt;</span>
    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Engine</span><span style="color: #0000ff">&gt;</span>
    

    </Service>
    </Server>

    复制代码

      5、同样,修改192.168.51.10上server.xml添加该session存储到memcached中。重启tomcate1和tomcate2。

      6、访问http://192.168.51.75/ClusterProject/index.jsp,并刷新界面,如下图所示

    可以看到this is Web1变成this is web2,说明刷新访问了tomcate1和tomcate2,但是上面的sessionId没有变化,说明,实现了session共享。

    至此,tomcate+Nginx+memcached,搭建的session共享集群完毕。

  • 相关阅读:
    CUDA编程学习(一)
    数据挖掘领域的十大经典算法
    MATLAB代码加密生成.p文件
    如何读入文件下的图像序列
    什么是co-training
    Addthis使用
    html5新增及删除标签
    html5语法
    设置SecureCRT配色和解决乱码问题
    html5新增及废除属性
  • 原文地址:https://www.cnblogs.com/jpfss/p/9443974.html
Copyright © 2011-2022 走看看