码迷,mamicode.com
首页 > 其他好文 > 详细

Centos 6.7 Tomcat Session集群

时间:2015-11-01 19:48:10      阅读:356      评论:0      收藏:0      [点我收藏+]

标签:服务器   客户端   信息   影响   结构图   

规划结构图

技术分享

 

nginx接收用户请求反向代理至后端应用服务器

应用服务器或者session服务器其中一台宕机,不影响客户端会话的信息


OS:CentOS release 6.7 (Final)

Memcached设置

两台memcached1 2 各进行配置

1.安装memcached

[root@Memcached1 ~]# yum install memcached

 

[root@Memcachd2 ~]#  yum install memcached

2.启动服务

[root@Memcached1 ~]# service memcachedstart

Starting memcached:                                        [  OK  ]

 

[root@Memcachd2 ~]# service memcached start

Starting memcached:                                        [ OK  ]

3.测试

[root@Memcached1 ~]# ss -tln

State       Recv-Q Send-Q                       Local  Address:Port                          Peer Address:Port

LISTEN      0       128                                      :::11211                                   :::*    

LISTEN      0       128                                       *:11211                                   *:*

 

]# telnet 192.168.1.180 11211

Trying 192.168.1.180...

Connected to 192.168.1.180.

Escape character is ‘^]‘.

quit

Connection closed by foreign host.

 

[root@Memcachd2 ~]# ss –tln

State       Recv-Q Send-Q                       Local  Address:Port                          Peer Address:Port

LISTEN      0       128                                      :::11211                                  :::*    

LISTEN      0       128                                       *:11211                                   *:*    

 

]# telnet 192.168.1.190 11211

Trying 192.168.1.190...

Connected to 192.168.1.190.

Escape character is ‘^]‘.

quit

Connection closed by foreign host.

Tomcat设置

1.TomcatA B 安装

参照tomcat基本安装

2.上传jar文件

分别把依赖包上传到 Tomcat A B

]# pwd

/usr/local/tomcat/lib

技术分享

3.TomcatA 设置

3.1创建目录

[root@TomcatA lib]# mkdir -pv  /usr/local/tomcat/webapps/test/WEB-INF/{classes,lib}

mkdir: created directory  `/usr/local/tomcat/webapps/test‘

mkdir: created directory  `/usr/local/tomcat/webapps/test/WEB-INF‘

mkdir: created directory  `/usr/local/tomcat/webapps/test/WEB-INF/classes‘

mkdir: created directory  `/usr/local/tomcat/webapps/test/WEB-INF/lib‘

修改权限

[root@TomcatA lib]# chown -R tomcat /usr/local/tomcat/webapps/test/

 

3.2 创建主页内容

]#  vim  /usr/local/tomcat/webapps/test/index.jsp

<%@ page  language="java" %>

<html>

   <head><title>TomcatA</title></head>

  <body>

    <h1><font color="palevioletred  ">TomcatA.zrd.com</font></h1>

    <table border="1">

      <tr>

        <td>Session ID</td>

    <% session.setAttribute("zrd.com","zrd.com");  %>

        <td><%= session.getId()  %></td>

      </tr>

      <tr>

        <td>Created on</td>

        <td><%=  session.getCreationTime() %></td>

     </tr>

    </table>

  </body>

</html>

 

3.3 修改server.xml

高亮部分为 添加的内容其他不要修改

[root@TomcatA conf]# vim  /usr/local/tomcat/conf/server.xml

<?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="8005"  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">

 

     <!--The connectors can use a shared executor, you can define one or  more named thread pools-->

     <!--

     <Executor name="tomcatThreadPool"  namePrefix="catalina-exec-"

         maxThreads="150" minSpareThreads="4"/>

     -->

 

 

     <!-- A "Connector" represents an endpoint by which  requests are received

          and responses are returned. Documentation at :

          Java HTTP Connector: /docs/config/http.html (blocking &  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

     -->

     <Connector port="8080" protocol="HTTP/1.1"

                connectionTimeout="20000"

               redirectPort="8443"  />

     <!-- A "Connector" using the shared thread pool-->

     <!--

     <Connector executor="tomcatThreadPool"

               port="8080"  protocol="HTTP/1.1"

                connectionTimeout="20000"

               redirectPort="8443"  />

     -->

     <!-- 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 -->

     <!--

     <Connector port="8443"  protocol="org.apache.coyote.http11.Http11Protocol"

               maxThreads="150"  SSLEnabled="true" scheme="https" secure="true"

               clientAuth="false"  sslProtocol="TLS" />

     -->

 

     <!-- Define an AJP 1.3 Connector on port 8009 -->

     <Connector port="8009" protocol="AJP/1.3"  redirectPort="8443" />

 

 

     <!-- 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 -->

 

     <!-- You should set jvmRoute to support load-balancing via AJP ie :

     <Engine name="Catalina" defaultHost="localhost"  jvmRoute="jvm1">

     -->

     <Engine name="Catalina"  defaultHost="localhost">

 

       <!--For clustering, please take a look at documentation at:

           /docs/cluster-howto.html   (simple how to)

           /docs/config/cluster.html (reference documentation) -->

       <!--

       <Cluster  className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>

       -->

 

       <!-- Use the LockOutRealm to prevent attempts to guess user  passwords

            via a brute-force attack -->

       <Realm  className="org.apache.catalina.realm.LockOutRealm">

         <!-- 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.  -->

         <Realm  className="org.apache.catalina.realm.UserDatabaseRealm"

               resourceName="UserDatabase"/>

       </Realm>

 

       <Host name="localhost"   appBase="webapps"

            unpackWARs="true"  autoDeploy="true">

 

         <!-- SingleSignOn valve, share authentication between web  applications

             Documentation at: /docs/config/valve.html  -->

         <!--

         <Valve  className="org.apache.catalina.authenticator.SingleSignOn" />

         -->

 

         <!-- Access log processes all example.

             Documentation at:  /docs/config/valve.html

             Note: The pattern used is  equivalent to using pattern="common" -->

         <Valve  className="org.apache.catalina.valves.AccessLogValve"  directory="logs"

                prefix="localhost_access_log." suffix=".txt"

               pattern="%h %l %u %t  &quot;%r&quot; %s %b" />

<!-- start-->

       <Context  path="/test" docBase="/usr/local/tomcat/webapps/test"  reloadable="true">

             <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"

                     memcachedNodes="  memcached1:192.168.1.180:11211,memcached2:192.168.1.190:11211 "

                     failoverNodes="  memcached1 "

                     requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"

       transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"

  />

 </Context>

<!-- failoverNodes=" memcached1 " memcached1为故障转移备用节点-->

<!-- end-->

      

       </Host>

     </Engine>

   </Service>

</Server>

4.TomcatB 设置

4.1创建目录

[root@TomcatB classes]# mkdir -pv  /usr/local/tomcat/webapps/test/WEB-INF/{classes,lib}

mkdir: created directory  `/usr/local/tomcat/webapps/test‘

mkdir: created directory  `/usr/local/tomcat/webapps/test/WEB-INF‘

mkdir: created directory `/usr/local/tomcat/webapps/test/WEB-INF/classes‘

mkdir: created directory  `/usr/local/tomcat/webapps/test/WEB-INF/lib‘

4.2创建主页内容

#]vim  /usr/local/tomcat/webapps/test/index.jsp

<%@ page  language="java" %>

<html>

   <head><title>TomcatB</title></head>

  <body>

    <h1><font color="skyblue">TomcatB.zrd.com</font></h1>

    <table  border="1">

      <tr>

        <td>Session ID</td>

    <%  session.setAttribute("magedu.com","magedu.com"); %>

        <td><%= session.getId()  %></td>

      </tr>

      <tr>

        <td>Created on</td>

        <td><%=  session.getCreationTime() %></td>

     </tr>

    </table>

  </body>

</html>

 

4.3 修改server.xml

添加高亮部分内容,其他不用改动

[root@TomcatB ~]# vim  /usr/local/tomcat/conf/server.xml

<?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="8005"  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">

 

     <!--The connectors can use a shared executor, you can define one or  more named thread pools-->

     <!--

     <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"

         maxThreads="150" minSpareThreads="4"/>

     -->

 

 

     <!-- A "Connector" represents an endpoint by which  requests are received

          and responses are returned. Documentation at :

          Java HTTP Connector: /docs/config/http.html (blocking &  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

     -->

     <Connector port="8080" protocol="HTTP/1.1"

               connectionTimeout="20000"

               redirectPort="8443"  />

     <!-- A "Connector" using the shared thread pool-->

     <!--

     <Connector executor="tomcatThreadPool"

               port="8080"  protocol="HTTP/1.1"

               connectionTimeout="20000"

               redirectPort="8443"  />

     -->

     <!-- 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 -->

     <!--

     <Connector port="8443"  protocol="org.apache.coyote.http11.Http11Protocol"

               maxThreads="150"  SSLEnabled="true" scheme="https" secure="true"

               clientAuth="false"  sslProtocol="TLS" />

     -->

 

     <!-- Define an AJP 1.3 Connector on port 8009 -->

     <Connector port="8009" protocol="AJP/1.3"  redirectPort="8443" />

 

 

     <!-- 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 -->

 

     <!-- You should set jvmRoute to support load-balancing via AJP ie :

     <Engine name="Catalina" defaultHost="localhost"  jvmRoute="jvm1">

     -->

     <Engine name="Catalina"  defaultHost="localhost">

 

       <!--For clustering, please take a look at documentation at:

           /docs/cluster-howto.html   (simple how to)

           /docs/config/cluster.html (reference documentation) -->

       <!--

       <Cluster  className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>

       -->

 

       <!-- Use the LockOutRealm to prevent attempts to guess user  passwords

            via a brute-force attack -->

       <Realm  className="org.apache.catalina.realm.LockOutRealm">

         <!-- 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.  -->

         <Realm className="org.apache.catalina.realm.UserDatabaseRealm"

                resourceName="UserDatabase"/>

       </Realm>

 

       <Host name="localhost"   appBase="webapps"

            unpackWARs="true"  autoDeploy="true">

 

         <!-- SingleSignOn valve, share authentication between web  applications

              Documentation at:  /docs/config/valve.html -->

         <!--

         <Valve  className="org.apache.catalina.authenticator.SingleSignOn" />

         -->

 

         <!-- Access log processes all example.

             Documentation at:  /docs/config/valve.html

             Note: The pattern used is  equivalent to using pattern="common" -->

         <Valve  className="org.apache.catalina.valves.AccessLogValve"  directory="logs"

                prefix="localhost_access_log." suffix=".txt"

               pattern="%h %l %u %t  &quot;%r&quot; %s %b" />

<!-- start-->

  <Context  path="/test" docBase="/usr/local/tomcat/webapps/test"  reloadable="true">

       <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"

       memcachedNodes=" memcached1:192.168.1.180:11211,memcached2:192.168.1.190:11211"

       failoverNodes=" memcached1"

       requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"

       transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"

       />

 </Context>

 

<!-- end-->

 

       </Host>

     </Engine>

   </Service>

</Server>

proxy设置

使用nginx作为反向代理服务器

1.安装

需要配置epel源(http://mirrors.sohu.com/fedora-epel/6/x86_64/  )

yum install nginx

2.修改配置文件

高亮部分为添加的内容,其他不动

[root@zrd0 nginx]# cat  /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;

 

     # 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;

 

     index   index.html index.htm;

       upstream tomcats {

       # ip_hash;

       server 192.168.1.150:8080;

       server 192.168.1.160:8080;

       }

     server {

         listen       80;

         server_name  localhost;

         root          /usr/share/nginx/html;

 

         #charset koi8-r;

 

         #access_log  /var/log/nginx/host.access.log  main;

 

         location / {

         

              proxy_pass http://tomcats/;

       }

         # 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 {

         }

 

         # 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;

     #    root         html;

 

     #    location / {

     #    }

     #}

 

 

     # HTTPS server

     #

     #server {

     #    listen       443;

     #    server_name  localhost;

     #    root         html;

 

     #    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 / {

     #    }

     #}

 

}

 

测试

启动服务

memcached

[root@Memcached1 ~]# service memcached  start

Starting memcached:                                         [  OK  ]

 

[root@Memcachd2 ~]# service memcached  start

Starting memcached:                                         [  OK  ]

 

Tomcat

[root@TomcatA conf]# service tomcat start

 

[root@TomcatB conf]# service tomcat start

 

nginx

[root@zrd0 nginx]# service nginx start

 

轮询测试

技术分享

技术分享

关闭Memcached2,故障转移是否成功

[root@Memcachd2 ~]# service memcached stop

Stopping memcached:                                        [  OK  ]

 

故障转移到Memcached1成功

技术分享

 

配置文件

TomcatA B

 

技术分享技术分享

nginx

技术分享


本文出自 “To live is to die” 博客,请务必保留此出处http://7431686.blog.51cto.com/7421686/1708380

Centos 6.7 Tomcat Session集群

标签:服务器   客户端   信息   影响   结构图   

原文地址:http://7431686.blog.51cto.com/7421686/1708380

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!