集群系统一般通过两台或多台节点服务器系统通过相应的硬件及软件互连,每个群集节点都是运行其自己进程的独立服务器。
这些进程可以彼此通信,对网络客户机来说就像是形成了一个单一系统,协同起来向用户提供应用程序、系统资源和数据。除了作为单一系统提供服务,集群系统还具有恢复服务器级故障的能力。而tomcat集群这是可以做到以上几点。
首先,安装nginx之前需要pcre依赖和jvm-remote补丁。
一、准备如下软件:
二、安装和安装
1、解压各软件
[root@localhost ~]# tar zxvf pcre-8.10.tar.gz [root@localhost ~]# tar zxvf nginx-upstream-jvm-route-0.1.tar.gz [root@localhost ~]# tar nginx-1.1.2.tar.gz
2、安装
[root@localhost ~]# cd nginx-1.1.2 [root@localhost ~]# patch -p0 < ${nginx-upstream-jvm-route解压目录}/jvm_route.patch [root@localhost ~]# ./configure --prefix=/usr/local/nginx --with-pcre=${pcre解压目录} --with-http_stub_status_module --with-http_ssl_module --add-module=${nginx-upstream-jvm-route解压目录} [root@localhost ~]# make [root@localhost ~]# make install
三、修改配置
1、修改tomcat的server.xml,服务器的tomcat的配置文件中分别找到:
<Engine name="Catalina" defaultHost="localhost" >
分别修改为:
Tomcat01: <Engine name="Catalina" defaultHost="localhost" jvmRoute="a"> Tomcat02: <Engine name="Catalina" defaultHost="localhost" jvmRoute="b"> Tomcat03: <Engine name="Catalina" defaultHost="localhost" jvmRoute="c">
2、修改nginx的nginx.conf文件
#运行NGINX所使用的用户和组 user root; #nginx进程数,建议按照cpu数目来指定,一般为它的倍数,每个进程消耗约10M内存 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 { #使用epoll的I/O模型 use epoll; #该值受系统进程最大打开文件数限制,需要使用命令ulimit -n 查看当前设置 worker_connections 24; } http { #这里是您需要修改的地方,修改为您的服务器IP:端口号 srun_id为您在tomcat中所配置的jvmRoute upstream backend{ server2.168.12.128:18080 srun_id=a; server2.168.12.128:28080 srun_id=b; server2.168.12.128:38080 srun_id=c; jvm_route $cookie_JSESSIONID|sessionid reverse; } include mime.types; #设置默认类型是二进制流,若未设置时,比如未加载PHP时,是不予解析,用浏览器访问则出现下载窗口 default_type application/octet-stream; charset UTF-8; server_names_hash_bucket_size8; client_header_buffer_sizek; large_client_header_buffers 4k; client_max_body_sizem; limit_rate24k; sendfile on; tcp_nopush on; keepalive_timeout; tcp_nodelay on; fastcgi_connect_timeout0; fastcgi_send_timeout0; fastcgi_read_timeout0; fastcgi_buffer_sizek; fastcgi_buffers 4k; fastcgi_busy_buffers_size8k; fastcgi_temp_file_write_size8k; gzip on; #gzip_min_length 1k; gzip_buffers 4k; gzip_http_version 1.0; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; #limit_zone crawler $binary_remote_addrm; server { listen ; server_name 2.168.12.128; #这里也是您所需要修改的地方,多域名用空格隔开 index index.html index.htm index.jsp; charset UTF-8; root /usr/local/tomcats/project/;# 这里也是您所需要修改的地方,虚拟机指向的路径(可能这里有点问题),我的web应用系统放在project下面的 #access_log logs/host.access.log main; #这里也是您所需要修改的地方,yourproject更换成您的项目路径 location /yourproject/ { proxy_pass http://backend; proxy_redirect off; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $http_host; index index.html index.htm index.jsp; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expiresd; } location ~ .*\.(js|css)?$ { expires 1h; } location /Nginxstatus{ stub_status on; access_log off; } log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for'; error_page 4 /404.html; error_page 0234 /50x.html; location = /50x.html { root html; } } }
3、检查nginx的配置
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t -c /usr/nginx/conf/nginx.conf
四、启动测试
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf /usr/local/tomcats/tomcat-a/bin/startup.sh /usr/local/tomcats/tomcat-b/bin/startup.sh /usr/local/tomcats/tomcat-c/bin/startup.sh
停止服务
/usr/local/tomcats/tomcat-a/bin/shutdown.sh /usr/local/tomcats/tomcat-b/bin/shutdown.sh /usr/local/tomcats/tomcat-c/bin/shutdown.sh pkill -9 nginx 我的配置文件 #运行NGINX所使用的用户和组 user root; #nginx进程数,建议按照cpu数目来指定,一般为它的倍数,每个进程消耗约10M内存 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 { #使用epoll的I/O模型 use epoll; #该值受系统进程最大打开文件数限制,需要使用命令ulimit -n 查看当前设置 worker_connections 1024; } http { #这里是您需要修改的地方,修改为您的服务器IP:端口号 srun_id为您在tomcat中所配置的jvmRoute upstream backend{ server 121.199.43.119:18080 srun_id=a; server 121.199.43.119:28080 srun_id=b; server 121.199.43.119:38080 srun_id=c; jvm_route $cookie_JSESSIONID|sessionid reverse; } include mime.types; #设置默认类型是二进制流,若未设置时,比如未加载PHP时,是不予解析,用浏览器访问则出现下载窗口 default_type application/octet-stream; charset UTF-8; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 20m; limit_rate 1024k; sendfile on; tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; gzip 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; gzip_vary on; #limit_zone crawler $binary_remote_addr 10m; server { listen 80; server_name 121.199.43.119; #这里也是您所需要修改的地方,多域名用空格隔开 index index.html index.htm index.jsp; charset UTF-8; root /usr/local/nginx/html/;# 这里也是您所需要修改的地方,虚拟机指向的路径(可能这里有点问题),我的web应用系统放在project下面的 #access_log logs/host.access.log main; #这里也是您所需要修改的地方,yourproject更换成您的项目路径 location / { proxy_pass http://backend; proxy_redirect off; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $http_host; index index.html index.htm index.jsp; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*\.(js|css)?$ { expires 1h; } location /Nginxstatus{ stub_status on; access_log off; } log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for'; error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
原文链接:https://blog.csdn.net/q364035622/article/details/84445287
原创文章,作者:优速盾-小U,如若转载,请注明出处:https://www.cdnb.net/bbs/archives/8004