这是分布式集群环境下,如何实现session共享系列的第三篇。在上一篇:分布式集群环境下,如何实现session共享二(项目开发)中,准备好了一个通过原生态的servlet操作session的案例。本篇需要搭建相关的环境,包括:tomcat、nginx、redis。
1.通过两个tomcat搭建集群:tomcat_1、tomcat_2
2.通过nginx实现负载均衡
3.通过redis存储session
1.安装tomcat
1.1.tomcat_1
上传tomcat_1到服务器192.168.80.22中。
#跳转到目录
cd /usr/local/develop #创建目录
mkdir atomcat cd atomcat
server.xml配置文件(端口):
进程监听端口:
<Server port="8005" shutdown="SHUTDOWN">
http服务端口:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
集群通信端口:
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
1.2.tomcat_2
上传tomcat_2到服务器192.168.80.22中。
#跳转到目录
cd /usr/local/develop #创建目录
mkdir atomcat cd atomcat
server.xml配置文件(端口):
进程监听端口:
<Server port="8006" shutdown="SHUTDOWN">
http服务端口:
<Connector port="8081" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
集群通信端口:
<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />
2.安装nginx
2.1.安装编译工具和库文件
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
2.2.安装pcre
#进入目录
cd /usr/local/develop/anginx #上传安装文件并解压
tar -zxvf pcre-8.38.tar.gz #进入安装目录
cd pcre-8.38 #检查配置
./configure #编译、安装
make && make install #查看pcre版本
pcre-config --version
2.3.安装nginx
#进入目录
cd /usr/local/develop/anginx #上传安装文件,并解压
tar -zxvf nginx-1.8.1.tar.gz #进入安装目录
cd nginx-1.8.1 #检查配置
./configure --prefix=/usr/local/develop/anginx/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/develop/anginx/pcre-8.38 #编译安装
make && make install #查看nginx版本
/usr/local/develop/anginx/webserver/nginx/sbin/nginx -v
--------------------------------------------------------
[root@hadoop02 webserver]# /usr/local/develop/anginx/webserver/nginx/sbin/nginx -v
nginx version: nginx/1.8.1 #配置nginx(检查)
/usr/local/develop/anginx/webserver/nginx/sbin/nginx -t #nginx管理命令
/usr/local/develop/anginx/webserver/nginx/sbin/nginx # 启动 Nginx
/usr/local/develop/anginx/webserver/nginx/sbin/nginx -s stop # 停止 Nginx
/usr/local/develop/anginx/webserver/nginx/sbin/nginx -s reload # 重新载入配置文件
/usr/local/develop/anginx/webserver/nginx/sbin/nginx -s reopen # 重启 Nginx
2.4.nginx基础配置
#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; sendfile on;
#tcp_nopush on; #keepalive_timeout 0;
keepalive_timeout 65; #gzip on; server {
listen 80;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / {
root html;
index index.html index.htm;
} #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;
} }
2.4.nginx负载均衡配置
#添加tomcat列表,真实应用服务器都放在这
upstream tomcat_pool{
#server tomcat地址:端口号 weight表示权值,权值越大,被分配的几率越大;
server 192.168.80.22:8080 weight=4 max_fails=2 fail_timeout=30s;
server 192.168.80.22:8081 weight=4 max_fails=2 fail_timeout=30s; #均衡策略
#ip_hash;
}
---------------------------------------------------------------------------
server {
listen 80;
server_name tomcat_pool; #charset koi8-r; #access_log logs/host.access.log main; location / {
#root html;
#index index.html index.htm;
proxy_pass http://tomcat_pool; #转向tomcat处理
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
} #error_page 404 /404.html; .....................................................
}
3.安装redis
#进入目录
cd /usr/local/develop/aredis #上传并解压redis
tar -zxvf redis-3.2.11.tar.gz #进入redis目录
cd redis-3.2.11 #编译
make #进入src目录,并执行安装
cd src make install PREFIX=/usr/local/develop/aredis/myredis #启动redis服务端
[root@hadoop02 bin]# ./redis-server #客户端连接操作
[root@hadoop02 bin]# ./redis-cli