nginx +复述,实现会话共享

  

上一篇我们介绍了nginx实现的负载均衡和动静分离,可看这边。

  

我们在文章的末尾说到,负载均衡需要面临的一个问题是内存数据的同步。例如:我有A, B两台服务器做了负载均衡,当我在一个服务器上执行了登录并且将登录数据存入会话的时候,这些会话数据只存在于一个服务器上,而没有在B服务器上,假如在处理下一个请求的时候,我需要用到会话的数据,而不巧的是,这个请求刚好被交由B服务器来处理,这时候就会出现B服务器拿不到会话数据的情况,从而造成错误。

  

这是一个无法避免的问题,有若干的解决方案,归结起来都是要实现会话等数据在各负载均衡分支中的同步,第一种想到的方案是把这些数据放在mysql等数据库,也就是说存在磁盘,但是我们都知道会话之所以出现是因为它是在内存中的,程序读取内存的数据要远远比读取磁盘的数据快,所以我们把一些经常用到的东西都放在会话里面。

  

有没有一种数据库,是存放在内存中的呢?这就是复述。通俗的讲,它就是一个数据库,但是这个数据库是存在与内存里面的,所以存取起来速度要比读取磁盘的数据快得多。又因为它是一个数据库,所以可以实现数据的同步。

  

我们把会话数据存放在复述中,然后所有的集群分支都可以去访问这个数据库里面的东西,这就是全局缓存的原理。

  

1。第一步是安装复述,我的服务器是windows的下载的是免安装版本,解压以后就可以了,其目录如下。一开始复述,是默认不需要密码,如果想要设置密码,可以进入redis.windows.conf文件下找到requirepass,删除前面的#号,在其后面便可以设置密码。

  

 nginx +复述,实现会话共享

  

2。从cmd进入复述的根目录,键入如下指令:redis-server.exeredis.windows。相依这样就可以启动复述了,如果启动成功,则会出现下面画面。当然还可以修改之文件,加上密码.requirepass xxxxx

  

 nginx +复述,实现会话共享

  

3。接下来我们就可以做一些配置工作,来实现会话数据的全局缓存。

  

1)首先是添加jar包,如果你是maven项目,需要在pom.xml加入下面代码

        & lt; !——复述,比;   & lt; dependency>   & lt; groupId> org.springframework.session   & lt; artifactId> spring-session-data-redis   & lt; version> 1.3.1.RELEASE   & lt; type> pom   & lt;/dependency>      

如果不是maven项目,你需要加入下面这些jar包。

  

 nginx +复述,实现会话共享

  

2)编写redis.properties,代码如下

        redis_isopen:是的   #主机地址   redis_hostName=xxx.xxx.xxx.xxx   #端口   redis_port=6379   #密码   redis_password=xxxxxxxx   #连接超时时间   redis_timeout=200000   redis_maxIdle: 300   redis_maxActive: 600   redis_maxWait: 100000   redis_testOnBorrow:真      

基本上与我们配置数据库的连接语句类似。

  

3)编写spring-redis.xml配置文件,这个文件配置关于复述的一些基本信息。

        & lt; & # 63; xml version=" 1.0 " encoding=" utf - 8 "独立="不" & # 63;比;   & lt;豆类xmlns=" http://www.springframework.org/schema/beans "   xmlns:上下文=" http://www.springframework.org/schema/context " xmlns: tx=" http://www.springframework.org/schema/tx "   xmlns: util=" http://www.springframework.org/schema/util " xmlns: xsi=" http://www.w3.org/2001/XMLSchema-instance "   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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd”比;   & lt; !——会议设置maxInactiveIntervalInSeconds为会话的失效时间,单位为秒——比;   & lt;豆   类=皁rg.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration”比;   & lt;属性名=" maxInactiveIntervalInSeconds " value=" https://www.yisu.com/zixun/3600 "祝辞& lt;/property>   & lt;/bean>   & lt; !——复述,连接池——比;   & lt; bean id=皃oolConfig”类=皉edis.clients.jedis.JedisPoolConfig”比;   & lt;属性名=" maxIdle " value=" https://www.yisu.com/zixun/$ {redis_maxIdle} "/比;   & lt;属性名=" testOnBorrow " value=" https://www.yisu.com/zixun/$ {redis_testOnBorrow} "/比;   & lt;/bean>   & lt; !——复述,连接工厂——比;   & lt; bean id=" connectionFactory "   类=皁rg.springframework.data.redis.connection.jedis.JedisConnectionFactory”比;   & lt;属性名="主机" value=" https://www.yisu.com/zixun/$ {redis_hostName} "/比;   & lt;属性名="端口" value=" https://www.yisu.com/zixun/$ {redis_port} "/比;   & lt;属性名="密码" value=" https://www.yisu.com/zixun/$ {redis_password} "/比;   & lt;属性名="超时" value=" https://www.yisu.com/zixun/$ {redis_timeout} "/比;   & lt;属性名=" poolConfig“ref=皃oolConfig祝辞& lt;/property>   & lt;/bean>   & lt;/beans>

nginx +复述,实现会话共享