redis5集群搭建系列--单机环境(1)

2018-12-20  本文已影响29人  lcjyzm

1. Redis是什么?

开源的NoSql数据库
C语言编写
基于内存运行,并且支持持久化
Key value存储
是主流的Nosql数据库之一

2. Redis的发行版本解释

格式: major.minor.patchlevel
说明: major 主版本号
      minor 次版本号,如果为偶数表示当前版本是一个稳定版本,否则是一个非稳定版本(不适合生产环境使用)
      patchlevel 补丁bug修复

3. Redis 5.0.2 新增特性

Redis 5.0版本引入了消费者组的新流数据类型、有序sort阻塞pop操作、RDB中的LFU/LRU信息、redis -cli内的集群管理器、活动碎片整理V2、HyperLogLogs改进和许多其他改进

4. 单机安装及代码测试

4.1 安装
1. 下载redis5.0.2二进制安装包
    wget http://download.redis.io/releases/redis-5.0.2.tar.gz
    
2. 解压到/opt目录下
    tar -xzvf redis-5.0.2.tar.gz -C /opt
    
3. 编译
    cd /opt/redis-5.0.2  && make
    
4. 指定安装位置
    make install PREFIX=/usr/local/redis
    
5. 拷贝安装目录下配置文件到 /usr/local/redis/etc
    mkdir /usr/local/redis/etc/
    cp  /opt/redis-5.0.2/redis.conf  /usr/local/redis/etc/
    
6. 修改配置文件 /usr/local/redis/etc/redis.conf
    vi /usr/local/redis/etc/redis.conf
    
    # 关闭保护模式
    protected-mode no
    
    # 以守护进程后台模式运行
    daemonize yes
    
    # 绑定本机ip
    bind 172.18.203.30
    
    # redis进程文件
    pidfile /usr/local/redis/redis_6379.pid
    
    # 日志文件
    logfile /usr/local/redis/log/redis_6379.log
    
    # 快照数据存放目录,一定是目录
    dir /usr/local/redis/data/
    
7. 启动redis
    /usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
    
8. 查看是否启动成功
    查看进程: 
    ps aux | grep redis
    
    查看日志:
    tail -fn 500 /usr/local/redis/log/redis_6379.log
    
    命令端验证:
    /usr/local/redis/bin/redis-cli -h 172.18.203.30 -p 6379
    
    172.18.203.30:6379> ping
    PONG
4.2 java客户端测试
4.2.1 Jedis直连方式
pom依赖
<!--jedis 客户端-->
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.0.0</version>
</dependency>
    
测试代码
public static void main(String[] args) {
    // 创建jedis对象
    Jedis jedis = new Jedis("172.18.203.30", 6379);
    // 操作string数据类型
    jedis.set("name", "helloworld");
    // 根据key取出对应的value值
    String value = jedis.get("name");
    // 值输出
    System.out.println(value);
    // 关闭连接
    jedis.close();
}
4.2.2 Jedis连接池方式
pom依赖
<!--jedis 客户端-->
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.0.0</version>
</dependency>

测试代码
public static void main(String[] args) {
    // 创建连接池
    JedisPool pool = new JedisPool("172.18.203.30", 6379);
    // 获得连接对象
    Jedis jedis = pool.getResource();
    // 操作hash类型
    Map<String, String> hash = new HashMap<>();
    hash.put("name", "tom");
    hash.put("age", "23");
    hash.put("address", "杭州公园大厦");
    jedis.hmset("student", hash);
    Map<String, String> all = jedis.hgetAll("student");
    Set<Map.Entry<String, String>> entrySet = all.entrySet();
    for (Map.Entry<String, String> entry : entrySet) {
        String key = entry.getKey();
        String value = entry.getValue();
        System.out.println(key + ":" + value);
    }

    // 关闭连接,连接池回收资源
    jedis.close();
    // 关闭连接池
    pool.close();
}
4.2.3 Lettuce连接
pom依赖
<!-- https://mvnrepository.com/artifact/io.lettuce/lettuce-core -->
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>

测试代码
public static void main(String[] args) {
    RedisClient client = RedisClient.create(RedisURI.create("redis://172.18.203.30:6379"));
    StatefulRedisConnection<String,String> connect = client.connect();

    /**
     * 同步调用
     */
    RedisCommands<String,String> commands = connect.sync();
    commands.set("hello","hello world");
    String str = commands.get("hello");
    System.out.println(str);

    /**
     * 异步调用
     */
    RedisAsyncCommands<String,String> asyncCommands = connect.async();
    RedisFuture<String> future = asyncCommands.get("hello");
    try {
        String str1 = future.get();
        System.out.println(str1);
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }


    connect.close();
    client.shutdown();
}
4.3 Jedis与Lettuce的区别
相同点: Lettuce 和 Jedis 的都是连接Redis Server的客户端程序;

不同点:
    (1)Jedis在实现上是直连redis server,多线程环境下非线程安全,除非使用连接池,为每个Jedis实例增加物理连接。
    
    (2)Lettuce基于Netty的连接实例(StatefulRedisConnection),可以在多个线程间并发访问,且线程安全,满足多线程环境下的并发访问,同时它是可伸缩的设计,一个连接实例不够的情况也可以按需增加连接实例;
    
    (3)Lettuce依赖了19个第三方jar;Jedis依赖了2个;
上一篇 下一篇

猜你喜欢

热点阅读