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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,560评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,104评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,297评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,869评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,275评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,563评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,833评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,543评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,245评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,512评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,011评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,359评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,006评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,062评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,825评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,590评论 2 273
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,501评论 2 268

推荐阅读更多精彩内容

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