再谈session共享

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介: 之前一篇已经写过了《springboot中redis的使用和分布式session共享问题》,但是示例不完全,本文加以完善。使用spring-session-data-redis解决session共享,而不需要再引入其他jar即可集成简单,上手迅速。

之前一篇已经写过了《springboot中redis的使用和分布式session共享问题》,但是示例不完全,本文加以完善。
使用spring-session-data-redis解决session共享,而不需要再引入其他jar即可
集成简单,上手迅速。

项目结构

img_88767eac3443c7cbd4a8dec167ad3c7b.png
1.png

pom

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>easy-boot</artifactId>
        <groupId>com.mos</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>easy-boot-web</artifactId>
    <packaging>jar</packaging>

    <name>easy-boot-web</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>

        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>
        <!--redis配置开始-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
        </dependency>
        <!--redis配置结束-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

配置

package com.mos.easyboot.web.config;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {

    @Bean
    public CacheManager cacheManager(RedisTemplate<?,?> redisTemplate) {
        CacheManager cacheManager = new RedisCacheManager(redisTemplate);
        return cacheManager;
    }

    @Bean
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);
        return redisTemplate;
    }
}
package com.mos.easyboot.web.config;

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

@Configuration
@EnableRedisHttpSession
public class RedisSessionConfig {
}

properties配置

server.port=8881

# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=192.168.48.2
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=123456789
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=0

测试

编写测试controller
package com.mos.easyboot.web.controller;


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

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("demo")
public class DemoController {

    @RequestMapping("index")
    public String index(HttpSession session){
        session.setAttribute("sessionId",session.getId());
        session.setAttribute("sessionUserId","123");
        return "test";
    }

    @RequestMapping("session")
    public String session(HttpSession session){
        String sessionId = session.getAttribute("sessionId").toString();
        String sessionUserId = session.getAttribute("sessionUserId").toString();
        return sessionId + "^" +  sessionUserId;
    }
}

分别启动两个项目,先访问http://localhost:8881/demo/index
再访问http://localhost:8881/demo/session
新打开一个浏览器tab,访问http://localhost:8882/demo/session
最终效果如下(可以看到两个请求获取到的sessionId和session内容都相同,实现session共享):

img_23acbd2d6f213f1af4c1d5df33cdafb2.png
7.png
相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
目录
相关文章
|
5月前
|
NoSQL 应用服务中间件 Redis
面试官:分布式环境下,如何实现session共享
随着互联网公司的项目在微服务和分布式的环境下进行的搭建,导致一个项目可能分别部署在几个甚至很多的服务器集群下,此时就会出现一个问题:
|
5月前
|
存储 缓存 监控
10 分钟搞懂缓存设计策略
10 分钟搞懂缓存设计策略
239 0
|
缓存 安全 网络协议
Web服务器的底层原理是什么?
Web服务器的底层原理是什么?
120 0
|
存储 前端开发 JavaScript
网站服务器是干什么的?底层原理是什么?
网站服务器是干什么的?底层原理是什么?
126 0
|
存储 缓存 网络协议
五分钟搞懂缓存
五分钟搞懂缓存
217 0
五分钟搞懂缓存
|
存储 缓存 关系型数据库
一文带你搞懂“缓存策略”
我们都知道,提高系统性能的最简单也最流行的方法之一其实就是使用缓存。我们引入缓存,相当于对数据进行了复制。每当系统数据更新时,保持缓存和数据源(如 MySQL 数据库)同步至关重要,当然,这也取决于系统本身的要求,看系统是否允许一定的数据延迟。
295 0
一文带你搞懂“缓存策略”
|
监控 Java 应用服务中间件
大部分程序员不知道的 Servelt3 异步请求,原来这么简单?阿粉带你全面扫盲!(上)
当一个 HTTP 请求到达 Tomcat,Tomcat 将会从线程池中取出线程,然后按照如下流程处理请求: 将请求信息解析为 HttpServletRequest 分发到具体 Servlet 处理相应的业务 通过 HttpServletResponse 将响应结果返回给等待客户端
大部分程序员不知道的 Servelt3 异步请求,原来这么简单?阿粉带你全面扫盲!(上)
|
Java 应用服务中间件 程序员
大部分程序员不知道的 Servelt3 异步请求,原来这么简单?阿粉带你全面扫盲!(下)
当一个 HTTP 请求到达 Tomcat,Tomcat 将会从线程池中取出线程,然后按照如下流程处理请求: 将请求信息解析为 HttpServletRequest 分发到具体 Servlet 处理相应的业务 通过 HttpServletResponse 将响应结果返回给等待客户端
大部分程序员不知道的 Servelt3 异步请求,原来这么简单?阿粉带你全面扫盲!(下)
|
JavaScript 前端开发 安全
彻底讲清Web开发的Cookie、Session机制(上)
彻底讲清Web开发的Cookie、Session机制
272 0
彻底讲清Web开发的Cookie、Session机制(上)
|
存储 算法 安全
彻底讲清Web开发的Cookie、Session机制(下)
彻底讲清Web开发的Cookie、Session机制
232 0
彻底讲清Web开发的Cookie、Session机制(下)