这是分布式集群环境下,如何实现session共享系列的第五篇。在上一篇:分布式集群环境下,如何实现session共享四(部署项目测试)中,针对nginx不同的负载均衡策略:轮询、ip_hash方式,测试了session的不同使用情况,并且留下了一个问题:有没有可能针对nginx负载均衡策略(轮询)的基础上,对session实现共享呢???

  本篇在nginx负载均衡策略(轮询的基础上),通过spring-session将session存储到redis,实现session共享。

1.改造项目

  1.1.导入依赖

 <!--spring 版本-->
<spring.version>5.0.2.RELEASE</spring.version>
<spring.session.data.redis.version>1.3.1.RELEASE</spring.session.data.redis.version>
<lettuce.version>3.5.0.Final</lettuce.version> <!-- spring web包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<version>${spring.session.data.redis.version}</version>
</dependency>
<dependency>
<groupId>biz.paluch.redis</groupId>
<artifactId>lettuce</artifactId>
<version>${lettuce.version}</version>
</dependency>

  1.2.编写redis配置文件

redis.host=192.168.80.22
redis.pass=myredis
redis.port=6379
redis.session.timeout=600

  1.3.编写spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config /> <!-- 加载properties文件 -->
<bean id="configProperties"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:session-redis.properties</value>
</list>
</property>
</bean> <!-- RedisHttpSessionConfiguration -->
<bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
<!-- session过期时间,单位是秒 -->
<property name="maxInactiveIntervalInSeconds" value="${redis.session.timeout}" />
</bean> <!--LettuceConnectionFactory -->
<bean class="org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory"
p:host-name="${redis.host}" p:port="${redis.port}" /> </beans>

  1.4.配置web.xml

<!--加载spring配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext-session.xml</param-value>
</context-param> <!--配置监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!--配置过滤器(该过滤器要配置在第一的位置)-->
<filter>
<filter-name>springSessionRepositoryFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter> <filter-mapping>
<filter-name>springSessionRepositoryFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>

  1.5.启动redis

#启动redis服务端
[root@hadoop02 bin]# ./redis-server #客户端连接操作
[root@hadoop02 bin]# ./redis-cli
#设置允许远程访问
127.0.0.1:6379> CONFIG SET protected-mode no
OK

2.nginx配置

#添加tomcat列表,真实应用服务器都放在这
upstream tomcat_pool{
#server tomcat地址:端口号 weight表示权值,权值越大,被分配的几率越大;
server 192.168.80.22:8080 weight=4 max_fails=2 fail_timeout=30s;
server 192.168.80.22:8081 weight=4 max_fails=2 fail_timeout=30s; }

3.测试

http://192.168.80.22/session-redis-demo/

  3.1.谷歌浏览器测试

  3.2.火狐浏览器测试

  3.3.查看redis中存储的session

[root@hadoop02 bin]# ./redis-cli
127.0.0.1:6379> keys *
1) "spring:session:expirations:1558614360000"
2) "spring:session:sessions:af12f7a7-3385-44dc-93b2-a3fa82026b25"
3) "spring:session:sessions:abc97363-8f1d-4d1c-a9df-e2a78628af05"
4) "site"
5) "spring:session:sessions:expires:abc97363-8f1d-4d1c-a9df-e2a78628af05"
127.0.0.1:6379>

总结:通过spring-session把session存储到redis中,即便nginx负载均衡策略为轮询方式,也一样实现了session的共享,这才是我们的终极目标,真好!!!

分布式集群环境下,如何实现session共享五(spring-session+redis 实现session共享)的更多相关文章

  1. 分布式集群环境下,如何实现session共享四(部署项目测试)

    这是分布式集群环境下,如何实现session共享系列的第四篇.在上一篇:分布式集群环境下,如何实现session共享三(环境搭建)中,已经准备好了相关的环境:tomcat.nginx.redis.本篇 ...

  2. 分布式集群环境下,如何实现session共享三(环境搭建)

    这是分布式集群环境下,如何实现session共享系列的第三篇.在上一篇:分布式集群环境下,如何实现session共享二(项目开发)中,准备好了一个通过原生态的servlet操作session的案例.本 ...

  3. 分布式集群环境下,如何实现session共享二(项目开发)

    在上一篇分布式集群环境下,如何实现session共享一(应用场景)中,介绍了在分布式集群下,需要实现session共享的应用场景.并且最后留下了一个问题:在集群环境下,如何实现session的共享呢? ...

  4. 在Hadoop1.2.1分布式集群环境下安装hive0.12

    在Hadoop1.2.1分布式集群环境下安装hive0.12 ● 前言: 1. 大家最好通读一遍过后,在理解的基础上再按照步骤搭建. 2. 之前写过两篇<<在VMware下安装Ubuntu ...

  5. 分布式集群环境下,如何实现session共享一(应用场景)

    在web应用中,由于http的请求响应式,无状态.要记录用户相关的状态信息,比如电商网站的购物车,比如用户是否登录等,都需要使用session.我们知道session是由servlet容器创建和管理, ...

  6. 基于HBase Hadoop 分布式集群环境下的MapReduce程序开发

    HBase分布式集群环境搭建成功后,连续4.5天实验客户端Map/Reduce程序开发,这方面的代码网上多得是,写个测试代码非常容易,可是真正运行起来可说是历经挫折.下面就是我最终调通并让程序在集群上 ...

  7. elasticsearch与mongodb分布式集群环境下数据同步

    1.ElasticSearch是什么 ElasticSearch 是一个基于Lucene构建的开源.分布式,RESTful搜索引擎.它的服务是为具有数据库和Web前端的应用程序提供附加的组件(即可搜索 ...

  8. 分布式集群环境下运行Wordcount程序

    1.分布式环境的Hadoop提交作业方式与本地安装的Hadoop作业提交方式相似,但有两点不同: 1)作业输入输出都存储在HDFS 2)本地Hadoop提交作业时将作业放在本地JVM执行,而分布式集群 ...

  9. Linux安装ElasticSearch与MongoDB分布式集群环境下数据同步

    ElasticSearch有一个叫做river的插件式模块,可以将外部数据源中的数据导入elasticsearch并在上面建立索引.River在集群上是单例模式的,它被自动分配到一个节点上,当这个节点 ...

随机推荐

  1. Windows程序设计(0)——编程之前

    Windows程序设计之前 1 做什么 2 解决什么问题 3 有哪些资源 在开始真正的编程之前,需要了解要做的事情是什么,要解决的解决的问题是什么,有哪些资源可以使用. 1 Windows程序设计之前 ...

  2. 报错:'Navigator is deprecated and has been removed from this package. It can now be installed

    报错:'Navigator is deprecated and has been removed from this package. It can now be installed ' +     ...

  3. iOS 两个tableview的 瀑布流

    iOS 两个tableview的 瀑布流1. [代码]Objective-C     ////  DocViewController.m//  getrightbutton////  Created ...

  4. PHP生成一个不重复随机数组的封装方法

    <?php /** array unique_rand( int $min, int $max, int $num )* 生成一定数量的不重复随机数* $min 和 $max: 指定随机数的范围 ...

  5. html5--5-16 综合实例绘制饼图

    html5--5-16 综合实例绘制饼图 实例 <!doctype html> <html> <head> <meta charset="utf-8 ...

  6. 【C/C++】任意大于1的整数分解成素数因子乘积的形式

    // #include<stdio.h> #include<math.h> #include<malloc.h> int isprime(long n); void ...

  7. HDU1525 Euclid's Game

    Two players, Stan and Ollie, play, starting with two natural numbers. Stan, the first player, subtra ...

  8. [BZOJ 1475] 方格取数

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1475 [算法] 首先将方格黑白染色 , 也就是说 , 如果(i + j)为奇数 , ...

  9. 【旧文章搬运】NtGlobalFlags

    原文发表于百度空间,2010-08-06========================================================================== - NtG ...

  10. Bishops

    题意: 给定一个 $n*n$ 的国际棋盘,求问在上面放 $K$ 个象的方案数. 解法: 首先可以发现黑格和白格互不干扰,这样我们可以将黑格,白格分别求出. 考虑 $f(i,j)$ 表示坐标化后考虑长度 ...