第三章-session的解决方案

基于Springboot的session解决

session

session机制是一种服务器端的机制,服务器使用一种类似于散列表的结构(也可能就是使用散列表)来保存信息。

当程序需要为某个客户端的请求创建一个session的时候,服务器首先检查这个客户端的请求里是否已包含了一个session标识 - 称为session id,如果已包含一个session id则说明以前已经为此客户端创建过session,服务器就按照session id把这个session检索出来使用(如果检索不到,可能会新建一个),如果客户端请求不包含session id,则为此客户端创建一个session并且生成一个与此session相关联的session id,session id的值应该是一个既不会重复,又不容易被找到规律以仿造的字符串,这个session id将被在本次响应中返回给客户端保存。

单项目的session

	@ResponseBody
    @RequestMapping(value = "/setSession",method = { RequestMethod.POST, RequestMethod.GET })
    public  String setCookies(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.setAttribute("data", "wangyun");
        return "success";
    }

    @ResponseBody
    @RequestMapping(value = "/getSession",method = { RequestMethod.POST, RequestMethod.GET })
    public String getCookies(HttpServletRequest request){
        HttpSession session = request.getSession();
        String data = (String) session.getAttribute("data");
        return data;
    }

但是吧,这次会话中可以互相取到session存放的数据,但是,如果不只是这一个项目呢,又该怎么样。说白了,就是如何解决一个session共享的问题。

我们可以通过SpringSession来解决这个问题

首先先导入相关依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
    <version>2.0</version>
</dependency>

在配置文件上配置上相关的redis信息

# 端口号需要8080/8081,需要模拟一下场景
server.port=8081
spring.redis.host=**.**.**.**
spring.redis.port=6379
#spring session使用存储类型,默认就是redis所以可以省略
spring.session.store-type=redis

然后我们还用上述的例子,但是启动类上需要加上缓存开启注解:

package com.session.learning;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@EnableCaching
@EnableRedisHttpSession
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @ResponseBody
    @RequestMapping(value = "/setSession",method = { RequestMethod.POST, RequestMethod.GET })
    public  String setCookies(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.setAttribute("data", "wangyun");
        return "添加成功";
    }

    @ResponseBody
    @RequestMapping(value = "/getSession",method = { RequestMethod.POST, RequestMethod.GET })
    public String getCookies(HttpServletRequest request){
        HttpSession session = request.getSession();
        String data = (String) session.getAttribute("data");
        return data;
    }

}

一切准备就绪,我们最后还需要开启一下项目的多实例模式。因为本机没有装nginx,就这样模拟一下吧。

我们先在8080的端口上写入session

image-20201208165556139

同时启动8081端口尝试获取:

image-20201208165624286

然后8080也可以获取到:

image-20201208165649982

查看redis的信息

image-20201208165717038

posted @ 2020-12-12 19:44  且I听  阅读(103)  评论(0)    收藏  举报