spring-session-data-redis 实现session共享

赵才俊
2023-12-01

1.添加依赖

<dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
</dependency>
 <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
            <version>1.3.1.RELEASE</version>
</dependency>

2.redis配置

<!-- 配置redis缓存服务器 -->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="${redis.node1.ip}" />
        <property name="port" value="${redis.node1.port}" />
        <!-- <property name="password" value="${redis.node1.pw}" />  -->
</bean>

<!-- spring-session -->
<bean id="redisHttpSessionConfiguration"       class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
      <property name="maxInactiveIntervalInSeconds" value="120"/>
</bean>

3.web.xml配置

<!-- 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>
 </filter-mapping>

4.nginx配置验证

#user  nobody;
worker_processes  2;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;
    upstream letmecookserver{
        server 127.0.0.1:8080;
        server 127.0.0.1:8899;
    }
    server {
        listen       80;
        server_name  localhost;

        location / {
            proxy_pass     http://letmecookserver;
        }
    }
}

 类似资料: