Redis介绍
什么是Redis
-
是用C语言开发的一个 的高性能 ( key-value ) ,它是一种NoSql数据库
-
它是【单进程单线程】的内存数据库,所以说不存在线程安全问题。
-
它可以支持并发 10W QPS(Query Per Second每秒查询率),所以说性能非常优秀。之所以单进程单线程性能还这么好,是因为底层采用了【IO 多路复用(NIO思想)】
-
相比Memcache这种专业缓存技术,它有更优秀的读写性能,及丰富的数据类型。
-
它提供了 来存储【值】:字符串类型(string)、散列类型(hash)、列表类型(list)、集合 类型(set)、有序集合类型(sortedset、zset)
Redis官网
官网地址:http://redis.io/
中文官网地址:http://www.redis.cn/
下载地址:http://download.redis.io/releases/
什么是NoSql
-
NoSQL,即Not-Only SQL(不仅仅是SQL),泛指 。
-
什么是关系型数据库? 有行有列的数据库
-
NoSQL 数据库是为了解决 高并发 高可用 高扩展 大数据存储 问题而产生的数据库解决方案。
-
NoSQL 可以作为关系型数据库的良好补充,但是不能替代关系型数据库noSQL 的优势与不足
发展经历了三代相关介绍:https://zhuanlan.zhihu.com/p/95650799
MySQL(关系型数据库) —> NoSQL —> NewSQL(VoltDB TiDB ScaleDB MemSQL)开源分布式关系型数据库
NoSql分类和应用
Redis发展历史
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XLASAiho-1627451651516)(/Users/apple/Library/Application Support/typora-user-images/image-20210711231906126.png)]
Redis应用场景
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ngqfy6rU-1627451651520)(/Users/apple/Library/Application Support/typora-user-images/image-20210711232408342.png)]
Redis单机安装配置
下载
-
官网地址:http://redis.io/
-
中文官网地址:http://www.redis.cn/
-
下载地址:http://download.redis.io/releases/
安装环境
Redis 没有官方的 Windows 版本,所以建议在 Linux 系统上安装运行,我们使用 CentOS 7作为安装环境。
安装步骤
-
安装依赖
yum install -y gcc-c++ yum install -y wget
-
下载并解压缩 Redis 源码压缩包
wget http://download.redis.io/releases/redis-5.0.4.tar.gz tar -zxf redis-5.0.4.tar.gz
-
编译 Redis 源码,进入 redis-5.0.4 目录,执行编译命令
cd redis-5.0.4
make
4. 安装Redis,需要通过PREFIX指定安装路径
make install PREFIX=/xxx/server/redis
### Redis启动和关闭
#### 前端启动
1.启动命令:./redis-server ,直接运行 bin/redis-server 将以前端模式启动
2.关闭命令: ctrl+c
#### 后端启动
1. 拷贝 redis-5.0.4/redis.conf 配置文件到 Redis 安装目录的 bin 目录
```shell
cp /root/redis-5.0.4/redis.conf /kkb/server/redis/bin/
- 修改 redis.conf
vim redis.conf
# 将`daemonize`由`no`改为`yes`
daemonize yes
# 默认绑定的是回环地址,默认不能被其他机器访问
# bind 127.0.0.1
# 是否开启保护模式,由yes该为no
protected-mode no
-
启动服务
./redis-server redis.conf
-
关闭服务
./redis-cli shutdown
其它命令说明
redis-server :启动 redis 服务
redis-cli :进入 redis 命令客户端
redis-benchmark : 性能测试的工具
redis-check-aof : aof 文件进行检查的工具
redis-check-dump : rdb 文件进行检查的工具
redis-sentinel : 启动哨兵监控服务
Redis客户端和Java操作
redis命令行客户端
./redis-cli -h 127.0.0.1 -p 6379
连接默认主机地址是127.0.0.1 默认端口是6379
./redis-cli
Java客户端jedis
Redis不仅使用命令来操作,而且可以使用程序客户端操作。现在基本上主流的语言都有客户端支持,比如java、 C、C#、C++、php、Node.js、Go等。
在官方网站里列一些Java的客户端,有Jedis、Redisson、Jredis、JDBC-Redis、等其中官方推荐使用 和 Redisson。 在企业中用的最多的就是Jedis,下面我们就重点学习下Jedis。
Jedis同样也是托管在github上,地址:https://github.com/xetorthio/jedis
1. 添加依赖
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<!-- 单元测试Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 配置Maven的JDK编译级别 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
2. 简单连接
@Test
public void testJedis() {
//创建一个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();
}
3. 连接池连接
@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();
}
4.连接redis集群
@Test
public void testJedisCluster() throws Exception {
//创建一连接,JedisCluster对象,在系统中是单例存在
Set<HostAndPort> nodes = new HashSet<>();
nodes.add(new HostAndPort("192.168.242.129", 7001));
nodes.add(new HostAndPort("192.168.242.129", 7002));
nodes.add(new HostAndPort("192.168.242.129", 7003));
nodes.add(new HostAndPort("192.168.242.129", 7004));
nodes.add(new HostAndPort("192.168.242.129", 7005));
nodes.add(new HostAndPort("192.168.242.129", 7006));
JedisCluster cluster = new JedisCluster(nodes); //执行JedisCluster对象中的方法,方法和redis一一对应。
cluster.set("cluster-test", "my jedis cluster test");
String result = cluster.get("cluster-test");
System.out.println(result);
//程序结束时需要关闭JedisCluster对象
cluster.close();
}
5. Jedis整合Spring
配置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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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="true" />