HTTPD anti-generational + tomcat cluster + Redis session hold

Source: Internet
Author: User
Tags set cookie install redis

Release Notes:

HTTPD version: 2.4.6

Tomcat version: 7.0.69

Dedis version: 3.2.3

OS version: CentOS Linux release 7.3.1611


Topology structure:

tomcat-a/192.168.1.106

Client------>HTTPD Reverse proxy------->-----> Redis Session Hold Server

192.168.1.101 tomcat-b/192.168.1.107 192.168.1.100

HTTPD Reverse Proxy:

Installing the HTTPD Service

Yum-y Install httpd

httpd Implementing a reverse proxy requires three modules support:

mod_proxy.so mod_proxy_http.so mod_proxy_balancer.so

Edit Proxy configuration

cat  /etc/httpd/conf.d/tomcat-proxy.conf<proxy balancer://tcsrvs>         balancermember http://192.168.1.106:8080         balancermember http://192.168.1.107:8080         proxyset lbmethod=byrequests    #调度方式, scheduled on request, similar to polling </proxy><virtualhost *:80 >        servername lb.tomcat.com         proxyvia on        proxyrequests off         proxypreservehost on         <proxy *>                 require all granted        </proxy>         proxypass / balancer://tcsrvs/         Proxypassreverse / balancer://tcsrvs/        <location  />                 Require all granted        </location></virtualhost >


Tomcat Server configuration:

Install the corresponding package

JAVA-1.8.0-OPENJDK Tomcat Tomcat-lib Tomcat-admin-webapps Tomcat-webappstomcat-docs-webapp

Prepare the test file

mkdir /usr/share/tomcat/webapps/test/tomcat-a:cat /usr/share/tomcat/webapps/test/index.jsp                                                           <%@ page language= "Java"  %>

Then start the Tomcat service

Systemctl Start Tomcat.service

At this point in the browser test http://192.168.1.101/test/

The above is just the need to complete the httpd scheduling back-end Tomcat host

When the client access, do not want to change the session information, imagine that you have a single e-commerce site, and then refresh the page scheduler to dispatch your request to another machine, the next single no, how do you feel? Therefore, in order to avoid this phenomenon, we bind the session by setting cookie information


httpd Server configuration:

cat tomcat-proxy.conf          header add  set-cookie  "routeid=.%{balancer_worker_route}e; path=/"  env=BALANCER_ROUTE_CHANGED           #设置cookie信息 <proxy balancer://tcsrvs>         balancermember http://192.168.1.106:8080 route=tomcata loadfactor =1        balancermember http://192.168.1.107:8080 route= Tomcatb loadfactor=1        proxyset lbmethod=byrequests         proxyset stickysession=routeid   # Set Cookie</proxy> #route与后端tomcat的jvmRoute对应 based on session stickiness <virtualhost *:80>         servername lb.tomcat.com        proxyvia  on    &nbSp;   proxyrequests off        proxypreservehost  on        <proxy *>                 require all granted         </proxy>        proxypass  / balancer://tcsrvs/        proxypassreverse /  balancer://tcsrvs/        <location />                 require all granted         </location></virtualhost>

Tomcat Host Configuration:

Vim server.xml <engine name= "Catalina" defaulthost= "localhost" jvmroute= "tomcata" > #修改tomcat主机, Jvmroute is supported for load balancing

Similar modifications are made to the TOMCATB host.

This will enable cookie-based session stickiness, and in the browser test http://192.168.1.101/test/will only be transferred to the same host, when this host failure is dispatched to another host

There is a problem with the above cookie-based session stickiness, that is, the client-side cookie-based access to back-end server A, if a host failure, HTTPD will be re-dispatched to Host B, but there is no previous session information, so the client browsing records, the situation and other information will disappear.

You can use the Tomcat cluster with a separate cache server Redis to solve the above problem, the Tomcat cluster resolves the host single point of failure problem, the session information is stored separately on the cache server instead of the Tomcat host

, but Redis is also highly available to avoid a single point of problem, where we can only implement session caching. Only high availability people are interested to study on their own


When configuring session server, do not use cookie information to delete the cookie configuration in the httpd configuration.

Vim tomcat-proxy.conf Header Add Set-cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}E; path=/"env=balancer_route_changed proxyset Stickysession=routeid #将上述两行删掉或者注释, and then restart the service

Tomcat Host Configuration cluster:

cat server.xml   #在Engine字段下配置下述内容 <cluster classname= " Org.apache.catalina.ha.tcp.SimpleTcpCluster " channelsendoptions=" 8 ">         <manager classname= "Org.apache.catalina.ha.session.DeltaManager"                  expiresessionsonshutdown= "false"  notifylistenersonreplication= "true"/>        <channel  classname= "Org.apache.catalina.tribes.group.GroupChannel" >         <membership classname= "Org.apache.catalina.tribes.membership.McastService"                  address= "228.0.0.4"                   port= "45564"             &nbSp;     frequency= "          "        droptime= "/>        " <receiver classname= "Org.apache.catalina.tribes.transport.nio.NioReceiver"                  address= "Auto"                   port= "4000"                   autobind= "  "                selectortimeout= " "                  maxthreads= "6"/>        <sender classname= " Org.apache.catalina.tribes.transport.ReplIcationtransmitter ">        <transport classname=" Org.apache.catalina.tribes.transport.nio.PooledParallelSender "/>         </sender>        <interceptor classname= " Org.apache.catalina.tribes.group.interceptors.TcpFailureDetector "/>         <interceptor classname= " Org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor "/>         </channel>        <valve classname= " Org.apache.catalina.ha.tcp.ReplicationValve " filter=" "/>         <valve classname= "Org.apache.catalina.ha.session.JvmRouteBinderValve"/>         <deployer classname= "Org.apache.catalina.ha.deploy.FarmWarDeployer" &nbsP;tempdir= "/tmp/war-temp/"                  deploydir= "/tmp/war-deploy/"  watchdir= "/tmp/war-listen/"  watchenabled= "false"/>         <clusterlistener classname= " Org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener " />         <clusterlistener classname= "Org.apache.catalina.ha.session.ClusterSessionListener"  / >        </Cluster>

Many of the above, can be copied directly, without making any changes, both host to configure

Reference Connection http://tomcat.apache.org/tomcat-7.0-doc/cluster-howto.html

Note There are two errors in the official site template configuration:

<clusterlistener classname= "Org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/> < Clusterlistener classname= "Org.apache.catalina.ha.session.ClusterSessionListener"/> #官网中此处末尾都没有加结束符/

then modify the Web. xml

Cat web.cml<distributable/> #此处指明tomcat的分布式集群

Install Redis, and set

Yum-y Install Redis vim/etc/redis.confbind 0.0.0.0# Specifies to listen for all addresses, the default listening port is tcp6379

Then start the Redis service

Then configure the Tomcat host, one for example, and the other to configure in this way

Cat context.xml #编辑如下内容 <valve classname= "Com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve"/ > <manager classname= "Com.orangefunction.tomcat.redissessions.RedisSessionManager" host= "192.168.1.100" PO Rt= "6379" database= "0" maxinactiveinterval= "/> #192.168.1.100 is the address of the Redis

Prepare the jar package, three jar packages, as follows

Https://github.com/izerui/tomcat-redis-session-manager.git

The three jar packages are then placed in the Tomcat Library directory, which is the/usr/share/tomcat/lib/

Restart the Tomcat service

When you open the browser test http://192.168.1.101/test/, you will find the SessionID unchanged, but the dispatched backend host changed


Summarize:

Redis only modifies the listening address, others do not have to modify any configuration;

HTTPD is only the role of initiating the reverse generation, the key is to configure the back-end host scheduling;

The Tomcat cluster needs to modify the Server.xml and Web. xml files, and the Redis cache also needs to be configured with context.xml and prepare the jar package


End

HTTPD anti-generational + tomcat cluster + Redis session hold

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.