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

redis环境搭建(Linux)、Jredis

2014-11-02 23:29 411 查看
[b]简介[/b]

  1. NoSql是以key-value形式存储,和传统的关系型数据库不一样,不一定遵循传统数据库的一些基本要求,比如说遵循SQL标准,ACID属性,表结构等等,这类数据库主要有一下特点:非关系型的,分布式的,开源的,水平可扩展的。

  2. NoSql的特点:
a) 处理超大量的数据。
b) 运行在便宜的pc服务器集群上
c) 击碎了性能瓶颈。

  3. NoSql适用场景
a) 对数据高并发读写。(咱们在对mysql进行上万次的写操作,那么咱们的硬盘io就可能无法承受了)
b) 对海量数据的高效率存储和访问。
c) 对数据的高可扩展和高可用性。(数据库可以增加一个服务器节点来完成数据迁移和数据库维护)

  4. Redis 是完全开源免费的,遵守BSD协议,先进的key - value持久化产品。它通常被称为(数据结构服务器),因为值(value)可以是 字符串(String), 哈希(Map), 链表(list), 集合(sets) 和 有序集合(sorted sets)等类型.

  5. Redis是一个key-value存储系统。它支持存储的value类型很多,包括string,list,set,zset(有序集合).这些数据类型都支持push/pop,add/remove及取交集和并集及更丰富的操作,Redis支持各种不同方式的排序(因为redis中有zset数据结构,这是通过一个键来存储它的顺序)。为了保证效率,数据都是缓存在内存中,它可以周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件。(把修改操作写入追加的记录文件这个类似于mysql中的bin-log,存储的都是更新数据,插入数据和删除数据的相关操作)

  6. Redis提供的API语言:http://www.redis.io/clients

  7. Redis适用场合
    a) 应用程序直接访问Redis数据库

    注意:这样直接访问Redis数据库可能存在某些问题,读写都是直接操作数据库,这样做比较简单,但是如果某天我的某台Redis数据库down掉了,那我的数据就会永久的丢失。

    b) 应用程序直接访问Redis,只有Redis访问失败时才访问Mysql

    分析:首先我们的应用程序直接先访问我们的Redis server,向redis里面写。此时redis会跟后面的mysql集群进行同步。当redis服务down掉以后,应用服务器会去找后面的mysql。  

    Redis更加实用的场景:
    取最新N个数据的操作
    排行榜应用,取TOP N操作
    需要精确设定过期时间的应用(可以对键设置过期时间,这个mysql就无法做到)
    计数器应用
    Uniq操作,获取某段时间所有数据排重值
    实时系统,反垃圾系统。
    Pub/Sub构建实时消息系统。(发布与订阅是redis独有的系统)
    构建队列系统。
    缓存

  8. mysql与redis、mongodb的比较

Redis

Mysql

mongodb

有库的概念

有库的概念

有库的概念

无表的概念

有表的概念

集合

无字段的概念

有字段的概念(行,列)

无字段的概念

[b]redis安装[/b]

  1.下载地址http://www.redis.io/download

  2.可以在linux下运行如下命令进行安装

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


  安装完成后 /opt/redis-2.8.17/src目录下会出现编译后的redis服务程序redis-server,还有用于测试的客户端程序redis-cli

  3.下面启动redis服务

  [root@gitclient redis-2.8.17]# src/redis-server

[8491] 02 Nov 22:45:39.989 # Warning: no config file specified, using the default config. In order to specify a config file use src/redis-server /path/to/redis.conf
[8491] 02 Nov 22:45:39.991 * Increased maximum number of open files to 10032 (it was originally set to 1024).
[8491] 02 Nov 22:45:39.992 # Warning: 32 bit instance detected but no memory limit set. Setting 3 GB maxmemory limit with 'noeviction' policy now.
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 2.8.17 (00000000/0) 32 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in stand alone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 8491
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'

[8491] 02 Nov 22:45:39.995 # Server started, Redis version 2.8.17
[8491] 02 Nov 22:45:39.997 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
[8491] 02 Nov 22:45:39.997 * The server is now ready to accept connections on port 6379

  注意这种方式启动redis 使用的是默认配置。也可以通过启动参数告诉redis使用指定配置文件使用下面命令启动.

[root@gitclient redis-2.8.17]# src/redis-server  redis.conf

  redis.conf是一个默认的配置文件。我们可以根据需要使用自己的配置文件。
  启动redis服务进程后,就可以使用测试客户端程序redis-cli和redis服务交互了。

[b]redis测试[/b]

  1.输入以下命令

[root@gitclient redis-2.8.17]# src/redis-cli
127.0.0.1:6379> set yml bling
OK
127.0.0.1:6379> get yml
"bling"
127.0.0.1:6379>


  这里演示了get和set命令操作简单类型value的例子。foo是key ,bar是个string类型的value
  没linux的可以通过这个在线的来练习,当然在线版的很多管理相关的命令是不支持的。
  http://try.redis-db.com/

[b]java客户端使用[/b]

  1.客户端jar下载地址:http://www.java2s.com/Code/Jar/j/DownloadJRedisjar.htm

    MAVEN地址:http://mvnrepository.com/artifact/redis.clients/jedis/2.6.0

  2.在eclipse中新建一个maven项目,然后添加jredis包依赖。下面是个简单的实例

package com.bling.redis;

import redis.clients.jedis.Jedis;

/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
Jedis j = new Jedis("192.168.168.128",6379);
String key = "yml";
j.set(key, "hello redis jun !");
String value = new String(j.get(key));

String key2 = "count";
j.incr(key2);
j.incr(key2);
String value2 = new String(j.get(key2));
System.out.println( "value = "+value );
System.out.println( "value2 = "+value2 );
}
}


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>

<groupId>com.bling.redis</groupId>
<artifactId>com.bling.redis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>com.bling.redis</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- jredis(客户端工具) -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.0</version>
</dependency>
</dependencies>
</project>


运行结果如下:

value = hello redis jun !
value2 = 2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: