01-1 redis单机搭建并使用:centos7,redis3.0.0,java-maven

  • 最近在使用redis 顺便做个笔记

一、redis搭建

1、下载安装gcc:

yum install gcc-c++
  • 因为下载的redis文件是c++源码需要安装gcc来编译源码

2、下载redis

[root@bogon ~]# cd /usr/local/
[root@bogon local]# wget http://download.redis.io/releases/redis-3.0.0.tar.gz
[root@bogon local]# ls
bin  games    lib    libexec             redis-3.0.0.tar.gz.1  share
etc  include  lib64  redis-3.0.0.tar.gz  sbin                  src
  • 进入local文件夹再下载是为了将文件下载到local文件夹

3、解压文件夹

[root@bogon local]# tar zxf redis-3.0.0.tar.gz 
[root@bogon local]# ls
bin  games    lib    libexec      redis-3.0.0.tar.gz  share  wget-log
etc  include  lib64  redis-3.0.0  sbin                src

4、将文件安装到指定文件夹

[root@bogon redis-3.0.0]# cd ..
[root@bogon local]# cd redis-3.0.0/
[root@bogon redis-3.0.0]# make PREFIX=/usr/local/redis install
  • 说明:
  • make PREFIX=/usr/local/redis install :是为了将文件安装到/usr/local/redis 中
  • /usr/local/redis-3.0.0:是redis的源码文件夹
  • /usr/local/redis:是redis的安装文件夹

5、拷贝配置文件

  • 将源码文件夹中的redis配置文件复制到安装目录
[root@localhost redis-3.0.0]# cd ..
[root@localhost local]# cd redis
[root@bogon redis]# mkdir conf
[root@bogon redis]# cp /usr/local/redis-3.0.0/redis.conf /usr/local/redis/bin/
[root@bogon redis]# ls
bin  conf

6、前端启动redis

[root@bogon redis]# cd bin
[root@bogon bin]# ls
redis-benchmark  redis-check-dump  redis.conf      redis-server
redis-check-aof  redis-cli         redis-sentinel
[root@localhost bin]# ./redis-server 
4657:C 09 Jul 22:51:15.379 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
4657:M 09 Jul 22:51:15.379 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.0.0 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 4657
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               
4657:M 09 Jul 22:51:15.416 # Server started, Redis version 3.0.0
4657:M 09 Jul 22:51:15.416 # 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.
4657:M 09 Jul 22:51:15.416 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
4657:M 09 Jul 22:51:15.416 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
4657:M 09 Jul 22:51:15.416 * The server is now ready to accept connections on port 6379
  • 说明:
  • ./redis-server :前端方式启动redis
  • ./redis-sentinel:启动哨兵,但不是启动redis,容易被弄错
  • ./redis-cli:连接redis数据库命令

7、配置后端启动

  • 修改配置文件:redis.conf
  • 文件地址:/usr/local/redis/bin/redis.conf
[root@localhost bin]# vim redis.conf 
  • 修改内容:
  • 将后台启动配置设置为yes即可
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes

8、后台启动

[root@localhost bin]# ./redis-server redis.conf 
[root@localhost bin]# 
  • 说明:
  • ./redis-server:为启动命令
  • redis.conf:指定启动文件

二、redis测试:

[root@localhost bin]# ./redis-cli 
127.0.0.1:6379> set testname 123
OK
127.0.0.1:6379> get testname
"123"
127.0.0.1:6379> 
  • 说明:
  • ./redis-cli :使用连接命令
  • set testname 123:通过set方法在redis中写入key为testname,value为123
  • get testname:通过get方法获取key为testname的值

三、redis代码测试

  • 采用maven方式创建项目

1、导入依赖库

<groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.0</version>
        </dependency>

2、直接使用redis

@Test
    public void singleTest() {
        Jedis jedis = new Jedis("192.168.58.233", 6379);
        jedis.set("test", "test data");
        String test = jedis.get("test");
        System.out.println(test);
        jedis.close();
    }

3、使用连接池的方式连接redis

 /**
     * 使用连接池
     */
    @Test
    public void testJedisPool() {
        //创建jedis连接池
        JedisPool pool = new JedisPool("192.168.58.233", 6379);
        //从连接池中获得Jedis对象
        Jedis jedis = pool.getResource();
        jedis.set("key1", "jedis test");
        String string = jedis.get("key1");
        System.out.println(string);
        //关闭jedis对象
        jedis.close();
        pool.close();
    }

猜你喜欢

转载自blog.csdn.net/qq_34231253/article/details/80982890