您的位置:首页 > 数据库 > Redis

Windows与Linux下部署redis及Java示例

2016-11-04 20:32 477 查看
Redis Introduction:

Redis is an open source (BSD licensed), in-memory data structure store, used as database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs and geospatial indexes with radius queries. Redis has built-in replication, Lua scripting, LRU eviction, transactions and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.(http://redis.io/)

Redis项目并不直接支持Windows,Windows版项目是由微软开放技术团队建立和维护的一个实验性项目(),支持32,、64位,所以并不适用于生产环境,但可供初学者练习使用或者做开发测试。在Windows系统部署时遇到的坑比较多,在此总结一下:

Redis Windows版本安装部署

Windows版本项目地址:https://github.com/MSOpenTech/redis。在github下载所需版本,解压到本地目录后用Visual Studio进行build

Open the solution file msvs\redisserver.sln in Visual Studio, select a build configuration (Debug or Release) and target (x64) then build.

This should create the following executables in the

\msvs$(Target)$(Configuration) folder:

redis-server.exe

redis-benchmark.exe

redis-cli.exe

redis-check-dump.exe

redis-check-aof.exe

2.在成功build之后的目录下,双击运行redis-server.exe或者用命令行redis-server,成功后新开个命令行窗口运行redis-cli或者双击redis-cli.exe,如下图所示则表示成功。





3.本地测试已成功,那么如何让远程客户端使用呢?一个比较简单的方法:在redis目录下redis-3.2\msvs\setups\documentation找到redis.windows.conf文件,做如下修改:

# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#bind 127.0.0.1    //将原来的bind注释掉
bind xx.xx.xx.xx    //新bind本机ip地址

# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.


如此之后,需要运行如下命令启动redis服务端与客户端

redis-server redis.windows.conf
redis-cli redis.windows.conf


4.如此之后如果远程客户端还不能正常使用,则s试下此命令:telnet xx.xx.xx.xx 6379,即redis安装的服务器ip地址与端口号(默认是6379),如果连接失败,可能是被防火墙墙了,打开 控制面板->Windows防火墙->高级设置->入站规则->新建规则,将redis程序添加进去。此步骤结束以后,正常情况下客户端即可成功连接上远程服务器redis。而且还可以安装Redis Studio图形化界面查看redis的数据。

Redis Linux版本安装部署

个人使用的Linux系统是centos6.7版本,安装部署过程相对于Windows下要简单容易一些。

$ wget http://download.redis.io/releases/redis-3.2.5.tar.gz $ tar xzf redis-3.2.5.tar.gz
$ cd redis-3.2.5
$ make

The binaries that are now compiled are available in the src directory. Run Redis with:
$ src/redis-server

You can interact with Redis using the built-in client:
$ src/redis-cli
redis> set hello world
OK
redis> get hello
"world"


在Linux下如果也想远程客户端连接,则也需要做以上第3步,配置文件名是redis.conf,正常情况下至此会安装成功。

Java示例

使用Jedis操作redis:

public class JedisTest {
public static void main(String[] argv) {
Jedis jedis = new Jedis("redis://xx.xx.xx.xx:6379/2");
jedis.flushDB();

// get set
jedis.set("hello", "world");
print(1, jedis.get("hello"));
jedis.rename("hello", "newhello");
print(1, jedis.get("newhello"));
jedis.setex("hello2", 1800, "world");
jedis.set("pv", "100");
jedis.incr("pv");
jedis.incrBy("pv", 5);
print(2, jedis.get("pv"));
jedis.decrBy("pv", 2);
print(2, jedis.get("pv"));

print(3, jedis.keys("*"));

String listName = "list";
jedis.del(listName);
for (int i = 0; i < 10; ++i) {
jedis.lpush(listName, "a" + String.valueOf(i));
}
print(4, jedis.lrange(listName, 0, 12));
print(4, jedis.lrange(listName, 0, 3));
print(5, jedis.llen(listName));
print(6, jedis.lpop(listName));
print(7, jedis.llen(listName));
print(8, jedis.lrange(listName, 2, 6));
print(9, jedis.lindex(listName, 3));
print(10, jedis.linsert(listName, BinaryClient.LIST_POSITION.AFTER, "a4", "xx"));
print(10, jedis.linsert(listName, BinaryClient.LIST_POSITION.BEFORE, "a4", "bb"));
print(11, jedis.lrange(listName, 0 ,12));
}
public static void print(int index, Object obj) {
System.out.println(String.format("%d, %s", index, obj.toString()));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  redis java