Tomcat and Memcached

Source: Internet
Author: User

first, Tomcat operating environment

Tomcat is a program that runs on a Java virtual machine, so you need to install the Java Runtime Environment before you install Tomcat, which is used to receive and request the appropriate user

JVM: All the different interfaces on all systems are converted to a unified programming interface, enabling a single compilation to run everywhere

JRE: A tool environment that contains some library files and startup code, including the JVM

JDK: Complete basic environment with debugging tools

JSP: Depending on the client, the code is executed in the server segment and the results are returned to the client

Package Installation

Yum installation, install the required JSP and servlet two components directly based on dependencies

[email protected] ~]# Yum install-y java-1.8.0-openjdk-devel

The official download binary package, directly decompression can be used, by default in the/usr/java/directory

Second, the installation of Tomcat

Yum installation, CD storage is available, so directly installed can

[email protected] ~]# Yum install-y Tomcat

Official download package, in the official download, you need to change the owner and all groups to root, where temp,work,logs, the host and the group to change to tomcat,conf directory requires read and write permissions, files in the directory need to have Read permissions,

Iii. Tomcat Configuration Instructions

Description of each configuration file role

Server.xml: Master configuration file, mainly used to set configuration of related components such as Server,connector,service,engine,host,context

Web. xml: Deployment profile, each application can only be accessed after deployment, he is typically deployed with the web. XML defined, which is stored in the Web-inf directory

Context.xml: Each application has a dedicated configuration file, which is stored in the Meta-inf directory

Tomcat-user.xml: User authentication user name and password, boot into the memory, after booting, in the use of user name and password directly from the memory, will not be found in the file, as long as the user name changes will need to restart the service to take effect

Catalina.policy: When Tomcat is currently started with the-security option, the security policy is set for Tomcat

The definition file for the Catalina.properties:java property, which sets the class loader path and the JVM tuning related parameters

Logging.properties: Log System related configuration

Server.xml configuration file main components related instructions

Server: class, servers process

Correlate components between Service:connector and engine

Connector: connector, used to accept user requests, can also be used to build corresponding messages, the supported protocols are HTTP,HTTPS,AJP (Tomcat exclusive protocol needs to be implemented with HTTPD service)

Engine: engines, used to run JSP code, can also be called containers

Host: A virtual host that is used to build multiple URL addresses that can be accessed in the same service

Context: Build multiple URI directories in the same host, by default if you do not replace the original directory, you do not need to specify to be accessible

<server> <service> <connector/> <connector/> <engine> 

JSP Application Organizational Structure

Index.jsp: Home

Web-inf: The currently applied private resource path, typically storing the web. XML and Context.xml configuration files

Meta-inf: Acts similar to Web-inf

Classes: Class file, the current application of the provided class, is packaged into a jar format file

LIB: Class file,

Four, Tomcat based on the httpd reverse proxy

Here is the reverse proxy based on the httpd2.4 version implementation

Preparation: Specifying servername in the configuration file requires that the corresponding IP address be specified in the Hosts file for resolution, or DNS can resolve

Step 1

Proxy_http_module Proxy Configuration Example: <virtualhost *:80>servername www.test.comProxyRequests Off #关闭正向正向代理ProxyVia on #修改响应报文, whether to add via message, display the proxy server hostname proxypreservehost on #保留客户端响应首部主机名报文, the agent has only one host, the agent has two hosts need to add this option <proxy *> # Authorization require all granted</proxy>proxypass/http://www.test.com:8080/#代理后端,/must be taken, if the map is added directly at the back Proxypa***everse/ Http://www.test.com:8080/#若被重定向, whether also proxy to backend <location/> #授权Require all Granted</location></virtualhost >

Step 2

Configure a tomcat on the back-end Tomcat and add a host configuration to

V. Tomcat session stickiness of httpd-based cookies

Step 1

header add set-cookie  "routeid=.%{balancer_worker_route}e; path=/"  env=BALANCER_ROUTE _changed<proxy balancer://tcsrvs>    balancermember http:// 192.168.1.151:8080 route=tomcata loadfactor=1    balancermember http:// 192.168.1.152:8080 route=tomcatb loadfactor=2    proxyset lbmethod= Byrequests    proxyset stickysession=routeid</proxy><virtualhost *:80 >    ServerName www.test.com    ProxyVia On     ProxyRequests Off    ProxyPreserveHost On     <Proxy *>        Require all granted     </Proxy>    ProxyPass / balancer://tcsrvs/     proxypa***everse / balancer://tCsrvs/    <location />        require  all granted    </Location></VirtualHost>

Step 2 Configure two Tomcat and add a context to the default page and point to the virtual host that should be specified, or you will not be able to bind the client

Vi. Tomcat Session Cluster

Tomcat shares the client's cookie information with each Tomcat server in the same multicast domain according to the broadcast, but this method is not suitable for a large network topology, the larger the broadcast domain, the greater the network IO and disk IO, and when the session cluster is implemented, It is recommended that the session stickiness be closed because the session stickiness is turned on, and the cookie synchronization will only be synchronized in one direction, without the session stickiness, the random direction of synchronization

Realize:

Step 1: Set on the Nginx reverse proxy server, as long as the request. jsp files are fully load balanced and proxied to the back-end server

upstream webjsp {        server 192.168.1.151:8080;         server 192.168.1.152:8080;         ip_hash;} server {        listen 80;         server_name www.test.com;        index   index.jsp index.html;                 root /web/;        location ~* \. (Jsp|do) $ {                 index index.jsp;                 proxy_pass http://.com:8080;        }         location / {                 root /app/ROOT/;         }}

Step 2: Configurations that need to be configured on both Tomcat

Configuration in the Server.xml configuration file

engine  rear configuration <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.5"      #组播地址, multiple servers need to be consistent                          port= "45564"                #监听端口                          frequency= "500"        #多久发送一次心跳, Unit milliseconds                          droptime= "/> "    #超时时间             <Receiver  Classname= "Org.apache.catalina.tribes.transport.nio.NioReceiver"                        address= "192.168.1.152"    #本地IP地址                         port= "4000"     #端口                        autobind= "   #" How often do you automatically bind                        selectortimeout=     #超时时长, unit milliseconds                         maxthreads= "6"/>    #最大线程数, this needs to be based on the membership of the multicast domain, because each member must communicate with each other, so no less than the multicast Members              <sender classname= " Org.apache.catalina.tribes.transport.ReplicationTransmitter ">&nbSp;             <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= ""/>          &Nbsp;<valve classname= "Org.apache.catalina.ha.session.JvmRouteBinderValve"/>           <deployer classname= " Org.apache.catalina.ha.deploy.FarmWarDeployer "                     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>

You also need to add the configuration,

1. <distributable/> needs to be added to the application-specific Web. XML, not in the default Web. xml

2. You need to add jvmroute to the engine in the Serser.xml configuration file

3. You need to ensure that the cluster nodes are synchronized

4. You can also do session binding, in order to enable a user session to always synchronize other servers from one server to another, instead of synchronizing in all directions

Vi. introduction of Memcached

Web application data are all stored in the RDBMS database, and as the business grows, queries and other speed drops rapidly, memcached is to cache the data into memory, reduce the number of user access to the database, thereby reducing the database access pressure

Characteristics of memcached

1. Simple protocol

2. Libevent-based event handling

3. Built-in memory storage means that data is at risk of loss

4.memcached does not communicate with each other's distributed, memcached can not communicate with each other, it is not possible to synchronize data, each memcached is independent of the existence

Memcached needs to rely on the front-end application application server, requiring front-end application support

Advantages: Can improve access speed, speed up user access, generally applicable to the data is not particularly demanding accurate and not a special focus on security data

Disadvantage: Access data is not accurate, when the cache hit rate is low, will not increase access speed but will reduce access speed, because each time you access the database before you need to go to the memcached cache server to see if there is a cache.

Application by adding memcached cache work Process

Application server first go to the memcached cache server to see if there is a cache, if any, directly the corresponding client, if none, then the application server itself to the database to view the results, and then in the data to be cached to the memcached cache server, the next visit to improve the speed

Vii. Session Server

Configuring the Topology Diagram

Need to connect Tomcat to memcached need the corresponding connector, warehouse default, need third-party software https://github.com/magro/memcached-session-manager download the corresponding version

Use sticky sessions + Kryo to download all of the following packages to the Java drop JAR file directory

Msm-kryo-serializer, kryo-serializers-0.34+, kryo-3.x, Minlog, Reflectasm, asm-5.x, objenesis-2.x

Add the following configuration snippet to the Tomcat server.xml configuration file

<context path= "" docbase= "ROOT" reloadable= "true" > <manager classname= " De.javakaffee.web.msm.MemcachedBackupSessionManager "memcachednodes=" n1:web1.lin.com:11211,n2:web2.lin.com : 11211 "#指定后端的memcached服务器 failovernodes=" N1 "#指定备用 requesturiignorepattern=". *\. (ICO|PNG|GIF|JPG|CSS|JS) $ "#指定哪些数据不被缓存 transcoderfactoryclass=" De.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory "#转码工厂类/></context>

memcached server only need to turn on the service

View Cache commands Memdump--server= ' 192.168.1.153:11211 '

Tomcat and Memcached

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.