标签:something javac ast bin ada 本机 contex 指令 mys
编程语言:程序:指令+数据
过程式编程:以指令为中心,数据服务于代码;
对象式编程:以数据为中心,指令服务于数据;
对象,方法(method)
Java:
Sun, Green Project, Oak, James Gosling;
1995:Java 1.0, Write once, Run Anywhere;
1996:JDK(Java Development Kit),包含类库(API)、开发工具(javac)、JVM(SUN Classic VM)
JDK 1.0, Applet, AWT
1997:JDK 1.1
1998: JDK 1.2
Sun分拆Java技术为三个方向:
J2SE:Standard Edition
J2EE:Enterprise Edition
J2ME:Mobile Edition
代表性技术:EJB,java plugin, Swing, JIT(Just In Time,即时编译)
2000:JDK 1.3
HotSpot VM
2002:JDK 1.4
2006:Sun开源了Java技术,GPL,建立一个称为OpenJDK组织;
Java 2 SE, Java 2 EE, Java 2 ME
2011:JDK 1.7
2014:JDK 1.8
2016:JDK 1.9
Java代码的运行:
*.java(source code) --> javac --> *.class(bytecode)
jvm:class loader,加载程序的类文件,及程序的类文件依赖到的其它的类文件而后运行; 整个运行表现为一个jvm进程;
threads;
java技术体系:
Java编程语言
Java Class文件格式
Java API
Java VM
class loader
执行引擎
JVM运行时区域:
方法区:线程共享; 用于存储被JVM加载的class信息、常量、静态变量、方法等;
堆:是jvm所管理的内存中占用空间最大的一部分;也是GC管理的主要区域;存储对象;
Java栈:线程私有,存储 线程自己的局部变量;
PC寄存器:线程私有的内存空间,程序的指令指针;
本地方法栈:
安装JDK
了解当前的java环境:
~]# java -version
OpenJDK:
java-VERSION-openjdk:
The OpenJDK runtime environment.
java-VERSION-openjdk-headless:
The OpenJDK runtime environment without audio and video support.
java-VERSION-openjdk-devel:
The OpenJDK development tools.
CentOS 7:
VERSION:1.6.0, 1.7.0, 1.8.0
注意:多版本并存时,可使用 alternatives命令设定默认使用的版本;
Oracle JDK:
安装相应版本的rpm包;
jdk-VERSION-OS-ARCH.rpm
例如:jdk-1.8.0_25-linux-x64.rpm
注意:安装完成后,要配置JAVA_HOME环境变量,指向java的安装路径;
OpenJDK:
JAVA_HOME=/usr
Oracle JDK:
JAVA_HOME=/usr/java/jdk_VERSION
Java 2 EE:
CGI: Common Gateway Interface
Servlet:
类库;web app;
Servlet container, Servlet Engine
JSP: Java Server Page
<html>
<title>TITLE</title>
<body>
<h1>...</h1>
<%
... java code...
%>
</body>
</html>
.jsp -->jasper--> .java --> javac --> .class --> jvm
注意:基于jasper将静态输出的数据转为java代码进行输出,结果为servlet规范的代码;
JSP Container:
JSP + Servlet Container
Java Web Server:JWS
ASF:JServ
Tomcat 3.x
Tomcat 4.x
Catalina
http://tomcat.apache.org/
商业实现:
WebSphere, WebLogic, Oc4j, Glassfish, Geronimo, JOnAS, JBoss, ...
开源实现:
Tomcat, Jetty, Resin, ...
Tomcat: Java 2 EE技术体系的不完整实现;
安装:jdk
102.70:
yum -y install java-1.8.0-openjdk-devel
java -version:启用
126.180:
rpm -ivh jdk-8u144-linux-x64.rpm
vim /etc/profile.d/java.sh
export JAVA_HOME=/usr/java/latest
export PATH=$JAVA_HOME/bin:$PATH
echo $JAVA_HOME
java -version
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)
安装:tomcat:
102.70:
yum -y install java-1.8.0-openjdk-devel
yum -y install tomcat tomcat-webapps tomcat-admin-webapps tomcat-docs-webapp
systemctl start tomcat
ss -tnl:默认三个端口:
8080:http服务
8009:AJP
127.0.0.1:8005:本地管理接口
126.180:
apache-tomcat-8.5.32.tar.gz
tar xvf apache-tomcat-8.5.32.tar.gz -C /usr/local/
cd /usr/local/
ln -sv apache-tomcat-8.5.32 tomcat
cd /usr/local/tomcat/
useradd tomcat
chown -R :tomcat ./*
chown -R tomcat logs/ temp/ work/
chmod g+rwx conf/
chmod g+r conf/*
su - tomcat
catalina.sh start --help
catalina.sh start
ss -tnl
8080
8009
127.0.0.1:8005
安装Tomcat:
Base Repo:
tomcat, tomcat-lib, tomcat-admin-webapps, tomcat-webapps, tomcat-docs-webapp
Tomcat binary release:
# tar xf apache-tomcat-VERSION.tar.gz -C /usr/local/
# cd /usr/local
# ln -sv apache-tomcat-VERSION tomcat
/etc/profile.d/tomcat.sh
export CATALINA_BASE=/usr/local/tomcat
export PATH=$CATALINA_BASE/bin:$PATH
tomcat程序环境:
tomcat的目录结构
bin:脚本,及启动时用到的类;
conf:配置文件目录;
lib:库文件,Java类库,jar;
logs:日志文件目录;
temp:临时文件目录;
webapps:webapp的默认目录;
work:工作目录;
# catalina.sh --help
debug Start Catalina in a debugger
debug -security Debug Catalina with a security manager
jpda start Start Catalina under JPDA debugger
run Start Catalina in the current window
run -security Start in the current window with security manager
start Start Catalina in a separate window
start -security Start in a separate window with security manager
stop Stop Catalina, waiting up to 5 seconds for the process to end
stop n Stop Catalina, waiting up to n seconds for the process to end
stop -force Stop Catalina, wait up to 5 seconds and then use kill -KILL if still running
stop n -force Stop Catalina, wait up to n seconds and then use kill -KILL if still running
configtest Run a basic syntax check on server.xml - check exit code for result
version What version of tomcat are you running?
rpm包安装的程序环境:
配置文件目录:/etc/tomcat
主配置文件:server.xml
webapps存放位置:/var/lib/tomcat/webapps/
examples
manager
host-manager
docs
Unit File:tomcat.service
环境配置文件:/etc/sysconfig/tomcat
回顾:
java技术体系:
Java编程语言、Java Class文件格式、Java API、JVM
java的技术方向:
J2SE --> JAVA 2 SE, JDK
j2EE --> JAVA 2 EE,
Servlet, JSP, EJB, ...
J2ME --> JAVA 2 ME
Servlet Container:
JDK + Servlet
JSP Container:
JSP + Servlet Container
JVM运行时区域:方法区、堆、Java栈、PC寄存器、本地方法栈;
Tomcat:事先部署JDK;
Java 2 EE不完整实现;
Tomcat(2)
Tomcat:
使用java语言编写:
tomcat的配置文件构成:
server.xml:主配置文件;
web.xml:每个webapp只有“部署”后才能被访问,它的部署方式通常由web.xml进行定义,其存放位置为WEB-INF/目录中;此文件为所有的webapps提供默认部署相关的配置;
context.xml:每个webapp都可以专用的配置文件,它通常由专用的配置文件context.xml来定义,其存放位置为WEB-INF/目录中;此文件为所有的webapps提供默认配置;
tomcat-users.xml:用户认证的账号和密码文件;
catalina.policy:当使用-security选项启动tomcat时,用于为tomcat设置安全策略;
catalina.properties:Java属性的定义文件,用于设定类加载器路径,以及一些与JVM调优相关参数;
logging.properties:日志系统相关的配置; log4j
Tomcat的核心组件:server.xml
<Server>
<Service>
<connector/>
<connector/>
...
<Engine>
<Host>
<Context/>
<Context/>
...
</Host>
<Host>
...
</Host>
...
</Engine>
</Service>
</Server>
每一个组件都由一个Java“类”实现,这些组件大体可分为以下几个类型:
顶级组件:Server
服务类组件:Service
连接器组件:http, https, ajp(apache jserv protocol)
容器类:Engine, Host, Context
被嵌套类:valve, logger, realm, loader, manager, ...
集群类组件:listener, cluster, ...
/var/www/html/
bbs/images
JSP WebAPP的组织结构:
/: webapps的根目录
index.jsp:主页;
WEB-INF/:当前webapp的私有资源路径;通常用于存储当前webapp的web.xml和context.xml配置文件;
META-INF/:类似于WEB-INF/;
classes/:类文件,当前webapp所提供的类;
lib/:类文件,当前webapp所提供的类,被打包为jar格式;
webapp归档格式:
.war:webapp
.jar:EJB的类打包文件;
.rar:资源适配器类打包文件;
.ear:企业级webapp;
部署(deploy)webapp的相关操作:
deploy:将webapp的源文件放置于目标目录(网页程序文件存放目录),配置tomcat服务器能够基于web.xml和context.xml文件中定义的路径来访问此webapp;将其特有的类和依赖的类通过class loader装载至JVM;
部署有两种方式:
自动部署:auto deploy
手动部署:
冷部署:把webapp复制到指定的位置,而后才启动tomcat;
热部署:在不停止tomcat的前提下进行部署;
部署工具:manager、ant脚本、tcd(tomcat client deployer)等;
undeploy:反部署,停止webapp,并从tomcat实例上卸载webapp;
start:启动处于停止状态的webapp;
stop:停止webapp,不再向用户提供服务;其类依然在jvm上;
redeploy:重新部署;
实例:手动提供一测试类应用,并冷部署:
102.70:
mkdir test/{classes,lib,WEB-INF,META-INF} -pv
vim test/index.jsp
<%@ page language="java" %>
<%@ page import="java.util.*" %>
<html>
<head>
<title>Test Page</title>
</head>
<body>
<% out.println("hello");
%>
</body>
</html>
热部署:
cp -r test/ /usr/share/tomcat/webapps/
测试:访问172.20.102.70:8080/test
102.70:赋予账号密码:
vim /etc/tomcat/tomcat-users.xml
<role rolename="manager-gui"/> //默认注释
<user username="admin" password="chen" roles="manager-gui"/ //手动添加
systemctl restart tomcat
测试:访问:172.20.102.70:8080/manager/html //登录账户admin 密码:chen
tomcat的两个管理应用:
manager:管理webapps应用程序
host-manager:管理虚拟主机
tomcat的常用组件配置:
Server:代表tomcat instance,即表现出的一个java进程;监听在8005端口,只接收“SHUTDOWN”。各server监听的端口不能相同,因此,在同一物理主机启动多个实例时,需要修改其监听端口为不同的端口;
Service:用于实现将一个或多个connector组件关联至一个engine组件;
Connector组件:端点
负责接收请求,常见的有三类http/https/ajp;
进入tomcat的请求可分为两类:
(1) standalone : 请求来自于客户端浏览器;
(2) 由其它的web server反代:来自前端的反代服务器;
nginx --> http connector --> tomcat
httpd(proxy_http_module) --> http connector --> tomcat
httpd(proxy_ajp_module) --> ajp connector --> tomcat
httpd(mod_jk) --> ajp connector --> tomcat
属性:
port="8080"
protocol="HTTP/1.1"
connectionTimeout="20000"
address:监听的IP地址;默认为本机所有可用地址;
maxThreads:最大并发连接数,默认为200;
enableLookups:是否启用DNS查询功能;
acceptCount:等待队列的最大长度;
secure:
sslProtocol:
Engine组件:Servlet实例,即servlet引擎,其内部可以一个或多个host组件来定义站点; 通常需要通过defaultHost属性来定义默认的虚拟主机;
属性:
name=
defaultHost="localhost"
jvmRoute=
Host组件:位于engine内部用于接收请求并进行相应处理的主机或虚拟主机,示例:
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
</Host>
Webapp ARchives
常用属性说明:
(1) appBase:此Host的webapps的默认存放目录,指存放非归档的web应用程序的目录或归档的WAR文件目录路径;可以使用基于$CATALINA_BASE变量所定义的路径的相对路径;
(2) autoDeploy:在Tomcat处于运行状态时,将某webapp放置于appBase所定义的目录中时,是否自动将其部署至tomcat;
实例:定义虚拟主机:
vim /etc/tomcat/server.xml
</Host>
<Host name="tom1.com" appBase="/data/webapps" unpackWARs="true" autoDeploy="true"/>
mkdir -pv /appdata/webapps
mkdir -pv /appdata/webapps/ROOT/{lib,classes,WEB-INF}
提供一个测试页即可:
vim /data/webapps/ROOT/index.jsp
<html>
<head><title>TomcatA</title></head>
<body>
<h1><font color="red">TomcatA.chen.com</font></h1>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("chen.com","chen.com"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
systemctl restart tomcat
添加日志:
vim /etc/tomcat/server.xml
<Host name="tom1.com" appBase="/data/webapps" unpackWARs="true" autoDeploy="true"/>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="tom1_access_log." suffix=".log"
pattern="%h %l %u %t "%r" %s %b" />
systemctl restart tomcat
查看:
ls /var/log/tomcat/
测试机:
vim /etc/hosts
172.20.102.70 tom1.com
测试:访问 tom1.com:8080 即可
Context组件:
示例:
vim /etc/tomcat/server.xml
<Context path="/PATH" docBase="/PATH/TO/SOMEDIR" reloadable=""/>
综合示例:
<Host name="node1.magedu.com" appBase="/web/apps" unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="node1_access" suffix=".log"
pattern="%h %l %u %t "%r" %s %b" />
<Context path="/test" docBase="test" reloadable="">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="node1_test_access_" suffix=".log"
pattern="%h %l %u %t "%r" %s %b" />
</Context>
</Host>
Valve组件:
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
Valve存在多种类型:
定义访问日志:org.apache.catalina.valves.AccessLogValve
定义访问控制:org.apache.catalina.valves.RemoteAddrValve
<Valve className="org.apache.catalina.valves.RemoteAddrValve" deny="172\.16\.100\.67"/>
实例:实现LNMT:Linux Nginx MySQL Tomcat
128.253:nginx 反代到tomcat服务上
vim /etc/nginx/conf.d/www.conf
server {
listen 80;
server_name www.chen.com;
location / {
proxy_pass http://172.20.102.70:8080;
}
}
systemctl start nginx
测试机:
vim /etc/hosts
172.20.102.70 tom1.com kai.com kai
172.20.128.253 www.chen.com
在测试机上浏览器直接访问:www.chen.com 即可将跳转到tomcat的服务上;
也可以使用tomcat服务上的之前定义的虚拟机:
128.253:nginx
vim /etc/nginx/conf.d/www.conf
server {
listen 80;
server_name www.chen.com;
location / {
proxy_pass http://tom1.com:8080;
}
}
systemctl restart nginx
测试机访问:tom1.com 显示之前定义的一台虚拟机 即可
实现负载均衡:
需要两台tomcat服务:
102.70:tomcat1
vim /etc/server.xml
<Engine name="Catalina" defaultHost="tom1.com">
systemctl restart tomcat
126.180:tomcat2
yum -y install java-1.8.0-openjdk tomcat tomcat-admin-webapps tomcat-docs-webapp tomcat-webapps
将102.70:tomcat的配置直接复制到tomcat2上面:
scp -rp /dat/* root@172.20.126.180:/data
cd /etc/tomcat/
scp -r server.xml tomcat-users.xml root@172.20.126.180:/etc/tomcat/
vim /etc/tomcat/server.xml
<Engine name="Catalina" defaultHost="tom2.com"> //后缀改成tom2.com
<Host name="tom2.com" appBase="/data/webapps" unpackWARs="true" autoDeploy="true"/>
//将定义名改一下
vim /data/webapps/ROOT/index.jsp
<html>
<head><title>TomcatB</title></head>
<body>
<h1><font color="red">TomcatB.chen.com</font></h1>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("chen.com","chen.com"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
systemctl start tomcat
128.253:nginx:负载均衡
128.253:nginx
vim /etc/hosts
172.20.102.70 tom1.com
172.20.126.180 tom2.com
vim /etc/nginx/cond.d/www.conf
upstream tz {
server tom1.com:8080;
server tom2.com:8080;
}
server {
listen 80;
server_name www.chen.com;
location / {
proxy_pass http://tz;
}
}
systemctl restart nginx
测试机:DNS解析:
vim /etc/hosts
172.20.102.70 tom1.com
172.20.128.253 www.chen.com
172.20.126.180 tom2.com
测试机:浏览器访问:www.chen.com 刷新 可两台tomcat来回跳转
用httpd做反代:
172.20.128.253:HTTPD
yum -y install httpd
vim /etc/httpd/conf.d/httpd-tomcat.conf //反代102.70:tomcat
<VirtualHost *:80>
ServerName www.chem.com
ProxyRequests Off
ProxyVia On
ProxyPreserveHost On
<Proxy *>
Require all granted
</Proxy>
ProxyPass / http://tom1.com:8080/
<Location />
Require all granted
</Location>
</VirtualHost>
systemctl start httpd
反代到126.180:tomcat 用ajp连接器:
vim /etc/httpd/conf.d/ajp-tomcat.conf
<VirtualHost *:80>
ServerName web.chem.com
ProxyRequests Off
ProxyVia On
ProxyPreserveHost On
<Proxy *>
Require all granted
</Proxy>
ProxyPass / ajp://tom2.com:8009/
<Location />
Require all granted
</Location>
</VirtualHost>
systemctl restart httpd
测试机DNS解析:
vim /etc/hosts
172.20.128.253 www.chen.com web.chen.com
测试机浏览器访问 web.chen.com 会显示代理126.180:tomcat2的虚拟主机
用httpd实现调度负载均衡器:
128.253:httpd服务
vim /etc/httpd/conf.d/httpd-tomcat.conf
<Proxy balancer://toms>
BalancerMember http://tom1.com:8080
BalancerMember http://tom2.com:8080
ProxySet lbmethod=byrequests
<VirtualHost *:80>
ServerName www.chem.com
ProxyRequests Off
ProxyVia On
ProxyPreserveHost On
<Proxy *>
Require all granted
</Proxy>
ProxyPass / balancer://toms/
<Location />
Require all granted
</Location>
</VirtualHost>
httpd -t
systemctl restart httpd.service
测试机:浏览器访问:www.chen.com 会跳转到tomcatA上,在刷新一下将跳转到tomcatB的机器上 即可
使用ajp的方式实现负载均衡:
128.253:httpd服务
vim /etc/httpd/conf.d/ajp-tomcat.conf
<Proxy balancer://tomcat>
BalancerMember ajp://tom1.com:8080
BalancerMember ajp://tom2.com:8080
</Proxy>
<VirtualHost *:80>
ServerName web.chen.com
ProxyRequests Off
ProxyVia On
ProxyPreserveHost On
<Proxy *>
Require all granted
</Proxy>
ProxyPass / balancer://tomcat/;
<Location />
Require all granted
</Location>
</VirtualHost>
httpd -t
systemctl restart httpd.service
测试机浏览器:web.chen.com
Client (http) --> nginx (reverse proxy)(http) --> tomcat (http connector)
location / {
proxy_pass http://tc1.chen.com:8080;
}
location ~* \.(jsp|do)$ {
proxy_pass http://tc1.chen.com:8080;
}
LAMT:Linux Apache(httpd) MySQL Tomcat
httpd的代理模块:
proxy_module
proxy_http_module:适配http协议客户端;
proxy_ajp_module:适配ajp协议客户端;
Client (http) --> httpd (proxy_http_module)(http) --> tomcat (http connector)
Client (http) --> httpd (proxy_ajp_module)(ajp) --> tomcat (ajp connector)
Client (http) --> httpd (mod_jk)(ajp) --> tomcat (ajp connector)
proxy_http_module代理配置示例:
<VirtualHost *:80>
ServerName tc1.chen.com
ProxyRequests Off //关闭正向代理
ProxyVia On //内建的httpd要不要 对每个响应客户端的报文加上via 加上自己的主机名 需要就:on 不需要就:off
ProxyPreserveHost On //代理报文被httpd代理至后端要不要保留后端请求指定的host
<Proxy *> //被虚拟主机代理的所有服务
Require all granted //授权所有客户端能驶入
</Proxy>
ProxyPass / http://tc1.chen.com:8080/ //把客户端请求到本机上的根,一律代理到对应的根
ProxyPa***everse / http://tc1.chen.com:8080/
<Location />
Require all granted
</Location>
</VirtualHost>
proxy_ajp_module代理配置示例:
<VirtualHost *:80>
ServerName tc1.magedu.com
ProxyRequests Off
ProxyVia On
ProxyPreserveHost On
<Proxy *>
Require all granted
</Proxy>
ProxyPass / ajp://tc1.chen.com:8009/
ProxyPa***everse / ajp://tc1.chen.com:8009/
<Location />
Require all granted
</Location>
</VirtualHost>
实践:client --> nginx --> httpd --> tomcat
proxy_http_module)(http) --> tomcat (http connector)
Client (http) --> httpd (proxy_ajp_module)(ajp) --> tomcat (ajp connector)
Client (http) --> httpd (mod_jk)(ajp) --> tomcat (ajp connector)
会话保持:
(1) session sticky
source_ip
nginx: ip_hash
haproxy: source
lvs: sh
cookie:
nginx:hash
haproxy: cookie
(2) session cluster:delta session manager
(3) session server:redis(store), memcached(cache)
Tomcat Cluster(session)
(1) session sticky
(2) session cluster
tomcat delta manager
(3) session server
memcached
Tomcat Cluster
(1) httpd + tomcat cluster
httpd: mod_proxy, mod_proxy_http, mod_proxy_balancer
tomcat cluster:http connector
(2) httpd + tomcat cluster
httpd: mod_proxy, mod_proxy_ajp, mod_proxy_balancer
tomcat cluster:ajp connector
(3) httpd + tomcat cluster
httpd: mod_jk
tomcat cluster:ajp connector
(4) nginx + tomcat cluster
第一种方法的实现:
<proxy balancer://tcsrvs>
BalancerMember http://172.18.100.67:8080
BalancerMember http://172.18.100.68:8080
ProxySet lbmethod=byrequests
</Proxy>
<VirtualHost *:80>
ServerName lb.magedu.com
ProxyVia On
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Require all granted
</Proxy>
ProxyPass / balancer://tcsrvs/
ProxyPa***everse / balancer://tcsrvs/
<Location />
Require all granted
</Location>
</VirtualHost>
BalancerMember:
BalancerMember [balancerurl] url [key=value [key=value ...]]
status:
D: Worker is disabled and will not accept any requests
被禁用,不会接受任何请求
S: Worker is administratively stopped.
处于管理被禁用
I: Worker is in ignore-errors mode and will always be considered available.
不设置时 即便不指定默认情况下 httpd也会对健康状态下做检测
H: Worker is in hot-standby mode and will only be used if no other viable workers are available.
Worker处于热备份模式,只有在没有其他可行的Worker可用时才使用它
E: Worker is in an error state.
Worker处于错误状态
N: Worker is in drain mode and will only accept existing sticky sessions destined for itself and ignore all other requests.
处于消耗状态,只接受现有的粘性会话,并忽略所有其他请求
loadfactor:
负载因子,即权重;
lbmethod:
Balancer load-balance method. Select the load-balancing scheduler method to use. Either byrequests, to perform weighted request counting; bytraffic, to perform weighted traffic byte count balancing; or bybusyness, to perform pending request balancing. The default is byrequests.
stickysession
Balancer sticky session name. The value is usually set to something like JSESSIONID or PHPSESSIONID, and it depends on the backend application server that support sessions. If the backend application server uses different name for cookies and url encoded id (like servlet containers) use | to separate them. The first part is for the cookie the second for the path.
Available in Apache HTTP Server 2.4.4 and later.
会话粘性的实现方法:
实例:172.20.128.253:httpd服务:
vim /etc/httpd/conf.d/httpd-tomcat.conf
Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
<Proxy balancer://tsrvs>
BalancerMember http://tom1.com:8080 loadfactor=2 route=TcA
BalancerMember http://tom2.com:8080 loadfactor=1 route=TcB
ProxySet stickysession=ROUTEID
</Proxy>
<VirtualHost *:80>
ServerName www.chen.com
ProxyRequests Off
ProxyVia On
ProxyPreserveHost On
<Proxy *>
Require all granted
</Proxy>
ProxyPass / balancer://tsrvs/
<Location />
Require all granted
</Location>
</VirtualHost>
httpd -t
systemctl restart httpd.service
测试机浏览器:www.chen.com 无法实现轮询 可在开发工具看查看
示例:
Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
<proxy balancer://tcsrvs>
BalancerMember http://172.18.100.67:8080 route=TomcatA loadfactor=1
BalancerMember http://172.18.100.68:8080 route=TomcatB loadfactor=2
ProxySet lbmethod=byrequests
ProxySet stickysession=ROUTEID
</Proxy>
<VirtualHost *:80>
ServerName lb.chen.com
ProxyVia On
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Require all granted
</Proxy>
ProxyPass / balancer://tcsrvs/
ProxyPa***everse / balancer://tcsrvs/
<Location />
Require all granted
</Location>
</VirtualHost>
启用管理接口:
<Location /balancer-manager>
SetHandler balancer-manager
ProxyPass !
Require all granted
</Location>
实现接口管理:连接到tomcat图形界面操作:
vim /etc/httpd/httpd-tomcat.conf
Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
<Proxy balancer://tsrvs>
BalancerMember http://tom1.com:8080 loadfactor=2 route=TcA
BalancerMember http://tom2.com:8080 loadfactor=1 route=TcB
ProxySet stickysession=ROUTEID
</Proxy>
<VirtualHost *:80>
ServerName www.chen.com
ProxyRequests Off
ProxyVia On
ProxyPreserveHost On
<Proxy *>
Require all granted
</Proxy>
ProxyPass / balancer://tsrvs/
<Location />
Require all granted
</Location>
<Location /balancer-manager>
SetHandler balancer-manager
ProxyPass !
Require all granted
</Location>
</VirtualHost>
httpd -t
systemctl restart httpd.service
测试机浏览器:www.chen.com/balancer-manager 即可;
示例程序:
演示效果,在TomcatA上某context中(如/test),提供如下页面
<%@ page language="java" %>
<html>
<head><title>TomcatA</title></head>
<body>
<h1><font color="red">TomcatA.chen.com</font></h1>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("chen.com","chen.com"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
演示效果,在TomcatB上某context中(如/test),提供如下页面
<%@ page language="java" %>
<html>
<head><title>TomcatB</title></head>
<body>
<h1><font color="blue">TomcatB.chen.com</font></h1>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("chen.com","chen.com"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
第二种方式:
<proxy balancer://tcsrvs>
BalancerMember ajp://172.18.100.67:8009
BalancerMember ajp://172.18.100.68:8009
ProxySet lbmethod=byrequests
</Proxy>
<VirtualHost *:80>
ServerName lb.magedu.com
ProxyVia On
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Require all granted
</Proxy>
ProxyPass / balancer://tcsrvs/
ProxyPa***everse / balancer://tcsrvs/
<Location />
Require all granted
</Location>
<Location /balancer-manager>
SetHandler balancer-manager
ProxyPass !
Require all granted
</Location>
</VirtualHost>
保持会话的方式参考前一种方式。
Tomcat Session Replication Cluster:
(1) 配置启用集群,将下列配置放置于<engine>或<host>中;
102.70:tomcat1
vim /etc/tomcat/server.xml
</Host>
<Host name="tom1.com" appBase="/data/webapps" unpackWARs="true" autoDeploy="true" />
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="8">
<Manager className="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>
<Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Membership className="org.apache.catalina.tribes.membership.McastService"
address="228.0.0.188"
port="45564"
frequency="500"
dropTime="3000"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="172.20.102.70"
port="4000"
autoBind="100"
selectorTimeout="5000"
maxThreads="6"/>
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<InterceptorclassName="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
</Channel>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=""/>
<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
<Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
<ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="8">
<Manager className="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>
<Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Membership className="org.apache.catalina.tribes.membership.McastService"
address="228.0.0.4"
port="45564"
frequency="500"
dropTime="3000"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="auto"
port="4000"
autoBind="100"
selectorTimeout="5000"
maxThreads="6"/>
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
</Channel>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=""/>
<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
<Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
<ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
cd /data/webapps/ROOT
cp
确保Engine的jvmRoute属性配置正确。
(2) 配置webapps
编辑WEB-INF/web.xml,添加<distributable/>元素;
注意:CentOS 7上的tomcat自带的文档中的配置示例有语法错误;
<ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
绑定的地址为auto时,会自动解析本地主机名,并解析得出的IP地址作为使用的地址;
Tomcat的常用优化配置:
(1) 内存空间:
/etc/sysconfig/tomcat, /etc/tomcat/tomcat.conf
JAVA_OPTS="-server -Xms32g -Xmx32g -XX:NewSize= -XX:MaxNewSize= "
-server:服务器模式
-Xms:堆内存初始化大小;
-Xmx:堆内存空间上限;
-XX:NewSize=:新生代空间初始化大小;
-XX:MaxNewSize=:新生代空间最大值;
(2) 线程池设置:
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
常用属性:
maxThreads:最大线程数;
minSpareThreads:最小空闲线程数;
maxSpareThreads:最大空闲线程数;
acceptCount:等待队列的最大长度;
URIEncoding:URI地址编码格式,建议使用UTF-8;
enableLookups:是否启用dns解析,建议禁用;
compression:是否启用传输压缩机制,建议“on";
compressionMinSize:启用压缩传输的数据流最小值,单位是字节;
compressableMimeType:定义启用压缩功能的MIME类型;
text/html, text/xml, text/css, text/javascript
(3) 禁用8005端口;
<Server port="-1" shutdown="SHUTDOWN">
(4) 隐藏版本信息:
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
Server="SOME STRING"
Tomcat Session Server (memcached)
https://github.com/magro/memcached-session-manager
支持的session server类型:
memcached:
couchbase:
redis:
标签:something javac ast bin ada 本机 contex 指令 mys
原文地址:http://blog.51cto.com/001230/2155439