tomcat,jdk编译安装,并基于Apache负载均衡,session绑定
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了tomcat,jdk编译安装,并基于Apache负载均衡,session绑定相关的知识,希望对你有一定的参考价值。
下载tomcat:
wget http://mirrors.shu.edu.cn/apache/tomcat/tomcat-8/v8.5.34/bin/apache-tomcat-8.5.34.tar.gz
安装jdk:
rpm -ivh jdk-8u181-linux-x64.rpm?AuthParam=1536721970_38dbf1e81e1c24c45ef2e0ee119b29a4
安装目录:
ls /usr/java
配置环境变量:
#vi /etc/profile.d/java.sh
export JAVA_HOME=/usr/java/latest
export PATH=$JAVA_HOME/bin:$PATH
重读配置:
#. /etc/profile.d/java.sh
查看版本:
java -version
java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)
查看环境变量:
]# echo $JAVA_HOME
/usr/java/latest
解压tomca至usr/local:
tar xf apache-tomcat-8.5.34.tar.gz -C /usr/local/
ln -sv apache-tomcat-8.5.34 tomcat
添加环境变量:
vi /etc/profile.d/tomcat.sh
export CATALINA_HOME=/usr/local/tomcat
export PATH=$CATALINA_HOME/bin:$PATH
重读:. /etc/profile.d/tomcat.sh
测试:
catalina.sh version
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/java/latest
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Server version: Apache Tomcat/8.5.34
Server built: Sep 4 2018 22:28:22 UTC
Server number: 8.5.34.0
OS Name: Linux
OS Version: 3.10.0-514.el7.x86_64
Architecture: amd64
JVM Version: 1.8.0_181-b13
JVM Vendor: Oracle Corporation
测试配置语法:
catalina.sh configtest
启动yomcat:
catalina.sh start
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/java/latest
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
查看端口:
ss -tnl
添加第一个java程序:
1、创建目录结构
#mkdir myapp/{lib,classes,WEB-INF,META-INF} -pv
mkdir: 已创建目录 "myapp/lib"
mkdir: 已创建目录 "myapp/classes"
mkdir: 已创建目录 "myapp/WEB-INF"
mkdir: 已创建目录 "myapp/META-INF"
2、编写程序
vi myapp/index.jsp
<%@ page language="java" %>
<%@ page import="java.util.*" %>
<html>
<head>
<title>JSP Test Page</title>
</head>
<body>
<% out.println("hello world"); %>
</body>
</html>
配置管理界面:
添加管理员账号和密码:
#vi /usr/local/tomcat/conf/tomcat-users.xml
<role rolename="manager-gui"/>
<user username="tomcat" password="tomcat1" roles="manager-gui"/>
配置远程访问manager:
tomat_home/conf/Catalina/localhost/下 添加manager.xml
<Context privileged="true" antiResourceLocking="false"
docBase="${catalina.home}/webapps/manager">
<Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="^.*$" />
</Context>
配置host manager:
vi tomcat-users.xml
<role rolename="manager-gui"/>
<role rolename="admin-gui"/>
<user username="tomcat" password="tomcat1" roles="manager-gui,admin-gui"/>
#vim webapps/host-manager/META-INF/context.xml
<Context antiResourceLocking="false" privileged="true" >
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="^.*$" /> #修改这里
</Context>
创建目录文件:
mkdir -pv /data/{webapps,logs}
拷贝测试文件:
cp -r /usr/local/tomcat/webapps/myapp/* /data/webapps/
编辑tomcat配置文件:
#vi server.xml
</Host>
<Host name="web.shengjing360.com" appBase="/data/webapps"
unpackWARs="true" autoDeploy="true">
<Context path="" docBase="/data/webapps" reloadable="true" />
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/data/logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
配置hosts文件,然后测试虚拟机:http://web.shengjing360.com:8080/
tomcat基于httpd负载均衡示例:
vi /etc/httpd/conf.d/vhosts.conf
基于http:
proxy balancer://lbcluster1>
balancermember http://192.168.102.44:8080 loadfactor=10 route=TomcatA
balancermember http://192.168.102.46:8080 loadfactor=10 route=TomcatB
</proxy>
<VirtualHost :80>
ServerName web.shengjing360.com
ProxyVia On
ProxyRequests Off
ProxyPreserveHost On
<Proxy >
Require all granted
</Proxy>
ProxyPass /status !
ProxyPass / balancer://lbcluster1/
ProxyPa***everse / balancer://lbcluster1/
<Location />
Require all granted
</Location>
<Location /balancer-manager> #balancer-manager管理页面
SetHandler balancer-manager
Proxypass !
Require all granted
</Location>
</VirtualHost>
会话绑定:
Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
<proxy balancer://lbcluster1>
balancermember http://192.168.102.44:8080 loadfactor=10 route=TomcatA
balancermember http://192.168.102.46:8080 loadfactor=10 route=TomcatB
ProxySet stickysession=ROUTEID
</proxy>
<VirtualHost :80>
ServerName web.shengjing360.com
ProxyVia On
ProxyRequests Off
ProxyPreserveHost On
<Proxy >
Require all granted
</Proxy>
ProxyPass /status !
ProxyPass / balancer://lbcluster1/
ProxyPa***everse / balancer://lbcluster1/
<Location />
Require all granted
</Location>
<Location /balancer-manager>
SetHandler balancer-manager
Proxypass !
Require all granted
</Location>
</VirtualHost>
基于ajp:
[[email protected] conf.d]# vi vhosts.ajp.conf
#Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
<proxy balancer://lbcluster1>
balancermember ajp://192.168.102.44:8009 loadfactor=10 route=TomcatA
balancermember ajp://192.168.102.46:8009 loadfactor=10 route=TomcatB
ProxySet stickysession=ROUTEID
</proxy>
<VirtualHost :80>
ServerName web.shengjing360.com
ProxyVia On
ProxyRequests Off
ProxyPreserveHost On
<Proxy >
Require all granted
</Proxy>
ProxyPass /status !
ProxyPass / balancer://lbcluster1/
ProxyPa***everse / balancer://lbcluster1/
<Location />
Require all granted
</Location>
<Location /balancer-manager>
SetHandler balancer-manager
Proxypass !
Require all granted
</Location>
</VirtualHost>
以上是关于tomcat,jdk编译安装,并基于Apache负载均衡,session绑定的主要内容,如果未能解决你的问题,请参考以下文章