栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

Redis共享Session原理及示例

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Redis共享Session原理及示例

Redis共享session的作用
  • 微服务自身可以保持无状态,应用实例数量的多少不会影响用户登录状态;
  • 可实现单点登录的踢出功能,如可以让上次异地登录的用户下线;
  • session在多个服务或服务器间共享,实现多站点单点登录(参考SSO原理)

Redis缓存session原理简述

其工作原理,可简单用图描述(假设服务A运行有有个多个实例):

Springboot-session结合Redis示例
  • 添加maven依赖
  
	org.springframework.boot  
	spring-boot-starter-redis
	1.4.0.RELEASE
  
  
	org.springframework.session  
	spring-session-data-redis  


	org.springframework.session
	spring-session
	1.3.3.RELEASE

  • 添加redis配置(application.properties)
spring.redis.database=1  
spring.redis.host=localhost
spring.redis.password=xxxxx
spring.redis.port=6379

# server.port=8080
server.port=8081
  • 添加配置类
package com.example.demo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

@Configuration  
@EnableRedisHttpSession  
public class RedisSessionConfig {  
}
  • 添加controller
package com.example.demo.controller;

import java.util.*;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@RestController  
@RequestMapping(value = "/")
public class Hello {  
    @RequestMapping(value = "/set", method = RequestMethod.GET)  
    public Map firstResp (HttpServletRequest request){    
 request.getSession().setAttribute("testKey", "testValue");

 Map map = new HashMap<>(); 
 map.put("testKey", "testValue");  
 return map;
    }
  
    @RequestMapping(value = "/query", method = RequestMethod.GET)  
    public Object sessions (HttpServletRequest request){
 Map map = new HashMap<>();
 map.put("sessionId", request.getSession().getId());
 map.put("testKey", request.getSession().getAttribute("testKey"));
 return map;
    }
} 
  • 浏览器访问测试

    • 启动两个springboot程序,分别监听8080,8081端口
    • 浏览器分别访问http://127.0.0.1:8080/set, 127.0.0.1:8081/query接口,效果如下:
    • 查看redis中的key

可以看出两个独立的应用已经共享了同一个session。

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号