SpringBoot2.x 整合Spring-Session实现Session共享

摘要:
SpringBoot2.x整合Spring-Session实现Session共享1.前言发展至今,已经很少还存在单服务的应用架构,不说都使用分布式架构部署,至少也是多点高可用服务。在多个服务器的情况下,Seession共享就是必须面对的问题了。LZ在这里采用的Spring-Session来实现。它使用代理过滤器,将Session操作拦截,自动将数据同步到Redis中,以及自动从Redis读取数据。从此,操作分布式的Session就像操作单服务的Session一样,可以为所欲为了。配置完成后,启动Nginx

SpringBoot2.x 整合Spring-Session实现Session共享

1.前言

发展至今,已经很少还存在单服务的应用架构,不说都使用分布式架构部署, 至少也是多点高可用服务。在多个服务器的情况下,Seession共享就是必须面对的问题了。

解决Session共享问题,大多数人的思路都是比较清晰的, 将需要共享的数据存在某个公共的服务中,如缓存。很多人都采用的Redis,手动将Session存在Redis,需要使用时,再从Redsi中读取数据。毫无疑问,这种方案是可行的,只是在手动操作的工作量确实不少。

LZ在这里采用的Spring-Session来实现。它使用代理过滤器,将Session操作拦截,自动将数据同步到Redis中,以及自动从Redis读取数据。从此,操作分布式的Session就像操作单服务的Session一样,可以为所欲为了。

2.实践
2.1 创建工程

使用idea创建SpringBoot工程, 添加组件Web、Spring Session和Redis。 我这里idea是2019版本,SpringBoot是2.1.6。

SpringBoot2.x 整合Spring-Session实现Session共享第1张

pom.xml文件

<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.session</groupId>
			<artifactId>spring-session-data-redis</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
2.2 配置Redis
spring:
  redis:
      port: 6379
      password: xofcO46Fy
      host: 10.17.153.104
server:
  port: 9090
2.3 测试

代码实现

package com.xiaoqiang.sessionshare.web;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * SessionShareController <br>
 * 〈session共享控制器〉
 *
 * @author XiaoQiang
 * @create 2019-7-6
 * @since 1.0.0
 */
@RestController
@RequestMapping(value = "/session")
public class SessionShareController {

    @Value("${server.port}")
    Integer port;


    @GetMapping(value = "/set")
    public String set(HttpSession session){
        session.setAttribute("user","wangwq8");
        return String.valueOf(port);
    }

    @GetMapping(value = "get")
    public String get(HttpSession session){
        return "用户:"+session.getAttribute("user")+",端口:"+port;
    }
}

maven package打包发布到服务器服务器,过程略。

分别使用9090 9091端口启动项目。

nohup java -jar sessionshare-0.0.1-SNAPSHOT.jar --server.port=9090 &

nohup java -jar sessionshare-0.0.1-SNAPSHOT.jar --server.port=9091 &

先访问http://10.17.158.136:9090/session/set,在9090这个服务的session保存用户变量;

SpringBoot2.x 整合Spring-Session实现Session共享第2张

然后再访问http://10.17.158.136:9091/session/get,从session中获取得到用户信息。

SpringBoot2.x 整合Spring-Session实现Session共享第3张

从上面样例,可以看出session已经实现了共享,只是测试过程是需要手动切换服务。为了更好地模式真实项目环境,为此,我们配置Nginx,来进行测试。

2.4 配置Nginx

在Nginx安装目录conf下,编辑nginx.conf,

 upstream tomcatServer {
        server 10.17.158.136:9092 weight=1;
        server 10.17.158.136:9091 weight=2;
        }

    server {
        listen       9000;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://tomcatServer;
            proxy_redirect default;
            #root   html;
            #index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

在这里我们只需要配置简单的负载均衡,端口是9000。所有localhost:9000都会按一定策略(这里是按权重分发,配置weight=1一样,随机分发的;nginx默认是轮询策略)分发到上游服务upstream配置的服务上。

配置完成后,启动Nginx;

/apps/test/software/nginx/nginx-1.6.2/sbin/nginx

首先访问http://10.17.158.136:9000/session/set,向seesion中保存数据,从下图中可知9090端口的服务处理了该请求。
SpringBoot2.x 整合Spring-Session实现Session共享第4张

然后在访问/get请求,是从9091端口的服务获取得到的用户信息,至此,测试完成。

SpringBoot2.x 整合Spring-Session实现Session共享第5张

3.总结

本文主要是Spring Session的简单使用,从上面可以看出,除了引入了Spring Session的jar, 其他方面,不管是代码还是配置,都与之没有什么关联,就相当于在操作最常用的HttpSession,在实际项目中用起来也是相当方便。

样例已上传github,地址:https://github.com/lanxuan826/sample-library/tree/master/sessionshare,有兴趣可下载测试。

另外,此文是从松哥博客中得到启示,在此推荐:https://blog.csdn.net/u012702547/article/list/2?

,还推荐一篇关于Spring Session原理的博客:https://blog.csdn.net/u010648555/article/details/79491988

免责声明:文章转载自《SpringBoot2.x 整合Spring-Session实现Session共享》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇(转)SVN教程总结【模式识别与机器学习】——4.1模式分类可分性的测度下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Java八股文——Redis与一致性协议

Redis Redis数据结构   String字符串,list链表,hash键值对,set集合,sortedset有序集合,BloomFilter布隆过滤器   布隆过滤器原理:当一个元素被加入到集合中时,通过K个散列函数将元素分布到一个位数组上的K个点,查询该元素的时候,如果hash出来的这个K个点都为1,则说明元素可能存在,如果有一个为0,则说明元素...

.NET平台常用的框架整理

分布式缓存框架 Microsoft Velocity:微软自家分布式缓存服务框架。 Memcahed:一套分布式的高速缓存系统,目前被许多网站使用以提升网站的访问速度。 Redis:是一个高性能的KV数据库。它的出现很大程度补偿了Memcached在某些方面的不足。 EnyimMemcached:访问Memcached最优秀的.NET客户端,集成不错的分布...

redis慢查询的简单认识和事务、订阅的认识

  有学习的小伙伴可以一起讨论有问题可以问我,微信 15321502296慢查询 阀值:规定的一个值 阈值:规定的一个范围 就是系统在执行命令前后计算每条命令的执行时间,当超过预设的阀值时,就见这条命令记录下来 slowlog-log-slower-than  微秒 1秒=1000毫秒=1000000微秒 0 记录所有命令 <0不进行记录 slowl...

window下远程连接redis服务

首先下redis包: 下载地址:https://github.com/MSOpenTech/redis/releases。 之后:1.注释掉redis.windows-service.conf 中的bind 127.0.0.1这一行(在前面加#) 3.同文件中将protected-mode yes 改成 protected-mode no 4.保存并重启r...

(一)Redis之简介和windows下安装radis

一、简介  1.1  关于nosql 介绍Redis之前,先了解下NoSQL (Not noly SQL)不仅仅是SQL, 属于非关系型数据库;Redis就属于非关系型数据库, 传统的Mysql ,oracle ,sql server 等 都是关系型数据库。   1.2  nosql的作用 为什么需要NoSQL,主要应对以下问题,传统关系型数据库力不从心...

会话、进程组与僵死进程

  1.   终端       在Linux系统中,用户通过终端登录系统后得到一个Shell进程,这个终端成为Shell进程的控制终端(Controlling Terminal),Shell进程启动的其他进程的控制终端也是这个终端。默认情况下(没有重定向),每个进程的标准输入、标准输出和标准错误输出都指向控制终端,进程从标准输入读也就是读用户的键盘输入,进...