第一章 測試環境說明
1.1 系統說明
系統均選用最小化安裝的centos 5.7
1.2 軟件說明
nginx-0.8.55
pcre-8.13
apache-tomcat-6.0.35
jdk-6u31-linux-x64
nginx-upstream-jvm-route-0.1
1.3 規劃說明
客戶端通過訪問nginx做的負載均衡層去訪問後端的web運行層(tomcat),如下圖:
另外,關於session複製原理,簡單來說如下圖:
負載層:192.168.254.200
安裝:pcre、nginx、nginx-upstream-jvm-route-0.1
後端tomcat運行層:192.168.254.221、192.168.254.222
安裝:tomcat、jdk
第2章 安裝部署說明
2.1 負載均衡層安裝部署說明
2.1.1 依賴包安裝
yum install wget make gcc gcc-c++ -y
yum install pcre-devel openssl-devel patch -y
2.1.2 創建nginx運行帳號
useradd www -s /sbin/nologin -M
2.1.3 Pcre安裝
解壓pcre安裝包:tar xvf pcre-8.13.tar.gz
cd pcre-8.13
編譯pcre:./configure --prefix=/usr/local/pcre
安裝:make && make install
2.1.4 Nginx安裝
解壓nginx和nginx-upstream
tar xvf nginx-upstream-jvm-route-0.1.tar.gz
tar xvf nginx-0.8.55.tar.gz
cd nginx-0.8.55
配置jvmroute路徑:
patch -p0 < ../nginx_upstream_jvm_route/jvm_route.patch
編譯nginx:
./configure \
--user=www \
--group=www \
--prefix=/usr/local/nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_gzip_static_module \
--pid-path=/var/run/nginx.pid \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--http-client-body-temp-path=/var/tmp/nginx/client_body_temp \
--http-proxy-temp-path=/var/tmp/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/tmp/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/tmp/nginx/scgi_temp \
--add-module=/root/scripts/src/nginx_upstream_jvm_route/
安裝:
make && make install
2.1.5 Nginx配置文件修改
Nginx作爲負載的配置文件修改很簡單,只需添加後端web服務器的ip及端口即可,修改運行帳號,下面配置文件中的紅色字體爲本次測試環境的修改值;
user www www;
worker_processes 8;
#error_log logs/nginx_error.log crit;
#pid /usr/local/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 2048;
}
http
{
upstream backend {
server 192.168.254.221:80 srun_id=real1;
server 192.168.254.222:80 srun_id=real2;
jvm_route $cookie_JSESSIONID|sessionid reverse;
}
include mime.types;
default_type application/octet-stream;
#charset gb2312;
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 192.168.254.250;
index index.jsp index.htm index.html;
root /data/www/;
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;
}
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';
# access_log off;
}
}
2.2 後端tomcat運行層部署說明
2.2.1 安裝jdk
創建jdk安裝目錄:
mkdir /opt/java
賦予執行權限:
chmod 755 jdk-6u31-linux-x64.bin
cd /opt/java
./jdk-6u31-linux-x64.bin
修改環境變量:
cat >> /etc/profile.d/java.sh << "EOF"
export JAVA_HOME=/opt/java/jdk1.6.0_31
export CLASSPATH=$CLASSPATH:./:$JAVA_HOME/lib
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
EOF
配置環境變量生效:
source /etc/profile
2.2.2 安裝tomcat
解壓安裝包,並將tomcat複製到/usr/local目錄下,命名爲tomcat;
tar xvf apache-tomcat-6.0.35.tar.gz
cp -r apache-tomcat-6.0.35 /usr/local/tomcat
2.2.3 修改配置文件 (其他配置請參考集羣配置)
2.2.3.1 修改server.xml文件
修改server.xml文件只需要修改下面兩點即可,因爲是兩臺機器,故兩臺機器配置相同即可;
1、修改jvmRoute="real1"(自定義)
<Engine name="Catalina" defaultHost="localhost" jvmRoute="real1">
2、下面的代碼是從官方網站上找到的默認的,具體運用中需要將auto的配置改成本機ip:
<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>
2.2.3.2 修改web.xml文件
配置web.xml文件只需要在末端web-app前面添加<distributable/>即可;
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<distributable/>
</web-app>
2.3 測試session複製
2.3.1 創建測試文件
分別在tomcat項目部署目錄下創建test文件夾,並創建index.jsp文件,文件內容如下:
<%@page language="java"%>
<html>
<body>
<h1><font color="red">Session serviced by tomcat</font></h1>
<table aligh="center" border="1">
<tr>
<td>Session ID</td>
<td><%=session.getId() %></td>
<% session.setAttribute("abc","abc");%>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
<html>
2.3.2 Web測試session複製
瀏覽器中輸入:http://192.168.254.200/test/
即可查看當前負載tomcat測試頁面輸出:
關閉221的tomcat,並刷新頁面
如此則是測試完成
centos下搭建nginx+tomcat實現集羣負載與session複製
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
apache反代tomcat出現的問題
隨風上升
2019-02-23 13:57:35
Tomcat虛擬主機配置
心碎小胖子
2019-02-23 13:34:47
windowXP上的jsp環境設置
爪哇
2019-02-23 13:12:07
Nginx 安裝與配置規則入門
Lmagic16
2019-02-24 22:13:58
nginx+php執行請求的工作原理
xavier
2019-02-24 15:52:41
配置Nginx支持php,出現No input file specified錯誤的解決方法
lvqingpu
2019-02-24 13:15:35
深入 Nginx 之架構篇
jeffrey_up
2019-02-23 19:23:46
tcpdump查看Nginx長連接還是短連接
ouyangbro
2019-02-23 15:52:48
Nginx使用安裝(一)
flashhand
2019-02-23 13:58:28
nginx中gzip模塊
小紅帽子
2019-02-23 13:37:48
nginx+keepalived配置高可用HTTP羣集
小紅帽子
2019-02-23 13:37:48
Nginx服務之防盜鏈功能
兩袖春風
2019-02-23 13:28:22
寶塔面板 + Rancher + 阿里雲鏡像倉庫 + Docker + Kubernetes,添加集羣、部署 web 應用
燕小范
2019-02-23 13:20:14
nginx添加系統服務
braoshan
2019-02-23 13:15:21
Nginx簡易實用的php域名配置
wx5c6f5798d627c
2019-02-23 13:07:16