redis

Redis安装

下载地址官网 https://redis.io/download 安装准备:(1)VMware Workation虚拟机(2)Linux(CentOS)系统(3)SecureCRT(Xshell也行) 安装过程: (1)安装编译器:yum install gcc-c++(需要先安装C++编译器,redis 使用c++写的) (2)下载Redis源码并上传到服务器 (3)解压Redis压缩包:tar -zxvf redis (4)进入Redis目录进行编译:make (2/3/4步)或者直接在Linux上(符不用输入) wget http://download.redis.io/releases/redis-3.2.9.tar.gz(下载) tar xzf redis-3.2.9.tar.gz(解压) cd redis-3.2.9 $ make(编译) 编译后是二进制文件仅在目录中 src 可用。运行Redis: 要想更好的使用还需 (5)安装Redis:make PREFIX=/user/local/redis install (6)将redis.conf拷贝到Redis安装目录:cp redis.conf /usr/local/redis (7)进入安装目录,更改redis.conf文件:vim redis.conf --> daemonize no 改为 yes(之后可以后台模式运行)(vi 下按i 进行编辑 按esc后shift+zz(或者直接l俩大写Z),或者:wq 保存并退出) (8)启动redis后端模式:./bin/redis-server ./redis.conf (9)终止redis的操作 : ./bin/redis-cli shutdown (10) 打开redis:./bin/redis-cli使用redis,使用 set name helloworld ,就可以插入值,使用get name 查看del删除keys * 查看所有的key (11)ps -ef | grep -i redis查看是否已经启动

上述命令输入时遇到错误比如-bash: wget: command not found 是你电脑上没装这个功能,百度装一下就好了 这个问题 : yum -y install wget 就OK了

Jedis的使用

Jedis 是 Redis 官方首选的 Java 客户端开发包

虚拟机设置

  • 查看虚拟机的ip

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" cid="n12" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background: rgb(51, 51, 51); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">ifconfig

将查到的ip地址写在Jedis("192.168.40.128",6379);中</pre>

  • 将虚拟机的6379端口打开

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" cid="n16" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background: rgb(51, 51, 51); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#运行下面的命令 如果是新建的一个新的 文件,你需要先安装 iptables,再打开
vim /etc/sysconfig/iptables

安装命令

yum install -y iptables-services</pre>

  • 重启服务

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="shell" cid="n20" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background: rgb(51, 51, 51); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">service iptables restart

执行上面的命令,如果提示

Redirecting to /bin/systemctl restart iptables.service

则执行

/bin/systemctl restart iptables.service</pre>

  • 启动redis服务 (参考上篇文章)

java代码

  • 新建一个maven的java项目

  • 引入依赖

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="xml" cid="n30" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background: rgb(51, 51, 51); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> <dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
</dependency></pre>

  • 建立测试类

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n34" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; background: rgb(51, 51, 51); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**

  • @Auther: curry
  • @Date: 2018/5/31 23:04
  • @Description:
    /

    public class Test {
    @Test
    /
  • jedis单例测试
  • */
    public void test01(){
    //1.设置IP地址和端口
    Jedis jedis=new Jedis("192.168.40.128",6379);
    //2.保存数据
    jedis.set("name","imooc");
    //3.获取数据
    String name = jedis.get("name");
    System.out.println(name);
    jedis.close();
    }
    @Test
    public void demo2(){
    JedisPoolConfig config = new JedisPoolConfig();
    config.setMaxTotal(30);
    config.setMaxIdle(10);
    JedisPool jedisPool = new JedisPool(config,"192.168.40.128",6379);
    Jedis jedis = null;
    try{
    jedis = jedisPool.getResource();
    jedis.set("name", "毛毛");
    String value = jedis.get("name");
    System.out.println(value);
    }catch(Exception e){
    e.printStackTrace();
    }finally{
    if(jedis != null){
    jedis.close();
    }
    if(jedisPool != null){
    jedisPool.destroy();
    }
    }
    }
    }</pre>

意外问题

  • 提示time out错误 需要再iptables防火墙上添加上6379端口 并重启防火墙服务

  • 提示connection refuse错误 需要注释掉redis.conf 文件中的 bind 127.0.0.1

  • 提示JedisDataException错误 需要将redis.conf文件中的protect-mode 置为no 最后重启redis

Redis数据结构

https://blog.csdn.net/Dream_Weave/article/details/85132547

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1 Redis介绍1.1 什么是NoSql为了解决高并发、高可扩展、高可用、大数据存储问题而产生的数据库解决方...
    克鲁德李阅读 5,370评论 0 36
  • github 项目地址 首先安装redisWindows 安装下载地址:https://github.com/mi...
    lingmacker阅读 1,079评论 0 0
  • NOSQL类型简介键值对:会使用到一个哈希表,表中有一个特定的键和一个指针指向特定的数据,如redis,volde...
    MicoCube阅读 4,062评论 2 27
  • 心疼内心不够自信的人 也许过往经历已经让他伤痕累累 哪一个年轻气盛的不觉得自己可 独一人之力开创不平凡的人生 却又...
    小七午阅读 58评论 0 1
  • 所谓念念不忘 会不会有回响
    小琳达阅读 91评论 0 0