1. 什么是Redis
Redis是使用c语言开发的一个高性能键值数据库。Redis可以通过一些键值类型来存储数据。
键值类型:
- String字符类型
- map散列类型
- list列表类型
- set集合类型
- sortedset有序集合类型
2. Redis历史发展
2008年,意大利的一家创业公司Merzia推出了一款基于MySQL的网站实时统计系统LLOOGG,然而没过多久该公司的创始人 Salvatore Sanfilippo便 对MySQL的性能感到失望,于是他决定亲自为LLOOGG量身定做一个数据库,并于2009年开发完成,这个数据库就是Redis。 不过Salvatore Sanfilippo并不满足只将Redis用于LLOOGG这一款产品,而是希望更多的人使用它,于是在同一年Salvatore Sanfilippo将Redis开源发布,并开始和Redis的另一名主要的代码贡献者Pieter Noordhuis一起继续着Redis的开发,直到今天。
Salvatore Sanfilippo自己也没有想到,短短的几年时间,Redis就拥有了庞大的用户群体。Hacker News在2012年发布了一份数据库的使用情况调查,结果显示有近12%的公司在使用Redis。国内如新浪微博、街旁网、知乎网,国外如GitHub、Stack Overflow、Flickr等都是Redis的用户。
VMware公司从2010年开始赞助Redis的开发, Salvatore Sanfilippo和Pieter Noordhuis也分别在3月和5月加入VMware,全职开发Redis。
3. Redis的应用场景
- 缓存(数据查询、短连接、新闻内容、商品内容等等)。(最多使用)
- 分布式集群架构中的session分离。
- 聊天室的在线好友列表。
- 任务队列。(秒杀、抢购、12306等等)
- 应用排行榜。
- 网站访问统计。
- 数据过期处理(可以精确到毫秒)
4. Redis安装
4.1 Redis下载
4.2 Redis的安装
Redis的安装环境会安装到Linux系统中。
安装VMware,并且在VMware中安装centos系统(参考linux教程)。
将redis的压缩包,上传到linux系统
-
对redis的压缩包进行解压缩
- Redis解压缩之后的文件是用c语言写的源码文件
[root@itheima ~]# tar -zxf redis-3.0.0.tar.gz
-
安装c语言环境(安装centos之后,自带c语言环境)
[root@itheima~]# yum install gcc-c++
-
编译redis源码
[root@itheima ~]# cd redis-3.0.0
[root@itheima redis-3.0.0]# make
-
安装redis
[root@itheima redis-3.0.0]# make install PREFIX=/usr/local/redis19
-
查看是否安装成功
4.3 Redis启动
4.3.1 前端启动
前端启动的命令:
[root@itheima bin]# ./redis-server
前端启动的关闭:
- 强制关闭:
Ctrl+c
- 正常关闭:
[root@itheima bin]# ./redis-cli shutdown
启动界面:
前端启动的问题:
一旦客户端关闭,则redis服务也停掉。
4.3.2 后端启动
-
需要将redis解压之后的源码包中的redis.conf文件拷贝到bin目录下
[root@itheima bin]# cp /root/redis-3.0.0/redis.conf ./
-
修改redis.conf文件,将daemonize改为yes
先要使用vim redis.conf
-
使用命令后端启动redis
[root@itheima bin]# ./redis-server redis.conf
-
查看是否启动成功
关闭后端启动的方式:
强制关闭:
[root@itheima bin]# kill -9 5071
正常关闭:
[root@itheima bin]# ./redis-cli shutdown
在项目中,建议使用正常关闭。
因为redis作为缓存来使用的话,将数据存储到内存中,如果使用正常关闭,则会将内存数据持久化到本地之后,再关闭。
如果是强制关闭,则不会进行持久化操作,可能会造成部分数据的丢失。
5. Redis客户端
5.1 Redis自带的客户端
-
启动
启动客户端命令:[root@itheima bin]# ./redis-cli -h 127.0.0.1 -p 6379
-h:指定访问的redis服务器的ip地址
-p:指定访问的redis服务器的port端口
还可以写成:[root@itheima bin]# ./redis-cli
使用默认配置:默认的ip【127.0.0.1】,默认的port【6379】
-
关闭
Ctrl+c
127.0.0.1:6379> quit
5.2 图形界面客户端
安装软件:
安装之后,打开如下:
防火墙设置:/etc/sysconfig/iptables
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
我当时发现我竟然没有iptables文件 尴尬!
执行完这些命令就有了
systemctl stop firewalld 关闭防火墙
yum install iptables-services 安装或更新服务
systemctl enable iptables 启动iptables
systemctl start iptables 打开iptables
接着重启服务
[root@itheima redis-3.0.0]# service iptables restart
这个命令执行完后应该是
iptables:清除防火墙规则: [确定]
iptables:将链设置为政策 ACCEPT:filter [确定]
iptables:正在卸载模块: [确定]
iptables:应用防火墙规则: [确定]
[root@itheima redis-3.0.0]#
我当时遇到这种情况
Redirecting to /bin/systemctl restart iptables.service
百度了下-解决办法如下
-
安装systemctl
yum install iptables-services
这里我的yum命令出现了问题
-
yum repolist
命令显示仓库为0 -
总结原因:问题应该出在了.repo文件上了。
cd /etc/yum.repos.d
ll
-
解决办法
-
查看是否已经安装了yum工具:
rpm -qa | grep yum
-
CentOS 报错:There are no enabled repos
-
报错情况:
yum grouplist
-
解决方法,输入命令:
cd ~ cd /etc/yum.repos.d ll mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
-
下载对应版本repo文件:
百度网盘下载地址:点击前往下载 提取码:2kxr
-
下载对应版本repo文件后, 放入/etc/yum.repos.d/(操作前请做好相应备份)
-
运行以下命令生成缓存:
yum clean all yum makecache
-
验证问题是否解决,输入命令:
yum repolist
-
-
-
-
设置开机启动
systemctl enable iptables.service
-
然后就可以执行以下命令
systemctl stop iptables systemctl start iptables systemctl restart iptables systemctl reload iptables
记得Redis服务要开启才能连接
Redis.conf中的数据库数量的设置:
选择数据库的方式:
使用select 加上数据库的下标 就可以选择指定的数据库来使用,下标从0开始
127.0.0.1:6379> select 15
OK
127.0.0.1:6379[15]>
5.3 Jedis客户端
5.3.1 jedis介绍
Redis不仅是使用命令来操作,现在基本上主流的语言都有客户端支持,比如java、C、C#、C++、php、Node.js、Go等。
在官方网站里列一些Java的客户端,有Jedis、Redisson、Jredis、JDBC-Redis、等其中官方推荐使用Jedis和Redisson。 在企业中用的最多的就是Jedis,下面我们就重点学习下Jedis。
Jedis同样也是托管在github上,地址
5.3.2 工程搭建
添加jar包
5.3.3 单实例连接redis
@Test
public void jedisClient(){
//Jedis
Jedis jedis = new Jedis("192.168.43.8",6379);
//通过Redis赋值
jedis.set("s2","222");
//通过Redis取值
String result = jedis.get("s2");
System.out.println(result);
//关闭jedis
jedis.close();
}
5.3.4 使用jedis连接池连接redis服务器
@Test
public void jedisPool(){
//JedisPool
JedisPool pool = new JedisPool("192.168.43.8",6379);
//从连接池获取jedis对象
Jedis jedis = pool.getResource();
//通过Redis赋值
jedis.set("s4","444");
//通过Redis取值
String result = jedis.get("s2");
System.out.println(result);
//关闭jedis客户端
jedis.close();
//关闭连接池
pool.close();
}
5.3.5 Spring整合jedisPool
添加spring的jar包
配置spring配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<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 ">
<!-- 连接池配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大连接数 -->
<property name="maxTotal" value="30" />
<!-- 最大空闲连接数 -->
<property name="maxIdle" value="10" />
<!-- 每次释放连接的最大数目 -->
<property name="numTestsPerEvictionRun" value="1024" />
<!-- 释放连接的扫描间隔(毫秒) -->
<property name="timeBetweenEvictionRunsMillis" value="30000" />
<!-- 连接最小空闲时间 -->
<property name="minEvictableIdleTimeMillis" value="1800000" />
<!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 -->
<property name="softMinEvictableIdleTimeMillis" value="10000" />
<!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 -->
<property name="maxWaitMillis" value="1500" />
<!-- 在获取连接的时候检查有效性, 默认false -->
<property name="testOnBorrow" value="false" />
<!-- 在空闲时检查有效性, 默认false -->
<property name="testWhileIdle" value="true" />
<!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
<property name="blockWhenExhausted" value="false" />
</bean>
<!-- redis单机 通过连接池 -->
<bean id="jedisPool" class="redis.clients.jedis.JedisPool"
destroy-method="close">
<constructor-arg name="poolConfig" ref="jedisPoolConfig" />
<constructor-arg name="host" value="192.168.43.8" />
<constructor-arg name="port" value="6379" />
</bean>
</beans>
-
测试代码
@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(); } } }