Tomcat installation services and load balancing based on centos 7

First, case studies

1, case Overview

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:
Tomcat installation services and load balancing based on centos 7

I can go to the network disk mirroring self-extracting:https://pan.baidu.com/s/1YAtbAVRg7wejN3XRl28J1g
extraction code: 2f7f

Second, the implementation of case

1, the first deployment of Tomcat servers

[root@centos02 ~]# java -version   <!--查看JDK是否安装,若没有,自行安装-->
openjdk version "1.8.0_131"
OpenJDK Runtime Environment (build 1.8.0_131-b12)
OpenJDK 64-Bit Server VM (build 25.131-b12, mixed mode)
[root@centos02 ~]# systemctl stop firewalld    <!--关闭防火墙-->
[root@centos02 ~]# systemctl disable firewalld   <!--禁止防火墙开机自动启动-->
[root@centos02 ~]# mount /dev/cdrom /mnt/    <!--挂载Linux光盘-->
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos02 ~]# rm -rf /etc/yum.repos.d/CentOS-* <!--清除系统自带yum源-->
[root@centos02 ~]# tar zxvf /mnt/apache-tomcat-7.0.54.tar.gz -C /usr/src/   
                                  <!--解压缩tomcat软件包-->
[root@centos02 ~]# mv /usr/src/apache-tomcat-7.0.54/ /usr/local/tomcat    
                               <!--将tomcat软件包剪切到/usr/local/tomcat目录-->
[root@centos02 ~]# /usr/local/tomcat/bin/startup.sh    <!--启动tomcat服务-->

Configuring client and server on the same segment of the IP address of the test access tomcat is normal http://192.168.100.20:8080
Tomcat installation services and load balancing based on centos 7

2, Tomcat publish a Web site

[root@centos02 ~]# mkdir /var/www    <!--创建网站根目录-->
[root@centos02 ~]# vim /var/www/index.jsp    <!--编写网站主页测试文件-->
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
        <head>
        <title>test01</title>
                </head>
        <body>
        <% out.println("www.benet.com!!!");%>
                </body>
</html>
[root@centos02 ~]# vim /usr/local/tomcat/conf/server.xml    
                                       <!--修改tomcat主配置文件发布网站-->                                                                          
<!--定位到该行,然后添加下面两行内容-->
126             <Context docBase="/var/www/" path="" reloadable="false">   
127                 </Context>     <!--docBase:web应用的文档默认目录-->
                                              <!--path=""设置默认“类”-->
                                              <!--reloadable设置监视“类”是否变化-->
[root@centos02 ~]# /usr/local/tomcat/bin/shutdown.sh   <!--关闭tomcat服务-->
[root@centos02 ~]# /usr/local/tomcat/bin/startup.sh    <!--启动tomcat服务-->

Client Access http://192.168.100.20:8080 will find the content of the website home page has become our own content written
Tomcat installation services and load balancing based on centos 7
so far, 192.168.100.20 configuration of Tomcat has been completed, another Tomcat server configuration and 192.168 192.168.100.30 .100.20 exactly the same configuration, the configuration of the above configuration it again 192.168.100.30 on the server (I am here to directly copy the easy way out, hehe). However, in order to test time can be seen load balancing effect, so that we can see each server access is not the same, you need two tomcat server homepage content settings called not the same content. 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.

3, the deployment of a second tomcat server

[root@centos03 ~]# mkdir /var/www   <!--第二台tomcat服务器上创建网站根目录-->
[root@centos02 ~]# scp /var/www/index.jsp [email protected]:/var/www/   
             <!--在第一台tomcat服务器复制网站主页测试文件到第二台tomcat服务器-->
The authenticity of host '192.168.100.30 (192.168.100.30)' can't be established.
ECDSA key fingerprint is SHA256:PUueT9fU9QbsyNB5NC5hbSXzaWxxQavBxXmfoknXl4I.
ECDSA key fingerprint is MD5:6d:f7:95:0e:51:1a:d8:9e:7b:b6:3f:58:51:51:4b:3b.
Are you sure you want to continue connecting (yes/no)? yes   <!--输入yes-->
[email protected]'s password:        <!--输入密码-->
index.jsp                                              100%  240    59.3KB/s   00:00    <!--上传完成-->
[root@centos03 ~]# cd /var/www/   <!--第二台tomcat服务器上查看是否复制成功-->
[root@centos03 www]# ls 
index.jsp
[root@centos02 ~]# scp -r /usr/local/tomcat/ [email protected]:/usr/local/tomcat
<!--复制tomcat服务所有配置文件到第二台服务器-->
[email protected]'s password:   <!--输入密码-->
[root@centos03 ~]# cd /usr/local/tomcat/  <!--查看是否复制成功-->
[root@centos03 tomcat]# ls
bin  conf  lib  LICENSE  logs  NOTICE  RELEASE-NOTES  RUNNING.txt  temp  webapps  work
[root@centos03 ~]# vim /var/www/index.jsp    
                        <!--修改第二台tomcat服务器的网站主页测试文件-->
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
        <head>
        <title>test02</title>
                </head>
        <body>
        <% out.println("www.accp.com!!!");%>
                </body>
</html>
[root@centos03 ~]# /usr/local/tomcat/bin/startup.sh   <!--启动tomcat服务-->

Client Access server http://192.168.100.30:8080 a second tomcat
Tomcat installation services and load balancing based on centos 7

4, Nginx server deployment

[root@centos01 ~]# groupadd nginx   <!--创建管理Nginx的用户-->
[root@centos01 ~]# useradd -M -s /sbin/nologin -g nginx nginx <!--创建管理Nginx的组-->
[root@centos01 ~]# mount /dev/cdrom /mnt/   <!--挂载操作系统盘-->
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos01 ~]# rm -rf /etc/yum.repos.d/CentOS-* <!--清除系统自带yum源-->
[root@centos01 ~]# yum -y install pcre-devel zlib-devel openssl-devel  
                                                   <!--安装Nginx依赖软件-->
[root@centos01 ~]# mount /dev/cdrom /mnt/   <!--切换Linux光盘-->
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos01 ~]# tar zxvf /mnt/nginx-1.6.0.tar.gz -C /usr/src/  <!--解压缩Nginx软件包-->
[root@centos01 ~]# cd /usr/src/nginx-1.6.0/  <!--进入nginx目录-->
[root@centos01 nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --user=nginx 
--group=nginx --with-http_stub_status_module --with-http_gzip_static_module 
--with-file-aio --with-http_ssl_module --with-http_flv_module <!--配置Nginx-->
[root@centos01 nginx-1.6.0]# make && make install   <!--编辑及安装Nginx-->
[root@centos01 ~]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/  <!--优化Nginx执行命令-->
[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf   <!--修改Nginx主配置文件 -->
34     upstream tomcat_server {        <!--添加此行-->
 35                 server  192.168.100.20:8080     weight=1;   <!--添加此行-->
 <!--weight参数表示权重,权重越高,表示被分配到的概率越大。
 <!--为了测试效果明显,这里将权重设置为一样-->
 36                 server  192.168.100.30:8080     weight=1;  <!--添加此行-->
 37         }
 38     server {
 39         listen       80;    <!--监听端口号-->
 40         server_name  www.benet.com;   <!--监听域名-->
 41         charset utf-8;    <!--默认支持字符集-->
 42         location / {
 43             root   html;
 44             proxy_pass  http://tomcat_server;  
 <!--定位到该{ }中,写入该行,“http://”后面的名字要和上面添加的upstream项后面的
 名字一致,才可实现调度-->
 45             index  index.html index.htm;
 46         }
[root@centos01 ~]# nginx -t  <!--检查Nginx主配置文件是否错误-->
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@centos01 ~]# nginx   <!--启动Nginx服务-->
[root@centos01 ~]# netstat -anptu | grep nginx  <!--查看Nginx服务运行状态-->
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      4186/nginx: master

5, the deployment of DNS

[root@centos01 ~]# umount /mnt/   <!--卸载光盘-->
[root@centos01 ~]# mount /dev/cdrom /mnt/  <!--切换操作系统光盘-->
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos01 ~]# yum -y install bind bind-chroot bind-utils  <!--安装DNS依赖软件-->
[root@centos01 ~]# echo "" > /etc/named.conf  <!--清空主配置文件-->
[root@centos01 ~]# vim /etc/named.conf    <!--编辑主配置文件-->
options {
        listen-on       port    53      { any; };
        directory       "/var/named";
};
zone    benet.com       IN      {
        type    master;
        file    "benet.com.zone";
};
[root@centos01 ~]# named-checkconf -z /etc/named.conf <!--检查主配置文件是否错误-->
[root@centos01 ~]# vim /var/named/benet.com.zone 
                              <!--编写benet.com的正向解析区域配置文件-->
$TTL    86400
@       SOA     benet.com.      root.benet.com.(
        2019113001
        1H
        15M
        1W
        1D
)
@       NS      centos01.benet.com.
centos01 A      192.168.100.10
www      A      192.168.100.10
[root@centos01 ~]# named-checkzone benet.com /var/named/benet.com.zone  
                                <!--检查正向解析区域配置文件是否错误-->
zone benet.com/IN: loaded serial 2019113001
OK
[root@centos01 ~]# chmod +x /var/named/benet.com.zone <!--添加执行权限-->
[root@centos01 ~]# chown named:named /var/named/benet.com.zone<!--修改属主属组-->
[root@centos01 ~]# systemctl start named   <!--启动named服务-->
[root@centos01 ~]# systemctl enable named    <!--设置开机自动启动-->

DNS client adds address
Tomcat installation services and load balancing based on centos 7

The client first visit www.benet.com
Tomcat installation services and load balancing based on centos 7

Refresh the page a second visit will see the following page content:
Tomcat installation services and load balancing based on centos 7
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, effect also obvious. Practice two home page content is the same, here I just want you to better see the effect it has become a different set of content.

------ This concludes the article, thanks for reading ------

Guess you like

Origin blog.51cto.com/14156658/2455866