准备工作

新建两个springboot2.0.2版本的服务,配置文件添加:

#在默认设置下,Eureka服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

配置文件需要添加以上两项配置,不然会报以下错误:

com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server

集成redis

添加Maven依赖:

<!--spring boot 与redis应用基本环境配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--spring session 与redis应用基本环境配置,需要开启redis后才可以使用,不然启动Spring boot会报错 -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<!--jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>

必须添加第三个依赖,不然会报以下错误:

Caused by: java.lang.NoClassDefFoundError: redis/clients/jedis/Tuple

添加redis配置信息:

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

**注:关于springboot各个版本的redis集成,参考springboot中各个版本的redis配置问题

新建sessionConfig类

//这个类用配置redis服务器的连接
//maxInactiveIntervalInSeconds为SpringSession的过期时间(单位:秒)
@EnableRedisHttpSession(maxInactiveIntervalInSeconds= 1800)
public class SessionConfig {
<span class="hljs-comment"><span class="hljs-comment">//冒号后的值为没有配置文件时,制动装载的默认值</span></span>
<span class="hljs-meta"><span class="hljs-meta">@Value</span></span>(<span class="hljs-string"><span class="hljs-string">"${spring.redis.host:localhost}"</span></span>)
String HostName;
<span class="hljs-meta"><span class="hljs-meta">@Value</span></span>(<span class="hljs-string"><span class="hljs-string">"${spring.redis.port:6379}"</span></span>)
<span class="hljs-keyword"><span class="hljs-keyword">int</span></span> Port;
<span class="hljs-meta"><span class="hljs-meta">@Bean</span></span>
<span class="hljs-function"><span class="hljs-keyword"><span class="hljs-function"><span class="hljs-keyword">public</span></span></span><span class="hljs-function"> JedisConnectionFactory </span><span class="hljs-title"><span class="hljs-function"><span class="hljs-title">connectionFactory</span></span></span><span class="hljs-params"><span class="hljs-function"><span class="hljs-params">()</span></span></span><span class="hljs-function"> </span></span>{
JedisConnectionFactory connection = <span class="hljs-keyword"><span class="hljs-keyword">new</span></span> JedisConnectionFactory();
connection.setPort(Port);
connection.setHostName(HostName);
<span class="hljs-keyword"><span class="hljs-keyword">return</span></span> connection;
}

}

test主类中添加测试接口(端口号:8763):

@SpringBootApplication
@EnableEurekaClient
@RestController
public class ServerTestApplication { public static void main(String[] args) {

SpringApplication.run(ServerTestApplication.class, args);

} @RequestMapping(value="/",method = RequestMethod.GET)

public String setSession(HttpServletRequest request){

Map<String,Object> map = new HashMap();

map.put("name","超级管理员");

map.put("account","admin");

request.getSession().setAttribute("userSession",map);

String sessionId = request.getSession().getId();

return sessionId;

}

}

返回结果:

4c0fe3e3-87cc-4991-acae-8bca2645705a

test1主类中添加测试(端口号:8764)

@SpringBootApplication
@EnableEurekaClient
@RestController
public class ServerTest1Application { public static void main(String[] args) {

SpringApplication.run(ServerTest1Application.class, args);

} @RequestMapping(value="/",method = RequestMethod.GET)

public Map<String,Object> getSession(HttpServletRequest request){

String sessionId = request.getSession().getId();

Object obj = request.getSession().getAttribute("userSession");

Map<String,Object> map = new HashMap();

map.put("sessionId",sessionId);

map.put("user",obj);

return map;

}

}

返回结果:

<Map>
<sessionId>4c0fe3e3-87cc-4991-acae-8bca2645705a</sessionId>
<user>
<name>超级管理员</name>
<account>admin</account>
</user>
</Map>

从以上结果可以看出,访问8763端口服务的sessionId信息和访问8764端口服务的sessionId信息一致

原文地址:https://www.jianshu.com/p/f0a069d2a85b

springboot2.0.2+redis+spring-session 解决session共享的问题的更多相关文章

  1. Spring Session解决Session共享

    1. 分布式Session共享   在分布式集群部署环境下,使用Session存储用户信息,往往出现Session不能共享问题.   例如:服务集群部署后,分为服务A和服务B,当用户登录时负载到服务A ...

  2. SpringBoot2.0 整合 Redis集群 ,实现消息队列场景

    本文源码:GitHub·点这里 || GitEE·点这里 一.Redis集群简介 1.RedisCluster概念 Redis的分布式解决方案,在3.0版本后推出的方案,有效地解决了Redis分布式的 ...

  3. Springboot2.0整合Redis(注解开发)

    一. pom.xm文件引入对应jar包 <dependency> <groupId>org.springframework.boot</groupId> <a ...

  4. SpringBoot2.0 整合 JWT 框架,解决Token跨域验证问题

    本文源码:GitHub·点这里 || GitEE·点这里 一.传统Session认证 1.认证过程 1.用户向服务器发送用户名和密码. 2.服务器验证后在当前对话(session)保存相关数据. 3. ...

  5. 使用 StateServer 保存 Session 解决 Session过期,登陆过期问题。

    使用 StateServer 保存 Session 正常操作情况下Session会无故丢失.因为程序是在不停的被操作,排除Session超时的可能.另外,Session超时时间被设定成60分钟,不会这 ...

  6. SpringBoot2.0整合Redis

    Spring Boot2.0在2018年3月份正式发布,相比1.0还是有比较多的改动,例如SpringBoot 自2.0起支持jdk1.8及以上的版本.第三方类库升级.响应式 Spring 编程支持等 ...

  7. Springboot2.0访问Redis集群

    Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作高性能的key-value数据库.缓存和消息中间件,掌握它是程序员的必备技能,下面是一个springboot访问redis的 ...

  8. springboot2.0整合redis作为缓存以json格式存储对象

    步骤1 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spr ...

  9. springboot2.0整合redis的发布和订阅

    1.Maven引用 <dependency> <groupId>org.springframework.boot</groupId> <artifactId& ...

随机推荐

  1. golang之常量

    1.  常量可以是全局常量,也可以是函数内部的局部常量.常量的值不可修改,常量表达式的值在编译期计算,而不是在运行期.存储在常量中的数据类型只可以是布尔型.数字型(整数型.浮点型和复数)和字符串型.当 ...

  2. golang之下载安装配置

    1.下载:根据操作系统和计算架构选择合适的安装包,操作系统类型有linux.mac.windows等,计算架构分为32位的386计算架构和64位的amd64计算架构 2.安装:推荐安装到 /usr/l ...

  3. SPSS统计基础-均值功能的使用

    SPSS统计基础-均值功能的使用 均值过程计算一个或多个自变量类别中因变量的子组均值和相关的单变量统计.您也可以获得单因素方差分析.eta 和线性相关检验. 统计量.合计.个案数.均值.中位数.组内中 ...

  4. web框架起源

    web框架 python三大主流web框架 django 大而全,自带的组件和功能极多, 缺点:写小项目时候会比较笨重(杀鸡用牛刀),大并发不行,3000撑死 flask 小而精 自带的组件和功能极少 ...

  5. day39-Spring 03-JDK的动态代理

    package cn.itcast.spring3; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Meth ...

  6. poj 2385【动态规划】

    poj 2385 Apple Catching Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14007   Accepte ...

  7. Unity3D 物体旋转之Quaternion.Slerp

    实现的功能:1个物体以一定的速度转向目标物体 Quaternion TargetRotation = Quaternion.LookRotation(m_Target.transform.positi ...

  8. Java练习 SDUT-2670_3-1 Point类的构造函数

    3-1 Point类的构造函数 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 通过本题目的练习可以掌握类的构造函数的定 ...

  9. linux 系统下如何进行用户之间的切换

    切换用户的命令是su,su是(switch user)切换用户的缩写.通过su命令,可以从普通用户切换到root用户,也可以从root用户切换到普通用户.从普通用户切换到root用户需要密码(该密码是 ...

  10. github怎样改动源代码并进行提交方法小结

    /*********************************************************************  * Author  : Samson  * Date   ...