利用memcached实现CAS单点登录集群部署
前言:利用memcached實現(xiàn)CAS單點登錄集群部署
- 負(fù)載均衡:
將接口請求的有狀態(tài)性變成無狀態(tài)性。是我們在實現(xiàn)負(fù)載均衡時必要要解決的問題。以應(yīng)用接口的session狀態(tài)為例,一般解決方法都是將session數(shù)據(jù)和應(yīng)用進行剝離,session數(shù)據(jù)統(tǒng)一保存到同一個數(shù)據(jù)源中,數(shù)據(jù)源就是保存數(shù)據(jù)的地方,我們常使用的MySQL數(shù)據(jù)庫就是一個數(shù)據(jù)源。通常為了性能更高,數(shù)據(jù)源一般采用內(nèi)存數(shù)據(jù)庫(memcached、redis)。每當(dāng)用戶訪問應(yīng)用集群時,該請求被隨機分配到任何一個節(jié)點,節(jié)點訪問同一個數(shù)據(jù)源取session信息,保證了多個節(jié)點獲取到的session狀態(tài)是一致的。 - 軟件版本:
| 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)圖:
實現(xiàn)思路
CAS(Central Authentication Service)單點登錄的實現(xiàn)依賴了spring-webflow和TGT的ST校驗,其中spring-webflow使用到了session數(shù)據(jù),TGT對ST校驗使用到了內(nèi)存中的數(shù)據(jù)。將這兩個功能的數(shù)據(jù)和應(yīng)用剝離,便是我們實現(xiàn)CAS集群化的關(guān)鍵。TGT對ST校驗使用了java類:TicketRegistry。
- spring-webflow:是基于Spring MVC,實現(xiàn)應(yīng)用程序的"流程化",用于指導(dǎo)業(yè)務(wù)邏輯按照固定的流程進行。對應(yīng)的數(shù)據(jù)時保存到server的session中。
- TicketRegistry:CAS票據(jù),存儲在TicketRegistry中,而TicketRegistry是存儲在內(nèi)存中的,CAS為了擴展,提供了支持TicketRegistry分布式的接口:org.jasig.cas.ticket.registry.AbstractDistributedTicketRegistry。通過實現(xiàn)這個接口,將TGT等數(shù)據(jù)放到內(nèi)存數(shù)據(jù)庫中。
步驟
主要只有三個步驟:1.安裝memcached并配置magent代理2.session共享的配置3.TicketRegistry內(nèi)存數(shù)據(jù)剝離的配置一:安裝memcached并配置magent代理
192.168.7.3和192.168.7.4兩個節(jié)點下都要安裝memcached和magent,安裝步驟只用192.168.7.3舉例。- 簡介
安裝memcached時,需要先安裝依賴庫-libevent。libevent是一個強大的跨平臺事件通知庫,在 memcached 中 libevent 被用來處理網(wǎng)絡(luò)事件(連接請求,讀和寫) 或者被用來實現(xiàn)定時器。 使用 libevent 需要包含頭文件 event.h, 并且在 GCC 鏈接時需要使用選項 -levent。 - 下載地址
libevent:https://libevent.org/
memcached:http://memcached.org/downloads
本文末提供百度網(wǎng)盤的下載鏈接。 - 安裝libevent和memcached
創(chuàng)建一個新的文件夾,將libevent和memcached放入其中,并解壓,結(jié)果如下圖。- libevent安裝命令如下: #安裝libevent-使用root賬號。
# 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命令對其他用戶授權(quán)了libevent的使用權(quán)限,非root用戶也可以使用libevent。libevent的github地址:https://github.com/libevent/libevent
執(zhí)行結(jié)果截圖:
- memcached安裝命令如下: #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命令對其他用戶授權(quán)了memcached的使用權(quán)限,非root用戶也可以使用memcached。
執(zhí)行結(jié)果截圖:
- 啟動命令,可使用非root用戶啟動: # /usr/local/memcached/bin/memcached -d -m 256 -p 22322 -c 1024 /tmp/memcached.pid
無日志輸出,且ps -ef | grep memcached 后出現(xiàn)進程,則為成功。
啟動參數(shù)說明:-d 選項是啟動一個守護進程。-m 是分配給Memcache使用的內(nèi)存數(shù)量,單位是MB,默認(rèn)64MB。-p 是設(shè)置Memcache的TCP監(jiān)聽的端口,最好是1024以上的端口。-c 選項是最大運行的并發(fā)連接數(shù),默認(rèn)是1024。-P 是設(shè)置保存Memcache的pid文件。
- 檢查memcached的運行狀態(tài):
首先用telnet 127.0.0.1 22322這樣的命令連接上memcache,然后直接輸入stats就可以得到當(dāng)前memcache的狀態(tài)。退出使用"ctrl+]",再輸入quit,進行退出。參數(shù)解讀參考:https://blog.csdn.net/andy_dou/article/details/84811715 - 停止命令: # kill -9 pid
- 檢查memcached的運行狀態(tài):
- magent安裝:
1.執(zhí)行以下命令修改文件 # 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" Makefile2.修改ketama.h文件:在第一行加入以下三行代碼
#ifndef SSIZE_MAX
#define SSIZE_MAX 32767
#endif
3.執(zhí)行編譯命令
#make
編譯成功后會在該目錄下生成magent啟動腳本。
4.執(zhí)行啟動命令
#./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主機上為:./magent -n 51200 -l 192.168.7.4 -p 12000 -s 192.168.7.4:22322 -b 192.168.7.3:22322。讓兩個memcached互為主備。
啟動參數(shù)說明:
-n 最大連接數(shù)。
-l 代理的IP地址,即magent的訪問IP。
-p 代理的端口地址,即magent的訪問端口。
-s 代理的主節(jié)點,可以使用多個-s參數(shù),代理多個主節(jié)點。
-b 代理的備份節(jié)點,可以使用多個-b參數(shù),代理多個備份節(jié)點。
具體參數(shù)介紹可以使用./magent -help命令查詢。
5.測試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é)果是否一致。一致則為成功。
如圖:
- libevent安裝命令如下: #安裝libevent-使用root賬號。
# 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命令對其他用戶授權(quán)了libevent的使用權(quán)限,非root用戶也可以使用libevent。libevent的github地址:https://github.com/libevent/libevent
二:session共享的配置
session共享完全由tomcat來實現(xiàn),不必修改web應(yīng)用。本文忽略Nginx實現(xiàn)負(fù)載的相關(guān)配置。- 上傳session共享所需的jar包
將以下jar包放到tomcat的lib下。本文是采用kryo序列化來實現(xiàn)session的序列化,不同的序列化實現(xiàn)方式,只是引用的jar包不同,方法一致。據(jù)說kryo效率比較高。
相關(guān)jar包如下文: asm-5.0.3.jarkryo-3.0.3.jarkryo-serializers-0.37.jarmemcached-session-manager-1.9.5.jarmemcached-session-manager-tc7-1.9.5.jarminlog-1.3.0.jarmsm-kryo-serializer-1.9.5.jarobjenesis-2.1.jarreflectasm-1.10.1.jarspymemcached-2.12.0.jar -
修改tomcat的配置文件
<?xml version='1.0' encoding='utf-8'?><!--Licensed to the Apache Software Foundation (ASF) under one or morecontributor license agreements. See the NOTICE file distributed withthis 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 withthe License. You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed 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 andlimitations 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 eventson 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>
tomcat的conf目錄下的context.xml文件,先把默認(rèn)的context.xml文件內(nèi)容清空,再將以下代碼復(fù)制到context.xml中。其中memcachedNodes是memcached地址,多個memcached使用需","隔開。 - 修改tomcat的conf/server.xml文件
修改server.xml中Engine標(biāo)簽,在其標(biāo)簽中添加jvmRoute="tomcat1"屬性,用于區(qū)分多個tomcat應(yīng)用 -
檢驗Session共享
<%@ 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> **分別**訪問兩個tomcat,只要session的后綴結(jié)尾類似于"-n1.tomcat1 "即為成功,sessionid是不一致的也是正確的。如下圖
利用如下代碼生成test.jsp,并將jsp放入tomcat的webapps中的工程里,可以訪問到的地方。
三:將TicketRegistry內(nèi)存數(shù)據(jù)寫入memcached
1.在cas的tomcat容器中添加一下jar包tomcat的路徑:/webapps/cas_sso/WEB-INF/lib/ asm-5.0.3.jarcas-server-integration-memcached-3.5.1.jarkryo-3.0.3.jarminlog-1.3.0.jarreflectasm-1.10.1.jarspymemcached-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 licenseagreements. See the NOTICE file distributed with this workfor 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 fileexcept in compliance with the License. You may obtain acopy of the License at the following location:http://www.apache.org/licenses/LICENSE-2.0Unless 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 ANYKIND, either express or implied. See the License for thespecific language governing permissions and limitationsunder 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.xsdhttp://www.springframework.org/schema/utilhttp://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的地址,多個用逗號隔開。<constructor-arg index="1">:TGT超時時間(秒)<constructor-arg index="1">:ST超時時間(秒)3.重啟應(yīng)用測試
使用Nginx做負(fù)載,代理兩個CAS服務(wù)端。
1.通過瀏覽器訪問Nginx單點登錄登陸后,通過日志查看訪問到哪個CAS節(jié)點,將該節(jié)點stop。
2.再次通過瀏覽器訪問Nginx,查看是否需要登錄,正常情況是不需要登錄的。成功后標(biāo)識session共享成功。
3.通過瀏覽器進行cas客戶端訪問,如果沒有跳轉(zhuǎn)單點,則TGT數(shù)據(jù)保存到memcached共享成功。
后續(xù):memcached的安全
使用memcached應(yīng)用時,只需要能連接上主機端口就能用,如果放到互聯(lián)網(wǎng)上,這是很不安全的操作。使用memcached時,不要使用默認(rèn)端口,換個1024以上的端口。針對于這個安全一般有三個解決方案,但每一種都有各自的局限性,可以根據(jù)自己的業(yè)務(wù)來做選擇。
1.雙網(wǎng)卡:應(yīng)用服務(wù)選擇外網(wǎng)的網(wǎng)卡,memcached使用內(nèi)網(wǎng)的網(wǎng)卡。
2.設(shè)置防火墻:使用主機的iptables進行白名單放行,阻止非信任的訪問請求。
3.memcached開啟SASL認(rèn)證。1.4.3版本后支持此功能。
附件信息
鏈接: https://pan.baidu.com/s/1aihl_abcNguh9QVNSAWwNA 提取碼: 7r1r
轉(zhuǎn)載于:https://blog.51cto.com/8595476/2387647
總結(jié)
以上是生活随笔為你收集整理的利用memcached实现CAS单点登录集群部署的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 云栖社区云栖号(团队博客)攻略【2018
- 下一篇: oracle的have,Does ora