Tomcat LB 负载均衡集群实现

时间:2022-09-23 20:29:11

Tomcat LB 负载均衡集群实现

主机说明:

Tomcat1:test.field.com/192.168.88.130

Tomcat2:web1.field.com/192.168.88.131

反向代理负载均衡器nginx:cen7.field.com/192.168.88.132

JDK包:jdk-8u151-linux-i586.tar.gz/jdk-8u151-linux-x64.rpm

Tomcat包:apache-tomcat-8.0.47.tar.gz

一、安装配置Tomcat1

1、安装jdk8

[root@www local]# tar -zxfjdk-8u151-linux-i586.tar.gz -C /usr/local

[root@www local]# ln -sv  jdk1.8.0_151 jdk8

"jdk8" ->"jdk1.8.0_151"

[root@www local]# ll

总用量 4

drwxr-xr-x. 8 uucp  143 4096 9月   6 2017 jdk1.8.0_151

lrwxrwxrwx. 1 root root   12 6月  14 14:31 jdk8 -> jdk1.8.0_151

1)、配置环境变量

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

[root@www ~]# more  /etc/profile.d/java.sh

export JAVA_HOME=/usr/local/jdk8

export PATH=$JAVA_HOME/bin:$PATH

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

[root@www java]# java

-bash: /usr/local/java/jdk8/bin/java:/lib/ld-linux.so.2: bad ELF interpreter: 没有那个文件或目录

此处报错:原因是解压安装jdk缺少glibc.i686rpm包安装无此现象。

[root@www~]# yum  install  glibc.i686

2)、测试Java

[root@www ~]# java

用法: java [-options] class [args...]

          (执行类)

2、Tomcat包安装配置

[root@www ~]# tar  -zxf  apache-tomcat-8.0.47.tar.gz  -C  /usr/local

[root@www ~]# cd  /usr/local/

[root@www local]# ln -sv  apache-tomcat-8.0.47   tomcat

"tomcat" ->"apache-tomcat-8.0.47"

[root@www local]# ll | grep tomcat

drwxr-xr-x. 9 root root      4096 11月 16 2017apache-tomcat-8.0.47

lrwxrwxrwx. 1 root root        20 11月 16 2017tomcat -> apache-tomcat-8.0.47

1)、配置环境变量

[root@www local]# vim  /etc/profile.d/tomcat.sh

[root@www local]# more  /etc/profile.d/tomcat.sh

export CATALINA_HOME=/usr/local/tomcat

export PATH=$CATALINA_HOME/bin:$PATH

[root@field tomcat]# .  /etc/profile.d/tomcat.sh

[root@www ~]# cd  /usr/local/tomcat/conf

[root@www conf]# cp  server.xml{,.backup}

2)、添加Tomcat实例

[root@www conf]# vi  server.xml

<Engine name="Catalina"defaultHost="web1.field.com">

</Host>

 <Host name="web1.field.com"appBase="/data/webapps" unpackWARs="true"autoDeploy="true">

   <Context path="" docBase="ROOT"reloadable="true">

   <ValveclassName="org.apache.catalina.valves.RemoteAddrValve"

   deny="192\.168\.88\.139" />

   </Context>

   <ValveclassName="org.apache.catalina.valves.AccessLogValve"directory="/data/log"

      prefix="web1_access_log" suffix=".txt"

      pattern="%h %l %u %t &quot;%r&quot; %s %b" />

</Host>

3)、创建目录及测试页:

[root@www ~]# mkdir  -pv  /data/webapps/ROOT/{classes,lib,META-INF,WEB-INF}

[root@www ~]# cd  /data/webapps/ROOT/

[root@www ROOT]# vim index.jsp

[root@www ROOT]# more  index.jsp

<%@ page language="java" %>

<%@ page import="jave.util.*"%>

<html>

<head>

<title>JSP Test Page</title>

</head>

<body>

<% out.println("Hello,Web1.");%>

</body>

</html>

[root@www conf]# catalina.sh  configtest

Tomcat语法检查

[root@www conf]# catalina.sh  stop

[root@www conf]# catalina.sh  start

4)、测试访问:成功

[root@test jdk1.8.0_151]# curl  http://192.168.88.131:8080/index.jsp

<html>

<head>

<title>JSP Test Page</title>

</head>

<body>

Hello,Web1.

</body>

</html>

二、安装配置Tomcat2

1、安装jdk8

[root@test ~]#  ll  jdk*

-rw-r--r--. 1 root root 192658627 11月 14 21:57 jdk-8u151-linux-i586.tar.gz

-rw-r--r--. 1 root root 174163338 11月 16 20:21 jdk-8u151-linux-x64.rpm

[root@test ~]# # rpm  -ivh  jdk-8u151-linux-x64.rpm

[root@test ~]# rpm  -ql jdk1.8

[root@test ~]# cd  /usr/java/

[root@field java]# ll

总用量 4

lrwxrwxrwx. 1 root root   16 11月 16 20:28 default-> /usr/java/latest

drwxr-xr-x. 9 root root 4096 11月 16 20:28jdk1.8.0_151

lrwxrwxrwx. 1 root root   22 11月 16 20:28 latest-> /usr/java/jdk1.8.0_151

1)、修改环境变量

[root@test jdk1.8.0_151]# vim  /etc/profile.d/java.sh

export JAVA_HOME=/usr/java/latest

export PATH=$JAVA_HOME/bin:$PATH

[root@test jdk1.8.0_151]# .  /etc/profile.d/java.sh

[root@test ~]# java

用法: java [-options] class [args...]

          (执行类)         

2、Tomcat包安装配置

tomcat2创建两个实例

[root@field ~]# tar  zxvfapache-tomcat-8.0.47.tar.gz  -C  /usr/local

[root@field ~]# cd  /usr/local

[root@field local]# ln  -sv  apache-tomcat-8.0.47  tomcat

"tomcat" ->"apache-tomcat-8.0.47"

[root@field local]# ll  tomcat*

lrwxrwxrwx. 1 root root        20 11月 16 20:50 tomcat-> apache-tomcat-8.0.47

drwxr-xr-x. 3 root root      4096 11月 15 12:07tomcat1

drwxr-xr-x. 3 root root      4096 11月 15 12:07tomcat2

1)、配置环境变量

[root@field tomcat]# vim  /etc/profile.d/tomcat.sh

export CATALINA_HOME=/usr/local/tomcat

export PATH=$CATALINA_HOME/bin:$PATH

[root@field tomcat]# .  /etc/profile.d/tomcat.sh

2)、添加2个Tomcat实例

<Engine name="Catalina"defaultHost="test.field.com">

</Host>

 <Host name="test.field.com"appBase="/data/webapps" unpackWARs="true"autoDeploy="false">

  <Context path="" docBase="ROOT"reloadable="true" />

  <Context path="/app1"docBase="/data/webapps/app1" reloadable="true" />

  <ValveclassName="org.apache.catalina.valves.AccessLogValve"directory="/data/log"

     prefix="test_access_log" suffix=".txt"     

     pattern="%h %l %u %t &quot;%r&quot; %s %b" />

</Host>

3)、创建目录和测试页

[root@test webapps]# mkdir  -pv  /data/webapps/ROOT/{classes,lib,META-INF,WEB-INF}

[root@test webapps]# mkdir  -pv  /data/webapps/app1/{classes,lib,META-INF,WEB-INF}

[root@test app1]# more  index.jsp

<%@ page language="java" %>

<%@ page import="jave.util.*"%>

<html>

<head>

<title>JSP Test Page ForApp1</title>

</head>

<body>

<% out.println("Hello, App1");%>

</body>

</html>

[root@test app1]# more   ../ROOT/index.jsp

<%@ page language="java" %>

<%@ page import="jave.util.*"%>

<html>

<head>

<title>JSP Test Page ForTest</title>

</head>

<body>

<% out.println("Hello,Test ");%>

</body>

</html>

[root@test app1]# catalina.sh  configtest

[root@test app1]# catalina.sh  stop

[root@test app1]# catalina.sh  start

4)、测试:成功

[root@www ROOT]# curl  test.field.com:8080/index.jsp

<html>

<head>

<title>JSP Test Page ForTest</title>

</head>

<body>

Hello,Test

</body>

</html>

[root@www ROOT]# curl  test.field.com:8080/app1/index.jsp

<html>

<head>

<title>JSP Test Page ForApp1</title>

</head>

<body>

 Hello,App1

</body>

</html>

三、Centos7安装Nginx

[root@cen7 etc]# yum  -y  install nginx

1)、配置反向代理和负载均衡

[root@cen7 nginx]# vim  nginx.conf

   upstream  tomsrv {

     server  web1.field.com:8080;

     server  test.field.com:8080;

}

   server {

       listen       80  default_server;

       listen       [::]:80  default_server;

       server_name  _;

        root         /usr/share/nginx/html;

       # Load configuration files for the default server block.

       include /etc/nginx/default.d/*.conf;

        location /app1/ {

       proxy_pass  http://test.field.com:8080/app1/index.jsp;

 }

        location / {

    root  /usr/share/nginx/html;

       index index.html index.phpindex.htm;       

}

        location  ~*  \.(jsp|do)$  {

      proxy_pass  http://tomsrv;

    }

2)、访问异常解决      

[root@test conf]# curl  http://192.168.88.132/index.jsp

curl: (7) couldn't connect to host

[root@test conf]#  curl  http://192.168.88.132

curl: (7) couldn't connect to host

[root@cen7 etc]# curl localhost

curl: (7) Failed connectto localhost:80; 拒绝连接

核查原因为centos7默认开启了firewall,关闭即可

[root@cen7 nginx]# firewall-cmd  --state

running

[root@cen7 nginx]# firewall-cmd  --list-all

public (active)

 target: default

 icmp-block-inversion: no

 interfaces: ens33

 sources:

 services: ssh dhcpv6-client

 ports:

 protocols:

 masquerade: no

 forward-ports:

 source-ports:

 icmp-blocks:

 rich rules:

[root@cen7 nginx]# systemctl  stop  firewalld

[root@cen7 etc]# systemctl  stop  firewalld

四、测试负载均衡和反向代理Tomcat集群

成功:

[root@test conf.d]# curl  http://192.168.88.132/index.jsp

<html>

<head>

<title>JSP Test Page</title>

</head>

<body>

Hello,Web1.

</body>

</html>

[root@test conf.d]# curl  http://192.168.88.132/index.jsp

<html>

<head>

<title>JSP Test Page ForTest</title>

</head>

<body>

Hello,Test

</body>

</html>

[root@test conf.d]# curl  http://192.168.88.132/app1/

<html>

<head>

<title>JSP Test Page ForApp1</title>

</head>

<body>

Hello, App1

</body>

</html>