Linux Tomcat Introduction

Source: Internet
Author: User
Tags memcached session id server port

Introduction to JDK JRE JVM

    • JDK (Java Development Kit): Is the Java language Development Toolkit, which provides the Java language development tool, which contains the JRE, but also contains the Jvmjava virtual machine.
      • JRE (Java Runtime Environment): Is the Java language operating environment, it contains the Java virtual machine, which is the JVM, but also contains the Java language to run the core class library.
        • JVM (Java Virtual Machine): Implementing virtual machines that rely on Java for cross-platform needs
Java Frame composition

It is said that Java was originally used to make a TV set-top box, then appeared web,jdk+ servlet,jsp mode to make Java suddenly popular, servlet function is similar to CGI, used to explain websocket,jsp is to better write HTML, Writes the Java statement in the JSP as a label, Jasper the. jsp file to be translated. java. Traditional. Java is translated into a. Class by the Javac compiler and runs on the JVM, so. Class is not a real computer binary code, but a JVM is compiled at a time. Class can be interpreted as a computer-aware binary code.
Tomcat implements some of the features of Java 2 EE, such as servlet,jsp, which is an open source software, followed by a major introduction to Tomcat.

OpenJDK with the JDK, the former open source, the latter is Oracle company owned, Red Hat's base warehouse is included in the OPENJDK.

OPENJDK Installation
[[email protected] ~]# yum -y install java-1.8.0-openjdk-devel.x86_64
Java_home Configuration
[[email protected] alertscripts]# which java/usr/bin/java[[email protected] alertscripts]# ll /usr/bin/javalrwxrwxrwx 1 root root 22 Jun 12 15:03 /usr/bin/java -> /etc/alternatives/java[[email protected] alertscripts]# ll /etc/alternatives/javalrwxrwxrwx 1 root root 73 Jun 12 15:03 /etc/alternatives/java -> /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.171-8.b10.el7_5.x86_64/jre/bin/java[[email protected] ~]# vim /etc/profile.d/java.shexport JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.171-8.b10.el7_5.x86_64
JDK Installation
http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html[[email protected] ~]# wget http://download.oracle.com/otn-pub/java/jdk/8u171-b11/512cd62ec5174c3487ac17c61aaa89e8/jdk-8u171-linux-x64.rpm[[email protected] ~]# rpm -ivh jdk-8u171-linux-x64.rpm
Java_home Configuration
[[email protected] ~]# vim /etc/profile.d/java.shexport JAVA_HOME=/usr/java/latestexport PATH=$JAVA_HOME/bin:$PATH
Tomcat installation
[[email protected] jvm]# yum install tomcat会依赖tomcat-jsp-2.2-api   tomcat-servlet-3.0-api 还有其他很多#web相关安装[[email protected] jvm]# yum install tomcat-webapps tomcat-admin-webapps tomcat-doc-webapps
[[email protected] jvm]# systemctl start tomcat[[email protected] jvm]# ss -lntup|grep javatcp    LISTEN     0      100      :::8080                 :::*                   users:(("java",pid=103600,fd=49))tcp    LISTEN     0      1      ::ffff:127.0.0.1:8005                 :::*                   users:(("java",pid=103600,fd=54))tcp    LISTEN     0      100      :::8009                 :::*                   users:(("java",pid=103600,fd=50))
Tomcat binary Installation
[[email protected] ~]# wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-7/v7.0.88/bin/apache-tomcat-7.0.88.tar.gz[[email protected] ~]# tar xf apache-tomcat-7.0.88.tar.gz  -C /usr/local/[[email protected] local]# pwd/usr/local[[email protected] local]# ln -sv apache-tomcat-7.0.88 tomcat‘tomcat’ -> ‘apache-tomcat-7.0.88’[[email protected] local]# cd tomcat/[[email protected] tomcat]# lsbin  conf  lib  LICENSE  logs  NOTICE  RELEASE-NOTES  RUNNING.txt  temp  webapps  work

Environment variable Configuration

[[email protected] tomcat]# vim /etc/profile.d/tomcat.shexport CATALINA_HOME=/usr/local/tomcatexport PAHT=$CATALINA_HOME/bin:$PATH

Startup scripts

[[email protected] tomcat]# catalina.sh -h[[email protected] tomcat]# catalina.sh configtest #配置文件测试[[email protected] tomcat]# catalina.sh start[[email protected] tomcat]# ss -lntup|grep javatcp    LISTEN     0      100      :::8080                 :::*                   users:(("java",pid=39807,fd=45))tcp    LISTEN     0      1      ::ffff:127.0.0.1:8005                 :::*                   users:(("java",pid=39807,fd=51))tcp    LISTEN     0      100      :::8009                 :::*                   users:(("java",pid=39807,fd=46))
Tomcat configuration file composition

Server.xml: Master config file;
Web. xml: Each webapp can only be accessed after "deployed", and it is typically deployed in the web-inf/directory, which is defined by Web. XML, and this file provides default deployment-related configuration for all WebApps;
Context.xml: Each webapp can be a dedicated configuration file, which is usually defined by a dedicated configuration file context.xml, which is stored in the web-inf/directory, which provides default configuration for all WebApps;
Tomcat-users.xml: User-authenticated account and password file;
Catalina.policy: Used to set the security policy for Tomcat when you start Tomcat with the-security option;
The definition file for the Catalina.properties:Java property, which sets the class loader path and some parameters related to tuning the JVM;
Logging.properties: Log system-related configuration;

SHUTDOWN

SHUTDOWN can shut down the service so only listen on the local-1 do not listen, the production environment is best to turn off.

[[email protected] tomcat]# vim /etc/tomcat/server.xml<Server port="8005" shutdown="SHUTDOWN">  #[[email protected] usr]# telnet 127.0.0.1 8005#SHUTDOWN#[[email protected] usr]# ss -lntup|grep 8080#[[email protected] usr]# ss -lntup|grep 8005
Manager-gui
 [[email protected] Catalina]# vim /etc/tomcat/tomcat-users.xml    <role rolename="manager-gui"  />  <user username="tomcat" password="tomcat" roles="manager-gui"/>[[email protected] Catalina]# systemctl restart tomcat

Admin-gui

Configure the virtual machine to be in memory and restart it.

[[email protected] Catalina]# vim /etc/tomcat/tomcat-users.xml<role rolename="admin-gui"/><!-- <role rolename="admin-script"/> --><!-- <role rolename="manager"/> --> <role rolename="manager-gui"  />  <user username="tomcat" password="tomcat" roles="manager-gui,admin-gui"/>

Connector

Connector can define multiple

    <Connector port="8080" protocol="HTTP/1.1"               connectionTimeout="20000"               redirectPort="8443" />       <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

Property:

port= "8080"
Protocol= "http/1.1"
connectiontimeout= "20000"
Address: The IP addresses of the listening, the default is the native all available addresses;
MaxThreads: Maximum number of concurrent connections, default is 200;
Enablelookups: Whether the DNS query feature is enabled; must be false, reverse client IP
Acceptcount: Maximum length of wait queue; half-link pool
Secure
Sslprotocol:

Host-context-valve
<Host name="www.test.deve" appBase="/www/data/test.dev" unpackWARs="true" autoDeploy="true">            <Context path="/abc" docBase="/www/data/test.dev/ROOT/abc/abc" reloadable=""/> #Context 跟 alias类似效果      <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" /></Host>

Host Common Properties Description:

(1) AppBase: The default directory of WebApps for this host, which is the directory of the non-archived Web application or the path to the archived war file directory; You can use a relative path based on the path defined by the $catalina_base variable;
(2) Autodeploy: When Tomcat is in the running state, it is automatically deployed to Tomcat when a webapp is placed in a directory defined by AppBase;

Log

Common-%h%l%u%t "%r"%s%b
Combined-%h%l%u%t "%r"%s%b "%{referer}i" "%{user-agent}i"
filedateformat= "Yyyy-mm-dd.hh"
%a-Remote IP address
· %A-Local IP address
· %b-Number of bytes sent, excluding HTTP headers, if 0, use "-"
· %B-Number of bytes sent, not including HTTP headers
· %H-Remote Host name (if resolvehost=false, remote IP address)
· %H-Request Agreement
· %L-The remote logical user name returned from Identd (Always return '-')
· %m-Method of request (Get,post, etc.)
· %p-Local port number to which the request was received
· %q-Query string (if present, to '? ') Start
· %r-the first line of the request, containing the requested method and URI
· %s-Status code of the response
· %s-session ID of the user
· %t-Log and time, using the usual log format
· %u-Remote user after authentication (if present, otherwise '-')
· %u-URI path of the request
· %V-Name of the local server
· %d-the time, in milliseconds, to process the request
· %T-the time, in seconds, to process the request

[[email protected] data]# cat /var/log/tomcat/test.deve_access_log.2018-06-13.log192.168.1.104 - - [13/Jun/2018:08:27:02 +0800] "GET /abc/index2.jsp HTTP/1.1" 200 91192.168.1.104 - - [13/Jun/2018:08:27:02 +0800] "GET /robots.txt HTTP/1.1" 404 971192.168.1.104 - - [13/Jun/2018:08:27:05 +0800] "GET / HTTP/1.1" 200 91
Anti-generational

Nginx Reverse, Load balancer refer to http://blog.51cto.com/marvin89/2122976

server {        listen       80;        server_name  www.test.deve;        index index.php;        location  / {                proxy_set_header Host $host;                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;                proxy_pass http://192.168.1.200:8080/;            }  }
Apache,proxy_http_module Proxy Configuration Example
    <VirtualHost *:80>        ServerName      www.test.deve        ProxyRequests Off           #关闭正向代理        ProxyVia        On        ProxyPreserveHost On        <Proxy *>            Require all granted        </Proxy>        ProxyPass / http://192.168.1.200:8080/        ProxyPa***everse / http://192.168.1.200:8080/   #它一般和ProxyPass指令配合使用,此指令使Apache调整HTTP重定向应答中Location, Content-Location, URI头里的URL,这样可以避免在Apache作为反向代理使用时。            <Location />            Require all granted        </Location>    </VirtualHost>
Apache,proxy_ajp_module Proxy Configuration Example
    <VirtualHost *:80>        ServerName      www.test.deve        ProxyRequests Off        ProxyVia        On        ProxyPreserveHost On        <Proxy *>            Require all granted        </Proxy>            ProxyPass / ajp://192.168.1.200:8009/             ProxyPa***everse / ajp://192.168.1.200:8009/         <Location />            Require all granted        </Location>    </VirtualHost>
Apache Load Balancer

Need to load modules, the latter several are algorithms, load on demand

LoadModule proxy_balancer_module modules/mod_proxy_balancer.soLoadModule slotmem_shm_module modules/mod_slotmem_shm.soLoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.soLoadModule lbmethod_bytraffic_module modules/mod_lbmethod_bytraffic.soLoadModule lbmethod_bybusyness_module modules/mod_lbmethod_bybusyness.soLoadModule lbmethod_heartbeat_module modules/mod_lbmethod_heartbeat.so

Introducing Session Binding Load

Header add Set-cookie "routeid=.%{balancer_worker_route}e; path=/"env=balancer_route_changed #session <proxy balancer://tcsrvs> balancermember http://192.168.1.201:808 0 Route=tomcata loadfactor=1 #route session balancermember http://192.168.1.200:8080 ROUTE=TOMCATB loadfactor= 1 #route session Proxyset lbmethod=byrequests proxyset Stickysession=routeid #session &LT;/PROXY&GT;&L T VirtualHost *:80> ServerName www.test.deve proxyrequests Off Proxyvia on Proxypreservehost        On <proxy *> Require all granted </Proxy> proxypass/balancer://tcsrvs/ proxypa***everse/balancer://tcsrvs/<location/> Require all granted </locat                ion> <Location/balancer-manager> SetHandler Balancer-manager proxypass! Require all granted </Location></VirtualHost>

Memcache Session Dual Write configuration
依赖包[[email protected] ~]# wget http://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager/2.1.1/memcached-session-manager-2.1.1.jar[[email protected] ~]# wget http://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager-tc7/2.1.1/memcached-session-manager-tc7-2.1.1.jar[[email protected] ~]# wget http://repo1.maven.org/maven2/net/spy/spymemcached/2.12.1/spymemcached-2.12.1.jar[[email protected] ~]# wget http://repo1.maven.org/maven2/de/javakaffee/msm/msm-javolution-serializer/2.1.1/msm-javolution-serializer-2.1.1.jar[[email protected] ~]# wget http://www.java2s.com/Code/JarDownload/javolution/javolution-5.4.3.1.jar.zip

Dependency Package File Location

[[email protected] tomcat]# rpm -ql tomcat-lib/usr/share/java/tomcat/ ******

Configuration file

<Context path="/test" docBase="test" reloadable="true">      <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"        memcachedNodes="n1:192.168.1.200:11211,n2:192.168.1.202:11211"        failoverNodes="n1"        requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"        transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"      /></Context>

Configure test scripts for each node

[[email protected] conf]# mkdir -pv /www/data/test.dev/WEB-INF/{classes,lib}[[email protected] conf]# vim /www/data/test.dev/test/index.jsp<%@ page language="java" %>



Compared to the program implementation advantages, no need to detect which node failure, automatic selection.

Optimize---memory

JVM Heap Memory Architecture diagram

Memory usage

Jstat

[[email protected] conf]# jstat -gc 116986 S0C    S1C    S0U    S1U      EC       EU        OC         OU       MC     MU    CCSC   CCSU   YGC     YGCT    FGC    FGCT     GCT37376.0 37376.0  0.0   24164.8 99840.0  27025.7   349696.0   55422.0   18432.0 17770.9 2048.0 1809.8      7    0.217   0      0.000    0.217

Parameter description

s0c, s1c, s0u, S1u:survivor 0/1 area Capacity (capacity) and usage (used)
EC, Eu:eden area capacity and usage
OC, OU: Capacity and usage for older generations
PC, PU: Permanent generation capacity and consumption
YGC, YGT: Young generation GC times and GC time-consuming
FGC, Fgct:full GC times, and full GC time-consuming
GCT:GC Total time-consuming

The contents of the display are described below (some of the results are shown by other parameters and are not described):
S0C: Capacity of the first Survivor (Survivor area) in the Young Generation (bytes)
S1C: Capacity of the second survivor (Survivor area) in the younger generation (bytes)
S0U: The first survivor (Survivor area) of the young generation has currently used space (bytes)

S1U: The second survivor (Survivor area) of the young generation has currently used space (bytes)
EC: The Capacity of Eden (Eden) in the Young Generation (bytes)
EU: Eden (Eden) in the young generation has currently used space (bytes)
Capacity of the Oc:old generation (bytes)
Ou:old currently used space (bytes)
Pc:perm (persistent generation) capacity (bytes)
Pu:perm (persistent generation) currently used space (bytes)
YGC: Number of GC times in young generations from application startup to sampling
YGCT: The time taken by the GC in the young generation from application startup to sampling (s)
FGC: The recycle mechanism (all memory runs out) of the old (full GC) GC when it is started from the application to sampling, and it will get stuck
FGCT: Time taken from an old (full GC) GC when the application boots to sampling (s)
GCT: GC Total time from Application boot to sample (s)
NGCMN: initial (minimum) size in Young generation (bytes)
NGCMX: Max capacity of young generation (bytes)
NGC: Current capacity in young generation (bytes)
The size (in bytes) of the initialization (minimum) in ogcmn:old generation
Maximum capacity of the Ogcmx:old generation (bytes)
The current newly generated capacity of the Ogc:old generation (bytes)
The size (in bytes) of the initialization (minimum) in pgcmn:perm generation
Maximum capacity of the Pgcmx:perm generation (bytes)
The current newly generated capacity of the Pgc:perm generation (bytes)
S0: The first survivor (Survivor area) of the young generation has been used as a percentage of current capacity
S1: The second survivor (Survivor area) in the younger generation has been used as a percentage of current capacity
E: Eden (Eden) in the young generation has been used as a percentage of current capacity
Percentage of current capacity used by O:old generation
Percentage of current capacity used by P:perm generation
S0CMX: Maximum Capacity (bytes) of the first Survivor (Survivor area) in the young generation
S1CMX: Maximum capacity of the second survivor (Survivor area) in the younger generation (bytes)
ECMX: Maximum capacity of Eden (Eden) in the Young Generation (bytes)
DSS: Capacity (bytes) currently required for Survivor (surviving area) (Eden area Full)
TT: Limited number of holds
MTT: Maximum number of hold limit

RPM installation, configuration parameter location

[[email protected] conf]# vim /etc/sysconfig/tomcat

Parameter description

java_opts= "-server-xms32g-xmx32g-xx:newsize=-xx:maxnewsize=-xx:permsize=-xx:maxpermsize="
-server: Server model
-XMS: Heap memory initialization size;
-XMX: Maximum heap memory space;
-xx:newsize=: The size of cenozoic space initialization;
-xx:maxnewsize=: Cenozoic space maximum;
-xx:permsize=: Persistent generation space initialization size; After JDK8 2 items are not specified
-xx:maxpermsize=: Maximum value of persistent generation space;

Demo (Test Demo): Single Java single serve memory up to 32G. The actual Java still eats the memory, as far as possible allocates

JAVA_OPTS="-Xmx512m -Xms512m"   #一般分配一个最大值和新生代最大值,老年代自动分配好了

Optimize---thread pool
<Connector port="8080" protocol="HTTP/1.1"  connectionTimeout="20000" redirectPort="8443" />

Common Properties:

MaxThreads: Maximum number of threads;
Minsparethreads: Minimum number of idle threads;
Maxsparethreads: Maximum number of idle threads;
Acceptcount: The maximum length of the wait queue;
Uriencoding:uri address encoding format, it is recommended to use UTF-8;
Enablelookups: Whether to enable DNS resolution, it is recommended to disable;
Compression: Whether to enable the transport compression mechanism, the proposed "on";
Compressionminsize: The minimum value of the data stream to enable compression transmission, in bytes;
Compressablemimetype: Defines the MIME type that enables the compression feature;
Text/html, Text/xml, Text/css, Text/javascript

Optimize---Hidden version information
<Connector port="8080"   protocol="HTTP/1.1" Server="SOME STRING"  connectionTimeout="20000" redirectPort="8443" />

Linux Tomcat Introduction

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.