redis redis集群 session

redis集群怎么共享session

小新
262
2021-02-22 18:18:11
栏目: 云计算

redis集群怎么共享session

redis集群共享session的示例:

1.建立maven结构的web项目。

2.在对应的文件中添加代码。

pom.xml文件

<!-- spring-session begin-->

    <dependency>

        <groupId>org.springframework.data</groupId>

        <artifactId>spring-data-redis</artifactId>

        <version>1.7.6.RELEASE</version>

    </dependency>

    <dependency>

        <groupId>org.springframework.session</groupId>

        <artifactId>spring-session</artifactId>

        <version>1.3.0.RELEASE</version>

    </dependency>

    <dependency>

        <groupId>redis.clients</groupId>

        <artifactId>jedis</artifactId>

        <version>2.8.1</version>

    </dependency>

<dependency>

<groupId>org.apache.commons</groupId>

<artifactId>commons-pool2</artifactId>

<version>2.4.2</version>

<scope>compile</scope>

</dependency>

    <!-- spring-session end-->

配置filter,在web.xml中,添加以下代码,必须位于filter链的最前面。

<!-- spring-session -->

<filter>

    <filter-name>springSessionRepositoryFilter</filter-name>

    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>

</filter>

<filter-mapping>

    <filter-name>springSessionRepositoryFilter</filter-name>

    <url-pattern>/*</url-pattern>

    <dispatcher>REQUEST</dispatcher>

    <dispatcher>ERROR</dispatcher>

</filter-mapping>

在applicationContext.xml(spring容器配置文件的名字)中注册需要的bean

<!-- redis -->

   <bean id="jedisPoolConfig"  class="redis.clients.jedis.JedisPoolConfig">

   </bean>

   <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">

        <property name="hostName" value="localhost" />

        <property name="port" value="6379" />

        <property name="password" value="****"/>

        <property name="usePool" value="true"/>

        <property name="poolConfig" ref="jedisPoolConfig"/>

   </bean>

   <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">

        <property name="connectionFactory" ref="jedisConnectionFactory"/>

   </bean>

   <!-- 将session放入redis -->

   <bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">

        <property name="maxInactiveIntervalInSeconds" value="1800"/>

   </bean>


0
看了该问题的人还看了