centos 7 Tomcat deployment and load balancing configuration in detail

Tomcat Server is a free open source web application server, are lightweight application server, not a lot of places that are commonly used in small and medium systems and concurrent users, is the preferred development and testing JSP program. In general, and although the Tomcat web server apache or Nginx such as an HTML page having a processing function, but because of its ability to handle much less apache static page or Nginx, it is generally as a Tomcat servlet and JSP container, operating alone the back-end, Tomcat application scenario is as follows:

centos 7 Tomcat deployment and load balancing configuration in detail

User access is always apache / Nginx server, and then by the apache / Nginx forwarded to the server process Tomcat server, all servers are connected to the shared storage servers, so that each time a user access to the data is the same, apache / Nginx is used to make scheduling, which is known as load balancing, load balancing on little explanation. . .

Typically, a Tomcat site due to possible single point of failure and can not cope with too many complex and diverse customer requests and other issues, can not be used alone in a production environment, we need to use load balancing to address these issues.

Nginx is a very good http server software, which supports the response up to 50,000 concurrent connections, has a strong ability to deal with static resources, stable, and memory, CPU and other system resource consumption is very low. At present, many large sites use Nginx server as a backend web application of a reverse proxy and load balancers to improve concurrency load the entire site.

Begin, set up the following environment, in order to simplify, do not deploy shared storage server, and the environment as follows:

centos 7 Tomcat deployment and load balancing configuration in detail

First, the pre-deployment preparation:

Three servers are using centos7 to deploy, the deployment process of the software used is as follows:

  • centos7 system image;
  • Nginx and Tomcat source package, available to download from the official website, I can also provide a link to download from (packaged as an ISO image file): link: https://pan.baidu.com/s/1hQOG-e9aaW8V2kvbBSzIxg
    extraction code: 9pdv

Second, configure Tomcat server:

1, began to deploy on the 192.168.1.1 server Tomcat (here on the firewall configuration is omitted, configure their own firewall to release relevant traffic directly to a firewall I stopped here, Tomcat default port number is 8080; Nginx default the port number is 80):

[root@localhost ~]# java -version    #查看JDK是否安装,若没有,自行安装
openjdk version "1.8.0_161"
OpenJDK Runtime Environment (build 1.8.0_161-b14)
OpenJDK 64-Bit Server VM (build 25.161-b14, mixed mode)
[root@localhost media]# tar zxf apache-tomcat-8.5.16.tar.gz -C /usr/src      #解压Tomcat包
[root@localhost media]# cd /usr/src/
[root@localhost src]# mv apache-tomcat-8.5.16/ /usr/local/tomcat8 
#Tomcat不用编译安装,解压后即可用
[root@localhost src]# mkdir -p /web/webapp1        #建立Java的web站点,用于存放网站文件
[root@localhost src]# vim /web/webapp1/index.jsp          #建立一个index.jsp的测试页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
 <head>
        <title>JSP test1 page</title>
 </head>
 <body>
        <% out.println("www.test1111.com");%>
 </body>
</html>
[root@localhost src]# vim /usr/local/tomcat8/conf/server.xml       #修改Tomcat的主配置文件

                    ......................................
 <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">     #定位到该行,然后添加下面两行内容
        <Context docBase="/web/webapp1" path="" reloadable="false">
        </Context>
#docBase:web应用的文档默认目录;
#path=""设置默认“类;”
#reloadable设置监视“类”是否变化;
[root@localhost ~]# /usr/local/tomcat8/bin/startup.sh              
#启动服务,停止服务的话,只需将startup.sh换为shutdown.sh即可。
Using CATALINA_BASE:   /usr/local/tomcat8
Using CATALINA_HOME:   /usr/local/tomcat8
Using CATALINA_TMPDIR: /usr/local/tomcat8/temp
Using JRE_HOME:        /usr
Using CLASSPATH: /usr/local/tomcat8/bin/usr/local/tomcat8/bin/tomcat-juli.jar
Tomcat started.

[root@localhost src]# netstat -antp | grep 8080             #查看默认端口8080是否在监听状态
tcp6       0      0 :::8080        :::*            LISTEN      13220/java          

The machine Test Access: 192.168.1.1: 8080, see the following test page:
centos 7 Tomcat deployment and load balancing configuration in detail

At this point, 192.168.1.1 configuration of Tomcat has been completed, another Tomcat server configuration 192.168.1.2 and 192.168.1.1 exactly the same configuration, the configuration of the above configuration it again on the server 192.168.1.2, but in order to test when you can see the load balancing effect, so that we can see each server access is not the same, you need to test pages Tomcat server 192.168.1.2 and 192.168.1.1 are not the same page.

But in the actual production environment, two Tomcat access must use the same shared storage server, no matter which server to provide services to the user, the user receives a page must be the same.

Themselves above configuration on the server 192.168.1.2 to it again, the test page content server 192.168.1.2 change it, as follows:

[root@localhost src]# 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("www.test22222222222222.com");%>
 </body>
</html>

Third, configure Nginx server (IP: 192.168.1.1):

1, install Nginx:

 [root@localhost ~]# yum -y install pcre-devel zlib-devel openssl-devel
 #安装依赖包
 [root@localhost ~]# useradd www -s /bin/false        #创建运行用户
[root@localhost media]# tar zxf nginx-1.12.0.tar.gz -C /usr/src     #解包
[root@localhost media]# cd /usr/src/nginx-1.12.0/       #切换至该目录
[root@localhost nginx-1.12.0]# ./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 && make && make install               #编译安装
[root@localhost nginx-1.12.0]# vim /usr/local/nginx/conf/nginx.conf              #编辑主配置文件
                 .............................
   #gzip  on;                  #定位到该行,写入下面四行
    upstream tomcat_server {          
        server 192.168.1.1:8080 weight=1;
        server 192.168.1.2:8080 weight=1;
}
   #写到这里结束
     #weight参数表示权重,权重越高,表示被分配到的概率越大。
     #为了测试效果明显,这里将权重设置为一样
    server {
        listen       80;
        server_name  localhost;
         ......................
  location / {
            root   html;
            index  index.html index.htm;
            proxy_pass  http://tomcat_server;               #定位到该{ }中,写入该行,“http://”后面的名字要和上面添加的upstream项后面的名字一致,才可实现调度。
        }

2, optimal control of Nginx:

[root@localhost nginx-1.12.0]#  ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/                 
#创建主程序的链接文件
[root@localhost ~]# vim /etc/init.d/nginx     #编辑服务脚本
#!/bin/bash
# chkconfig: - 99 20
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
  start)
        $PROG
  ;;
  stop)
        kill -s QUIT $(cat $PIDF)
  ;;
  restart)
        $0 stop
        $0 start
  ;;
  reload)
        kill -s HUP $(cat $PIDF)
  ;;
  *)
        echo "USAGE:$0 {start | stop | restart | reload}"
        exit 1
esac
exit 0
[root@localhost ~]# chmod +x /etc/init.d/nginx      #添加执行权限
[root@localhost ~]# chkconfig --add nginx           #添加为系统服务
[root@localhost nginx-1.12.0]# nginx -t              #检查主配置文件是否有误
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# systemctl start nginx             #启动Nginx服务,以确认脚本的正常运行
[root@localhost ~]# netstat -anpt | grep nginx                    #查看80端口是否处于监听状态
tcp        0      0 0.0.0.0:80      0.0.0.0:*      LISTEN      90475/nginx: master 

Fourth, access the test:

So far, the deployment has been completed, and now use the client access server Nginx 192.168.1.1 test, the effect is as follows:

The first visit will see the following interface:

centos 7 Tomcat deployment and load balancing configuration in detail

Refresh the page will see the following interface

centos 7 Tomcat deployment and load balancing configuration in detail

You can see that our visit is Nginx server, an access request is actually handle Tomcat server, and each access request is different Tomcat servers to handle, the effect is also obvious.

V. Written in the last: little problems encountered during deployment:

After the change in the profile of the Tomcat server, when accessing a test, to see the visit is still the default page that comes with Tomcat, a little puzzled, trying to use the following commands to start and stop the Tomcat service several times, like a:

[root@localhost webapp1]# /usr/local/tomcat8/bin/shutdown.sh                   #停止服务
Using CATALINA_BASE:   /usr/local/tomcat8
Using CATALINA_HOME:   /usr/local/tomcat8
Using CATALINA_TMPDIR: /usr/local/tomcat8/temp
Using JRE_HOME:        /usr
Using CLASSPATH:/usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jar
[root@localhost webapp1]# /usr/local/tomcat8/bin/startup.sh                #启动服务
Using CATALINA_BASE:   /usr/local/tomcat8
Using CATALINA_HOME:   /usr/local/tomcat8
Using CATALINA_TMPDIR: /usr/local/tomcat8/temp
Using JRE_HOME:        /usr
Using CLASSPATH:/usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jar
Tomcat started.

This service may be the start and stop of the problem, not go into this service and other services are not the same:
after Nginx service stops, check the port number are finding out, is as follows:

[root@localhost ~]# systemctl stop nginx
[root@localhost ~]# netstat -anpt | grep nginx                 #Nginx服务停止后,什么都查不到
[root@localhost ~]# 

And after Tomcat service stops, check the port number will find the following status (Listen state at startup into a TIME_WAIT state), after waiting for a moment, before finding out the information:

[root@localhost webapp1]# netstat -antp | grep 8080
tcp6       0      0 ::1:56448               ::1:8080                TIME_WAIT

Do not get to the bottom, the last results came out just fine, but also the first time I met this situation.

Guess you like

Origin blog.51cto.com/14154700/2412180