Nginx + Shiro + Redis 實現負載均衡集群(成績報告查詢係統升級篇)
寫在開始
上一篇講到使用Ehcache實現分布式緩存,盡管其直接操作JVM內存,速度快,效率高,但是緩存同步麻煩,分布式集群配置不方便,如果應用服務器重啟會丟失緩存數據。
下麵來分析一下Redis做係統session緩存實現。
Redis介紹
Redis是一個key-value存儲係統。和Memcached類似, 它的出現,很大程度補償了memcached這類key/value存儲的不足,在部 分場合可以對關係數據庫起到很好的補充作用。它支持存儲的value類型相對更多,包括string(字符串)、list(鏈表)、set(集合)、zset(sorted set --有序集合)和hash(哈希類型)。
Redis還支持主從同步。數據可以從主服務器向任意數量的從服務器上同步,從服務器可以關聯其他從服務器的主服務器。
Redis是通過socket訪問到緩存服務,效率比ecache低,但比數據庫要快很多,處理集群和分布式緩存方便,有成熟的方案。
Redis安裝
https://blog.52itstyle.com/archives/590/
項目架構
Spring MVC4 + Shiro-1.3.2 + Redis3.2.8
運行環境
Nginx + Tomcat7(3台) + JDK1.7
項目架構圖
項目實現
Redis類庫 jedis-2.9.0依賴commons-pool2-2.4.1
redis.properties配置:
#============================#
#===== redis sttings ====#
#============================#
redis.host=127.0.0.1
redis.port=6379
redis.password=123456
#單位秒
redis.expire=1800
redis.timeout=2000
redis.usepool=true
redis.database=1
重寫 AbstractSessionDAO 和 CacheManager
applicationContext-redis.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:context="https://www.springframework.org/schema/context"
xmlns:aop="https://www.springframework.org/schema/aop" xmlns:tx="https://www.springframework.org/schema/tx"
xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-4.0.xsd
https://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop-4.0.xsd
https://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx-4.0.xsd
https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context-4.0.xsd">
<description>Shiro redisManager(集群環境下使用此配置,單點也可以使用)</description>
<!-- shiro redisManager -->
<bean >
<property name="host" value="${redis.host}" />
<property name="password" value="${redis.password}"/>
<property name="port" value="${redis.port}" />
<property name="expire" value="${redis.expire}" />
<property name="timeout" value="${redis.timeout}"/>
</bean>
<!-- redisSessionDAO -->
<bean >
<property name="redisManager" ref="redisManager" />
</bean>
<!-- cacheManager -->
<bean >
<property name="redisManager" ref="redisManager" />
</bean>
</beans>
聲明:本文內容大體流程僅供參考,有些並未涉及到具體代碼實現。
最後更新:2017-04-01 17:13:52