Redis入门系列之二:Redis的安装配置

1 Redis安装配置

1.1 Redis下载

官网地址:http://redis.io/

下载地址:http://download.redis.io/releases/redis-3.0.0.tar.gz

在Linux中使用wget下载到linux或者下载到window在上传到linux

wget http://download.redis.io/releases/redis-3.0.0.tar.gz

1.2 Redis安装

Redis是C语言开发,建议在linux上运行,本教程使用Centos6.5作为安装环境。

第一步:在VMware中安装CentOS(参考Linux教程中的安装虚拟机)

第二步:在Linux下安装gcc环境(该步骤可以省略,CentOS中默认自带C环境)

yum install gcc-c++可以通过rpm -qa | grep gcc 来查询是否已经安装了gccimg

第三步:将下载的Redis源码包上传到Linux服务器中【如果是linux直接下载的,就省略这个步骤】

第四步:解压缩Redis源码包

tar -zxf redis-3.0.0.tar.gz 【直接解压到当前文件夹】

扫描二维码关注公众号,回复: 5816181 查看本文章

第五步:编译redis源码

cd redis-3.0.0# make

第六步:安装redis

make install PREFIX=/usr/local/redis

1.3 Redis启动

1.3.1 前端启动

Ø 启动方式:

直接运行bin/redis-server将以前端模式启动。【bin目录是在/usr/local/redis/bin】

./redis-server

Ø 启动缺点:

ssh命令窗口关闭则redis-server程序结束,不推荐使用此方法

Ø 启动图例:

img

Ø 前端启动的关闭:ctrl+c

1.3.2 后端启动

第一步:将redis源码包中的redis.conf配置文件复制到/usr/local/redis/bin/下

cd /root/redis-3.0.0# cp redis.conf /usr/local/redis/bin/

第二步:修改redis.conf,将daemonize由no改为yes

vi redis.conf

img

第三步:执行命令

./redis-server redis.conf

Ø 后端启动的关闭方式

非正常关闭(不推荐使用):

kill 5528

正常关闭:

./redis-cli shutdown

2 Redis客户端

2.1 Redis自带的客户端

img

Ø 指定主机和端口

./redis-cli -h 127.0.0.1 -p 6379127.0.0.1:6379> exit 【退出】

-h:redis服务器的ip地址

-p:redis实例的端口号

Ø 如果不指定主机和端口也可以

./redis-cli

默认主机地址是127.0.0.1

默认端口是6379

2.2 图形界面客户端

前提:需要安装图形界面管理器【redis-desktop-manager-0.8.0.3841.exe】

img

img

2.2.1 连接超时解决

远程连接redis服务,需要关闭或者修改防火墙配置。

2.将修改后的端口添加到防火墙中./sbin/iptables -I INPUT -p tcp --dport 8081 -j ACCEPT/etc/rc.d/init.d/iptables save

第一步:编辑iptables /etc/sysconfig/iptables

vim

在命令模式下,****选定要复制的那一行的末尾****,****然后点击键盘yyp****,****就完成复制****,****然后修改**。

img

第二步:重启防火墙

service iptables restartimg

注意:

默认一共是16个数据库,每个数据库之间是相互隔离。数据库的数量是在redis.conf中配置的。

img

切换数据库使用命令:select 数据库编号

例如:select 1【相当于mysql 的use databasename】

img

2.3 Java客户端Jedis

2.3.1 jedis介绍

​ Redis不仅是使用命令来操作,现在基本上主流的语言都有客户端支持,比如java、C、C#、C++、php、Node.js、Go等。

​ 在官方网站里列一些Java的客户端,有Jedis、Redisson、Jredis、JDBC-Redis、等其中官方推荐使用Jedis和Redisson。 在企业中用的最多的就是Jedis,下面我们就重点学习下Jedis。

Jedis同样也是托管在github上,地址:https://github.com/xetorthio/jedis

2.3.2 添加jar包

img

2.3.3 单实例连接

​ @Test public void testJedis() { jedis.select(1);//设置数据库//创建一个Jedis的连接 Jedis jedis = new Jedis("127.0.0.1", 6379); //执行redis命令 jedis.set("mytest", "hello world, this is jedis client!"); //从redis中取值 String result = jedis.get("mytest"); //打印结果 System.out.println(result); //关闭连接 jedis.close(); }

2.3.4 连接池连接

​ @Test public void testJedisPool() { //创建一连接池对象 JedisPool jedisPool = new JedisPool("127.0.0.1", 6379); //从连接池中获得连接 Jedis jedis = jedisPool.getResource(); String result = jedis.get("mytest"); System.out.println(result); //关闭连接 jedis.close(); //关闭连接池 jedisPool.close(); }

2.3.5 Spring整合jedis**Pool

Ø 添加spring的jar包

Ø 配置spring配置文件applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

Ø 测试代码

@Test public void testJedisPool() {
JedisPool pool = (JedisPool) applicationContext.getBean("jedisPool");
Jedis jedis = null;
try {
jedis = pool.getResource();
jedis.set("name", "lisi");
String name = jedis.get("name");
System.out.println(name);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (jedis != null) {
// 关闭连接
jedis.close();
}

}
}

猜你喜欢

转载自www.cnblogs.com/gxh195/p/10674051.html