前沿知識(shí)點(diǎn):
-
nginx負(fù)責(zé)負(fù)載均衡(反向代理)
-
msm(memcached session manager)負(fù)責(zé)緩存會(huì)話信息,從而實(shí)現(xiàn)會(huì)話保持
所需包:
-
nginx和memcached采用最新穩(wěn)定版
-
tomcat版本需要與msm版本一致,這里采用tomcat7
-
msm包共計(jì)9個(gè)包,包名具體信息查看附件,msm的所有包放到$CATALINA_HOME/lib
配置過(guò)程:
nginx的配置信息如下-->
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
...
?
http {
?
????
...
?
????
upstream tomcat {
?
????????
server node1:8080;
?
????????
server node2:8080;
?
#這里具體使用什么算法,暫定,不過(guò)我覺(jué)得ip_hash不好,會(huì)造成負(fù)載不均衡
?
????
}
?
????
server {
?
????
...
?
????????
location ~* ^
/testapp
{
?
????????????
proxy_pass http:
//tomcat
;
?
????????????
proxy_redirect off;
?
????????????
proxy_set_header X-real-ip $remote_addr;
?
????????????
proxy_set_header X-forwarded-
for
$proxy_add_x_forwarded_for;
?
????????????
proxy_set_header Host $host;
?
????????
}
?
????
...
?
????
}
?
}
|
?
tomcat的配置信息如下-->
首先修改server.xml,在默認(rèn)的host flag中添加context
1
2
3
4
5
6
7
8
9
10
11
12
13
|
...
?
????
<
host
>
?
????
...
?
????
<
context
path
=
"/testapp"
docbase
=
"testapp/"
/>
?
????
...
?
????
</
host
>
?
...
|
?
其次修改context.xml,在默認(rèn)的context flag中添加manager
其中粘性session方式如下: ?Sticky? 模式 : tomcat 本地容器 session? 為 主 session , ?memcached 為備 session 。 Request 請(qǐng)求到來(lái)時(shí), ?判斷tomcat容器是否發(fā)生變化,若變化(即原tomcat down掉),則 從 memcached 加載備 session 到 tomcat2 ,響應(yīng)給客戶端,請(qǐng)求結(jié)束后,重置session_id,并更新到memcached 。 ?
?
1
2
3
4
5
6
7
8
9
|
<
Context
>
??
...
??
<
Manager
className
=
"de.javakaffee.web.msm.MemcachedBackupSessionManager"
????
memcachedNodes
=
"n1:host1.yourdomain.com:11211,n2:host2.yourdomain.com:11211"
???
failoverNodes
=
"n1"
????
requestUriIgnorePattern
=
".*\.(ico|png|gif|jpg|css|js)$"
????
transcoderFactoryClass
=
"de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
????
/>
</
Context
>
|
?
非粘性session如下: ?Non-Sticky 模式 : tomcat session? 為 ? 中轉(zhuǎn) session , ?memcached1? 為主, memcached 2? 為備 session 。 Request 請(qǐng)求到來(lái)時(shí),從 memcached 2 加載備 ?session? 到 ?tomcat ,(另外,若 只有一個(gè) memcached 節(jié)點(diǎn),或者 memcached2? 出錯(cuò)時(shí),且 tomcat本地 容器 中還沒(méi)有 session, 則從 memcached1 加載主 ?session? 到 ?tomcat ), Request 請(qǐng)求結(jié)束時(shí),將 tomcat session 更新至 ? 主 memcached1 和備 memcached2 ,并且清除 tomcat session? 。以達(dá)到主備同步之目的,如下是non-sticky模式的響應(yīng)流程圖:(圖片來(lái)源網(wǎng)絡(luò))。
此模式下,基于session的驗(yàn)證碼將無(wú)法使用,因?yàn)榇四J较?第一次session是用本地,然后存放到mem1和mem2中,之后客戶再次請(qǐng)求,則路由到了另外一臺(tái)tomcat上,又因?yàn)槭峭粋€(gè)session_id,所以使用的是mem1中,但是mem1中確是舊的session.(但是驗(yàn)證碼要求每一次都不一樣...)
?
?
1
2
3
4
5
6
7
8
9
10
11
|
<
Context
>
??
...
?
<
ManagerclassName
=
"de.javakaffee.web.msm.MemcachedBackupSessionManager"
???
memcachedNodes
=
"n1:host1.yourdomain.com:11211,n2:host2.yourdomain.com:11211"
???
sticky
=
"false"
???
sessionBackupAsync
=
"false"
???
lockingMode
=
"uriPattern:/path1|/path2"
???
requestUriIgnorePattern
=
".*\.(ico|png|gif|jpg|css|js)$"
???
transcoderFactoryClass
=
"de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
???
/>
</
Context
>
|
?
keepalived配置信息如下-->這里只貼出主的,從的就不貼了
?
?
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
! Configuration File
for
keepalived
?
global_defs {
???
notification_email {
?????
acassen@firewall.loc
?????
failover@firewall.loc
?????
sysadmin@firewall.loc
???
}
???
notification_email_from Alexandre.Cassen@firewall.loc
???
smtp_server 127.0.0.1
???
smtp_connect_timeout 30
???
router_id LVS_DEVEL
}
#<Spinestars
vrrp_script chk_keepalived_down {
????
script
"[ -f /var/run/keepaliveddown ] && exit 1 || exit 0"
????
interval 2
????
weight -2
}
#nginx_check_script
vrrp_script chk_nginx {
????
script
"killall -0 nginx && exit 0 || exit 1"
????
interval 2
????
weight -2
}
#/Spinestars>
vrrp_instance VI_1 {
????
state MASTER
????
interface eth1
????
virtual_router_id 20
????
mcast_src_ip 192.168.100.1
????
priority 100
????
advert_int 1
????
authentication {
????????
auth_type PASS
????????
auth_pass 1111
????
}
????
virtual_ipaddress {
????????
10.88.100.2
????
}
????
track_script {
????
chk_nginx
????
chk_keepalived_down
????
}
}
|
?
nginx動(dòng)靜分離配置:
?
...
#<Spinestars
upstream?tomcat_servers?{
????????server?node1
:
8080
;
????????server?node2
:
8080
;
}
server?{
????????listen??????
*
:
80
;
????????server_name??
test
.shop.com;
????????root
/
var
/
www
/
shop;
????????index?index.html?index.jsp?index.htm;
#/Spinestars>?
#<Spinestars
??????? location?
~
*
?\.(html
|
jpg
|
png
|
jpeg
|
css
|
gif
|
ico)$?{
? ? ? ? ? ?root?
/
var
/
www;
? ? ? ??}
? ? ? ? location?
~
*
?\.(js
|
jhtml)$?{
? ? ? ? ? ? ? ??proxy_pass?http
:
/
/
tomcat_servers;
? ? ? ? ? ? ? ??proxy_redirect?off;
? ? ? ? ? ? ? ? proxy_set_header?X
-
Forwarded
-
For?$proxy_add_x_forwarded_for;
? ? ? ? ??? ? ??proxy_set_header?Host?$host;
? ? ? ? ? ? ? ??proxy_set_header?X
-
Real
-
IP?$remote_addr;
? ? ? ? }
? ? ? ??
if
?(?$host?
=
?
'test.shop.com'
?){
? ? ? ? ? ? ? ? rewrite?
^
/
$?
http
:
/
/
test
.shop.com
/
shop
?permanent;
? ? ? ??}
? ??
? ? #以下location是測(cè)試用的
? ? ? ? location?
~
*
?
^
/
testapp?{
? ? ? ? ? ? ? ??proxy_pass?http
:
/
/
tomcat_servers;
????????????????proxy_redirect?off;
????????????????proxy_set_header?X
-
Forwarded
-
For?$proxy_add_x_forwarded_for;
????????????????proxy_set_header?Host?$host;
????????????????proxy_set_header?X
-
Real
-
IP?$remote_addr;
? ? ? ??}
#/Spinestars>
...
}
?
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
