美文网首页
Redis数据库实操详解

Redis数据库实操详解

作者: Demon先生 | 来源:发表于2020-05-08 10:33 被阅读0次

1. 什么是Redis

Redis是使用C语言开发的支持网络、基于内存可持久化的日志型的一个开源的高性能键值对(key-value)数据库。目前为止,Redis支持5种键值数据类型,分别是字符串类型(String)、散列类型(Hash)、列表类型(List)、集合类型(Set)和有序集合类型(SortedSet)。

2. Redis的安装与连接

2.1. 下载与安装

在不同系统环境下,安装方式不同,这里只介绍Linux系统下的安装方式。需要注意的是,Redis的安装需要使用C语言的编译环境,所以需要提前安装编译环境,具体操作步骤如下:

第一步:使用yum进行C语言环境的在线安装


image.png

第二步:检查是否安装成功,输入gcc,出现gcc:no input files 表示安装成功。

image.png

第三步:下载Redis压缩包

第四步:拷贝压缩包到Linux系统上,并解压。

第五步:进行编译安装,cd到Redis解压目录,输入命令进行安装。输入命令:make install PREFIX=/usr/local/redis,注意PREFIX 必须是大写的。当出现如下图所示,表示安装成功。

image.png

2.2. Redis服务两种启动模式

2.2.1 前端启动

进入到redis的安装目录下的bin目录/usr/local/redis/bin,输入命令./redis-server,进行启动,出现下图所示,表示启动成功。

image.png

2.2.2 后台启动

第一步:把redis解压包下面的redis.conf文件拷贝到/usr/local/redis/bin目录下。

image.png

第二步:使用vim命令修改redis.conf配置文件,将daemonize no 修改为daemonize yes

image.png

第三步:将protected-mode yes改为protected-mode no,否则外部访问会被拒绝。

image.png

第四步:将bind 127.0.0.1语句注释掉(该语句前面加#注释),否则只能本地访问,外部ip不能访问。

image.png

第五步:输入启动命令,并检查启动是否成功,在进程发现redis服务表示启动成功。

image.png

2.3. 服务的连接与关闭

2.3.1 连接Redis服务

适用redis-cli建立连接,输入如下命令:

-h:表示连接的服务器地址

-p:表示连接的端口

image.png

2.3.2 关闭Redis服务

关闭服务有两种方式,第一种是通过连接上的客户端进行关闭,使用shutdown命令进行关闭。

image.png

第二种,是用kill命令杀死进程进行关闭,找到对应Redis服务的进程id,输入命令进行关闭。kill -9 pid(pid为进程id)

image.png

3. Redis的两种持久化方案

3.1. RDB快照形式

在Redis服务中,默认开启的就是RDB快照模式,会定期将当前时刻的数据保存到dump.rdb的文件中,但是保存周期较长,容易存在数据丢失,对服务器性能要求较低。如下图所示,redis.conf中默认设置了RDB模式的保存规则和间隔时间。

image.png

3.2. AOF形式

AOF,全称append only file,是指对所有redis的操作命令都会记录在appendonly.aof的文件中,对数据保存比较完整,每秒进行保存一次,但比较耗费性能。若要开启AOF模式,需对redis.conf文件进行修改,将appendonly设置为yes,如图所示:

image.png

若同时开启两个持久化方案,则会按照AOF的持久化方案进行数据恢复。

4. Redis集群的搭建和配置

搭建Redis集群至少需要3个节点,为了提高集群的高可用性,还需要为每一个节点增加一台备份机,总共需要6台服务器。

第一步:在每台服务器上安装Redis,具体安装步骤参考第2章。

第二步:在每台服务器上安装集群需要的gem包,安装命名为:

image.png

第三步:修改每台服务器上面的redis.conf配置文件,将cluster-enable yes前面的注释去掉。

image.png

第四步:启动每个redis实例

5. Java中Jedis的使用

在Java中Jedis是对Redis服务的具体操作,可以直接将数据库中的数据通过开放的服务存储到Redis中。

5.1. Maven工程中配置依赖

需要把Jedis依赖的jar包添加到工程中。Maven工程中需要把jedis的坐标添加到依赖,配置pom.xml文件如下。

<!-- Redis客户端 -->
<dependency>
     <groupId>redis.clients</groupId>
     <artifactId>jedis</artifactId>
     <version>2.7.2</version>
</dependency>

5.2. 连接单个Redis服务

第一步:创建一个Jedis对象。需要指定服务端的ip及端口。

第二步:使用Jedis对象操作数据库,每个redis命令对应一个方法。

第三步:打印结果。

第四步:关闭Jedis

@Test
    public void testJedis(){
        //1.创建Jedis 对象 需要指定 连接的地址和端口
        Jedis jedis = new Jedis("192.168.36.128", 6379);
        //2.直接操作redis set
        jedis.set("key1", "value1");
        System.out.println(jedis.get("key1"));
        //3.关闭Jedis
        jedis.close();
    }

5.3. 使用连接池连接单个Redis服务

第一步:创建一个JedisPool对象。需要指定服务端的ip及端口。

第二步:从JedisPool中获得Jedis对象。

第三步:使用Jedis操作Redis服务器。

第四步:操作完毕后关闭jedis对象,连接池回收资源。

第五步:关闭JedisPool对象。

@Test
    public void teatJedisPool(){
        //1.创建jedispool 对象需要指定端口和地址
        JedisPool pool = new JedisPool("192.168.36.128", 6379);
        //2.获取jedis的对象
        Jedis jedis = pool.getResource();
        //3.直接操作redis
        jedis.set("keypool", "value");
        System.out.println(jedis.get("keypool"));
        //4.关闭redis  (释放资源到连接池)
        jedis.close();
        //5.关闭连接池(应用系统关闭的时候才关闭)
        pool.close();
    }

5.4. 连接Redis集群

以六台服务器为例,操作步骤如下:

第一步:创建Set<HostAndPort>对象并添加ip和端口。这个作为Redis节点的列表。

第二步:创建JedisCluster对象,在系统中以单例的形式存在。

第三步:直接使用JedisCluster操作Redis服务器集群,操作与之前一致。

第三步:打印结果

第四步:关闭JedisCluster对象。

@Test
    public void testjediscluster(){
        Set<HostAndPort> nodes = new HashSet<>();
        nodes.add(new HostAndPort("192.168.36.128", 7001));
        nodes.add(new HostAndPort("192.168.36.136", 7002));
        nodes.add(new HostAndPort("192.168.36.129", 7003));
        nodes.add(new HostAndPort("192.168.36.130", 7004));
        nodes.add(new HostAndPort("192.168.36.131", 7005));
        nodes.add(new HostAndPort("192.168.36.132", 7006));
        //1.创建jediscluster对象
        JedisCluster cluster = new JedisCluster(nodes );
        //2.直接根据jediscluster对象操作redis集群
        cluster.set("keycluster", "cluster的value");
        System.out.println(cluster.get("keycluster"));
        //3.关闭jediscluster对象(是在应用系统关闭的时候关闭) 封装了连接池
        cluster.close();
    }

6. Java中SpringDataRedis的使用

6.1. Maven工程中配置依赖

要使用SpringDataRedis框架需加入Spring其他相关依赖以及Jedis相关依赖,为了方便测试,同时引入Junit依赖包,具体引用如下:


    <properties>
        <junit.version>4.12</junit.version>
        <spring.version>4.2.4.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
        </dependency>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- 缓存 -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.8.6.RELEASE</version>
        </dependency>
     </dependencies>

6.2. 添加配置文件

第一步:新建SpringDataRedis相关的配置文件,applicationContext-redis.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:p="http://www.springframework.org/schema/p" 
  xmlns:context="http://www.springframework.org/schema/context" 
  xmlns:mvc="http://www.springframework.org/schema/mvc" 
  xmlns:cache="http://www.springframework.org/schema/cache"
  xsi:schemaLocation="http://www.springframework.org/schema/beans   
            http://www.springframework.org/schema/beans/spring-beans.xsd   
            http://www.springframework.org/schema/context   
            http://www.springframework.org/schema/context/spring-context.xsd   
            http://www.springframework.org/schema/mvc   
            http://www.springframework.org/schema/mvc/spring-mvc.xsd 
            http://www.springframework.org/schema/cache  
            http://www.springframework.org/schema/cache/spring-cache.xsd">  
  
   <context:property-placeholder location="classpath*:properties/*.properties" />   
  
   <!-- redis 相关配置 --> 
   <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">  
     <property name="maxIdle" value="${redis.maxIdle}" />   
     <property name="maxWaitMillis" value="${redis.maxWait}" />  
     <property name="testOnBorrow" value="${redis.testOnBorrow}" />
   </bean>  
  
   <bean id="JedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" 
       p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/>  
   
   <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">  
        <property name="connectionFactory" ref="JedisConnectionFactory" />  
   </bean>  
      
</beans>  

第二步:新建Redis连接配置文件,redis-config.properties。配置项如下:

# Redis settings 
# server IP 
redis.host=192.168.25.129
# server port 
redis.port=6379 
# server pass 
redis.pass= 
# use dbIndex 
redis.database=0 
redis.maxIdle=300 
redis.maxWait=3000 
redis.testOnBorrow=true

6.3. 引入RedisTemplate测试调用

新建测试脚本,使用@Autowired注解,引入RedisTemplate类,操作方法,进行Redis操作。

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml")
public class SpringDataRedisTest {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void setValue(){
        redisTemplate.boundValueOps("name").set("测试值");
    }
    @Test
    public void getValue(){
        String str = (String) redisTemplate.boundValueOps("name").get();
        System.out.println(str);
    }
    @Test
    public void deleteValue(){
        redisTemplate.delete("name");;
    }
}

7.扩展知识

7.1. 配置Mybatis二级缓存

示例项目为SpringBoot项目

7.1.1 配置文件修改

配置Redis数据库连接和打开Mybatis二级缓存。

spring:
  redis:
    lettuce:
      pool:
        max-active: 8
        max-idle: 8
        max-wait: -1ms
        min-idle: 0
    sentinel:
      master: mymaster
      nodes: 192.168.25.132:26379, 192.168.25.132:26380, 192.168.25.132:26381
mybatis:
  configuration:
    cache-enabled: true

7.1.2 实体类实现序列化接口并声明序列号

在所有的实体类中继承接口Serializable,并什么序列号,序列号如下:

private static final long serialVersionUID = 546523264786598665L;

IDEA 提示生成序列号
默认情况下 Intellij IDEA 不会提示继承了 Serializable 接口的类生成 serialVersionUID 的警告。如果需要生成 serialVersionUID,需要手动配置。
File -> Settings -> Inspections -> Serialization issues -> Serialization class without ‘serialVersionUID’

image.png

7.1.3 实现 Spring ApplicationContextAware 接口,用于手动注入 Bean

创建一个名为 ApplicationContextHolder 的工具类,代码如下:

import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class ApplicationContextHolder implements ApplicationContextAware, DisposableBean {
    private static final Logger logger = LoggerFactory.getLogger(ApplicationContextHolder.class);

    private static ApplicationContext applicationContext;

    /**
     * 获取存储在静态变量中的 ApplicationContext
     *
     * @return
     */
    public static ApplicationContext getApplicationContext() {
        assertContextInjected();
        return applicationContext;
    }

    /**
     * 从静态变量 applicationContext 中获取 Bean,自动转型成所赋值对象的类型
     *
     * @param name
     * @param <T>
     * @return
     */
    public static <T> T getBean(String name) {
        assertContextInjected();
        return (T) applicationContext.getBean(name);
    }

    /**
     * 从静态变量 applicationContext 中获取 Bean,自动转型成所赋值对象的类型
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> clazz) {
        assertContextInjected();
        return applicationContext.getBean(clazz);
    }

    /**
     * 实现 DisposableBean 接口,在 Context 关闭时清理静态变量
     *
     * @throws Exception
     */
    public void destroy() throws Exception {
        logger.debug("清除 SpringContext 中的 ApplicationContext: {}", applicationContext);
        applicationContext = null;
    }

    /**
     * 实现 ApplicationContextAware 接口,注入 Context 到静态变量中
     *
     * @param applicationContext
     * @throws BeansException
     */
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ApplicationContextHolder.applicationContext = applicationContext;
    }

    /**
     * 断言 Context 已经注入
     */
    private static void assertContextInjected() {
        Validate.validState(applicationContext != null, "applicationContext 属性未注入,请在 spring-context.xml 配置中定义 ApplicationContextHolder");
    }
}

7.1.4 实现 MyBatis Cache 接口,自定义缓存为 Redis

在项目中创建一个名为 RedisCache 的工具类,代码如下:

import com.funtl.myshop.commons.context.ApplicationContextHolder;
import org.apache.ibatis.cache.Cache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * Redis 缓存工具类
 * <p>Title: RedisCache</p>
 * <p>Description: </p>
 *
 * @version 1.0.0
 */
public class RedisCache implements Cache {
    private static final Logger logger = LoggerFactory.getLogger(RedisCache.class);

    private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
    private final String id; // cache instance id
    private RedisTemplate redisTemplate;

    private static final long EXPIRE_TIME_IN_MINUTES = 30; // redis过期时间

    public RedisCache(String id) {
        if (id == null) {
            throw new IllegalArgumentException("Cache instances require an ID");
        }
        this.id = id;
    }

    @Override
    public String getId() {
        return id;
    }

    /**
     * Put query result to redis
     *
     * @param key
     * @param value
     */
    @Override
    public void putObject(Object key, Object value) {
        try {
            RedisTemplate redisTemplate = getRedisTemplate();
            ValueOperations opsForValue = redisTemplate.opsForValue();
            opsForValue.set(key, value, EXPIRE_TIME_IN_MINUTES, TimeUnit.MINUTES);
            logger.debug("Put query result to redis");
        } catch (Throwable t) {
            logger.error("Redis put failed", t);
        }
    }

    /**
     * Get cached query result from redis
     *
     * @param key
     * @return
     */
    @Override
    public Object getObject(Object key) {
        try {
            RedisTemplate redisTemplate = getRedisTemplate();
            ValueOperations opsForValue = redisTemplate.opsForValue();
            logger.debug("Get cached query result from redis");
//            System.out.println("****" + opsForValue.get(key).toString());
            return opsForValue.get(key);
        } catch (Throwable t) {
            logger.error("Redis get failed, fail over to db", t);
            return null;
        }
    }

    /**
     * Remove cached query result from redis
     *
     * @param key
     * @return
     */
    @Override
    @SuppressWarnings("unchecked")
    public Object removeObject(Object key) {
        try {
            RedisTemplate redisTemplate = getRedisTemplate();
            redisTemplate.delete(key);
            logger.debug("Remove cached query result from redis");
        } catch (Throwable t) {
            logger.error("Redis remove failed", t);
        }
        return null;
    }

    /**
     * Clears this cache instance
     */
    @Override
    public void clear() {
        RedisTemplate redisTemplate = getRedisTemplate();
        redisTemplate.execute((RedisCallback) connection -> {
            connection.flushDb();
            return null;
        });
        logger.debug("Clear all the cached query result from redis");
    }

    /**
     * This method is not used
     *
     * @return
     */
    @Override
    public int getSize() {
        return 0;
    }

    @Override
    public ReadWriteLock getReadWriteLock() {
        return readWriteLock;
    }

    private RedisTemplate getRedisTemplate() {
        if (redisTemplate == null) {
            redisTemplate = ApplicationContextHolder.getBean("redisTemplate");
        }
        return redisTemplate;
    }
}

7.1.5 Mapper 接口中增加注解

在 Mapper 接口中增加 @CacheNamespace(implementation = RedisCache.class) 注解,声明需要使用二级缓存。

import com.funtl.myshop.commons.domain.TbUser;
import com.funtl.myshop.commons.utils.RedisCache;
import org.apache.ibatis.annotations.CacheNamespace;
import tk.mybatis.mapper.MyMapper;

@CacheNamespace(implementation = RedisCache.class)
public interface TbUserMapper extends MyMapper<TbUser> {}

文档下载地址:

https://wenku.baidu.com/view/30cd862bdc88d0d233d4b14e852458fb770b38b7

相关文章

  • Redis数据库实操详解

    1. 什么是Redis Redis是使用C语言开发的支持网络、基于内存可持久化的日志型的一个开源的高性能键值对(k...

  • Redis入门

    @(数据库) Redis C语言开发的非关系型键值对数据库 1 基本介绍 redis 的数据结构redis存储的实...

  • 数据库交互(总结)

    防SQL注入知乎详解 常用数据库交互代码 mysql mongo mongo知识大全 redis

  • Redis缓存和MySQL数据库同步

    深度详解Redis缓存和MySQL数据库同步 - 知乎 (zhihu.com)[https://zhuanlan....

  • Redis详解1.安装及使用

    章节目录Redis详解1.安装及使用Redis详解2.数据结构Redis详解3.发布订阅Redis详解4.事务Re...

  • Redis详解5.数据持久化

    章节目录Redis详解1.安装及使用Redis详解2.数据结构Redis详解3.发布订阅Redis详解4.事务Re...

  • Redis详解4.事务

    章节目录Redis详解1.安装及使用Redis详解2.数据结构Redis详解3.发布订阅Redis详解4.事务Re...

  • Redis详解8.Cluster模式

    章节目录Redis详解1.安装及使用Redis详解2.数据结构Redis详解3.发布订阅Redis详解4.事务Re...

  • Redis详解6.主从模式

    章节目录Redis详解1.安装及使用Redis详解2.数据结构Redis详解3.发布订阅Redis详解4.事务Re...

  • Redis详解7.哨兵模式

    章节目录Redis详解1.安装及使用Redis详解2.数据结构Redis详解3.发布订阅Redis详解4.事务Re...

网友评论

      本文标题:Redis数据库实操详解

      本文链接:https://www.haomeiwen.com/subject/hfbxnhtx.html