---
title: "Redis服务快速部署"
author: "bert82503"
date: "2014-11-15T10:51:31Z"
tags: []
categories: ["互联网"]
slug: "redis"
draft: false
lastmod: "2014-12-22T11:00:43Z"
id: 2156351
url: "https://bert82503.iteye.com/blog/2156351"
---

官方对Redis的阐述：

[Redis](http://redis.io/) is an open source, BSD licensed, advanced **key-value cache** and **store**. It is often referred to as a **data structure server** since keys can contain [strings](http://redis.io/topics/data-types-intro#strings), [hashes](http://redis.io/topics/data-types-intro#hashes), [lists](http://redis.io/topics/data-types-intro#lists), [sets](http://redis.io/topics/data-types-intro#sets), [sorted sets](http://redis.io/topics/data-types-intro#sorted-sets), [bitmaps](http://redis.io/topics/data-types-intro#bitmaps) and [hyperloglogs](http://redis.io/topics/data-types-intro#hyperloglogs).

[Redis](http://redis.io/)是一个开源的，BSD许可的，高级的“**键-值**”**缓存**与**存储**。它经常被称为是一个**数据结构服务器**，因为[键](http://redis.readthedocs.org/en/latest/key/index.html)可以[字符串](http://redis.readthedocs.org/en/latest/string/index.html)、[哈希表](http://redis.readthedocs.org/en/latest/hash/index.html)、[列表](http://redis.readthedocs.org/en/latest/list/index.html)、[集合](http://redis.readthedocs.org/en/latest/set/index.html)、[有序集合](http://redis.readthedocs.org/en/latest/sorted_set/index.html)、位图和[hyperloglogs](http://redis.io/topics/data-types-intro#hyperloglogs)。

 

“[Introduction to Redis](http://redis.io/topics/introduction)”概要地介绍了Redis的关键特性和功能：

01. **atomic operations** (所有命令都是**原子操作**)
02. **in-memory dataset** (所有数据都放在**内存**中)
03. [具有有限存活时间的**键**](http://redis.io/commands/expire)
04. [LRU**缓存**](http://redis.io/topics/lru-cache)
05. **持久化**([RDB-内存快照|镜像](http://redis.io/topics/persistence#snapshotting)、[AOF-追加每个命令到日志](http://redis.io/topics/persistence#append-only-file))
06. [**主从**异步**复制**](http://redis.io/topics/replication)
07. [Sentinel-**自动故障转移**](http://redis.io/topics/sentinel)
08. [事务](http://redis.io/topics/transactions)
09. [发布/订阅](http://redis.io/topics/pubsub)
10. [Lua脚本](http://redis.io/commands/eval)
11. 推荐使用**Linux**系统部署

和常见命令的**使用场景**：

1. [增加**哈希表**中的值](http://redis.io/commands/hincrby)(某物不同维度的**计数器**或属性)
2. [压入一个元素到一个**列表**](http://redis.io/commands/lpush)(消息列表、队列)
3. [从**有序集合**中获取排名最高的成员](http://redis.io/commands/zrangebyscore)(TOP N、热点实时新闻、热点搜索)

\[参考]

1. [Redis 命令中文版](http://redis.readthedocs.org/en/latest/)， [huangz](http://weibo.com/huangz1990)同学的翻译

 

【动手实践】

实践目标：探索高可靠和可扩展的分布式Redis缓存方案

 

在实践过程中有不懂的地方，都会去看看 [Documentation](http://redis.io/documentation) 有没我想要的东东~

 

从Redis源代码编译和安装的步骤，如下：

1\. 从[Download](http://redis.io/download)下载最新的稳定版并解压

```
$ wget http://download.redis.io/releases/redis-2.8.17.tar.gz
$ tar -xzf redis-2.8.17.tar.gz
```

 

 

2\. 在解压文件之后，请务必先仔细阅读发布文件(redis-2.8.17)下的 [INSTALL](https://github.com/EdwardLee03/redis-java-client-profile/blob/master/redis/2.8.17/INSTALL) 和 [README](https://github.com/EdwardLee03/redis-java-client-profile/blob/master/redis/2.8.17/README) (里面有一些很重要的信息等待你去了解，能为你日后节省不少时间哦！你懂~ ^\_^)

 

3\. 按照 [README](https://github.com/EdwardLee03/redis-java-client-profile/blob/master/redis/2.8.17/README) 里的步骤说明来构建Redis、运行Redis、玩转Redis、安装Redis

3.1 构建Redis（务必在服务器上运行**测试**，确保所有命令都是可用的）

```
$ cd redis-2.8.17
$ make
$ make test
```

    构建时，我遇到提示"缺少C++编译器"的错误，并会提示可用的安装包("sudo apt-get install **g++**")。

 

3.2 运行Redis服务器

```
$ src/redis-server
```

 

3.3 玩转Redis客户端

```
$ src/redis-cli
redis> set foo bar
OK
redis> get foo
"bar"
```

 

3.4 安装Redis服务

```
$ sudo make install
```

 

4\. 若使用 [utils/install\_server.sh](https://github.com/EdwardLee03/redis-java-client-profile/blob/master/redis/2.8.17/utils/install_server.sh) 来生成**初始化脚本**、**配置文件**、**安装并启动服务**的话，可以大大地节省配置的时间。但在使用之前，请先浏览一下其实现，它会使用 [redis.conf](https://github.com/EdwardLee03/redis-java-client-profile/blob/master/redis/2.8.17/redis.conf "redis.conf") 作为模板配置文件 (注意：服务已作为系统的一部分，会在系统重启时**自动启动**！)。所以，若有些配置不满足你的需求，得先修改。

```
$ sudo sh install_server.sh
```

 

5\. 在修改默认的配置属性之前，请浏览 [redis.conf](https://github.com/EdwardLee03/redis-java-client-profile/blob/master/redis/2.8.17/redis.conf "redis.conf")。若要修改默认的一些配置属性，可以参考 [Redis configuration](http://redis.io/topics/config)，更详细的信息就参考 [Documentation](http://redis.io/documentation) 的 **Administration** 主题内容

 

6\. “[Redis as an LRU cache](http://redis.io/topics/lru-cache)”，Redis作为一个LRU缓存的配置参考 (内容很有价值！)

 

```
# 同步修改"ulimit -n"的值
maxclients 65536

maxmemory 128MB

maxmemory-policy allkeys-lru

maxmemory-samples 10

# 注释掉 RDB 的所有触发规则
#save 900 1
#save 300 10
#save 60 10000
```

  

7\. “Master-Slave”主从架构的配置修改，请参考 [@江南白衣](http://weibo.com/calvin1978) 整理的关于Redis方面最全面的 [Redis springside4](https://github.com/springside/springside4/wiki/Redis) 的 “[附录1： 对redis.conf默认配置的修改](https://github.com/springside/springside4/wiki/Redis#user-content-%E9%99%84%E5%BD%951-%E5%AF%B9redisconf%E9%BB%98%E8%AE%A4%E9%85%8D%E7%BD%AE%E7%9A%84%E4%BF%AE%E6%94%B9)”

 

```
daemonize yes
```

  

8\. 当 [redis.conf](https://github.com/EdwardLee03/redis-java-client-profile/blob/master/redis/2.8.17/redis.conf "redis.conf") 中的相关属性都修改完成后，就可以执行 [utils/install\_server.sh](https://github.com/EdwardLee03/redis-java-client-profile/blob/master/redis/2.8.17/utils/install_server.sh) 脚本了

 

9\. 使用“sudo **netstat** -anp | grep redis”来确认所有Redis服务是否都已启动成功

 

至此，所有Redis服务都启动完毕。

 

 

玩的开心！^\_^