龚哥哥 爱生活、做自己!
redis事务
发表于 2017-6-26 | 浏览(859) | 服务器

一、事务

1、开启事务 multi

2、提交事务 exec

3、取消事务 discard

127.0.0.1:6379> multi
OK
127.0.0.1:6379> set name devil
QUEUED
127.0.0.1:6379> set age 26
QUEUED
127.0.0.1:6379> exec
1) OK
2) OK
127.0.0.1:6379> get sex
"2"
127.0.0.1:6379> get age
"26"
127.0.0.1:6379>
127.0.0.1:6379> multi
OK
127.0.0.1:6379> set a 100
QUEUED
127.0.0.1:6379> del age
QUEUED
127.0.0.1:6379> discard
OK
127.0.0.1:6379> get age
"26"
127.0.0.1:6379> get a
(nil)
127.0.0.1:6379>

二、乐观锁

client-1 开启watch,并在事务中修改name

127.0.0.1:6379> get name
"mogui"
127.0.0.1:6379> watch name
OK
127.0.0.1:6379> multi
OK
127.0.0.1:6379> set name devil
QUEUED
127.0.0.1:6379> exec
(nil)

client-2 修改name

127.0.0.1:6379> get name
"mogui"
127.0.0.1:6379> get name
"mogui"
127.0.0.1:6379> set name gong
OK
127.0.0.1:6379> get name
"gong"

client-1 执行exec

127.0.0.1:6379> exec
(nil)
127.0.0.1:6379> get name
"gong"

三、发布及订阅消息

client-1 订阅devil频道

127.0.0.1:6379> subscribe devil
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "devil"
3) (integer) 1

client-2 发布频道devil消息

127.0.0.1:6379> publish devil hello
(integer) 1

client-1 订阅频道接收到的消息

1) "message"
2) "devil"
3) "hello"

阅读全文

redis分布式缓存搭建与使用
发表于 2017-6-11 | 浏览(1307) | 服务器

redis官网地址 https://redis.io/

准备6台redis服务

127.0.0.1:7000
127.0.0.1:7001
127.0.0.1:7002
127.0.0.1:7003
127.0.0.1:7004
127.0.0.1:7005

下载安装redis

mkdir /data
cd /data
mkdir src soft
cd /data/src/
wget http://download.redis.io/releases/redis-3.2.9.tar.gz
tar -zxvf redis-3.2.9.tar.gz
cd redis-3.2.9
make
make install

创建集群位置

cd /data/soft
mkdir redis-cluster
cd redis-cluster
mkdir 7000 7001 7002 7003 7004 7005

拷贝配置文件

cp /data/src/redis-3.2.9/redis.conf /data/soft/redis-cluster/7000
cp /data/src/redis-3.2.9/src/redis-trib.rb /data/soft/redis-cluster/

修改配置文件

cd /data/soft/redis-cluster/7000
vim redis.conf 修改以下选项内容

port 7000
daemonize yes  # 后台启动
cluster-enabled yes
cluster-config-file nodes.conf  # 指定集群节点配置文件
cluster-node-timeout 5000  # 指定集群节点超时时间
appendonly yes  # 指定持久化方式

完成后分别把 redis.conf 文件拷贝到7000 ~ 7005目录下,修改 port 对应目录名称的端口号

完成后分别启动6个实例

重启需要先删除数据文件和节点配置文件

for((i=0;i<=5;i++)); do cd /data/soft/redis-cluster/700$i/;rm -rf appendonly.aof  dump.rdb nodes.conf; done

循环启动redis

for((i=0;i<=5;i++)); do cd /data/soft/redis-cluster/700$i/; redis-server redis.conf; done

创建集群

/data/soft/redis-cluster/redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005

循环停止redis服务

for((i=0;i<=5;i++)); do redis-cli -c -p 700$i shutdown; done

启动redis服务,启动完成后查看实例列表 [ ps -ef | grep redis ],如下显示则说明启动成功

ps -ef | grep redis

root      32631      1  0 15:39 ?        00:00:00 redis-server 127.0.0.1:7000 [cluster]
root      32668      1  0 15:40 ?        00:00:00 redis-server 127.0.0.1:7001 [cluster]
root      32675      1  0 15:40 ?        00:00:00 redis-server 127.0.0.1:7002 [cluster]
root      32682      1  0 15:41 ?        00:00:00 redis-server 127.0.0.1:7003 [cluster]
root      32687      1  0 15:41 ?        00:00:00 redis-server 127.0.0.1:7004 [cluster]
root      32696      1  1 15:41 ?        00:00:00 redis-server 127.0.0.1:7005 [cluster]

安装ruby,由于redis集群客户端实现很少,redis集群的启动需要用到ruby实现的redis-trib.rb,所以我们需要先安装ruby。

yum -y install ruby

安装gem

yum -y install rubygems

gem 安装 redis ruby 接口

gem install redis

创建redis集群

--replicas 则指定了为 Redis Cluster 中的每个 Master 节点配备几个 Slave 节 点,节点角色由顺序决定,先 master 之后是 slave

/data/soft/redis-cluster/redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005


>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:7000
127.0.0.1:7001
127.0.0.1:7002
Adding replica 127.0.0.1:7003 to 127.0.0.1:7000
Adding replica 127.0.0.1:7004 to 127.0.0.1:7001
Adding replica 127.0.0.1:7005 to 127.0.0.1:7002
M: 443adf2c1d13b41866dc0dc108ffe858d931ac77 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
M: d74ee0110d7bcf0858ca76eb32caeca5094fa5ff 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
M: 13fd8b25f070382657c700fc9d8184418a82f8b6 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
S: 229a83d4997539771bf6792455c992cd215b7273 127.0.0.1:7003
   replicates 443adf2c1d13b41866dc0dc108ffe858d931ac77
S: 179a946b3ab66fac33d19316076e8c93d607a53a 127.0.0.1:7004
   replicates d74ee0110d7bcf0858ca76eb32caeca5094fa5ff
S: f6894bfcca6a22f426722a9b9843ba95cd20bb53 127.0.0.1:7005
   replicates 13fd8b25f070382657c700fc9d8184418a82f8b6
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join.
....
>>> Performing Cluster Check (using node 127.0.0.1:7000)
M: 443adf2c1d13b41866dc0dc108ffe858d931ac77 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: d74ee0110d7bcf0858ca76eb32caeca5094fa5ff 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: 13fd8b25f070382657c700fc9d8184418a82f8b6 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 229a83d4997539771bf6792455c992cd215b7273 127.0.0.1:7003
   slots: (0 slots) slave
   replicates 443adf2c1d13b41866dc0dc108ffe858d931ac77
S: f6894bfcca6a22f426722a9b9843ba95cd20bb53 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 13fd8b25f070382657c700fc9d8184418a82f8b6
S: 179a946b3ab66fac33d19316076e8c93d607a53a 127.0.0.1:7004
   slots: (0 slots) slave
   replicates d74ee0110d7bcf0858ca76eb32caeca5094fa5ff
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.


这里输入 [ yes ] 如下
Can I set the above configuration? (type 'yes' to accept): yes

检查集群 状态

/data/soft/redis-cluster/redis-trib.rb check 127.0.0.1:7000


>>> Performing Cluster Check (using node 127.0.0.1:7000)
M: 443adf2c1d13b41866dc0dc108ffe858d931ac77 127.0.0.1:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: d74ee0110d7bcf0858ca76eb32caeca5094fa5ff 127.0.0.1:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: 13fd8b25f070382657c700fc9d8184418a82f8b6 127.0.0.1:7002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 229a83d4997539771bf6792455c992cd215b7273 127.0.0.1:7003
   slots: (0 slots) slave
   replicates 443adf2c1d13b41866dc0dc108ffe858d931ac77
S: f6894bfcca6a22f426722a9b9843ba95cd20bb53 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 13fd8b25f070382657c700fc9d8184418a82f8b6
S: 179a946b3ab66fac33d19316076e8c93d607a53a 127.0.0.1:7004
   slots: (0 slots) slave
   replicates d74ee0110d7bcf0858ca76eb32caeca5094fa5ff
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

连接redis服务测试一下

[root@My src]# redis-cli -c -p 7000
127.0.0.1:7000> set name 200
-> Redirected to slot [5798] located at 127.0.0.1:7001
OK
127.0.0.1:7001> set devil mogui
-> Redirected to slot [893] located at 127.0.0.1:7000
OK
127.0.0.1:7000> get devil
"mogui"
127.0.0.1:7000> quit
[root@My src]# redis-cli -c -p 7003
127.0.0.1:7003> get devil
-> Redirected to slot [893] located at 127.0.0.1:7000
"mogui"
127.0.0.1:7000> 
127.0.0.1:7000> quit
[root@My src]# redis-cli -c -p 7002
127.0.0.1:7002> get name
-> Redirected to slot [5798] located at 127.0.0.1:7001
"200"
127.0.0.1:7001> get devil
-> Redirected to slot [893] located at 127.0.0.1:7000
"mogui"
127.0.0.1:7000> set age 20
OK
127.0.0.1:7000> quit
[root@My src]# redis-cli -c -p 7005
127.0.0.1:7005> get age
-> Redirected to slot [741] located at 127.0.0.1:7000
"20"
127.0.0.1:7000> set sex 30
OK
127.0.0.1:7000> quit
[root@My src]# redis-cli -c -p 7001
127.0.0.1:7001> get sex
-> Redirected to slot [2584] located at 127.0.0.1:7000
"30"
127.0.0.1:7000>

使用add-node命令来添加节点,第一个参数是新节点的地址,第二个参数是任意一个已经存在的节点的IP和端口

添加一个从节点,使用[ --slave ] 参数

/data/soft/redis-cluster/redis-trib.rb add-node --slave 127.0.0.1:7007 127.0.0.1:7000

添加从节点还可以指定这个从节点的主节点id,使用[ --master-id ] 参数指定masterid

/data/soft/redis-cluster/redis-trib.rb add-node --slave --master-id 3c3a0c74aae0b56170ccb03a76b60cfe7dc1912e 127.0.0.1:7007 127.0.0.1:7000

重新分配哈希槽

/data/soft/redis-cluster/redis-trib.rb reshard 127.0.0.1:7006

移除一个节点,第一个参数是集群中的任意一个节点地址,第二个是需要移除的节点id(移除前请确保节点哈希槽数量是空的)当移除一个master节点的时候,如果节点有哈希槽则失败

/data/soft/redis-cluster/redis-trib.rb del-node 127.0.0.1:7000 fea8868aeb4ce79d447b602e1cf07b4318adb852

改变从节点的主节点id[ CLUSTER REPLICATE <master-node-id> ](如把7007的主节点id改成7001的id)

[root@My redis-cluster]# redis-cli -c -p 7007
127.0.0.1:7007> cluster replicate cba8ca301ef0fc8039af8313d580b0a5bc8ea8cf
OK


添加一个新节点演示

cd /data/soft/redis-cluster/
cp -r 7005 7006
cd 7006
rm -rf dump.rdb appendonly.aof nodes.conf
vim redis.conf # 修改7005为7006保存
redis-server redis.conf

使用add-node命令来添加节点,第一个参数是新节点的地址,第二个参数是任意一个已经存在的节点的IP和端口. 我们可以看到新的节点已经添加到集群中

/data/soft/redis-cluster/redis-trib.rb add-node 127.0.0.1:7006 127.0.0.1:7000

查看7006节点是否正常加入了集群(可以看出 7006 成为了master节点,但是还未分配哈希槽)

[root@My redis-cluster]# redis-cli -p 7000 cluster nodes

e3cb1f4805d31dc0dc11b1e5539653b2c4144216 127.0.0.1:7000 myself,master - 0 0 1 connected 0-5460
c7a0afa1c64045bad02de7478cc23f1d65a3b035 127.0.0.1:7006 master - 0 1497061063704 0 connected
64d1a9e01ca3820288c7e5c4adc3a161b7816895 127.0.0.1:7005 slave 20c27ce2235485f2d5aa5d941674d7f81ee22682 0 1497061064812 6 connected
dffe09f5b1739dc5b53f8299d20b06508eccb15d 127.0.0.1:7004 slave cba8ca301ef0fc8039af8313d580b0a5bc8ea8cf 0 1497061065820 5 connected
20c27ce2235485f2d5aa5d941674d7f81ee22682 127.0.0.1:7002 master - 0 1497061064309 3 connected 10923-16383
cba8ca301ef0fc8039af8313d580b0a5bc8ea8cf 127.0.0.1:7001 master - 0 1497061065316 2 connected 5461-10922
3f363ffcacfa225ee098819b67ab4bd5d0e5def8 127.0.0.1:7003 slave e3cb1f4805d31dc0dc11b1e5539653b2c4144216 0 1497061063805 4 connected

给7006分配哈希槽

[root@My redis-cluster]# /data/soft/redis-cluster/redis-trib.rb reshard 127.0.0.1:7006

How many slots do you want to move (from 1 to 16384)? 需要分配的哈希槽数量 如:100
What is the receiving node ID? 7006节点的ID 如:c7a0afa1c64045bad02de7478cc23f1d65a3b035
Source node #1:需要从哪个节点分配哈希槽,all则从所有master节点上随机凑100个哈希槽。或者输入节点ID换行以done结束。如输入7001的节点id:cba8ca301ef0fc8039af8313d580b0a5bc8ea8cf
Source node #2:done
Do you want to proceed with the proposed reshard plan (yes/no)? 打印被移动的 slot 后,输入 yes 开始移动 slot 以及对应的数据

哈希槽分配完成,可以看出7006刚好100个哈希槽数量,7001也刚好少了100个哈希槽

[root@My redis-cluster]# redis-cli -p 7000 cluster nodes

e3cb1f4805d31dc0dc11b1e5539653b2c4144216 127.0.0.1:7000 myself,master - 0 0 1 connected 0-5460
c7a0afa1c64045bad02de7478cc23f1d65a3b035 127.0.0.1:7006 master - 0 1497061327609 7 connected 5461-5560
64d1a9e01ca3820288c7e5c4adc3a161b7816895 127.0.0.1:7005 slave 20c27ce2235485f2d5aa5d941674d7f81ee22682 0 1497061325594 6 connected
dffe09f5b1739dc5b53f8299d20b06508eccb15d 127.0.0.1:7004 slave cba8ca301ef0fc8039af8313d580b0a5bc8ea8cf 0 1497061327609 5 connected
20c27ce2235485f2d5aa5d941674d7f81ee22682 127.0.0.1:7002 master - 0 1497061326097 3 connected 10923-16383
cba8ca301ef0fc8039af8313d580b0a5bc8ea8cf 127.0.0.1:7001 master - 0 1497061327106 2 connected 5561-10922
3f363ffcacfa225ee098819b67ab4bd5d0e5def8 127.0.0.1:7003 slave e3cb1f4805d31dc0dc11b1e5539653b2c4144216 0 1497061326602 4 connected

当移除一个master节点的时候,不能有哈希槽数量(把当7002前master节点的哈希槽转移到7006节点上去)

[root@My redis-cluster]# ./redis-trib.rb reshard 127.0.0.1:7002
How many slots do you want to move (from 1 to 16384)? 输入7002的哈希槽数量,16383-10923如:5461
What is the receiving node ID? 输入7006的id,如:c7a0afa1c64045bad02de7478cc23f1d65a3b035
Source node #1: 从哪个节点分配哈希槽,这里输入7002的id,ID换行以done结束如:20c27ce2235485f2d5aa5d941674d7f81ee22682
Source node #2:done
Do you want to proceed with the proposed reshard plan (yes/no)? 打印被移动的 slot 后,输入 yes 开始移动 slot 以及对应的数据

查看7002集群状态,由此可以看到7002节点已经没有哈希槽数量了,而7006已经多了5461个哈希槽数[ 10923-16383 ]

[root@My redis-cluster]# redis-cli -p 7000 cluster nodes
e3cb1f4805d31dc0dc11b1e5539653b2c4144216 127.0.0.1:7000 myself,master - 0 0 1 connected 0-5460
c7a0afa1c64045bad02de7478cc23f1d65a3b035 127.0.0.1:7006 master - 0 1497070326503 7 connected 5461-5560 10923-16383
64d1a9e01ca3820288c7e5c4adc3a161b7816895 127.0.0.1:7005 slave c7a0afa1c64045bad02de7478cc23f1d65a3b035 0 1497070326000 7 connected
dffe09f5b1739dc5b53f8299d20b06508eccb15d 127.0.0.1:7004 slave cba8ca301ef0fc8039af8313d580b0a5bc8ea8cf 0 1497070327004 5 connected
20c27ce2235485f2d5aa5d941674d7f81ee22682 127.0.0.1:7002 master - 0 1497070326000 3 connected
cba8ca301ef0fc8039af8313d580b0a5bc8ea8cf 127.0.0.1:7001 master - 0 1497070327507 2 connected 5561-10922
3f363ffcacfa225ee098819b67ab4bd5d0e5def8 127.0.0.1:7003 slave e3cb1f4805d31dc0dc11b1e5539653b2c4144216 0 1497070327004 4 connected

从集群中移除7002节点

[root@My redis-cluster]# ./redis-trib.rb del-node 127.0.0.1:7000 20c27ce2235485f2d5aa5d941674d7f81ee22682
>>> Removing node 20c27ce2235485f2d5aa5d941674d7f81ee22682 from cluster 127.0.0.1:7000
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

查看7002已经不在集群中了,原来的哈希槽已由7006接手,当前集群的数据也是不会受到任何影响

[root@My redis-cluster]# redis-cli -p 7000 cluster nodes
e3cb1f4805d31dc0dc11b1e5539653b2c4144216 127.0.0.1:7000 myself,master - 0 0 1 connected 0-5460
c7a0afa1c64045bad02de7478cc23f1d65a3b035 127.0.0.1:7006 master - 0 1497070633845 7 connected 5461-5560 10923-16383
64d1a9e01ca3820288c7e5c4adc3a161b7816895 127.0.0.1:7005 slave c7a0afa1c64045bad02de7478cc23f1d65a3b035 0 1497070634348 7 connected
dffe09f5b1739dc5b53f8299d20b06508eccb15d 127.0.0.1:7004 slave cba8ca301ef0fc8039af8313d580b0a5bc8ea8cf 0 1497070633343 5 connected
cba8ca301ef0fc8039af8313d580b0a5bc8ea8cf 127.0.0.1:7001 master - 0 1497070632843 2 connected 5561-10922
3f363ffcacfa225ee098819b67ab4bd5d0e5def8 127.0.0.1:7003 slave e3cb1f4805d31dc0dc11b1e5539653b2c4144216 0 1497070632340 4 connected

添加7002为master,7007为slave两个节点(可以看出7002为master还未分配哈希槽,7007为slave)

[root@My redis-cluster]# ./redis-trib.rb add-node 127.0.0.1:7002 127.0.0.1:7000
[root@My redis-cluster]# ./redis-trib.rb add-node --slave 127.0.0.1:7007 127.0.0.1:7000

[root@My redis-cluster]# redis-cli -p 7000 cluster nodes
e3cb1f4805d31dc0dc11b1e5539653b2c4144216 127.0.0.1:7000 myself,master - 0 0 1 connected 0-5460
c7a0afa1c64045bad02de7478cc23f1d65a3b035 127.0.0.1:7006 master - 0 1497078346719 7 connected 5461-5560 10923-16383
64d1a9e01ca3820288c7e5c4adc3a161b7816895 127.0.0.1:7005 slave c7a0afa1c64045bad02de7478cc23f1d65a3b035 0 1497078346216 7 connected
dffe09f5b1739dc5b53f8299d20b06508eccb15d 127.0.0.1:7004 slave cba8ca301ef0fc8039af8313d580b0a5bc8ea8cf 0 1497078347221 5 connected
bde1b4378b10c6c7fbb8ba62189fc4d8fbbc6da7 127.0.0.1:7007 slave a7aeb2c3bef177ff671c5304d3baf0a825025d3c 0 1497078347723 0 connected
cba8ca301ef0fc8039af8313d580b0a5bc8ea8cf 127.0.0.1:7001 master - 0 1497078347221 2 connected 5561-10922
a7aeb2c3bef177ff671c5304d3baf0a825025d3c 127.0.0.1:7002 master - 0 1497078346719 0 connected
3f363ffcacfa225ee098819b67ab4bd5d0e5def8 127.0.0.1:7003 slave e3cb1f4805d31dc0dc11b1e5539653b2c4144216 0 1497078348226 4 connected

把7002节点从集群中移除(7002还未分配哈希槽数量,所以可以直接移除成功)

[root@My redis-cluster]# ./redis-trib.rb del-node 127.0.0.1:7000 a7aeb2c3bef177ff671c5304d3baf0a825025d3c
>>> Removing node a7aeb2c3bef177ff671c5304d3baf0a825025d3c from cluster 127.0.0.1:7000
>>> Sending CLUSTER FORGET messages to the cluster...
>>> 127.0.0.1:7007 as replica of 127.0.0.1:7000
>>> SHUTDOWN the node.

把7007节点中添加点数据

[root@My redis-cluster]# ./redis-trib.rb del-node 127.0.0.1:7000 bde1b4378b10c6c7fbb8ba62189fc4d8fbbc6da7
>>> Removing node bde1b4378b10c6c7fbb8ba62189fc4d8fbbc6da7 from cluster 127.0.0.1:7000
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

PHP中使用方式

<?php

$redis = new RedisCluster(null, ['127.0.0.1:7000', '127.0.0.1:7001', '127.0.0.1:7002', '127.0.0.1:7003', '127.0.0.1:7004', '127.0.0.1:7005']);
var_dump($redis->set('gong', 'gongfuxiang'));
var_dump($redis->set('xxx', 'XIANG'));
var_dump($redis->get('devil'));
var_dump($redis->get('sex'));
var_dump($redis->get('age'));
var_dump($redis->get('ggg'));
var_dump($redis->get('ppp'));
?>

docker镜像下载

docker pull gongfuxiang/centos7.3-redis-cluster

阅读全文

CentOS下PHP7安装Redis扩展
发表于 2017-5-18 | 浏览(1094) | 服务器

下载扩展包

wget https://github.com/phpredis/phpredis/archive/php7.zip
mv php7.zip phpredis-php7.zip
unzip phpredis-php7.zip
cd phpredis-php7/
/data/soft/php7/bin/phpize
./configure --with-php-config=/data/soft/php7/bin/php-config
make && make install

php.ini中添加redis扩展

vim /data/soft/php7/lib/php.ini 
搜索 extension 在底部添加 redis 扩展记录
extension=redis.so

重启php-fpm后,查看扩展

/data/soft/php7/bin/php -m

阅读全文

Nginx+Lua+Redis搭建高并发服务
发表于 2016-10-2 | 浏览(2224) | 服务器

架构图


一、准备

1、服务器首先是需要安装redis服务,查看本博客中的另一篇文章
2、安装nginx+luq服务,查看本博客中的另一篇文章

二、下载lua redis库

lua-redis库地址 https://github.com/openresty/lua-resty-redis
cd /data/www/lua/vendor
git clone https://github.com/openresty/lua-resty-redis.git

三、vim nginx.conf,http中添加

lua_package_path "/data/www/lua/vendor/lua-resty-redis/lib/?.lua;;";

四、nginx server中操作redis

1、在nginx.conf中嵌入lua代码

location /hello {
	default_type 'text/json';
	local redis = require "resty.redis";
	local instance = redis:new();
	local host = "127.0.0.1";
	local port = 6379;
	local ok,err = instance:connect(host,port);
	if not ok then
	   ngx.log(ngx.ERR,err);
	   ngx.exit(ngx.HTTP_SERVICE_UNAVAILABLE);
	end
	local suc, err instance:set('devil', 'hello world')
	f not suc then
		ngx.say("error")
	else
		ngx.say(instance:get('instance'))
	end
}

2、在实际业务中,独立lua代码(下面是一个redis简单的demo)

location /hello {
    default_type 'text/json';
    lua_need_request_body on;
    content_by_lua_file /data/www/lua/www/redis.lua;
}

2.1、lua文件的代码   vim /data/www/lua/www/redis.lua

#!/usr/local/bin/lua

--[[
	redis操作demo
	Devil
	http://gongfuxiang.com
--]]

-- 引入redis库
local redis = require "resty.redis";

-- 实例化redis
local instance = redis:new();

-- redis配置参数
local host = "127.0.0.1";
local port = 6379;

-- 创建redis连接
local ok,err = instance:connect(host,port);
if not ok then
   ngx.log(ngx.ERR,err);
   ngx.exit(ngx.HTTP_SERVICE_UNAVAILABLE);
end

-- 获取客户端post过来的body数据
local request_body = ngx.req.get_body_data() or nil
if( request_body ~= nil )
then
	-- 客户端ip
    local client_ip =ngx.req.get_headers()["X-Real-IP"]
    if client_ip == nil then
            client_ip = ngx.req.get_headers()["x_forworded_for"]
    end
    if client_ip == nil then
            client_ip = ngx.var.remote_addr
    end

    -- 当前时间戳
    local time = os.time()

    -- 拼接redis数据
    local data = client_ip.."{-}"..time.."{-}"..request_body

    -- 队列方式存储redis数据
    local suc, err = instance:lpush('key_list', data)
    if not suc then
        ngx.say('{"code":-2, "msg":"操作失败"}')
    end
    ngx.say('{"code":0, "msg":"操作成功"}')
else
    ngx.say('{"code":-1, "msg":"数据有误"}')
end

五、测试,访问 http://localhost/hello

hello world

阅读全文

win安装PHP的Redis扩展
发表于 2015-8-31 | 浏览(588) | 服务器

在win下安装php的redis扩展非常的简单,下载一个.dll扩展包放到php的ext目录下,在php.ini里边添加一行配置就可以了。

下载扩展包:

下载地址:https://github.com/nicolasff/phpredis/downloads

安装扩展包

解压后把dll放到php的ext目录下,打开php.ini 添加
extension=php_igbinary.dll
extension=php_redis.dll

检测是否安装成功,可以打开phpinfo看下
安装成功后,php测试

安装完成,进行测试

vim redis_test.php
<?php
  $r = new Redis();
  $r->connect('127.0.0.1', 6379);
  $r->set('devil', 'Hello World!', 10);
  echo $r->get('devil');
?>

阅读全文

CentOS安装Redis服务
发表于 2015-8-31 | 浏览(685) | 服务器

1;下载安装包

官网地址:http://www.redis.io/download

下载地址:http://download.redis.io/releases/redis-3.0.3.tar.gz

wget http://download.redis.io/releases/redis-3.0.3.tar.gz

2;解压

tar -zxvf redis-3.0.3.tar.gz
cd redis-3.0.3

3;编译

make
make && install

4;创建开机启动

vim /etc/init.d/redis-start 保存
/data/soft/redis-3.0.3/src/redis-server &

vim /etc/init.d/redis-stop 保存
/data/soft/redis-3.0.3/src/redis-cli shutdown

vim /etc/init.d/redis-cli 保存
/data/soft/redis-3.0.3/src/redis-cli
chmod 755 /etc/init.d/redis-*
echo "/etc/init.d/redis-start" >> /ect/rc.local

阅读全文

Redis同一台服务器开启不同工作空间
发表于 2015-8-30 | 浏览(639) | 服务器

1;拷贝redis.conf配置文件

redis6379.conf
pidfile /var/run/redis6380.pid
port 6380

redis6380.conf
pidfile /var/run/redis6380.pid
port 6380

2;启动redis同时加载配置文件

redis-server redis6379.conf &
redis-server redis6380.conf &

3;php测试

<?php

	/**
	 * redis测试
	 */

	$redis = new Redis();
	$redis->connect('127.0.0.1', 6379);
	$redis->set('devil', 'hello world!');
	echo $redis->get('devil');


	echo '<br />---------------------<br />';

	$redis2 = new Redis();
	$redis2->connect('127.0.0.1', 6380);
	$redis2->set('devil', 'gongfuxiang');
	echo $redis2->get('devil');

	echo '<br />---------------------<br />';

	echo $redis->get('devil');


	// 输出
	hello world!
	---------------------
	gongfuxiang
	---------------------
	hello world!

?>

阅读全文

TOP