前言:利用memcached實(shí)現(xiàn)CAS單點(diǎn)登錄集群部署
參考信息:https://github.com/magro/memcached-session-manager/wiki/SetupAndConfiguration https://code.google.com/archive/p/memcached-session-manager/wikis/SetupAndConfiguration.wiki目前成都創(chuàng)新互聯(lián)公司已為超過千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)絡(luò)空間、網(wǎng)站托管運(yùn)營(yíng)、企業(yè)網(wǎng)站設(shè)計(jì)、青田網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
應(yīng)用名稱 | 端口 | 版本 | 主機(jī)IP |
---|---|---|---|
Nginx | 8088 | 1.0.1 | 192.168.7.2 |
CAS-TomcatA | 8089 | 7.0.35 | 192.168.7.3 |
n1:libevent | 2.1.8 | 192.168.7.3 | |
n1:memcached | 22322 | 1.5.12 | 192.168.7.3 |
n1:magent | 12000 | 1.5.12 | 192.168.7.3 |
CAS-TomcatB | 8089 | 7.0.35 | 192.168.7.4 |
n2:libevent | 2.1.8 | 192.168.7.4 | |
n2:memcached | 22322 | 1.5.12 | 192.168.7.4 |
n2:magent | 12000 | 1.5.12 | 192.168.7.4 |
應(yīng)用架構(gòu)圖:
實(shí)現(xiàn)思路
CAS(Central Authentication Service)單點(diǎn)登錄的實(shí)現(xiàn)依賴了spring-webflow和TGT的ST校驗(yàn),其中spring-webflow使用到了session數(shù)據(jù),TGT對(duì)ST校驗(yàn)使用到了內(nèi)存中的數(shù)據(jù)。將這兩個(gè)功能的數(shù)據(jù)和應(yīng)用剝離,便是我們實(shí)現(xiàn)CAS集群化的關(guān)鍵。TGT對(duì)ST校驗(yàn)使用了java類:TicketRegistry。
主要只有三個(gè)步驟:
1.安裝memcached并配置magent代理
2.session共享的配置
3.TicketRegistry內(nèi)存數(shù)據(jù)剝離的配置
192.168.7.3和192.168.7.4兩個(gè)節(jié)點(diǎn)下都要安裝memcached和magent,安裝步驟只用192.168.7.3舉例。
#安裝libevent-使用root賬號(hào)。
# tar -zxvf libevent-2.1.8-stable.tar.gz
# cd libevent-2.1.8-stable
#./configure -prefix=/usr && make && make install
#chmod -R 755 /usr/include
#chmod命令對(duì)其他用戶授權(quán)了libevent的使用權(quán)限,非root用戶也可以使用libevent。libevent的github地址:https://github.com/libevent/libevent
執(zhí)行結(jié)果截圖:
#root用戶安裝memcached
# tar -zxvf memcached-1.5.12.tar.gz
# cd memcached-1.5.12
# ./configure -with-libevent=/usr --prefix=/usr/local/memcached && make && make install
#chmod -R 755 /usr/local/memcached/
#chmod命令對(duì)其他用戶授權(quán)了memcached的使用權(quán)限,非root用戶也可以使用memcached。
執(zhí)行結(jié)果截圖:
# /usr/local/memcached/bin/memcached -d -m 256 -p 22322 -c 1024 /tmp/memcached.pid
無(wú)日志輸出,且ps -ef | grep memcached 后出現(xiàn)進(jìn)程,則為成功。
如果想查看memcached的日志詳情,可以追加上-vv >> /tmp/memcached.log 2>&1參數(shù)。日志級(jí)別有三個(gè),v最低,vv中等,vvv最詳細(xì)。
啟動(dòng)參數(shù)說明:
-d 選項(xiàng)是啟動(dòng)一個(gè)守護(hù)進(jìn)程。
-m 是分配給Memcache使用的內(nèi)存數(shù)量,單位是MB,默認(rèn)64MB。
-p 是設(shè)置Memcache的TCP監(jiān)聽的端口,最好是1024以上的端口。
-c 選項(xiàng)是大運(yùn)行的并發(fā)連接數(shù),默認(rèn)是1024。
-P 是設(shè)置保存Memcache的pid文件。
# kill -9 pid
# mkdir magent
#mv magent-0.5.tar.gz magent/
#cd magent/
# tar -zxvf magent-0.5.tar.gz
# /sbin/ldconfig
# sed -i "s#CFLAGS = -Wall -O2 -g#CFLAGS =-lrt -Wall -O2 -g#g" Makefile
# sed -i "s#LIBS = -levent#LIBS = -levent -lm#g" Makefile
2.修改ketama.h文件:在第一行加入以下三行代碼
#ifndef SSIZE_MAX
#define SSIZE_MAX 32767
#endif
3.執(zhí)行編譯命令
#make
編譯成功后會(huì)在該目錄下生成magent啟動(dòng)腳本。
4.執(zhí)行啟動(dòng)命令
#./magent -n 51200 -l 192.168.7.3 -p 12000 -s 192.168.7.3:22322 -b 192.168.7.4:22322
192.168.7.4主機(jī)上為:./magent -n 51200 -l 192.168.7.4 -p 12000 -s 192.168.7.4:22322 -b 192.168.7.3:22322。讓兩個(gè)memcached互為主備。
啟動(dòng)參數(shù)說明:
-n 大連接數(shù)。
-l 代理的IP地址,即magent的訪問IP。
-p 代理的端口地址,即magent的訪問端口。
-s 代理的主節(jié)點(diǎn),可以使用多個(gè)-s參數(shù),代理多個(gè)主節(jié)點(diǎn)。
-b 代理的備份節(jié)點(diǎn),可以使用多個(gè)-b參數(shù),代理多個(gè)備份節(jié)點(diǎn)。
具體參數(shù)介紹可以使用./magent -help命令查詢。
5.測(cè)試magent代理
使用“telnet 192.168.7.3 12000”連接到magent代理上,執(zhí)行“set test 0 0 2”的插入數(shù)據(jù)操作,退出12000連接。再次連接“telnet 192.168.7.3 22322”,執(zhí)行“get test”命令,看是否可以輸出數(shù)據(jù);連接“telnet 192.168.7.4 22322”,執(zhí)行“get test”命令,看結(jié)果是否一致。一致則為成功。
如圖:
session共享完全由tomcat來(lái)實(shí)現(xiàn),不必修改web應(yīng)用。本文忽略Nginx實(shí)現(xiàn)負(fù)載的相關(guān)配置。
asm-5.0.3.jar
kryo-3.0.3.jar
kryo-serializers-0.37.jar
memcached-session-manager-1.9.5.jar
memcached-session-manager-tc7-1.9.5.jar
minlog-1.3.0.jar
msm-kryo-serializer-1.9.5.jar
objenesis-2.1.jar
reflectasm-1.10.1.jar
spymemcached-2.12.0.jar
修改tomcat的配置文件
tomcat的conf目錄下的context.xml文件,先把默認(rèn)的context.xml文件內(nèi)容清空,再將以下代碼復(fù)制到context.xml中。其中memcachedNodes是memcached地址,多個(gè)memcached使用需","隔開。
<?xml version='1.0' encoding='utf-8'?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- The contents of this file will be loaded for each web application -->
<Context>
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
-->
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:192.168.7.3:12000,n2:192.168.7.4:12000"
sticky="false"
sessionBackupAsync="false"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|jpeg|bmp|css|js|html|htm)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"/>
</Context>
檢驗(yàn)Session共享
利用如下代碼生成test.jsp,并將jsp放入tomcat的webapps中的工程里,可以訪問到的地方。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<BR>
SessionID:<%=session.getId()%>
<BR>
SessionIP:<%=request.getServerName()%>
<BR>
SessionPort:<%=request.getServerPort()%>
<BR>
**分別**訪問兩個(gè)tomcat,只要session的后綴結(jié)尾類似于"-n1.tomcat1 "即為成功,sessionid是不一致的也是正確的。如下圖
1.在cas的tomcat容器中添加一下jar包
tomcat的路徑:/webapps/cas_sso/WEB-INF/lib/
asm-5.0.3.jar
cas-server-integration-memcached-3.5.1.jar
kryo-3.0.3.jar
minlog-1.3.0.jar
reflectasm-1.10.1.jar
spymemcached-2.12.0.jar
2.修改ticketRegistry.xml文件
文件在Tomact中的路徑:/webapps/cas_sso/WEB-INF/spring-configuration/ticketRegistry.xml
備份ticketRegistry.xml,并創(chuàng)建新的ticketRegistry.xml文件,內(nèi)容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to Jasig under one or more contributor license
agreements. See the NOTICE file distributed with this work
for additional information regarding copyright ownership.
Jasig licenses this file to you under the Apache License,
Version 2.0 (the "License"); you may not use this file
except in compliance with the License. You may obtain a
copy of the License at the following location:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.1.xsd">
<bean id="ticketRegistry" class="org.jasig.cas.ticket.registry.MemCacheTicketRegistry">
<constructor-arg index="0">
<bean class="net.spy.memcached.spring.MemcachedClientFactoryBean"
p:servers="192.168.7.3:12000,192.168.7.4:12000"
p:protocol="BINARY"
p:locatorType="ARRAY_MOD"
p:failureMode="Redistribute"
p:transcoder-ref="serialTranscoder">
<property name="hashAlg">
<util:constant static-field="net.spy.memcached.DefaultHashAlgorithm.FNV1A_64_HASH" />
</property>
</bean>
</constructor-arg>
<constructor-arg index="1" value="36000" />
<constructor-arg index="2" value="2" />
</bean>
<bean id="serialTranscoder" class="net.spy.memcached.transcoders.SerializingTranscoder"
p:compressionThreshold="2048" />
</beans>
參數(shù)說明:
<constructor-arg index="0">:memcached的地址,多個(gè)用逗號(hào)隔開。
<constructor-arg index="1">:TGT超時(shí)時(shí)間(秒)
<constructor-arg index="1">:ST超時(shí)時(shí)間(秒)
3.重啟應(yīng)用測(cè)試
使用Nginx做負(fù)載,代理兩個(gè)CAS服務(wù)端。
1.通過瀏覽器訪問Nginx單點(diǎn)登錄登陸后,通過日志查看訪問到哪個(gè)CAS節(jié)點(diǎn),將該節(jié)點(diǎn)stop。
2.再次通過瀏覽器訪問Nginx,查看是否需要登錄,正常情況是不需要登錄的。成功后標(biāo)識(shí)session共享成功。
3.通過瀏覽器進(jìn)行cas客戶端訪問,如果沒有跳轉(zhuǎn)單點(diǎn),則TGT數(shù)據(jù)保存到memcached共享成功。
后續(xù):memcached的安全
使用memcached應(yīng)用時(shí),只需要能連接上主機(jī)端口就能用,如果放到互聯(lián)網(wǎng)上,這是很不安全的操作。使用memcached時(shí),不要使用默認(rèn)端口,換個(gè)1024以上的端口。針對(duì)于這個(gè)安全一般有三個(gè)解決方案,但每一種都有各自的局限性,可以根據(jù)自己的業(yè)務(wù)來(lái)做選擇。
1.雙網(wǎng)卡:應(yīng)用服務(wù)選擇外網(wǎng)的網(wǎng)卡,memcached使用內(nèi)網(wǎng)的網(wǎng)卡。
2.設(shè)置防火墻:使用主機(jī)的iptables進(jìn)行白名單放行,阻止非信任的訪問請(qǐng)求。
3.memcached開啟SASL認(rèn)證。1.4.3版本后支持此功能。
附件信息
鏈接: https://pan.baidu.com/s/1aihl_abcNguh9QVNSAWwNA 提取碼: 7r1r
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
當(dāng)前題目:利用memcached實(shí)現(xiàn)CAS單點(diǎn)登錄集群部署-創(chuàng)新互聯(lián)
當(dāng)前網(wǎng)址:http://www.chinadenli.net/article36/ddcopg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)、云服務(wù)器、域名注冊(cè)、小程序開發(fā)、網(wǎng)站建設(shè)、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)