- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章nginx+redis实现session共享由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
上一篇我们介绍了nginx实现的负载均衡和动静分离,可看这边.
我们在文章的末尾说到,负载均衡需要面临的一个问题是内存数据的同步。例如:我有A,B两台服务器做了负载均衡,当我在A服务器上执行了登录并且将登录数据存入session的时候,这些session数据只存在于A服务器上,而没有在B服务器上,假如在处理下一个请求的时候,我需要用到session的数据,而不巧的是,这个请求刚好被交由B服务器来处理,这时候就会出现B服务器拿不到session数据的情况,从而造成错误.
这是一个无法避免的问题,有若干的解决方案,归结起来都是要实现session等数据在各负载均衡分支中的同步,第一种想到的方案是把这些数据放在mysql等数据库,也就是说存在磁盘,但是我们都知道session之所以出现是因为它是在内存中的,程序读取内存的数据要远远比读取磁盘的数据快,所以我们把一些经常用到的东西都放在session里面.
有没有一种数据库,是存放在内存中的呢?这就是redis。通俗的讲,它就是一个数据库,但是这个数据库是存在与内存里面的,所以存取起来速度要比读取磁盘的数据快得多。又因为它是一个数据库,所以可以实现数据的同步.
我们把session数据存放在redis中,然后所有的集群分支都可以去访问这个数据库里面的东西,这就是全局缓存的原理.
1.第一步是安装redis,我的服务器是windows的,下载的是免安装版本,解压以后就可以了,其目录如下。一开始redis是默认不需要密码,如果想要设置密码,可以进入redis.windows.conf文件下找到requirepass,删除前面的#号,在其后面便可以设置密码.
2.从cmd进入redis的根目录,键入如下指令:redis-server.exeredis.windows.conf。这样就可以启动redis了,如果启动成功,则会出现下面画面。当然还可以修改conf文件,加上密码。requirepass xxxxx 。
3.接下来我们就可以做一些配置工作,来实现session数据的全局缓存.
1)首先是添加jar包,如果你是maven项目,需要在pom.xml加入下面代码 。
1
2
3
4
5
6
7
|
<!-- redis -->
<
dependency
>
<
groupId
>org.springframework.session</
groupId
>
<
artifactId
>spring-session-data-redis</
artifactId
>
<
version
>1.3.1.RELEASE</
version
>
<
type
>pom</
type
>
</
dependency
>
|
如果不是maven项目,你需要加入下面这些jar包.
2)编写redis.properties,代码如下 。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
redis_isopen:
yes
#主机地址
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:
true
|
基本上与我们配置数据库的连接语句类似.
3)编写spring-redis.xml配置文件,这个文件配置关于redis的一些基本信息.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
standalone
=
"no"
?>
<
beans
xmlns
=
"http://www.springframework.org/schema/beans"
xmlns:context
=
"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 "
>
<!-- session设置 maxInactiveIntervalInSeconds为session的失效时间,单位为秒-->
<
bean
class
=
"org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"
>
<
property
name
=
"maxInactiveIntervalInSeconds"
value
=
"3600"
></
property
>
</
bean
>
<!-- redis连接池 -->
<
bean
id
=
"poolConfig"
class
=
"redis.clients.jedis.JedisPoolConfig"
>
<
property
name
=
"maxIdle"
value
=
"${redis_maxIdle}"
/>
<
property
name
=
"testOnBorrow"
value
=
"${redis_testOnBorrow}"
/>
</
bean
>
<!-- redis连接工厂 -->
<
bean
id
=
"connectionFactory"
class
=
"org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
>
<
property
name
=
"hostName"
value
=
"${redis_hostName}"
/>
<
property
name
=
"port"
value
=
"${redis_port}"
/>
<
property
name
=
"password"
value
=
"${redis_password}"
/>
<
property
name
=
"timeout"
value
=
"${redis_timeout}"
/>
<
property
name
=
"poolConfig"
ref
=
"poolConfig"
></
property
>
</
bean
>
</
beans
>
|
4)在application.xml(spring的主配置文件)需要加入redis.properties配置文件的扫描,如下.
1
2
3
4
5
6
7
8
9
|
<!-- 读取redis参数配置 -->
<
bean
id
=
"propertyConfigurer"
class
=
"org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
>
<
property
name
=
"locations"
>
<
list
>
<
value
>/WEB-INF/classes/redis.properties</
value
>
</
list
>
</
property
>
</
bean
>
|
5)在主配置文件中引入spring-redis.xml,如下.
1
|
<
import
resource
=
"spring-redis.xml"
/>
|
6)在web.xml中,加入关于session的过滤器,只有这样session才会被redis所操纵.
1
2
3
4
5
6
7
8
|
<
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
>
</
filter-mapping
>
|
这样以后,我们就实现了redis对session的管理.
7)我们可以安装一个redis的客户端来查看里面的数据,叫做Redis Desktop Manager。如下图,很好用,可以看到redis数据库中的数据.
PS.再退出的时候,需要这样写才不会出错。(ssh项目) 。
1
2
3
4
5
6
7
8
|
public
String yipinExit(){
Iterator<String>keys=session.keySet().iterator();
while
(keys.hasNext()){
String key=keys.next();
session.remove(key);
}
return
"yipinExit"
;
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.
原文链接:http://www.cnblogs.com/roy-blog/p/7196054.html 。
最后此篇关于nginx+redis实现session共享的文章就讲到这里了,如果你想了解更多关于nginx+redis实现session共享的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我有一个无所事事的盒子,已经运行了一段时间,今天,由于某种原因,当我尝试重新启动nginx时,得到了以下提示。 nginx: [emerg] host not found in upstream "w
我注意到,当我使用 ubuntu 命令“nginx”启动 nginx 并执行 systemctl status nginx 时。它表明 systemctl 已禁用。此外,如果我首先使用命令 syste
我的 nginx 配置如下: proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $re
周围有两个配置文件,/etc/nginx/conf.d/default.conf和 /etc/nginx/nginx.conf,但是启用了哪一个呢?我运行的是 CentOS6.4 和 nginx/1.
我的 Nginx 配置仅适用于根位置,所有其他位置都返回“Cannot GET {location}”,其中位置是域后地址的其余部分。 这是我的/etc/nginx/sites-enabled/def
我在 nginx 中为 node.js 服务器设置了反向代理。 server { listen 80; server_name sub.domain.tld; location
我的应用程序将在两个位置提供静态文件,一个是/my/path/project/static,另一个是/my/path/project/jsutils/static。 我很难让网络服务器在两个目录中查找
我的域名注册商的 DNS 访问我的服务器并获取 nginx 默认页面,因此配置正确 我复制了一个当前正在工作的 nginx 虚拟主机,更改了 server_name和 conf 文件的名称,仅此而已。
这个问题在这里已经有了答案: Can't login in to phpPgAdmin (2 个回答) 3年前关闭。 我在centos中遇到了phpPgAdmin登录的奇怪问题,我做了所有需要的事情
我要为PoC进行的操作是向来自动态后端服务器的网页添加href。使用“ subs_filter”可以很容易地添加href,但是我需要使用响应中嵌入的信息来构造href。 是否可以使用LUA处理来自pr
我有网站服务器,它有两个代理(鱿鱼,CF),它们使用不同的 header 来获取真实的 ip。 我猜 nginx 命令 set_real_ip_from ;real_ip_header X-Forwa
在控制台显示如下: Job for nginx.service failed because the control process exited witherror code. See "syste
我有一个问题,我怀疑是 NGINX 问题。基本上,当我尝试登录到我创建的网站时,出现以下错误…… 您要查找的页面暂时不可用。请稍后再试。 有没有人以前遇到过这个? 最佳答案 如果 NGINX 虚拟主机
这是我的 nginx 配置文件: server { listen 80; server_name localhost; location / {
在我的/etc/nginx/nginx.conf 文件中,我有配置。作为:- user nginx; worker_processes 1; error_log /var/log/nginx/e
有谁知道nginx支持软退出吗?这意味着它会一直运行直到所有连接都消失或超时(超过特定时间间隔)并且在此期间也不允许新连接吗? 例如: nginx stop nginx running (2 conn
有没有办法将 Nginx 配置为类似这样的直接服务器返回 (DSR) 负载平衡器: http://blog.haproxy.com/2011/07/29/layer-4-load-balancing-
我通过 apt-get 安装了 Nginx不久前在 Debian 上,我有几个网站在上面。现在我需要安装一些额外的模块,因为我不想搞砸任何事情,所以我想在执行之前仔细检查我的过程。希望这也能帮助其他不
我知道 Apache 的 pagespeed 模块可以使页面访问更快,所以,我想知道 Nginx 是否有等效的模块? 提前致谢! 最佳答案 https://github.com/pagespeed/n
如何将worker_rlimit_nofile设置为一个更大的数字,它可以是或建议最大为多少? 我正在尝试遵循以下建议: 大多数人遇到的第二个最大限制是 与您的操作系统有关。打开一个shell,su给
我是一名优秀的程序员,十分优秀!