基於redis的cas集羣配置(轉)

一、cas ticket統一存儲html

  作cas集羣首先須要將ticket拿出來,作統一存儲,以便每一個節點訪問到的數據一致。官方提供基於memcached的方案,因爲項目須要,須要作計入redis,根據官方例子改了一個基於redis版本的。git

複製代碼
public class RedisTicketRegistry extends AbstractDistributedTicketRegistry{
    @NotNull
    private final RedisTemplate<String,Object> reidsTemplate;

    /**
     * TGT cache entry timeout in seconds.
     */
    @Min(0)
    private final int tgtTimeout;

    /**
     * ST cache entry timeout in seconds.
     */
    @Min(0)
    private final int stTimeout;
    
    public RedisTicketRegistry(RedisTemplate<String,Object> reidsTemplate,int tgtTimeout,int stTimeout){
        this.reidsTemplate=reidsTemplate;
        this.tgtTimeout=tgtTimeout;
        this.stTimeout=stTimeout;
    }
    @Override
    public void addTicket(Ticket ticket) {
        logger.debug("Adding ticket {}", ticket);
        try {
            reidsTemplate.opsForValue().set(ticket.getId(),ticket, getTimeout(ticket), TimeUnit.SECONDS);
        } catch (final Exception e) {
            logger.error("Failed adding {}", ticket, e);
        }
    }

    @Override
    public Ticket getTicket(String ticketId) {
         try {
                final Ticket t = (Ticket) this.reidsTemplate.opsForValue().get(ticketId);
                if (t != null) {
                    return getProxiedTicketInstance(t);
                }
            } catch (final Exception e) {
                logger.error("Failed fetching {} ", ticketId, e);
            }
            return null;
    }

    @Override
    public boolean deleteTicket(String ticketId) {
         logger.debug("Deleting ticket {}", ticketId);
            try {
                 this.reidsTemplate.delete(ticketId);
                 return true;
            } catch (final Exception e) {
                logger.error("Failed deleting {}", ticketId, e);
            }
            return false;
    }

    @Override
    public Collection<Ticket> getTickets() {
         throw new UnsupportedOperationException("GetTickets not supported.");
    }

    @Override
    protected void updateTicket(Ticket ticket) {
     logger.debug("Updating ticket {}", ticket);
        try {
              this.reidsTemplate.delete(ticket.getId());
              reidsTemplate.opsForValue().set(ticket.getId(),ticket, getTimeout(ticket), TimeUnit.SECONDS);
        } catch (final Exception e) {
            logger.error("Failed updating {}", ticket, e);
        }
    }

    @Override
    protected boolean needsCallback() {
        // TODO Auto-generated method stub
        return true;
    }
   private int getTimeout(final Ticket t) {
        if (t instanceof TicketGrantingTicket) {
            return this.tgtTimeout;
        } else if (t instanceof ServiceTicket) {
            return this.stTimeout;
        }
        throw new IllegalArgumentException("Invalid ticket type");
    }
}
複製代碼

  對應的ticketRegistry.xml的配置以下:github

複製代碼
 <bean id="ticketRegistry" class="com.test.cas.ticket.registry.RedisTicketRegistry">
        <constructor-arg index="0" ref="redisTemplate" />
        <constructor-arg index="1" value="1800" />
        <constructor-arg index="2" value="10" />
    </bean>
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">  
        <property name="maxIdle" value="200" />  
        <property name="testOnBorrow" value="true" />  
    </bean>  
      
    <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"  
        p:host-name="redis_server_ip" p:port="6379"   p:pool-config-ref="poolConfig"/>  
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
        p:connection-factory-ref="connectionFactory">
    </bean>
複製代碼

  這裏既然使用了redis做爲ticket存儲,就須要將原來的方案給註釋掉:redis

複製代碼
 <!-- Ticket Registry 
  <bean id="ticketRegistry" class="org.jasig.cas.ticket.registry.DefaultTicketRegistry" />
  -->
    <!--Quartz -->
    <!-- TICKET REGISTRY CLEANER 
    <bean id="ticketRegistryCleaner" class="org.jasig.cas.ticket.registry.support.DefaultTicketRegistryCleaner"
        p:ticketRegistry-ref="ticketRegistry"
        p:logoutManager-ref="logoutManager" />
    
    <bean id="jobDetailTicketRegistryCleaner" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"
        p:targetObject-ref="ticketRegistryCleaner"
        p:targetMethod="clean" />
    
    <bean id="triggerJobDetailTicketRegistryCleaner" class="org.springframework.scheduling.quartz.SimpleTriggerBean"
        p:jobDetail-ref="jobDetailTicketRegistryCleaner"
        p:startDelay="20000"
        p:repeatInterval="5000000" />
    -->
複製代碼

  到這裏,cas的改進就OK了,下面就須要將session也作集羣同步。spring

二、tomcat session集羣同步tomcat

  這裏採用開源的tomcat-redis-session-manager,git hub地址爲:https://github.com/jcoleman/tomcat-redis-session-managersession

  這裏只使用的是jdk1.7,tomcat7,tomcat6須要從新編譯好像。app

  1)拷貝編譯打包以後的tomcat-redis-session-manager-VERSION.jar,jedis-2.5.2.jar,commons-pool2-2.2.jar到tomcat/lib目錄下ide

  2)修改Tomcat context.xml (or the context block of the server.xml if applicable.)memcached

複製代碼
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve"/>
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
         host="redis_server_name"
         port="6379"
         database="0"
         maxInactiveInterval="1800"/>
複製代碼

至此cas集羣配置就能夠了。

http://www.cnblogs.com/lcxdever/p/4308759.html

相關文章
相關標籤/搜索