1. 程式人生 > >session共享機制(nginx+tomcat+memcached)

session共享機制(nginx+tomcat+memcached)

jdk環境的配置、tomcat、session共享機制

一、配置jdk環境java的編譯環境------server2和server3同時配置

jdk是JAVA的開發編譯環境是java語言的軟件開發工具包主要用於移動設備的嵌入式設備上的java應用程序

jdk的安裝基礎過程

1將jdk的包解壓在指定路徑 使用-C來指定路徑

2進入指定的路徑給jdk解壓後的目錄做個軟連接

3編輯系統的環境變量使得java命令可以使用更改後讓文件生效讓環境變量文件即時生效使用source命令

4編輯java測試文件編譯執行

1. get jdk-7u79-linux-x64.tar.gz -C /usr/local/(from 老吳)

tar zxf jdk-7u79-linux-x64.tar.gz -C /usr/local/

2. ln -s /usr/local/jdk1.7.0_79/ /usr/local/java##軟鏈接方便版本升級

3. vim /etc/profile##編輯系統的環境變量全局變量所有用戶都可以用而~/.bash_profile是局部變量只針對某些用戶加載才可以使用

在配置文件末端加上

export JAVA_HOME=/usr/local/java

export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib

export PATH=$PATH:$JAVA_HOME/bin

4. source /etc/profile##讓更改在當前位置生效

查看是否生效

[[email protected]

/* */ local]# echo $PATH

/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/java/bin

[[email protected] local]# echo $CLASSPATH

.:/usr/local/java/lib:/usr/local/java/jre/lib

5.寫java證明環境配置是ok的

[[email protected] ~]# vim test.java

public class test {

public static void main(String[] arge) {

System.out.println("Hello World!");

}

}

[[email protected] ~]# javac test.java

[[email protected] ~]# java test

Hello World!

二、安裝tomcatjsp頁面的解釋------server2和server3同時配置

Tomcat服務器是一個免費的開放源代碼的web應用服務器其屬於輕量級應用服務器很適合開發和調試jsp程序

Tomcat是 Apache服務器的擴展但運行時它是獨立運行的所以當tomcat運行時它實際上作為一個與Apache獨立的進程單獨運行的

Tomcat 的安裝很簡單其基本步驟如下

1從官網 down 一個安裝包 例如apache-tomcat-7.0.37.tar.gz

2解壓 也可以在解壓的時候加上你要解壓到的目錄等參數

3進入tomcat的bin目錄開啟服務就好

4測試可以編寫一個測試頁

1.get apache-tomcat-7.0.37.tar.gz(from 老吳)

tar zxf apache-tomcat-7.0.37.tar.gz -C /usr/local/

2. ln -s /usr/local/apache-tomcat-7.0.37/ /usr/local/tomcat

3. cd /usr/local/tomcat/bin/

./startup.sh##開啟tomcat服務

4. netstat -antlp #此時8080端口打開

tcp 0 0 :::8080 :::* LISTEN 1122/java

5.cd /usr/local/tomcat/webapps/ROOT

vim test.jsp

the time is: <%=new java.util.Date() %>

server3中

tar zxf apache-tomcat-7.0.37.tar.gz -C /usr/local/

ln -s /usr/local/apache-tomcat-7.0.37/ /usr/local/tomcat

cd tomcat/bin/

./startup.sh

netstat -antlp

測試在網頁搜索 172.25.78.2:8080/test.jsp

技術分享


三、session共享機制tomcat將緩存信息備份存在memcache中

1.tomcat和nginx的使用

[[email protected] conf]# cd /usr/local/lnmp/nginx/conf/

[[email protected] conf]# /etc/init.d/php-fpm start

Starting php-fpm done

[[email protected] conf]# nginx##開啟服務

[[email protected] conf]# vim nginx.conf

[[email protected] conf]# nginx -t

nginx: the configuration file /usr/local/lnmp/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/lnmp/nginx/conf/nginx.conf test is successful

[[email protected] conf]# nginx -s reload

(18)

http {

upstream westos{

ip_hash;##開啟ip_hash固定一個主機訪問的後端服務器

server 172.25.39.2:8080;

server 172.25.39.3:8080;

}

(90) location ~ \.jsp$ {

proxy_pass http://westos; ##所有以.jsp結尾的都轉換

}

[[email protected] conf]# cat nginx.conf-----所有沒有大規模註釋的文件內容如下

#user nobody;

worker_processes 2;

worker_cpu_affinity 01 10;

#error_log logs/error.log;

#error_log logs/error.log notice;

#error_log logs/error.log info;

#pid logs/nginx.pid;

events {

worker_connections 65535;

}

http {

upstream westos{

ip_hash;

server 172.25.78.2:8080;

server 172.25.78.3:8080;

}

include mime.types;

default_type application/octet-stream;

sendfile on;

#tcp_nopush on;

#keepalive_timeout 0;

keepalive_timeout 65;

#gzip on;

server {

listen 80;

server_name localhost;

location / {

root html;

index index.php index.html index.htm;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

location ~ \.php$ {

root html;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;

include fastcgi.conf;

}

location ~ \.jsp$ {

proxy_pass http://westos;

}

}

server {

listen 443 ssl;

server_name localhost;

ssl_certificate cert.pem;

ssl_certificate_key cert.pem;

ssl_session_cache shared:SSL:1m;

ssl_session_timeout 5m;

ssl_ciphers HIGH:!aNULL:!MD5;

ssl_prefer_server_ciphers on;

location / {

root html;

index index.html index.htm;

}

}

server {

listen 80;

server_name www.westos.org;

location / {

proxy_pass http://westos;

}

}

}

測試頁碼的編寫

[[email protected] ~]# cd /usr/local/tomcat/webapps/ROOT/

[[email protected] ROOT]# vim test.jsp

內容從老吳文檔復制

<%@ page contentType="text/html; charset=GBK" %>

<%@ page import="java.util.*" %>

<html><head><title>Cluster App Test</title></head>

<body>

Server Info:

<%

out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>

<%

out.println("<br> ID " + session.getId()+"<br>");

String dataName = request.getParameter("dataName");

if (dataName != null && dataName.length() > 0) {

String dataValue = request.getParameter("dataValue");

session.setAttribute(dataName, dataValue);

}

out.print("<b>Session list</b>");

Enumeration e = session.getAttributeNames();

while (e.hasMoreElements()) {

String name = (String)e.nextElement();

String value = session.getAttribute(name).toString();

out.println( name + " = " + value+"<br>");

Enumeration e = session.getAttributeNames();

while (e.hasMoreElements()) {

String name = (String)e.nextElement();

String value = session.getAttribute(name).toString();

out.println( name + " = " + value+"<br>");

System.out.println( name + " = " + value);

}

%>

<form action="test.jsp" method="POST">

name:<input type=text size=20 name="dataName">

<br>

key:<input type=text size=20 name="dataValue">

<br>

<input type=submit>

</form>

</body>

</html>

scp test.jsp 172.25.39.3:/root/tomcat/webapps/ROOT##給3

中復制相同的頁

###nginx+tomcat+memcache####共享的實現

1.

get {---from 老吳

asm-3.2.jar

kryo-1.04.jar

kryo-serializers-0.10.jar

memcached-session-manager-1.6.3.jar

memcached-session-manager-tc6-1.6.3.jar

memcached-session-manager-tc7-1.6.3.jar

minlog-1.2.jar

msm-kryo-serializer-1.6.3.jar

reflectasm-1.01.jar

spymemcached-2.7.3.jar }

因為使用的tomcat是7的所以把6的刪掉

2.把這些東西放到/usr/local/tomcat/lib

3.打開server2和server3的memc

/etc/init.d/memecached start

4. vim /usr/local/tomcat/conf/context.xml

<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"

memcachedNodes="n1:172.25.39.2:11211,n2:172.25.39.3:11211"

failoverNodes="n1"##在server2和server3上分別弄n1和n2如果tomcat1 故障了那麽就n1取緩存數據否則就是取自己的緩存---n1就相當於上課講解的memc2

requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"

transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"

/>

<T1><T2>

. \ / .

. X .

. / \ .

<M1><M2>

Tomcat-1 (T1) 將 session 存儲在 memcached-2 (T2)上。只有當 M2 不可用時,T1 才將 session 存儲在 memcached-1 上(M1 是 T1 failoverNode)。使用這種配置的好處是,當 T1 和 M1 同時崩潰時也不會丟失 session 會話,避免單點故障。

5. 打開tomcat服務

/usr/local/tomcat/bin/startup.sh

##關閉服務

/usr/local/tomcat/bin/shutdown.sh

cd /usr/local/tomcat

tail -f logs/catalina.out 試驗服務打開是否成功

(INFO: MemcachedSessionService finished initialization, sticky true, operation timeout 1000, with node ids [n1] and failover node ids [n2])證明成功

6. yum install telnet -y兩臺都做-----telnet作用監控

測試

172.25.78.1/test.jsp

telnet localhost 11211遠程監控本地的11211端口 監控存儲

[[email protected] lib]# telnet localhost 11211

Trying ::1...

Connected to localhost.

Escape character is ‘^]‘.

get 48844C8F70F022944863813004730A7E-n2

VALUE 48844C8F70F022944863813004730A7E-n2 2048 136

W]hB8]h01]h)]h)#48844C8F70F022944863813004730A7E-n2user2456user3789user8888usr1123

END

技術分享

集群-----------------保證時間同步

ricci是集群的管理工具

一個客戶端兩個集群節點

*********保證兩個server純凈***********

1.先重新配置yum把這些全部配置才能滿足集群的需要

yum裏面的文件

[HighAvailability]高可用

name=HighAvailability

baseurl=http://172.25.39.250/rhel6.5/HighAvailability

gppcheck=0

[LoadBalancer]負載均衡

name=LoadBalancer

baseurl=http://172.25.39.250/rhel6.5/LoadBalancer

gpgcheck=0

[ResilientStorage]存儲

name=ResilientStorage

baseurl=http://172.25.39.250/rhel6.5/ResilientStorage

gpgcheck=0

[ScalableFileSystem]大文件系統支持

name=ScalableFileSystem

baseurl=http://172.25.39.250/rhel6.5/ScalableFileSystem

gpgcheck=0

yum repolist##更新yum

*****************************************************

*repo id repo name status

*HighAvailability HighAvailability 56

*LoadBalancer LoadBalancer 4

*ResilientStorage ResilientStorage 62

*ScalableFileSystem ScalableFileSystem 7

*rhel6.5 Red Hat Enterprise Linux 3,690

*repolist: 3,819

*****************************************************

2.安裝 yum install -y ricci

passwd ricci在企業六裏面必須要給密碼

3. /etc/init.d/ricci start##開啟服務

chkconfig ricci on##開機自啟動

4.[[email protected] ~]# clustat##查看集群的狀態

Cluster Status for hahaha @ Sat Jul 22 17:02:21 2017

Member Status: Quorate

Member Name ID Status

------ ---- ---- ------

server1 1 Online

server4 2 Online, Local

5.相同配置server1

a.vim /etc/yum.repos.d/rhel-source.repo

yum repolist

b. yum install -y ricci

passwd ricci

/etc/init.d/ricci start

chkconfig ricci on

c. yum install -y luci##圖形操作界面-------測試的那個服務器安裝就行(會發現全是用python寫的)

/etc/init.d/luci start

chkconfig luci on##開機自啟動

d. clustat

在瀏覽器

https://172.25.39.1:8084----在哪個主機安裝了luci用哪個主機測試

技術分享

本文出自 “AELY木” 博客,請務必保留此出處http://12768057.blog.51cto.com/12758057/1950223

session共享機制(nginx+tomcat+memcached)