• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

SpringBoot中实现分布式的Session共享的详细教程

springboot 搞代码 4年前 (2022-01-09) 18次浏览 已收录 0个评论

一. SpringBoot中实现Session共享

1. 创建web项目

我们按照之前的经验,创建一个web程序,并将之改造成Spring Boot项目,具体过程略。

2.添加依赖包

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-data-redis</artifactId>
 </dependency>
 <dependency>
 <groupId>org.springframework.session</groupId>
 <artifactId>spring-session-data-redis</artifactId>
 </dependency>
 <dependency>
 <groupId>org.springframework.session</groupId>
 <artifactId>spring-session-core</artifactId>
 </dependency>

3.创建application.yml文件

server:
 port: 8080
 #配置redis
 spring:
 redis:
 host: 127.0.0.1
 port: 6379
 #password: 123456
 jedis:
 pool:
  max-idle: 8
  min-idle: 0
  max-active: 8
  #max-wait: 60000
 #timeout: 3000 #超时一定要大于0
 session:
 #设置session存储类型
 store-type: redis

这里可以设置多种session的store-type:

我们这里选择利用redis来对session进行集中存储,实现session共享。

4.创建Session配置类

package com.yyg.boot.config;
 
import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
 
/**
 * @Author 一一哥Sun
 * @Date Created in 2020/4/28
 * @Description 开启Redis Http Session
 */
@Configuration
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 3600)
public class RedisHttpSessionConfiguration {
 
}

在这里添加@EnableRedisHttpSession注解,可以通过maxInactiveIntervalInSeconds属性设置Session的过期时间。

5.创建一个Controller接口方法

该接口方法当用户不存在时提示“用户不存在”,否则会提示“用户存在”。

package com.yyg.boot.web;
 
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.Reque<a style="color:transparent">本文来源gao($daima.com搞@代@#码$网</a>stParam;
import org.springframework.web.bind.annotation.RestController;
 
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
 
/**
 * @Author 一一哥Sun
 * @Date Created in 2020/4/28
 * @Description Description
 */
@Slf4j
@RestController
public class SessionController {
 
 @RequestMapping("/session")
 public Object springSession(@RequestParam("username") String username, HttpServletRequest request, HttpSession session) {
 Cookie[] cookies = request.getCookies();
 if (cookies != null && cookies.length > 0) {
  for (Cookie cookie : cookies) {
  log.warn(cookie.getName() + "=" + cookie.getValue());
  }
 }
 
 Object value = session.getAttribute("username");
 if (value == null) {
  log.warn("用户不存在");
  //保存session
  session.setAttribute("username", "{username: '" + username + "', age: 30}");
 } else {
  log.warn("用户存在");
 }
 
 return "username=" + value;
 }
 
}

6.创建入口类

package com.yyg.boot;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
/**
 * @Author 一一哥Sun
 * @Date Created in 2020/4/28
 * @Description Description
 */
@SpringBootApplication
public class SpringSessionApplication {
 
 public static void main(String[] args){
 springapplication.run - 这个网站可出售。 - 最佳的springapplication 来源和相关信息。(SpringSessionApplication.class,args);
 }
 
}

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:SpringBoot中实现分布式的Session共享的详细教程

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址