Deploy Tomcat Load Balancing

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )

First, the theoretical part

Tomcat Introduction

Tomcat is a core project of the Apache Software Foundation (Apache Software Foundation) in Jakarta project by the Apache, Sun and other companies and individuals to develop from. Thanks to Sun's involvement and support, the latest Servlet and JSP specifications can always be reflected in Tomcat, Tomcat 5 supports the latest Servlet 2.4 and JSP 2.0 specification. Because Tomcat advanced technology, stable performance, and it's free, so loved by fans of Java and has been recognized by some software developers become more popular Web application server.

Tomcat server is a free open source Web application server, are lightweight application server, it is not widely used in many occasions small and medium systems and concurrent user access is the preferred development and debugging JSP program. For a beginner, it can be considered that when the Apache server configured on a single machine, it responds to the access request can use HTML (under application of a Standard Generalized Markup Language) pages. Apache Tomcat server is actually an extension, but when it is run independently run, so when you run tomcat, it is in fact as a separate process with Apache running alone.

When configured correctly, Apache serves HTML pages, but actually running Tomcat JSP pages and Servlet. In addition, as Tomcat and IIS Web servers, etc., it has a function to handle HTML pages, while it is a Servlet and JSP container, independent of Tomcat Servlet container is the default mode. However, Tomcat ability to deal with static HTML is not as Apache server. At present the latest version of Tomcat 9.0.

Tomcat nodes introduction

Server node
  topmost element Tomcat instance, Tomcat server corresponding to only one. 
  port: Specifies the listening port shut down Tomcat server requests. Port marker for a process on the network, a process whereby only one port on a unique tag of a host, but the process may correspond to a plurality of ports. 
  shutdown: the above specified string to the command sent by the port.

Service node
  monitor http requests from the client, a node can have multiple Server Service nodes. Personally I think that the so-called multiple Service, the server opens up multiple threads to listen for http requests from clients different ports received. 
  name: Name Service.

Connector node
  main listening port settings Service. 
  The link between the client and the Service, configure the client and server link information. Tomcat default listening port is 8080, released commercial websites, if you want the user does not enter the port, use the default port http protocol, where they must be changed to 80. A Connector Service node can have multiple nodes. A port can only correspond to a process, but a process can correspond to multiple ports. 
  port: Specifies the port to listen for client requests. 
  protocol: Specifies the transport protocol. 
  connectionTimeout: Specifies the link timeout milliseconds. 
  redirectPort: SSL requests received after the time specified port redirected Service http request is being processed.

Engine node
  process to monitor http requests from the client, a Service Engine node with only one node. 
  name: Specifies the name of Engine. 
  defaultHost: When you specify a host default, the default host name must be the value of a Host node. 
  A complete request URL is http: protocol: // host: port / virtual directory / site root directory / resources such as http: // localhost: 8080 / aa  / examples / index.html.
  Host (also can be a virtual host or domain name), corresponding to ip. A server on the network has only one ip, ip but may correspond to a plurality of logical host, i.e., a plurality of domain names can ip. This multiple logical hosts actually only corresponds to a real physical host, whereby called virtual host. 
  Virtual directory, an alias for the time being that is the true path, you can not.

Host node
  main set of Web Hosting Service. 
  Virtual host configuration, a node may have multiple Engine Host nodes, i.e., a plurality of virtual hosts. 
  name: Name of the virtual host. The name can be easily defined, but must be DNS to resolve to the corresponding to the specified ip address. To define a virtual host, you must first apply for a domain name.
  appBase: specify where the root directory of the web application, such as webapps, the root of our site should be placed in this directory. The actual site is a file with a specific structure of folders, the folder structure has its own characteristics, such as the need to have a WEB-INF subfolders, the subfolders it must have a web.xml file. 
  There is a lower conf Tomcat web.xml file information for configuring web applications, and is shared all the sites on the server, where the change will affect all sites. Usually we are web.xml configuration file in the web application root directory of the site, it only changes the application works. This file can be used to set the default web application resources


Context node
  main virtual directory and default site Service. 
  Configure the virtual directory and default site, a Engin Context node can have multiple nodes that multiple virtual directories. We visit http: // locaohost: 8080, when the actual visit is http: // locaohost: 8080 / ROOT / index.jsp. Using the http protocol, according to the host localhost positioning, according to port 8080 to locate on the host server Tomcat Service process, this time because the user does not specify the sites and resources, the access to the default configuration of sites and resources server.xml under conf. This configuration can not be found in server.xml, because ROOT is hard-coded to write, but can be modified to other sites through the configuration file. 
  The virtual path specified web application, such as / AA: path 
  docBase: actual storage path specified web application, that is the actual path of the virtual directory, specific to the site directory
       reloadable: if it is true, Tomcat will automatically detect the project WEB-INF directory content and classes in the lib folder, if the change will automatically reload the project without having to restart the server. 

Logger node
  log information Tomcat configuration. 
  className: Class Specifies the log name used, must implement org.apache.catalina.Logger interface. 
  prefix: the prefix specified log file. 
  suffix: the suffix specified log file. 
  timestamp: If true, then the log file name must be added to the time stamp.

Value node
  similar Logger. 
  directory: specifies where the log file. 

Directory (designated log files stored position):
    pattern has two values, recorded Common remote ip address or host name, user name, date, string, the first line of the request, HTTP response code, number of bytes sent. more than the combined value of the common mode recording mode.

Tomcat directory


| --- bin: to store tomcat startup and shutdown scripts;
| --- conf: store different configuration file (server.xml and the web.xml); |
--- DOC: store Tomcat documentation;
| --- lib / japser / common: repository file (JARS) Tomcat operational needs; |
--- logs: LOG files to store the Tomcat execution;
| --- src: store Tomcat source code;
| --- webapps: Tomcat main Web publishing catalog (including application examples); |
--- Work: store the class after jsp files generated by the compiler;

 

Second, the deployment process

Preparing the Environment

            operating system              IP addresses               Deployment Services
centos7.6 192.168.81.210 Tomcat
centos7.6 192.168.81.220 Tomcat
centos7.6 192.168.81.230 NGINX

 

Tomcat deployment platform

Install JDK 1.1

   [root@localhost ~]# tar xvf jdk-7u65-linux-x64.gz

   [root@localhost ~]# mv jdk1.7.0_65/ /usr/local/java

1.2 Establish java.sh   

 [root@localhost ~]# vim /etc/profile.d/java.sh

                      export JAVA_HOME=/usr/local/java

                      export PATH=$JAVA_HOME/bin:$PATH

   [root@localhost ~]# source /etc/profile.d/java.sh

   [root@localhost ~]# java –version                                  //检测java版本

   [root@localhost ~]# java version "1.7.0_65"

                       Java(TM) SE Runtime Environment (build 1.7.0_65-b17)

                       Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

2. Install tomcat

    [root@localhost ~]# tar xvf  apache-tomcat-7.0.54.tar.gz

    [root@localhost ~]# mv apache-tomcat-7.0.54 /usr/local/tomcat

    [root@localhost ~]# /usr/local/tomcat/bin/startup.sh     //启动Tomcat程序

3. build success stories

Visit: http: //127.0.0.1: 8080

Build nginx + tomcat load balancing cluster

1. Establish a java web site (on Tomcat servers, each doing one page)   

[root@localhost ~]# mkdir -p /web/webapp1

[root@localhost ~]# vim /web/webapp1/index.jsp

                    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

                    <html>

                    <head>

                    <title>JSP test1 page</title>

                    </head>

                    <body>

                    <% out.println("Welcom to test site,http://www.test1.com");%>

                    </body>

                    </html>

2. Modify the main configuration file

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

                      <Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true">   //存在

                      <Context docBase="/web/webapp1" path="" reloadable="false" >   //写入

                       </Context>             //写入

3.Nginx configure load balancing

Yum ready to implement source, you need to rely on software download

3.1 compile and install nginx 

  [root@localhost ~]# yum -y install pcre-devel zlib-devel openssl-devel

  [root@localhost ~]# groupadd www

  [root@localhost ~]# useradd -g www  www -s /bin/false

  [root@localhost ~]# tar xvf nginx-1.6.0.tar.gz

  [root@localhost ~]# cd nginx-1.6.0

  [root@localhost ~]# ./configure --prefix=/usr/local/nginx --user=www --group=www --with- 
  file-aio --with-http_stub_status_module --with-http_gzip_static_module --with- 
  http_flv_module --with-http_ssl_module && make && make install

  [root@localhost ~]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin

3.2 modify nginx configuration file

  [root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

                      ·····

                      #gzip  on;

                      upstream tomcat_server {

                      server 192.168.1.100:8080 weight=1;

                      server 192.168.1.101:8080 weight=1;

                    }

                   server {

                   listen       80;

                   server_name  localhost;

                   #charset koi8-r;

                   #access_log  logs/host.access.log  main;

                   location / {

                   root   html;

                   index  index.html index.htm;

                   proxy_pass http://tomcat_server;

                  }

3.3 Starting nginx

  [root@localhost ~]# nginx

4. Test

Visit: http: // nginx server IP without ports, constantly refresh

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_44953658/article/details/92564376