使用Nginx作为Tomcat的负载平衡器,Tomcat的会话Session数据存储在Redis,能够实现零宕机的7x24效果。因为将会话存储在Redis中,因此Nginx就不必配置成stick粘贴某个Tomcat方式,这样才能真正实现后台多个Tomcat负载平衡。 用了tomcat7,redis3.0.7,nginx1.12.2,jdk1.7
node1节点
安装nginx
[root@node1 ~]#yum install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre* -y
[root@node1 ~]#tar xf nginx-1.12.2.tar.gz
[root@node1 ~]#cd nginx-1.12.2
[root@node1 ~]#./configure --prefix=/opt/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre --with-http_ssl_module --with-http_realip_module --with-http_gzip_static_module
[root@node1 ~]#make && make install
[root@node1 ~]#useradd -s /sbin/nologin nginx
配置文件
user nginx nginx;
worker_processes 4;
worker_cpu_affinity 0001 0010 0100 1000;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
worker_rlimit_nofile 10240;
use epoll;
worker_connections 4096;
}
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;
server_tokens off;
sendfile on;
tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#Compression Settings
gzip on;
gzip_comp_level 6;
gzip_http_version 1.1;
gzip_proxied any;
gzip_min_length 1k;
gzip_buffers 16 8k;
gzip_vary on;
#end gzip
# http_proxy Settings
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 75;
proxy_send_timeout 75;
proxy_read_timeout 75;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
#load balance Settings
upstream backend_tomcat {
server 128.0.0.103:8080 weight=1 max_fails=2 fail_timeout=10s;
server 128.0.0.102:8080 weight=1 max_fails=2 fail_timeout=10s;
}
#virtual host Settings
server {
listen 80;
server_name localhost;
charset utf-8;
location / {
root html;
index index.jsp index.html index.htm;
}
location ~* \.(jsp|do)$ {
proxy_pass http://backend_tomcat;
proxy_redirect off;
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_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
}
location /nginx_status {
stub_status on;
access_log off;
allow 128.0.0.101/24;
deny all;
}
}
}
启动nginx
[root@node1 ~]#/opt/nginx/sbin/nginx
安装redis
[root@node1 ~]#tar xf redis-3.0.7.tar.gz
[root@node1 ~]#cd redis-3.0.7/
启动redis
[root@node1 ~]#redis-server redis.conf
安装node2节点(node3节点和node2一样)
```
tar xf apache-tomcat-7.0.82.tar.gz -C /opt/
tar xf jdk-7u79-linux-x64.tar.gz -C /opt
export JAVA_HOME=/opt/jdk1.7/
export JAVA_BIN=/opt/jdk1.7/bin
export PATH=${JAVA_HOME}bin:$PATH
export CLASSPATH=.:${JAVA_HOME}/lib/dt.jar:${JAVA_HOME}/lib/tools.jar
[root@node2 ~]# cat /etc/profile
[root@node2 ~]# java -version
java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)
开始配置
[root@node2 opt]# vim apache-tomcat-7.0.82/conf/server.xml
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat-1">加上jvmRoute="tomcat-1"好分辨
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
#添加这行 <Context docBase="/web/webapp1" path="" reloadable="true"/>
修改文件
[root@node2 apache-tomcat-7.0.82]# vim conf/context.xml
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="128.0.0.101"
password="123"
port="6379"
database="0"
maxInactiveInterval="60" />
#这行不要 </Context>
测试文件
[root@node2 opt]# vim /web/webapp1/index.jsp
<%@page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>tomcat-1</title>
</head>
<body>
<h1><font color="red">Session serviced by tomcat1</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>
最重要的3个包
tomcat-redis-session-manage-tomcat7.jar
jedis-2.5.2.jar
commons-pool2-2.2.jar
放到lib下面
最后启动tomcat
redis里面的值
127.0.0.1:6379> keys *
1) "DD8D493FE510152E656884E8C853A00D.tomcat-2.tomcat-2"