Spring Boot利用Redis进行Session共享

pom.xml




    4.0.0
    com.xwb
    spring-session-share
    1.0.0
    spring-session-share

    
        UTF-8
        1.7
        1.7
    

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.9.RELEASE
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.session
            spring-session-data-redis
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


Redis连接工厂配置

package com.xwb;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

@EnableRedisHttpSession()
@SpringBootApplication(exclude = org.springframework.boot.autoconfigure.session.SessionAutoConfiguration.class)
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public RedisConnectionFactory redisConnectionFactory() {
        JedisConnectionFactory factory = new JedisConnectionFactory();
        factory.setHostName("127.0.0.1");
        factory.setPort(6379);
        return factory;
    }
}

Controller

package com.xwb.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
public class SessionShareController {
    private static final Logger LOGGER = LoggerFactory.getLogger(SessionShareController.class);
    @GetMapping("/login")
    public String share(HttpServletRequest request){
        request.getSession().setAttribute("userId", "123456");
        LOGGER.info("port:{}, login success", request.getLocalPort());
        return "login success";
    }

    @GetMapping("/user")
    public String getUser(HttpServletRequest request){
        LOGGER.info("port:{}, get user", request.getLocalPort());
        HttpSession session = request.getSession();
        return session.getAttribute("userId").toString();
    }
}

测试

  1. 将项目打成可运行jar
    • 项目根目录下执行mvn package
  2. 将项目分别运行在两个不同的端口下
    • 进入jar包所在目录执行命令java -jar spring-session-share-1.0.0.jar --server.port=8080 --server.context-path=/test
    • 更改端口,重复上一步java -jar spring-session-share-1.0.0.jar --server.port=8888 --server.context-path=/test
  3. 配置Nginx
#user  nobody;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    upstream xwb-session-share {
        server 127.0.0.1:8080;
        server 127.0.0.1:8888;
    }
    
    server {
        listen 80;
        server_name      localhost;

        location / {
            proxy_pass   http://xwb-session-share;
        }
    }
}
  1. 启动Nginx后,访问页面
    • 登录localhost/test/login
    • 多次访问localhost/test/user
    • 查看日志,验证结果

日志截图

登录
第一次访问/test/user
第二次访问/test/user

你可能感兴趣的:(Spring Boot利用Redis进行Session共享)