nginx+tomcat8+memcached实现session共享具体操作
本次試驗(yàn)用到的軟件包的版本如下:
一、針對10.43.2.134的操作
1.安裝jdk環(huán)境
tar zxf jdk-8u5-linux-x64.tar.gz?
mkdir /usr/java
mv jdk1.8.0_05/ /usr/java/
編輯/etc/profile
在文檔的末尾追加如下5行內(nèi)容:
JAVA_HOME=/usr/java/jdk1.8.0_05
JRE_HOME=/usr/java/jdk1.8.0_05/jre
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
CLASSPATH=.:${JAVA_HOME}/lib/:$JRE_HOME/lib
export JAVA_HOME JRE_HOME PATH CLASSPATH
[root@localhost jdk1.8.0_05]# source /etc/profile
查看Java的版本
[root@localhost ~]# java -version
java version "1.8.0_05"
Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)
2.安裝Tomcat?
tar zxvf apache-tomcat-8.0.8.tar.gz
mv apache-tomcat-8.0.8 /usr/local/tomcat/
#默認(rèn)tomcat是root身份運(yùn)行的,這樣不安全,我們設(shè)置來用普通用戶
groupadd tomcat?
useradd -g tomcat tomcat?
passwd tomcat?
chown tomcat.tomcat -R /usr/local/tomcat?
su – tomcat /usr/local/tomcat/bin/startup.sh?
echo “su – tomcat /usr/local/tomcat/bin/startup.sh” >> /etc/rc.local ?#開機(jī)啟動(dòng)
確認(rèn)服務(wù)是否啟動(dòng)成功:http://localhost:8080
同樣的方式再部署一個(gè)tomcat,端口號:8081
tar zxvf apache-tomcat-8.0.8.tar.gz
mv apache-tomcat-8.0.8 /usr/local/tomcat2
chown tomcat.tomcat -R /usr/local/tomcat2?
編輯 /usr/local/tomcat2/conf/server.xml
三處修改分別是:
su – tomcat /usr/local/tomcat2/bin/startup.sh?
echo “su – tomcat /usr/local/tomcat2/bin/startup.sh” >> /etc/rc.local
確認(rèn)服務(wù)是否啟動(dòng)成功:http://localhost:8081
3.制作tomcat服務(wù)器測試頁,并測試訪問
分別在$CATALINA/webapps/ROOT/下建立測試頁面t.jsp
# vim /usr/local/tomcat/webapps/ROOT/t.jsp
<html>
? ? <body bgcolor="green"> ? ? ?
? ? <center> ? ??
? ? <%= ?request.getSession().getId() ?%> ? ??
? ? <h1>10.43.2.134</h1>?
? ? <h1>port:8080</h1> ?
? ? </center>
? ? </body>
</html>
# vim /usr/local/tomcat2/webapps/ROOT/t.jsp
<html>
? ? <body bgcolor="red"> ? ? ?
? ? <center> ? ??
? ? <%= ?request.getSession().getId() ?%> ? ??
? ? <h1>10.43.2.134</h1>?
? ? <h1>port:8081</h1> ?
? ? </center>
? ? </body>
</html>
打開http://10.43.2.134:8080/t.jsp和http://10.43.2.134:8081/t.jsp可以看到不同的頁面
二、針對10.43.2.135的操作
nginx的安裝
安裝依賴包:yum -y install gcc openssl-devel pcre-devel zlib-devel?
安裝nginx
useradd nginx -s /sbin/nologin
tar zxvf nginx-0.8.46.tar.gz ?
cd nginx-0.8.46/ ?
./configure --user=nginx--group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module ?&& make ?&& ?make install
啟動(dòng)服務(wù):/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf?
[root@localhost ~]# netstat -anpt|grep nginx?
tcp ? ? ? ?0 ? ? ?0 0.0.0.0:80 ? ? ? ? ? ? ? ? ?0.0.0.0:* ? ? ? ? ? ? ? ? ? LISTEN ? ? ?7920/nginx
測試nginx默認(rèn)web服務(wù)器是否能正常運(yùn)行
設(shè)置nginx開機(jī)自動(dòng)啟動(dòng)?
[root@rhel6u3-7 ~]# echo "/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf" >> /etc/rc.local ?
[root@rhel6u3-7 ~]# cat /etc/rc.local | grep nginx?
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf?
nginx的啟動(dòng)腳本
#編寫nginx啟動(dòng)、停止、重啟的管理腳本,方便使用
cat /etc/init.d/nginx
#!/bin/sh?
#?
# nginx - this script starts and stops the nginx daemon?
#?
# chkconfig: - 85 15?
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \?
# ? proxy and IMAP/POP3 proxy server?
# processname: nginx?
# config: /etc/nginx/nginx.conf?
# config: /etc/sysconfig/nginx?
# pidfile: /var/run/nginx.pid?
# Source function library.?
. /etc/rc.d/init.d/functions?
# Source networking configuration.?
. /etc/sysconfig/network?
# Check that networking is up.?
[ "$NETWORKING" = "no" ] && exit 0?
? ? nginx="/usr/local/nginx/sbin/nginx"?
? ? prog=$(basename $nginx)?
? ? NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"?
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx?
? ? lockfile=/var/lock/subsys/nginx?
start() {?
? ? [ -x $nginx ] || exit 5?
? ? [ -f $NGINX_CONF_FILE ] || exit 6?
? ? echo -n $"Starting $prog: "?
? ? daemon $nginx -c $NGINX_CONF_FILE?
? ? retval=$??
? ? echo?
[ $retval -eq 0 ] && touch $lockfile?
? ? return $retval?
}?
stop() {?
? ? echo -n $"Stopping $prog: "?
? ? killproc $prog -QUIT?
? ? retval=$??
? ? echo?
[ $retval -eq 0 ] && rm -f $lockfile?
? ? return $retval?
? ? killall -9 nginx?
}?
restart() {?
? ? configtest || return $??
? ? stop?
? ? sleep 1?
? ? start?
}?
reload() {?
? ? configtest || return $??
? ? echo -n $"Reloading $prog: "?
? ? killproc $nginx -HUP?
? ? RETVAL=$??
? ? echo?
}?
force_reload() {?
? ? restart?
}?
configtest() {?
? ? $nginx -t -c $NGINX_CONF_FILE?
}?
rh_status() {?
? ? status $prog?
}?
rh_status_q() {?
? ? rh_status >/dev/null 2>&1?
}?
case "$1" in?
? ? start)?
? ? ? ? rh_status_q && exit 0?
? ? ? ? $1?
? ? ;;?
? ? stop)?
? ? ? ? rh_status_q || exit 0?
? ? ? ? $1?
? ? ;;?
? ? restart|configtest)?
? ? ? ? $1?
? ? ;;?
? ? reload)?
? ? ? ? rh_status_q || exit 7?
? ? ? ? $1?
? ? ;;?
? ? force-reload)?
? ? ? ? force_reload?
? ? ;;?
? ? status)?
? ? ? ? rh_status?
? ? ;;?
? ? condrestart|try-restart)?
? ? ? ? rh_status_q || exit 0?
? ? ;;?
? ? *)?
? ? ? ? echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"?
? ? ? ? exit 2?
esac?
[root@rhel6u3-7 init.d]# chmod 755 nginx ?
[root@rhel6u3-7 init.d]# chkconfig --add nginx ?
[root@rhel6u3-7 init.d]# chkconfig --level 35 nginx on ??
[root@rhel6u3-7 init.d]# chkconfig --list | grep nginx?
nginx ? ? ? ? ? 0:off ? 1:off ? 2:off ? 3:on ? ?4:off ? 5:on ? ?6:off?
測試nginx腳本文件是否能夠正常使用
[root@rhel6u3-7 init.d]# /etc/init.d/nginx restart?
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok?
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful?
Stopping nginx: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?[ ?OK ?]?
Starting nginx: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?[ ?OK ?]?
[root@rhel6u3-7 init.d]# /etc/init.d/nginx reload ?
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok?
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful?
Reloading nginx: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [ ?OK ?]?
[root@rhel6u3-7 ~]# cat /usr/local/nginx/logs/nginx.pid ?
15799?
[root@rhel6u3-7 ~]# kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
?[root@rhel6u3-7 init.d]# /etc/init.d/nginx stop?
Stopping nginx: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?[ ?OK ?]?
2.編輯nginx的主配置文件,實(shí)現(xiàn)對10.43.2.134上兩個(gè)tomcat的代理
cat nginx.conf
user ?nginx nginx;
worker_processes ?1;
error_log ?logs/error.log;
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;
? ? upstream tomcatweb {
server 10.43.2.134:8080;
server 10.43.2.134:8081;
? ? }
? ? server {
? ? ? ? listen ? ? ? 80;
? ? ? ? server_name ?localhost;
? ? ? ? #charset koi8-r;
? ? ? ? #access_log ?logs/host.access.log ?main;
? ? ? ? location / {
? ? ? ? ? ? root ? html;
? ? ? ? ? ? index ?index.html index.htm;
? ?proxy_pass http://tomcatweb;
? ? ? ? }
? ? ? ? error_page ? 500 502 503 504 ?/50x.html;
? ? ? ? location = /50x.html {
? ? ? ? ? ? root ? html;
? ? ? ? }
? ? }
}
3.重新加載nginx服務(wù)并驗(yàn)證
重新加載:/etc/init.d/nginx reload?
驗(yàn)證:可以看到nginx已經(jīng)成功代理10.43.2.134上的兩個(gè)tomcat,訪問http://10.43.2.135/t.jsp能夠正常訪問到10.43.2.134:8080/test.jsp和http://10.43.2.134:8081/test.jsp交替出現(xiàn),并且session ?id 刷新一次變化一次。
4.安裝memcache
安裝libevent
注:memcached是基于libevent進(jìn)行事件處理的,所以我們得先安裝libevent
[root@memcache src]# tar xf libevent-2.0.21-stable.tar.gz
[root@memcache src]# cd libevent-2.0.21-stable
[root@memcache libevent-2.0.21-stable]# ./configure --prefix=/usr/local/libevent
[root@memcache libevent-2.0.21-stable]# make && make install
安裝memcached
[root@memcache src]# tar xf memcached-1.4.15.tar.gz
[root@memcache src]# cd memcached-1.4.15
[root@memcache memcached-1.4.15]# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent
[root@memcache memcached-1.4.15]# make && make install
[root@memcache ~]# memcached -d -m 500 -u root -l 192.168.18.201 -c 256 -P /tmp/memcached.pid -vvv ?
查看一下啟動(dòng)端口
root@memcache ~]# netstat -ntulp?
Active Internet connections (only servers) ?
Proto Recv-Q Send-Q Local Address ? ? ? ? ? ? ? Foreign Address ? ? ? ? ? ? State ? ? ? PID/Program name
tcp ? ? ? ?0 ? ? ?0 192.168.18.201:11211 ? ? ? ?0.0.0.0:* ? ? ? ? ? ? ? ? ? LISTEN ? ? ?8086/memcached ??
tcp ? ? ? ?0 ? ? ?0 0.0.0.0:22 ? ? ? ? ? ? ? ? ?0.0.0.0:* ? ? ? ? ? ? ? ? ? LISTEN ? ? ?1026/sshd ? ? ? ?
tcp ? ? ? ?0 ? ? ?0 127.0.0.1:25 ? ? ? ? ? ? ? ?0.0.0.0:* ? ? ? ? ? ? ? ? ? LISTEN ? ? ?1103/master ? ? ?
tcp ? ? ? ?0 ? ? ?0 127.0.0.1:6010 ? ? ? ? ? ? ?0.0.0.0:* ? ? ? ? ? ? ? ? ? LISTEN ? ? ?1137/sshd ? ? ? ?
tcp ? ? ? ?0 ? ? ?0 127.0.0.1:6011 ? ? ? ? ? ? ?0.0.0.0:* ? ? ? ? ? ? ? ? ? LISTEN ? ? ?8044/sshd ? ? ? ?
tcp ? ? ? ?0 ? ? ?0 :::22 ? ? ? ? ? ? ? ? ? ? ? :::* ? ? ? ? ? ? ? ? ? ? ? ?LISTEN ? ? ?1026/sshd ? ? ? ?
tcp ? ? ? ?0 ? ? ?0 ::1:25 ? ? ? ? ? ? ? ? ? ? ?:::* ? ? ? ? ? ? ? ? ? ? ? ?LISTEN ? ? ?1103/master ? ? ?
tcp ? ? ? ?0 ? ? ?0 ::1:6010 ? ? ? ? ? ? ? ? ? ?:::* ? ? ? ? ? ? ? ? ? ? ? ?LISTEN ? ? ?1137/sshd ? ? ? ?
tcp ? ? ? ?0 ? ? ?0 ::1:6011 ? ? ? ? ? ? ? ? ? ?:::* ? ? ? ? ? ? ? ? ? ? ? ?LISTEN ? ? ?8044/sshd ? ? ? ?
udp ? ? ? ?0 ? ? ?0 192.168.18.201:11211 ? ? ? ?0.0.0.0:* ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 8086/memcached
提供SysV的startup腳本
[root@memcache ~]# vim /etc/init.d/memcached?
#!/bin/bash ?
# ?
# Init file for memcached ?
# ?
# chkconfig: - 86 14 ?
# description: Distributed memory caching daemon ?
# ?
# processname: memcached ?
# config: /etc/sysconfig/memcached
. /etc/rc.d/init.d/functions
## Default variables?
PORT="11211" ?
USER="root" ?
MAXCONN="1024" ?
CACHESIZE="64" ?
OPTIONS=""
RETVAL=0?
prog="/usr/local/memcached/bin/memcached" ?
desc="Distributed memory caching" ?
lockfile="/var/lock/subsys/memcached"
start() {?
? ? ? ? echo -n $"Starting $desc (memcached): " ?
? ? ? ? daemon $prog -d -p $PORT -u $USER -c $MAXCONN -m $CACHESIZE ?$OPTIONS ?
? ? ? ? RETVAL=$? ?
? ? ? ? echo ?
? ? ? ? [ $RETVAL -eq 0 ] && touch $lockfile ?
? ? ? ? return $RETVAL ?
}
stop() {?
? ? ? ? echo -n $"Shutting down $desc (memcached): " ?
? ? ? ? killproc $prog ?
? ? ? ? RETVAL=$? ?
? ? ? ? echo ?
? ? ? ? [ $RETVAL -eq 0 ] && rm -f $lockfile ?
? ? ? ? return $RETVAL ?
}
restart() {?
? ? ? ? stop ?
? ? ? ? start ?
}
reload() {?
? ? ? ? echo -n $"Reloading $desc ($prog): " ?
? ? ? ? killproc $prog -HUP ?
? ? ? ? RETVAL=$? ?
? ? ? ? echo ?
? ? ? ? return $RETVAL ?
}
case "$1" in?
? start) ?
? ? ? ? start ?
? ? ? ? ;; ?
? stop) ?
? ? ? ? stop ?
? ? ? ? ;; ?
? restart) ?
? ? ? ? restart ?
? ? ? ? ;; ?
? condrestart) ?
? ? ? ? [ -e $lockfile ] && restart ?
? ? ? ? RETVAL=$? ?
? ? ? ? ;; ? ?
? reload) ?
? ? ? ? reload ?
? ? ? ? ;; ?
? status) ?
? ? ? ? status $prog ?
? ? ? ? RETVAL=$? ?
? ? ? ? ;; ?
? ?*) ?
? ? ? ? echo $"Usage: $0 {start|stop|restart|condrestart|status}" ?
? ? ? ? RETVAL=1 ?
esac
增加執(zhí)行權(quán)限
[root@memcache ~]# chmod +x /etc/init.d/memcached
加入服務(wù)列表并設(shè)置開機(jī)自啟動(dòng)
[root@memcache ~]# chkconfig --add memcached?
[root@memcache ~]# chkconfig memcached on ?
[root@memcache ~]# chkconfig memcached --list ?
memcached ? ? ? ? ?0:關(guān)閉 ? ?1:關(guān)閉 ? ?2:啟用 ? ?3:啟用 ? ?4:啟用 ? ?5:啟用 ? ?6:關(guān)閉
三 、如何實(shí)現(xiàn)memcached session共享
以下操作在每個(gè)tomcat上都需要執(zhí)行
在$CATALINA/lib中添加如下jar包
jar包的下載地址:http://down.51cto.com/data/1634273
2.修改配置文件context.xml
在context.xml中添加如下內(nèi)容
<ManagerclassName="de.javakaffee.web.msm.MemcachedBackupSessionManager"
???????memcachedNodes="n1:10.43.2.135:11211"
??????? sticky="false"
??????? sessionBackupAsync="false"
??????? lockingMode="none"
???????requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
???????transcoderFactoryClass="de.javakaffee.web.msm.JavaSerializationTranscoderFactory"/>
3.重啟tomcat服務(wù)并進(jìn)行驗(yàn)證
再次http://10.43.2.135/t.jsp 可以看到10.43.2.134:8080/t.jsp和http://10.43.2.134:8081/t.jsp交替出現(xiàn),端口變化,session ?id保持不變
轉(zhuǎn)載于:https://blog.51cto.com/5250070/1531488
總結(jié)
以上是生活随笔為你收集整理的nginx+tomcat8+memcached实现session共享具体操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。