学习笔记 学习笔记
🐱 首页
    • 🚅 JAVA
    • 🚆 Python
    • 🧭 VUE
    • 🌐 JavaScript
    • 🗺 CSS
  • 🎃 MySQL
  • 🛶 Redis
  • 🛳 Nginx
  • ⚽ Dokcer
  • 🏓 Elasticsearch
  • 🏙 Windows
  • 🗽 Centos
  • ⚓ Gitlab
  • 🙈 分类
  • 🙉 标签
  • 🙊 归档
    • 👣 随笔
    • 🌹 关于
GitHub (opens new window)

爱做梦的奋斗青年

曾梦想仗剑走天涯,后来bug太多就没去
🐱 首页
    • 🚅 JAVA
    • 🚆 Python
    • 🧭 VUE
    • 🌐 JavaScript
    • 🗺 CSS
  • 🎃 MySQL
  • 🛶 Redis
  • 🛳 Nginx
  • ⚽ Dokcer
  • 🏓 Elasticsearch
  • 🏙 Windows
  • 🗽 Centos
  • ⚓ Gitlab
  • 🙈 分类
  • 🙉 标签
  • 🙊 归档
    • 👣 随笔
    • 🌹 关于
GitHub (opens new window)
  • spring-boot

  • mysql

  • 达梦数据库

  • Redis

    • Redis初识
      • Redis官网
      • Windows安装
      • CentOS安装
      • Redis客户端链接
      • Redis开机自启
      • Java客户端Jedis
    • 一键安装Redis的任意版本
    • Redis数据类型
    • Redis常见延迟问题定位与分析
    • Redis内存模型
    • Redis高并发应用
  • nginx

  • Docker

  • Elasticsearch

  • oss

  • 应用框架
  • Redis
爱做梦的奋斗青年
2020-08-27
目录

Redis初识

# Redis官网

  • 官网地址:http://redis.io/ (opens new window)
  • 中文官网地址:http://www.redis.cn/ (opens new window)
  • 下载地址: http://download.redis.io/releases/ (opens new window)
  • Github 源码:https://github.com/antirez/redis (opens new window)
  • 官方命令大全网址:http://www.redis.cn/commands.html (opens new window)
  • 测试工具redis-benchmark:https://redis.io/topics/benchmarks (opens new window)

# Windows安装

下载地址:https://github.com/microsoftarchive/redis/releases/ (opens new window)

# CentOS安装

# gcc环境
yum install -y wget gcc-c++  
# 下载并解压缩Redis源码压缩包
wget http://download.redis.io/releases/redis-5.0.4.tar.gz 
mkdir -p ~/tools/ && tar -zxf redis-5.0.4.tar.gz -C ~/tools/
cd ~/tools/redis-5.0.4  && make
# 安装Redis,需要通过PREFIX指定安装路径
cd ~/tools/redis-5.0.4  && make install PREFIX=/usr/local/redis
mkdir -p /usr/local/redis/etc/ && cp -r /root/tools/redis-5.0.4/redis.conf /usr/local/redis/etc/
# 修改配置文件 `vi /usr/local/redis/bin/redis.conf`
# 将daemonize由no改为yes
sed -i "s/daemonize no/daemonize yes/g" /usr/local/redis/etc/redis.conf
# 是否开启保护模式,由yes该为no
sed -i "s/protected-mode yes/protected-mode no/g" /usr/local/redis/etc/redis.conf

# 启动redis服务
/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
# 查看redis监听端口
netstat -tanp|grep redis
# 后端启动的关闭方式
/usr/local/redis/bin/redis-cli shutdown
# 查看redis 版本
/usr/local/redis/bin/redis-server --version
/usr/local/redis/bin/redis-server -v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

其他命令说明

  • redis-server :启动 redis 服务
  • redis-cli :进入redis命令客户端
  • redis-benchmark: 性能测试的工具
  • redis-check-aof : aof文件进行检查的工具
  • redis-check-dump : rdb文件进行检查的工具
  • redis-sentinel : 启动哨兵监控服务

# Redis客户端链接

# 默认端口:6379
redis-cli -h 127.0.0.1 -p 6379
1
2

# Redis开机自启

cat >> /usr/lib/systemd/system/redis.service << EOF
[Unit]
Description=Redis In-Memory Data Store
Documentation=https://redis.io/
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
ExecStop=/usr/local/redis/bin/redis-cli shutdown
ExecReload=/bin/kill -s HUP \$MAINPID
Restart=always

[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload && systemctl enable redis && systemctl start redis && systemctl status redis
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# Java客户端Jedis

github地址:https://github.com/xetorthio/jedis

  • 添加依赖

    <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>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
  • 单实例连接

    @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();
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
  • 连接池连接

    @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();
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
  • 连接redis集群

    创建JedisCluster类连接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();
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
  • 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" />
            <!-- 在空闲时检查有效性, 默认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.10.135" />
            <constructor-arg name="port" value="6379" />
        </bean>
        
        <!-- redis集群 -->
        <bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
            <constructor-arg index="0">
                <set>
                    <bean class="redis.clients.jedis.HostAndPort">
                        <constructor-arg index="0" value="192.168.10.135">
                        </constructor-arg>
                        <constructor-arg index="1" value="7001"></constructor-arg>
                    </bean>
                    <bean class="redis.clients.jedis.HostAndPort">
                        <constructor-arg index="0" value="192.168.10.135">
                        </constructorarg>
                        <constructor-arg index="1" value="7002"></constructor-arg>
                    </bean>
                    <bean class="redis.clients.jedis.HostAndPort">
                        <constructor-arg index="0" value="192.168.10.135">
                        </constructor-arg>
                        <constructor-arg index="1" value="7003"></constructor-arg>
                    </bean>
                    <bean class="redis.clients.jedis.HostAndPort">
                        <constructor-arg index="0" value="192.168.10.135">
                        </constructor-arg>
                        <constructor-arg index="1" value="7004"></constructor-arg>
                    </bean>
                    <bean class="redis.clients.jedis.HostAndPort">
                        <constructor-arg index="0" value="192.168.10.135">
                        </constructor-arg>
                        <constructor-arg index="1" value="7005"></constructor-arg>
                    </bean>
                </set>
            </constructor-arg>
            <constructor-arg index="1" ref="jedisPoolConfig"></constructor-arg>
        </bean>
    </beans>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70

    测试代码

    import javax.annotation.Resource;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisCluster;
    import redis.clients.jedis.JedisPool;
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = "classpath:application.xml")
    public class TestJedis2 {
        @Autowired
        private JedisPool jedisPool;
        @Resource
        private JedisCluster cluster;
        @Test
        public void testJedisPool() {
            // 从连接池中获得连接
            Jedis jedis = jedisPool.getResource();
            String result = jedis.get("mytest");
            System.out.println(result);
            // 关闭连接
            jedis.close();
        }
        @Test
        public void testJedisCluster() throws Exception {
            // 执行JedisCluster对象中的方法,方法和redis一一对应。
            cluster.set("cluster-test", "my jedis cluster test");
            String result = cluster.get("cluster-test");
            System.out.println(result);
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
编辑此页 (opens new window)
#Redis
上次更新: 2022/05/24 01:08:08
达梦数据库配置
一键安装Redis的任意版本

← 达梦数据库配置 一键安装Redis的任意版本→

Theme by Vdoing
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式