基於redis的cas叢集配置

來源:互聯網
上載者:User

標籤:

1、cas ticket統一儲存

  做cas叢集首先需要將ticket拿出來,做統一儲存,以便每個節點訪問到的資料一致。官方提供基於memcached的方案,由於項目需要,需要做計入redis,根據官方例子改了一個基於redis版本的。

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的配置如下:

 <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儲存,就需要將原來的方案給注釋掉:

 <!-- 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也做叢集同步。

2、tomcat session叢集同步

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

  這裡只使用的是jdk1.7,tomcat7,tomcat6需要重新編譯好像。

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

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

<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叢集配置就可以了。

基於redis的cas叢集配置

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.