最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
一台服务器实现运行两个Tomcat并和Nginx实现负载均衡
时间:2022-06-29 00:22:40 编辑:袖梨 来源:一聚教程网
为了实现服务器升级不影响用户使用,有必要配置两个tomcat在一个或多个服务器中。下面介绍在同一个服务器配置两个Tomcat的过程。
步骤如下:
1、拷贝原来的Tomcat到另外一个目录,如:tomcat-8090,清除logs目录
2、修改Tomcat中的server.xml文件,把shutdown和Connector端口修改成另外的数值,如8006/8090
3、修改startup.sh和shutdown.sh文件,在第一行加入:
export CATALINA_HOME=/opt/app/tomcat-8090
4、启动tomcat
5、Nginx中配置负载均衡:
Nginx配置文件,添加:
upstream tomcatCluster {
server
server
}
server
{
listen 80;
server_name
index ……;
root ……;
location / {
location ~* /<项目名>/ {
proxy_pass http://tomcatCluster;
}
}
}
重启Nginx:service nginx restart
6、测试请求是否被轮流转发到两个tomcat中
CentOS 6.5系统下Nginx反向代理实现Tomcat负载均衡
1.查看当前系统的内核和系统参数以及版本。
[root@node1 ~]# uname -a
Linux node1 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
[root@node1 ~]# cat /etc/issue
CentOS release 6.5 (Final)
Kernel r on an m
2.安装nginx。
1)安装gcc编译器及相关工具和依赖库。
[root@node1 ~]# yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers pcre
2)编译安装pcre库。
PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正规表达式。Pcre的作用主要是使nginx支持HTTP rewrite模块。
创建一个指定放置压缩包的目录。
[root@node1 ~]# mkdir -p /linuxidc/tools
[root@node1 ~]# cd /linuxidc/tools/
[root@node1 tools]# tar -zxf pcre-8.33.tar.gz
[root@node1 pcre-8.33]# ./configure
[root@node1 pcre-8.33]# make && make install
[root@node1 nginx-1.5.8]# ./configure --with-http_stub_status_module --with-http_ssl_module --prefix=/data/nginx
[root@node1 nginx-1.5.8]# make && make install
注意:安装成功启动nginx,有可能会遇到下面错误,
[root@node1 nginx-1.5.8]# /data/nginx/sbin/nginx -t
/data/nginx/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
解决办法:
[root@node1 ~]# cd /lib64/
[root@node1 lib64]# ln -s libpcre.so.0.0.1 libpcre.so.1
3)此事再启动nginx,查看下进程和端口。
[root@node1 lib64]# /data/nginx/sbin/nginx -t
nginx: the configuration file /data/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /data/nginx/conf/nginx.conf test is successful
[root@node1 lib64]# /data/nginx/sbin/nginx
[root@node1 lib64]# ps -ef | grep nginx
root 8991 1 0 16:43 ? 00:00:00 nginx: master process /data/nginx/sbin/nginx
nobody 8992 8991 0 16:43 ? 00:00:00 nginx: worker process
root 8994 1907 0 16:44 pts/1 00:00:00 grep nginx
[root@node1 lib64]# netstat -anpt | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 8991/nginx
3.配置nginx web反向代理,实现两个tomcat负载均衡:
nginx配置文件如下:
[root@node1 ~]# cat /data/nginx/conf/nginx.conf
user root;
worker_processes 1;
#error_log logs/error.log info;
pid /data/nginx/logs/nginx.pid;
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 65535;
multi_accept on;
}
http {
include mime.types;
default_type application/octet-stream;
charset utf-8;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
server_tokens off;
limit_rate_after 3m;
limit_rate 512k;
tcp_nodelay on;
client_header_buffer_size 256k;
large_client_header_buffers 4 256k;
# Define nginx proxy module
proxy_http_version 1.1;
proxy_connect_timeout 60;
proxy_read_timeout 60;
proxy_send_timeout 60;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
proxy_headers_hash_max_size 51200;
proxy_headers_hash_bucket_size 6400;
# Define Gzip compression module
gzip on;
gzip_vary on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
# Define realserver pool
upstream linuxidc.com {
ip_hash;
server 192.168.1.15:8080 max_fails=0 weight=5;
server 192.168.1.19:8080 max_fails=0 weight=5;
}
server {
listen 80;
server_name linuxidc.net www.linuxidc.net;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://linuxidc.com;
expires 1d;
access_log logs/host.access.log main;
}
# Define 404 502 503 504 error page
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
Linux下Apache与多个Tomcat 集群负载均衡
多台电脑默认tomcat端口都是8080
每个tomcat下完全相同的部署
在此前面apache 与 tomcat整合的基础之上
修改vi /etc/httpd/conf/httpd.conf
[java]
LoadModule jk_module modules/mod_jk-1.2.26-httpd-2.2.4.so
JkWorkersFile "conf/workers.properties"
JkLogFile "logs/mod_jk.log"
JkLogLevel debug
JkMount /*.jsp loadbalancer
conf 下的 worker.properties 修改如下
[java]
ps=/
worker.list=worker1,worker2,loadbalancer
worker.worker1.port=8009
worker.worker1.host=localhost #本机一台tomcat
worker.worker1.type=ajp13
worker.worker1.lbfactor=1
worker.worker2.port=8009
worker.worker2.host=10.228.56.80 #另外一台机器tomcat
worker.worker2.type=ajp13
worker.worker2.lbfactor=1
worker.loadbalancer.type=lb
worker.loadbalancer.balanced_workers=worker1, worker2
# 负载指向 的机器
配置成功 这样就实现了负载均衡的 配置
Nginx Tomcat 集群负载均衡解决笔记
1、Windows
下载nginx 地址:http://nginx.org/ 本文章使用的版本为1.5.1
下载完后解压,在conf目录修改nginx.conf我的配置如下:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream localhost {
#根据ip计算将请求分配各那个后端tomcat,许多人误认为可以解决session问题,其实并不能。
#同一机器在多网情况下,路由切换,ip可能不同
#ip_hash;
server localhost:9097 weight=1;
server localhost:9098 weight=1;
server localhost:9097 backup;
server localhost:9098 backup;
#jvm_route $cookie_JSESSIONID|sessionid reverse; #session复制的配置
ip_hash;
}
upstream backend {
server localhost:9097;
server localhost:9098;
}
server {
listen 9096;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location ~ ^/(WEB-INF)/ {
deny all;
}
###所以的静态文件人gif、jpg等都在本地打开,存放的目录为html,保存时间为30天
location ~ .*.(gif|jpg|jpeg|png|bmp|swf|css|js|htm|html)$ {
root html;
access_log off;
expires 30d;
}
location / {
root html;
index index.jsp;
#include proxy.conf; #这个文件是我们新建的,要导入
proxy_pass http://localhost;
proxy_redirect off;#default
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 3; #90
proxy_send_timeout 3; #90
proxy_read_timeout 3; #90
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ .php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ .php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
下载Tomcat
修改端口为9097
另一个修改为9098
分别启动二个tomcat和nginx在地址栏中输入http://locahost/则可以看到tomcat欢迎界面了
Linux 和window配置基本一样,只有linux下tomcat的配置要注意,需要添加环境变量
Apache+Tomcat 环境搭建(JK部署过程)
Apache只是一个web服务器,并不能运行JSP程序。如果要运行JSP程序,还需要安装Tomcat服务器软件,通过连接器组件整合Tomcat与Apche。当用户请求访问*.jsp网页时,由Aapache自动调用tomcat服务进行解析,并将结果页面返回到客户端浏览器。
系统环境:
RHEL6.0,内核心版本号2.6.32-71.el6.x86_64
所需软件:
httpd-2.2.15.tar.gz
apache-tomcat-6.0.18.tar.gz
jdk-6u25-linux-x64.bin
mod_jk-1.2.28-httpd-2.2.X.so
下载mod_jk 各种版本,别下错了
http://archive.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/
下载jdk
http://www.Oracle.com/technetwork/java/javase/downloads/jdk-6u25-download-346242.html
一、安装httpd 服务
[[email protected] ~]#tar xzvf httpd-2.2.15.tar.gz
[[email protected] ~]#cd httpd-2.2.15
[[email protected] ~]#./configure –prefix=/usr/local/apache2 –enable-so –enable-rewrite && make && make install
二、安装配置jdk开发环境
1、 安装jdk
[[email protected] ~]# mv jdk-6u25-linux-x64.bin /usr/bin/
[[email protected] ~]# cd /usr/local/
[[email protected] local]# chmod a+x jdk-6u25-linux-x64.bin
[[email protected] local]# ./ jdk-6u25-linux-x64.bin
执行jdk安装程序后将会显示软件许可协议,按回车或空格至末尾,按照提示输入“yes”回车确认,等待1~2分钟左右即可完成安装。
2、为jdk的安装目录及执行程序创建链接文件,以方便使用。
[[email protected] ~]# ln -sf /usr/local/jdk1.6.0_25 /usr/local/jdk6
[[email protected] ~]# ln -sf /usr/local/jdk6/bin/java /usr/bin/
[[email protected] ~]# ln -sf /usr/local/jdk6/bin/javac /usr/bin/
修改/etc/profile文件,添加JAVA开发环境需要的变量设置。
[[email protected] ~]# vi /etc/profile
JAVA_HOME=/usr/local/jdk6
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME CLASSPATH PATH
[[email protected] ~]# source /etc/profile
3、测试java
查看安装的jdk 程序版本信息。
java version "1.6.0_25"
Java(TM) SE Runtime Environment (build 1.6.0_13-b03)
Java HotSpot(TM) Client VM (build 11.3-b02, mixed mode, sharing)
编写一个java测试小程序,并编译执行。
public class HelloWorld {
public static void main(String args[]) {
System.out.println("HelloWorld!!");
}
}
[[email protected] ~]# javac HelloWorld.java //编译程序
[[email protected] ~]# java HelloWorld //运行HelloWorld.class程序
HelloWorld!! //显示运行结果
三、安装配置Tomcat
1、安装tomcat
Tomcat的安装过程非常简单,只要将源码包解压至目的文件夹即可。
[[email protected] ~]# tar zxvf apache-tomcat-6.0.18.tar.gz
-C /usr/local/
2、配置。
① 为Tomcat的安装目录及启动、关闭服务的脚本创建链接文件,方便使用。
[[email protected] ~]# ln –sf /usr/local/apache-tomcat-6.0.18.tar.gz /usr/local/tomcat
[[email protected] ~]# ln -sf /usr/local/tomcat/bin/startup.sh /usr/bin/tomcat-up
[[email protected] ~]# ln -sf /usr/local/tomcat/bin/shutdown.sh /usr/bin/tomcat-down
② 修改/etc/profile文件,设置Tomcat服务需要使用的变量CATALINA_HOME。
[[email protected] ~]# vi /etc/profile //结合第一步,确认有如下内容:
JAVA_HOME=/usr/local/jdk6
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$CATALINA_HOME/lib/servlet-api.jar
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME CACTALINA_HOME CLASSPATH PATH
[[email protected] ~]# source /etc/profile //执行profile文件使新配置生效
3、修改tomcat-users.xml文件,添加管理员账户“tomcat”设置。
[[email protected] ~]# vi /usr/local/tomcat/conf/tomcat-users.xml
(3) 测试。
① 启动Tomcat服务,并确认Tomcat服务的监听状态。
[[email protected] ~]# tomcat-up
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk6
[[email protected] ~]# netstat -anpt | grep java
tcp 0 0 :::8009 :::* LISTEN 7399/java
tcp 0 0 :::8080 :::* LISTEN 7399/java
② 访问http://172.16.12.40:8080 ,可以看到Tomcat的web首页(如下图所示)
③ 通过单击“Tomacat Manager”链接可以登录后对Tomcat进行管理(如下图所示),如需停止Tomcat服务,可执行“tomcat-down”。
4、 将Tomcat服务与Aptech服务进行连接
虽然Tomcat可以独立作为Web服务器运行,但是Tomcat的Web功能远没有Apache强大。在实际应用中,通过mod_jdk连接器(Connectors)将Apache和Tomcat整合在一起。调用Tomcat服务的工作由Apache来完成,而用户只需要访问Apache服务。
(1) 安装mod_jdk模块。
[[email protected] ~]# cp mod_jk-1.2.28-httpd-2.2.X.so /usr/local/apache2/modules
(2) 调整Apache服务、Tomcat服务的相关配置,并重启tomcat、httpd服务。
① 修改http.conf配置文件,添加jdk调用配置。
[[email protected] ~]# vi /usr/local/apache2/conf/httpd.conf
...
ServerName l-www.linuxidc.com
DocumentRoot "/usr/local/apache2/htdocs"
...
LoadModule jk_module modules/mod_jk-1.2.28-httpd-2.2.X.so
JkWorkersFile conf/workers.properties
JkLogFile logs/mod_jk.log
JkShmFile logs/mod_jk.shm
JkLogLevel notice
JkMount /*/*.jsp work1
JkMountCopy All
AddType application/x-jsp .jsp
② 添加tomcat配置文件,并建立Web应用目录链接
[[email protected] ~]# vi /usr/local/apache2/conf/workers.properties
worker.list=work1
worker.work1.port=8009
worker.work1.host=l-www.linuxidc.com
worker.work1.type=ajp13
worker.work1.lbfactor=1
[[email protected] ~]# cd /usr/local/tomcat
[[email protected] tomcat]# mv webapps webapps.bak
[[email protected] tomcat]# ln -sf /usr/local/apache2/htdocs webapps
③ 重启 apache 和tomcat服务。
[[email protected] ~]# tomcat-down ; tomcat-up
[[email protected] ~]# /usr/local/apache2/bin/apachectl restart
(3) 测试
① 创建显示系统时间的JSP测试网页。
[[email protected] ~]# mkdir /usr/local/apache2/htdocs/apps/
[[email protected] ~]# vi /usr/local/apache2/htdocs/apps/showtime.jsp
<%@page language="java" import="java.util.*"%>
Now,the time&date is : <%out.println(new Date());%>
② http://192.168.0.10/apps/showtime.jsp , 应在网页中显示当前的系统时间
相关文章
- 时空中的绘旅人天宇之间怎么玩 绘旅人天宇之间活动玩法介绍 12-25
- QQ2024年度报告怎么看 2024qq年度报告玩法介绍 12-25
- 归龙潮珠砂什么时候up 归龙潮红缘绮梦卡池介绍 12-25
- 王者荣耀S38赛季有什么更新 12-25
- 王者荣耀S38赛季有什么更新 王者荣耀S38赛季更新内容介绍 12-25
- 世界之外12.25有什么更新 世界之外12月25日更新内容介绍 12-25