首页 技术 正文
技术 2022年11月13日
0 收藏 682 点赞 2,448 浏览 1165 个字
  • 在使用spring boot做负载均衡的时候,多个app之间的session要保持一致,这样负载到不同的app时候,在一个app登录之后,而打到另外一台服务器的时候,session丢失。

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

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

    1、增加repository到pom.xml

    1. <repository>
    2. <id>spring-milestone</id>
    3. <url>https://repo.spring.io/libs-milestone</url>
    4. </repository>

    2、增加相关依赖

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

    3、RedisSessionConfig.java

    Java代码  

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

    4、相关配置修改

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

    1. spring.redis.host=192.168.1.103

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

    1. public class SysResource implements Serializable

    6、查看效果

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

    7、总结

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,497
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,910
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,744
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,498
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,136
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,300