Tomcat的负载均衡及热部署

一.安装负载均衡的Lnmt

lmnt:clent->nginx->reverse proxy->tomcat (http connector )

1.下载安装

nginx-1.6.2-1.el6.ngx.x86_64.rpm

[cc lang="bash"]
[root@node1 ~]# rpm -ivh nginx-1.6.2-1.el6.ngx.x86_64.rpm

warning: nginx-1.6.2-1.el6.ngx.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID 7bd9bf62: NOKEY

Preparing...               ########################################### [100%]

1:nginx                 ########################################### [100%]

----------------------------------------------------------------------

Thanks for using nginx!
[/cc]

2.启动nginx

[cc lang="bash"]
[root@node1 nginx]# /etc/init.d/nginx start
[/cc]

3.让nginx以反代模式工作,修改配置文件

[cc lang="bash"]
[root@node1 conf.d]# vim default.conf
[/cc]

[cc lang="bash"]
location / {

# root   /usr/share/nginx/html; //禁用该段

proxy_pass http://172.16.249.249:8080/; //反代指向tomcat

index index.html index.htm;

}
[/cc]

4.重新载入nginx之后,再次访问。打开的将是后端的tomcat。

[cc lang="bash"]
[root@node1 conf.d]# /etc/init.d/nginx reload
[/cc]

5.在nginx主配置文件中定义集群

[cc lang="bash"]
[root@node1 nginx]# vim nginx.conf
[/cc]

[cc lang="bash"]
upstream tomcatser{

server 172.16.249.249:8080;

server 172.16.249.113:8080;

}
[/cc]

6.在default文件的localtion中调用

[cc lang="bash"]
location / {

# root   /usr/share/nginx/html;

proxy_pass http://tomcatser; //主要设置这段

index index.html index.htm;

}

[/cc]
7.测试访问

http://172.16.249.249

动静分离:静态内容前段响应,动态内容交给后端的172.16.249.249:8080

[cc lang="bash"]
location / {

root   /usr/share/nginx/html;

#   proxy_pass http://tomcatser;

index index.jsp   index.html index.htm;

}

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

proxy_pass http://172.16.249.249:8080;

}
[/cc]

二.安装Lamt

lamt: client->httpd->reverse proxy -> tomcat

基于http connector: http协议

1.httpd:需要装载mod_proxy模块,否则无法支持代理。

mod_proxy_http,mod_proxy_ajp ajp是独有协议,只能支持httpd到tomcat之间通讯。

2.模块检查

[cc lang="bash"]
[root@node1 etc]# httpd -M 查看模块命令
[/cc]

proxy_module (shared)

proxy_http_module (shared)

3.编辑主配置文件,把中心主机注释掉

[cc lang="bash"]
[root@node1 conf]# vim httpd.conf
[/cc]
[cc lang="bash"]

#DocumentRoot "/var/www/html"
[/cc]

4.添加配置文件

[cc lang="bash"]
[root@node1 conf.d]# vim http-tomcat.conf
[/cc]

NameVirtualHost *:80

<VirtualHost *:80>

ServerName www.a.com

proxyVia On //经过服务器代理响应

ProxyRequests Off//关闭正向代理

ProxyPreserveHost On//启用反向代理

<Proxy * > //对所有人开放代理权限

Order deny,allow

Allow from all

</proxy>

ProxyPass /status ! //该页面不做代理

ProxyPass / http://172.16.249.249:8080/

ProxyPassReverse / http://172.16.249.249:8080/

<Location />

Order deny,allow

Allow from all

</Location>

</VirtualHost>

5.启动httpd并测试访问即可

[cc lang="bash"]
[root@node1 conf.d]# service httpd start
[/cc]

http://172.16.249.249/

基于ajp connect:ajp协议

1.赋值配置文件,并加入下面配置

[cc lang="bash"]
[root@node1 conf.d]# cp http-tomcat.conf ajp-tomcat.conf
[/cc]

[cc lang="bash"]
[root@node1 conf.d]# mv http-tomcat.conf http-tomcat.conf.bak
[/cc]

[cc lang="bash"]
[root@node1 conf.d]# vim ajp-tomcat.conf
[/cc]

NameVirtualHost *:80

<VirtualHost *:80>

ServerName www.a.com

proxyVia On

ProxyRequests Off

ProxyPreserveHost On

<Proxy * >

Order deny,allow

Allow from all

</proxy>

ProxyPass /status !

ProxyPass / ajp://172.16.249.249:8009/ //就此处的ajp协议和8009端口不同,其他与http相同

ProxyPassReverse / ajp://172.16.249.249:8009/

<Location />

Order deny,allow

Allow from all

</Location>

</VirtualHost>

2.测试语法没问题后重启httpd,访问测试

[root@node1 conf.d]# httpd -t                

http://172.16.249.249/

三.Server Status和Host-Manager管理

Server Status管理:

1)编辑配置文件,添加以下字段:

[cc lang="bash"]
[root@node1 conf]# vim tomcat-users.xml
[/cc]

<role rolename="manager-gui"/>

<user username="admin" password="admin" roles="manager-gui"/>

2)重新启动tomcat即可输入帐号密码访问。

[cc lang="bash"]
[root@node1 concatalina.sh stop
[/cc]

[cc lang="bash"]
[root@node1 concatalina.sh startl
[/cc]

tomcat2015061001

Host-Manager管理:可以在此模式下进行程序热部署

[部署方式]

冷部署:在tomcat启动之前进行的部署。

热部署:在不停止tomcat的前提下进行部署。

1.编辑配置文件,添加以下字段:

[cc lang="bash"]
[root@node1 conf]# vim tomcat-users.xml
[/cc]
<role rolename="admin-gui"/>

<user username="tomcat" password="tomcat" roles="admin-gui"/>

2)重新启动tomcat即可输入帐号密码访问。

[cc lang="bash"]
[root@node1 concatalina.sh stop
[/cc]

[cc lang="bash"]
[root@node1 concatalina.sh startl
[/cc]

 

tomcat2015061002


发表评论

登录 后发表评论.