且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

webdis实现Redis的http接口及多数据格式共享 [含json,restful]

更新时间:2022-10-02 10:09:40

前言:

最近用tornado写了redis的接口,为自己的做cs之间的数据中转,以及后期给别部门的临时数据调用。这些东西难度真不大,但是很杂乱,还要保证性能。

朋友推荐了我这款redis的http的服务端利器,要功能他有,要性能他也有。。。


webdis是一个简单的 Web 服务器,提供了 HTTP 接口来访问 Redis 服务器,使用了 hiredis, jansson, libevent, and http-parser 等软件。


Redis 一直以来只提供纯文本操作协议(只有在 Cluster 中应用了二进制协议),这可能令很多推崇 RESTFul 的同学感觉不爽了,最近,一位名叫高手的同学业余开发了一个支持 HTTP 协议的 Redis Proxy,取名Webdis。其在Redis 的讨论区一发布,则引来一遍赞扬之声。(鼓掌!!!)

开始:

项目地址https://github.com/nicolasff/webdis/


1
2
3
4
5
git clone git://github.com/nicolasff/webdis.git
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
cd webdis
make
./webdis &


配置webdis

webdis实现Redis的http接口及多数据格式共享 [含json,restful]

但是提示错误~

为了彻底点,咱们把主要的开发包都搞下,redis-server也配置下 ~

1
yum install libevent-devel

1
yum install redis


webdis实现Redis的http接口及多数据格式共享 [含json,restful]

redis server 的启动 !

webdis实现Redis的http接口及多数据格式共享 [含json,restful]

编译文件及启动webdis~

webdis实现Redis的http接口及多数据格式共享 [含json,restful]

简单的测试下哈~

1
2
3
4
5
6
curl http://127.0.0.1:7379/SET/hello/world
→ {"SET":[true,"OK"]}
curl http://127.0.0.1:7379/GET/hello
→ {"GET":"world"}
curl -d "GET/hello" http://127.0.0.1:7379/
→ {"GET":"world"}


webdis实现Redis的http接口及多数据格式共享 [含json,restful]

webdis还支持post的方式 ~


webdis实现Redis的http接口及多数据格式共享 [含json,restful]

webdis支持很多的格式:

1
2
3
4
5
6
7
8
9
10
.json for application/json (this is the default Content-Type).
.msg for application/x-msgpack. See http://msgpack.org/ for the specs.
.txt for text/plain
.html for text/html
xhtml for application/xhtml+xml
xml for text/xml
.png for image/png
jpg or jpeg for image/jpeg
Any other with the ?type=anything/youwant query string.
Add a custom separator for list responses with ?sep=, query string.


下面是txt的格式 ~


webdis实现Redis的http接口及多数据格式共享 [含json,restful]

下面是json的格式 ~

webdis实现Redis的http接口及多数据格式共享 [含json,restful]


webdis也可以直接上传文件的 ~

1
2
Webdis supports file upload using HTTP PUT. The command URI is slightly different, as the last argument is taken from the HTTP body. For example: instead of /SET/key/value, the URI becomes /SET/key and the value is the entirety of the body. This works for other commands such as LPUSH, etc.
webdis 是支持文件上传的~


上传文件~

webdis实现Redis的http接口及多数据格式共享 [含json,restful]

查看上传好的页面 ~

webdis实现Redis的http接口及多数据格式共享 [含json,restful]

对于二进制文件的上传 ~

上传 ~

我上传了一个nginx文件,下载成一个nginx rpm文件 ~

webdis实现Redis的http接口及多数据格式共享 [含json,restful]

查看长传的情况 ~

webdis实现Redis的http接口及多数据格式共享 [含json,restful]

但是有个极大的问题是,二进制的文件不能太大 ~

咱们测试下在速度快的情况下他的大小能到多少~ 5.3m 没啥压力~速度很快 ~

webdis实现Redis的http接口及多数据格式共享 [含json,restful]

我测试了100M左右的文件~ 速度还算可以~ 在3.1s秒左右~

大小就这样了,这算是个小应用,可以做共享数据的时候,也能搞搞数据文件啥的,但是没有共享数据的应用,估计就没人这么蛋疼用redis存文件。

webdis实现Redis的http接口及多数据格式共享 [含json,restful]


webdis 也是支持ACL的,来源的ip地址限制以及基本认证的方式 !


webdis.json是webdis的配置文件 ~


这个可以配置webdis连接后端时候的所需要的信息,以及自己的配置信息。

哎呀~ 居然还有多线程和线程池的配置~ 不错不错 !!!

1
2
3
4
5
6
7
8
9
10
"redis_host":   "127.0.0.1",
"redis_port":   6379,
"redis_auth":   null,
"http_host":    "0.0.0.0",
"http_port":    7379,
"threads":      5,
"pool_size"20,
"daemonize":    false,
"websockets":   false,
"database":     0


下面是安全的信息~ 基本认证,ip地址的限制 ~

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
    "disabled": ["DEBUG""FLUSHDB""FLUSHALL"],
},
{
    "http_basic_auth""user:password",
    "disabled": ["DEBUG""FLUSHDB""FLUSHALL"],
    "enabled":  ["SET"]
},
{
    "ip":       "192.168.10.0/24",
    "enabled":  ["SET"]
},
{
    "http_basic_auth""user:password",
    "ip":       "192.168.10.0/24",
    "enabled":  ["SET""DEL"]
}


webdis还有个websocket的模式~


1
2
3
4
5
6
7
8
9
10
11
12
function testJSON() {
    var jsonSocket = new WebSocket("ws://127.0.0.1:7379/.json");
    jsonSocket.onopen = function() {
        console.log("JSON socket connected!");
        jsonSocket.send(JSON.stringify(["SET""hello""world"]));
        jsonSocket.send(JSON.stringify(["GET""hello"]));
    };
    jsonSocket.onmessage = function(messageEvent) {
        console.log("JSON received:", messageEvent.data);
    };
}
testJSON();


我们开始来个简单的性能测试吧~ (我原本想用gevent和curllib2来测试,但是没找到以前写过的demo例子。。。只能简单用ab来测试啦~)

10000个请求用了不到3秒解决 ~没有报错的~

webdis实现Redis的http接口及多数据格式共享 [含json,restful]

又测试了一遍~ (我晕,居然又到1.8啦~ )

webdis实现Redis的http接口及多数据格式共享 [含json,restful]

他的cpu情况,每个内核都有跑,每个核心都在有效的利用~ 不像python,nodejs那样只能单核的跑~

webdis实现Redis的http接口及多数据格式共享 [含json,restful]


好嘞~ 就介绍这里啦~~~ 希望这webdis 越来越好,github里有不少人fork他的代码,issue里面也是很热乎的~

我现在尽量用这个做redis的http,而不是像以前python自己写了。

总结:

webdis是个很方便的redis http接口程序~ 推荐大家使用~ 前提是你懒得自己写http的接口 ~



 本文转自 rfyiamcool 51CTO博客,原文链接:http://blog.51cto.com/rfyiamcool/1320624,如需转载请自行联系原作者