`

Spring Boot 负载均衡之外置session状态保存

 
阅读更多

在使用spring boot做负载均衡的时候,多个app之间的session要保持一致,这样负载到不同的app时候,在一个app登录之后,而打到另外一台服务器的时候,session丢失。

 

常规的解决方案都是使用:如apache使用mod_jk.conf。

 

在开发spring boot app的时候可以借助 spring session 和redis,用外置的redis来存储session的状态。

 

直接上代码,我这边直接默认你使用spring boot,如果你是普通的spring web项目,请参照https://github.com/spring-projects/spring-session,在spring boot配置更简单

1、增加repository到pom.xml

Xml代码  收藏代码
  1. <repository>  
  2.           <id>spring-milestone</id>  
  3.           <url>https://repo.spring.io/libs-milestone</url>  
  4.       </repository>  

 

2、增加相关依赖

Xml代码  收藏代码
  1. <dependency>  
  2.         <groupId>org.springframework.boot</groupId>  
  3.         <artifactId>spring-boot-starter-redis</artifactId>  
  4.     </dependency>  
  5.   
  6. <dependency>  
  7.       <groupId>org.springframework.session</groupId>  
  8.       <artifactId>spring-session-data-redis</artifactId>  
  9.       <version>1.0.0.RC1</version>  
  10.       <type>pom</type>  
  11.     </dependency>  

 

 

3、RedisSessionConfig.java

Java代码  收藏代码
  1. package com.wisely.base;  
  2.   
  3. import org.springframework.context.annotation.Configuration;  
  4. import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;  
  5.   
  6. @Configuration  
  7. @EnableRedisHttpSession  
  8. public class RedisSessionConfig {  
  9.   
  10. }  

 

 

4、相关配置修改

在application.properties修改redis配置信息(请自行安装redis),请根据实际修改。如:

Java代码  收藏代码
  1. spring.redis.host=192.168.1.103  

 

 

5、所有实体类实现Serializable接口

Java代码  收藏代码
  1. public class SysResource implements Serializable  

 

6、查看效果

这时候登录系统在不同的app之间跳转的时候,session都是一致了,redis上可以看到:

 

7、总结

使用这些代码之后,无论你使用nginx或者apache,都无须在关心多个app之间的session一致的问题了。

 

分享到:
评论

相关推荐

    38. Spring Boot分布式Session状态保存Redis【从零开始学Spring Boot】

    NULL 博文链接:https://412887952-qq-com.iteye.com/blog/2295146

    Spring Boot+Nginx实现负载均衡1

    Spring Boot+Nginx实现负载均衡1

    Spring boot集成spring session实现session共享的方法

    主要介绍了Spring boot集成spring session实现session共享的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    spring boot实战教程之shiro session过期时间详解

    主要给大家介绍了关于spring boot实战教程之shiro session过期时间的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧。

    spring_cloud_gateway负载均衡,动态路由

    spring cloud gateway的负载均衡和动态路由的实现 demo_01,demo_02,demo_03 这三个服务相当于是集群的微服务 gateway这个服务是 springcloude gateway + ribbon 做的负载均衡 gateway_01 这个服务 是动态路由的...

    springcloud-负载均衡,服务注册demo

    springcloud-负载均衡,服务注册demo springcloud-负载均衡,服务注册demo

    Spring boot 示例 官方 Demo

    spring-boot-helloWorld:spring-boot的helloWorld版本 spring-boot-mybaits-annotation:注解版本 spring-boot-mybaits-xml:xml配置版本 spring-boot-mybatis-mulidatasource:springboot+mybatis多数据源最简解决...

    springcloud负载均衡

    springcloud负载均衡和zuul路由功能,zuul的过滤功能等

    从零开始学Spring Boot

    1.1 前言 1.2 资料官网 ...1.41 Spring Boot分布式Session状态保存Redis 1.42 Spring Boot Shiro权限管理 1.43 Spring Boot Shiro权限管理 1.44 Spring Boot Shiro权限管理 1.45 Spring Boot Shiro权限管理

    Spring Boot 2 Recipes

    获取Spring Boot 2微框架的可重用代码配方和代码段 了解Spring Boot 2如何与其他Spring API,工具和框架集成 访问Spring MVC和新的Spring Web Sockets,以实现更简单的Web开发 使用微服务进行Web服务开发并与Spring ...

    Beginning Spring Boot 2

    Beginning Spring Boot 2 Beginning Spring Boot 2 Beginning Spring Boot 2

    基于 Spring Boot + MySQL 开发的博客系统源码.zip

    基于 Spring Boot + MySQL 开发的博客系统源码 基于 Spring Boot + MySQL 开发的博客系统源码 基于 Spring Boot + MySQL 开发的博客系统源码 基于 Spring Boot + MySQL 开发的博客系统源码 基于 Spring ...

    spring-boot示例项目

    本项目示例基于spring boot 最新版本(2.1.9)实现,Spring Boot、Spring Cloud 学习示例,将持续更新…… 在基于Spring Boot、Spring Cloud 分布微服务开发过程中,根据实际项目环境,需要选择、集成符合项目...

    基于spring boot餐厅管理系统源码.zip

    基于spring boot餐厅管理系统源码 基于spring boot餐厅管理系统源码 基于spring boot餐厅管理系统源码 基于spring boot餐厅管理系统源码 基于spring boot餐厅管理系统源码 基于spring boot餐厅管理系统源码 ...

    Spring cloud和Spring boot介绍

    Spring Boot简化了基于Spring的应用开发,通过少量的代码就能创建一个独立的、产品级别的Spring应用。Spring Boot为Spring平台及第三方库提供开箱即用的设置,这样你就可以有条不紊地开始。多数Spring Boot应用只...

    Beginning Spring Boot 2 Applications and Microservices with the Spring Framework

    This book will help you understand what Spring Boot is, how Spring Boot helps you build Spring-based applications quickly and easily, and the inner workings of Spring Boot using easy-to-follow ...

    Pro Spring Boot 2第2版-2009-EPUB版

    Pro Spring Boot 2: An Authoritative Guide to Building Microservices, Web and Enterprise Applications, and Best Practices Quickly and productively develop complex Spring applications and microservices...

    Spring Boot Examples

    Spring boot使用的各种示例,以最简单、最实用为标准 spring-boot-helloWorld:spring-boot的helloWorld版本 spring-boot-mybaits-annotation:注解版本 spring-boot-mybaits-xml:xml配置版本 spring-boot-...

    Spring Boot视频教程大合集,完美帮助你学习Spring Boot,百度网盘

    Spring Boot视频教程大合集,完美帮助你学习Spring Boot,内部有3套Spring Boot学习视频教程,另附一篇Security Oauth2.0认证授权视频教程

    SpringCloud之四 负载均衡Feign

    SpringCloud之四 负载均衡Feign,使用Feign进行微服务的负载均衡处理

Global site tag (gtag.js) - Google Analytics