侧边栏壁纸
博主头像
孔子说JAVA博主等级

成功只是一只沦落在鸡窝里的鹰,成功永远属于自信且有毅力的人!

  • 累计撰写 292 篇文章
  • 累计创建 132 个标签
  • 累计收到 4 条评论

目 录CONTENT

文章目录

Linux下JAVA WEB服务器的搭建三(REDIS集群)

孔子说JAVA
2021-10-11 / 0 评论 / 0 点赞 / 144 阅读 / 29,982 字 / 正在检测是否收录...

要部署普通的java web项目,需要在linux上搭建好web服务器运行环境,包括JDK、tomcat、mysql、redis、nginx等,本文主要介绍redis集群的配置及使用,linux系统采用的是ubuntu。redis安装见《Linux下JAVA WEB服务器的搭建二(REDIS单机)》《Linux下JAVA WEB服务器的搭建二(REDIS单机)》/archives/linuxweb2

1、redis集群版的安装配置

1.1 创建redis集群目录

在/opt/redis目录下创建redis集群目录(目录一般也可以设置到/usr/local/下面),命令:mkdir redis-cluster

image-1648800851378

1.2 创建redis集群节点

进入redis-cluster目录,创建6个文件夹,作为redis的6个节点,对应3个服务和3个备份机。文件夹以端口命名,文件夹名分别为7000 7001 7002 7003 7004 7005。命令:mkdir 700{0..5}

搭建的redis的集群,因为选举投票的机制,所以必须为奇数,并且为了保证高可用,所以搭建了3个机器的redis集群,并且为每个节点,搭建了一个备份机。

image-1648800916892

1.3 复制配置文件到6个节点目录

从已经安装的redis中,复制redis.conf文件到上面的6个文件目录。

cp  redis.conf  /opt/redis/redis-cluster/7000/7000.conf
cp  redis.conf  /opt/redis/redis-cluster/7001/7001.conf
cp  redis.conf  /opt/redis/redis-cluster/7002/7002.conf
cp  redis.conf  /opt/redis/redis-cluster/7003/7003.conf
cp  redis.conf  /opt/redis/redis-cluster/7004/7004.conf
cp  redis.conf  /opt/redis/redis-cluster/7005/7005.conf

image-1648800957272

1.4 修改6个节点的redis配置文件

修改每个节点的port、daemonize、pidfile、cluster-enabled、cluster-config-file、cluster-node-timeout、appendonly、appendfilename、loglevel等配置。参考下图:

image-1648800984334

pidfile和配置文件路径可以只写文件名,表示路径和配置文件路径一致。

image-1648800991858

1.5 复制启动文件到/opt/redis/redis-cluster/bin目录中

1)在redis-cluster节点下创建bin目录,命令:mkdir bin

image-1648801016431

2)复制解压缩文件中的src下面的7个启动文件到/opt/redis/redis-cluster/bin目录中, 以后可以在bin目录中启动。

  • 7个启动文件分别为:redis-benchmark、redis-check-aof、redis-check-rdb、redis-cli、redis-sentinel、redis-server、mkreleasehdr.sh
  • 该步骤若省略,则在src目录使用集群各个节点的配置启动命令如下: ./redis-server /opt/redis/redis-cluster/7000.conf)

image-1648801089217

image-1648801096050

1.6 启动各个节点服务

进入/opt/redis/redis-cluster/bin目录,指定配置文件分别启动6个节点的服务。

root@xnzysq20210118001:/opt/redis/redis-cluster/bin# ./redis-server /opt/redis/redis-cluster/7000/7000.conf 
root@xnzysq20210118001:/opt/redis/redis-cluster/bin# ./redis-server /opt/redis/redis-cluster/7001/7001.conf 
root@xnzysq20210118001:/opt/redis/redis-cluster/bin# ./redis-server /opt/redis/redis-cluster/7002/7002.conf 
root@xnzysq20210118001:/opt/redis/redis-cluster/bin# ./redis-server /opt/redis/redis-cluster/7003/7003.conf 
root@xnzysq20210118001:/opt/redis/redis-cluster/bin# ./redis-server /opt/redis/redis-cluster/7004/7004.conf 
root@xnzysq20210118001:/opt/redis/redis-cluster/bin# ./redis-server /opt/redis/redis-cluster/7005/7005.conf 

image-1648801122094

使用ps查看进程,可以看到集群的6个节点均已启动。

image-1648801130417

1.7 安装ruby(redis 5.0版本之前需要)

由于Redis 集群客户端实现很少,redis集群的启动需要用到ruby实现的redis-trib.rb,所以我们需要先安装ruby。从Ubuntu存储库安装Ruby,以超级用户或具有sudo特权的用户身份运行以下命令,以更新软件包索引并安装Ruby:

sudo apt update
sudo apt install ruby-full

image-1648801168540

完成安装后,通过打印Ruby版本来验证是否安装正确:

ruby --version

image-1648801201643

1.8 安装redis的接口程序(redis 5.0版本之前需要)

使用gem install redis 安装redis的接口程序。

image-1648801240634

1.9 执行redis的创建集群命令创建集群(注意ip地址和端口号)

1.9.1 redis 5.0版本之前创建集群

启动集群用的是 redis解压包下的src目录中的 redis-trib.rb启动,redis-trib.rb 的命令 create,表示创建一个新的集群。选项 --replicas 1 表示为集群中的每个主节点创建一个从节点,1其实代表的是一个比例,就是主节点数/从节点数的比例。slot对于Redis集群而言,一个存放数据的地方,对应一个槽。对于每一个Master而言,会存在一个slot的范围,而Slave则没有。在Redis集群中,依然是Master可以读、写,而Slave只读。数据的写入,实际上是分布的存储在slot中。

./redis-trib.rb create --replicas 1 172.19.82.206:7000 172.19.82.206:7001 172.19.82.206:7002 172.19.82.206:7003 172.19.82.206:7004 172.19.82.206:7005

image-1648801283412

这是因为从redis5.0开始,建议使用redis-cli作为创建集群的命令,不推荐再使用redis-trib.rb来创建集群了,毕竟使用redis-trib.rb还要安装Ruby程序,比redis-cli麻烦的多。

1.9.2 redis 5.0版本之后创建集群

Redis Cluster 在5.0之后取消了ruby脚本 redis-trib.rb的支持(手动命令行添加集群的方式不变),集合到redis-cli里,避免了再安装ruby的相关环境。直接使用redis-clit的参数–cluster 来取代。

  • 进入集群bin目录 cd /opt/redis/redis-cluster/bin 。通过下面①的方式创建好集群之后不再需求其他步骤。

  • 注意:Redis Cluster最低要求是3个主节点,如果需要集群需要认证,则在最后加入 -a xx 即可。

① 创建集群主从节点

# 无认证方式
redis-cli --cluster create 172.19.82.206:7000 172.19.82.206:7001 172.19.82.206:7002 172.19.82.206:7003 172.19.82.206:7004 172.19.82.206:7005 --cluster-replicas 1

# 有认证方式在最后加上 -a 密码
redis-cli --cluster create 172.19.82.206:7000 172.19.82.206:7001 172.19.82.206:7002 172.19.82.206:7003 172.19.82.206:7004 172.19.82.206:7005 --cluster-replicas 1 -a 111111
  • 说明:–cluster-replicas 参数为数字,1表示每个主节点需要1个从节点。

  • 通过该方式创建的带有从节点的机器不能够自己手动指定主节点,所以如果需要指定的话,需要自己手动指定,先使用② 或③创建好主节点后,再通过④来处理。

image-1648801417862

② 创建集群主节点

redis-cli --cluster create 172.19.82.206:7000 172.19.82.206:7001 172.19.82.206:7002

③ 添加集群主节点

redis-cli --cluster add-node 172.19.82.206:7003 172.19.82.206:7004
  • 说明:为一个指定集群添加节点,需要先连到该集群的任意一个节点IP(172.19.82.206:7000),再把新节点加入。该2个参数的顺序有要求:新加入的节点放在前面。

④ 添加集群从节点

redis-cli --cluster add-node 172.19.82.206:7005 172.19.82.206:7000 --cluster-slave --cluster-master-id 117457eab5071954faab5e81c3170600d5192270
  • 说明:把7005节点加入到7000节点的集群中,并且当做node_id为 117457eab5071954faab5e81c3170600d5192270 的从节点。如果不指定 --cluster-master-id 会随机分配到任意一个主节点。

⑤ 删除节点

redis-cli --cluster del-node 172.19.82.206:7005 f6a6957421b80409106cb36be3c7ba41f3b603ff
  • 说明:指定IP、端口和node_id 来删除一个节点,从节点可以直接删除,有slot分配的主节点不能直接删除。删除之后,该节点会被shutdown。

  • 注意:当被删除掉的节点重新起来之后不能自动加入集群,但其和主的复制还是正常的,也可以通过该节点看到集群信息(通过其他正常节点已经看不到该被del-node节点的信息)。

  • 如果想要再次加入集群,则需要先在该节点执行cluster reset,再用add-node进行添加,进行增量同步复制。

1.10 集群测试

1)任意连接一个集群节点,进行集群状态检查

# 无认证方式
redis-cli --cluster check 172.19.82.206:7000 --cluster-search-multiple-owners
# 有认证方式在最后加上 -a 密码
redis-cli --cluster check 172.19.82.206:7000 --cluster-search-multiple-owners -a 111111

image-1648801548279

2)集群信息查看

检查key、slots、从节点个数的分配情况

redis-cli --cluster info 172.19.82.206:7001

image-1648801579541

3)修复集群

# 修复集群和槽的重复分配问题
redis-cli --cluster fix 172.19.82.206:7001 --cluster-search-multiple-owners

**4)设置集群的超时时间 **

redis-cli --cluster set-timeout 172.19.82.206:7000 10000
# 说明:连接到集群的任意一节点来设置集群的超时时间参数cluster-node-timeout

5)集群中执行相关命令

# 连接到集群的任意一节点来对整个集群的所有节点进行设置。
redis-cli --cluster call 172.19.82.206:7001 config set requirepass cc
redis-cli -a cc --cluster call 172.19.82.206:7001 config set masterauth cc
redis-cli -a cc --cluster call 172.19.82.206:7001 config rewrite

1.11 关闭集群、删除集群文件

for((i=1;i<=6;i++)); do /usr/local/redis/bin/redis-cli -c -h 192.168.174.128 -p 703$i shutdown; done  

for((i=1;i<=6;i++)); do cd 703$i; rm -rf appendonly.aof; rm -rf dump.rdb; rm -rf nodes-703$i.conf; cd ..; done

1.12 redis-cli --cluster help 说明

redis-cli --cluster help
Cluster Manager Commands:
  create         host1:port1 ... hostN:portN   #创建集群
                 --cluster-replicas <arg>      #从节点个数
  check          host:port                     #检查集群
                 --cluster-search-multiple-owners #检查是否有槽同时被分配给了多个节点
  info           host:port                     #查看集群状态
  fix            host:port                     #修复集群
                 --cluster-search-multiple-owners #修复槽的重复分配问题
  reshard        host:port                     #指定集群的任意一节点进行迁移slot,重新分slots
                 --cluster-from <arg>          #需要从哪些源节点上迁移slot,可从多个源节点完成迁移,以逗号隔开,传递的是节点的node id,还可以直接传递--from all,这样源节点就是集群的所有节点,不传递该参数的话,则会在迁移过程中提示用户输入
                 --cluster-to <arg>            #slot需要迁移的目的节点的node id,目的节点只能填写一个,不传递该参数的话,则会在迁移过程中提示用户输入
                 --cluster-slots <arg>         #需要迁移的slot数量,不传递该参数的话,则会在迁移过程中提示用户输入。
                 --cluster-yes                 #指定迁移时的确认输入
                 --cluster-timeout <arg>       #设置migrate命令的超时时间
                 --cluster-pipeline <arg>      #定义cluster getkeysinslot命令一次取出的key数量,不传的话使用默认值为10
                 --cluster-replace             #是否直接replace到目标节点
  rebalance      host:port                                      #指定集群的任意一节点进行平衡集群节点slot数量 
                 --cluster-weight <node1=w1...nodeN=wN>         #指定集群节点的权重
                 --cluster-use-empty-masters                    #设置可以让没有分配slot的主节点参与,默认不允许
                 --cluster-timeout <arg>                        #设置migrate命令的超时时间
                 --cluster-simulate                             #模拟rebalance操作,不会真正执行迁移操作
                 --cluster-pipeline <arg>                       #定义cluster getkeysinslot命令一次取出的key数量,默认值为10
                 --cluster-threshold <arg>                      #迁移的slot阈值超过threshold,执行rebalance操作
                 --cluster-replace                              #是否直接replace到目标节点
  add-node       new_host:new_port existing_host:existing_port  #添加节点,把新节点加入到指定的集群,默认添加主节点
                 --cluster-slave                                #新节点作为从节点,默认随机一个主节点
                 --cluster-master-id <arg>                      #给新节点指定主节点
  del-node       host:port node_id                              #删除给定的一个节点,成功后关闭该节点服务
  call           host:port command arg arg .. arg               #在集群的所有节点执行相关命令
  set-timeout    host:port milliseconds                         #设置cluster-node-timeout
  import         host:port                                      #将外部redis数据导入集群
                 --cluster-from <arg>                           #将指定实例的数据导入到集群
                 --cluster-copy                                 #migrate时指定copy
                 --cluster-replace                              #migrate时指定replace
  help           

For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.

2、java连接Redis集群和spring整合

2.1 创建maven工程

<dependencies>  
	    <dependency>  
	        <groupId>org.springframework.data</groupId>  
	        <artifactId>spring-data-redis</artifactId>  
	        <version>1.0.2.RELEASE</version>  
	    </dependency>  
	    <dependency>  
	        <groupId>org.springframework</groupId>  
	        <artifactId>spring-test</artifactId>  
	        <version>3.1.2.RELEASE</version>  
	        <scope>test</scope>  
	    </dependency>  
	      
	    <dependency>  
	        <groupId>redis.clients</groupId>  
	        <artifactId>jedis</artifactId>  
	        <version>2.9.0</version>  
	    </dependency>  
	      
	     <dependency>  
	        <groupId>junit</groupId>  
	        <artifactId>junit</artifactId>  
	        <version>4.8.2</version>  
	        <scope>test</scope>  
	    </dependency>  
</dependencies>

2.2 spring和redis的整合配置文件

<?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:aop="http://www.springframework.org/schema/aop"   
	     xmlns:context="http://www.springframework.org/schema/context"  
	     xmlns:jee="http://www.springframework.org/schema/jee"  
	     xmlns:tx="http://www.springframework.org/schema/tx"  
	     xsi:schemaLocation="http://www.springframework.org/schema/aop 
					         http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
					         http://www.springframework.org/schema/beans 
					         http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
					         http://www.springframework.org/schema/context 
					         http://www.springframework.org/schema/context/spring-context-4.0.xsd  
					         http://www.springframework.org/schema/jee 
					         http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
					         http://www.springframework.org/schema/tx 
					         http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">   
		
		<context:property-placeholder location="classpath:redis.properties"/>
		<context:annotation-config/>
		
		
		<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
			 <property name="maxIdle" value="${redis.maxIdle}"/>
	         <property name="maxTotal" value="${redis.maxActive}" />
	         <property name="maxWaitMillis" value="${redis.maxWait}" />
	         <property name="testOnBorrow" value="${redis.testOnBorrow}"/>
		</bean>
		
		<bean id="hostport1" class="redis.clients.jedis.HostAndPort">
			<constructor-arg name="host" value="192.168.129.128"/>
			<constructor-arg name="port" value="7000"/>
		</bean>
		
		<bean id="hostport2" class="redis.clients.jedis.HostAndPort">
			<constructor-arg name="host" value="192.168.129.128"/>
			<constructor-arg name="port" value="7001"/>
		</bean>
		
		<bean id="hostport3" class="redis.clients.jedis.HostAndPort">
			<constructor-arg name="host" value="192.168.129.128"/>
			<constructor-arg name="port" value="7002"/>
		</bean>
		
		<bean id="hostport4" class="redis.clients.jedis.HostAndPort">
			<constructor-arg name="host" value="192.168.129.128"/>
			<constructor-arg name="port" value="7003"/>
		</bean>
		
		<bean id="hostport5" class="redis.clients.jedis.HostAndPort">
			<constructor-arg name="host" value="192.168.129.128"/>
			<constructor-arg name="port" value="7004"/>
		</bean>
		
		<bean id="hostport6" class="redis.clients.jedis.HostAndPort">
			<constructor-arg name="host" value="192.168.129.128"/>
			<constructor-arg name="port" value="7005"/>
		</bean>
		
		<bean id="redisCluster" class="redis.clients.jedis.JedisCluster">
			<constructor-arg name="jedisClusterNode">
					<set>
						<ref bean="hostport1"/>
						<ref bean="hostport2"/>
						<ref bean="hostport3"/>
						<ref bean="hostport4"/>
						<ref bean="hostport5"/>
						<ref bean="hostport6"/>
					</set>
			</constructor-arg>
			
			<constructor-arg name="connectionTimeout" value="6000"/>
			<constructor-arg name="soTimeout" value="5000"/>
			<constructor-arg name="maxAttempts" value="3" />
			<constructor-arg name="poolConfig">
             <ref bean="jedisPoolConfig"/>
        	</constructor-arg>
		</bean>
</beans>

2.3 redis.properties文件配置

redis.maxIdle=10
redis.maxActive=1000
redis.maxWait=5000
redis.testOnBorrow=true

2.4 测试方法,测试是否连接和存取数据

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.JedisCluster;
 
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {"classpath:spring-redis.xml"})
public class RedisCluterTest {
 
	@Autowired
	JedisCluster jedisCluster;
	
	//通过jedisCluster 操作redis集群
	@Test
	public void fun1() {
		//返回ack
		String name = jedisCluster.set("name", "xxx");
		String age = jedisCluster.set("age", "28");
		String sex = jedisCluster.set("sex", "1");
		
		System.out.println(name+"  "+age+"   "+sex);
		
		String name1 = jedisCluster.get("name");
		String age1 = jedisCluster.get("age");
		String sex1 = jedisCluster.get("sex");
		System.out.println("get的值"+name1+"   "+ age1 +"  "+ sex1);
	}
}

2.5 测试方法2

import java.util.LinkedList;
import java.util.List;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
public class RedisUtils {
    private static ShardedJedisPool pool;
    static {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(100);
        config.setMaxIdle(50);
        config.setMaxWaitMillis(3000);
        config.setTestOnBorrow(true);
        config.setTestOnReturn(true);
        // 集群
        JedisShardInfo jedisShardInfo1 = new JedisShardInfo("120.26.1.1", 6379);
        jedisShardInfo1.setPassword("123456888888");
        List<JedisShardInfo> list = new LinkedList<JedisShardInfo>();
        list.add(jedisShardInfo1);        pool = new ShardedJedisPool(config, list);
    }

    public static void main(String[] args) {
        ShardedJedis jedis = pool.getResource();
        String keys = "myname";
        String vaule = jedis.set(keys, "lxr");
        System.out.println(vaule);
    }
}

3、redis安装过程常见错误

3.1 没有识别make命令

错误提示如 Command ‘make’ not found, but can be installed with,使用 apt install make 安装。

image-1648801815347

3.2 没有识别pkg-config

错误提示如pkg-config: not found,使用 apt install pkg-config ( 或 apt-get install pkg-config) 安装。

  • PKG CONFIG是编译应用程序和库时使用的辅助工具。它帮助您在命令行中插入正确的编译器选项,以便应用程序可以使用gcc-o test test.c'pkg-config--libs--cflags glib-2.0',例如,而不是硬编码在哪里找到glib(或其他库)的值。

image-1648801914466

3.3 没有识别 cc: not found

错误提示如cc/bin/sh: 1: cc: not found     make: *** [Makefile:374: adlist.o] Error 127,这是由于系统没有安装gcc环境,因此在进行编译时才会出现上面提示,使用命令apt-get install build-essential (或 apt-get build-dep gcc )进行安装,安装完了可以使用 gcc --version 命令来查看版本。

image-1648801986616

image-1648801990640

3.4 错误 fatal error: jemalloc/jemalloc.h: No such file or directory

fatal error: jemalloc/jemalloc.h: No such file or directory,这个错误的本质是我们在开始执行make 时遇到了错误(大部分是由于gcc未安装),然后我们安装好了gcc 后,我们再执行make ,这时就出现了jemalloc/jemalloc.h: No such file or directory。这是因为上次的编译失败,有残留的文件,我们需要清理下(make distclean),然后重新编译(make)就可以了。

  • 网上错误解决办法如  make MALLOC=libc , 虽然最后也是可以成功安装好 redis ,但是是有一些隐患的,首先我们要知道redis 需要使用内存分配器的, make MALLOC=jemalloc 就是指定内存分配器为 jemalloc ,make MALLOC=libc 就是指定内存分配器为 libc ,这个是有安全隐患的,jemalloc 内存分配器在实践中处理内存碎片是要比libc 好的,而且在README.md 文档也说明到了,jemalloc内存分配器也是包含在源码包里面的,可以在deps 目录下看到 jemalloc 目录。

image-1648802023814

4、Redis 主从复制、哨兵和集群三者区别

  • 主从复制(Replication)是为了数据备份(备份数据、负载均衡,一个Master可以有多个Slaves)
  • 哨兵(Sentinel)是为了高可用,可以管理多个Redis服务器,它提供了监控,提醒以及自动的故障转移的功能(sentinel发现master挂了后,就会从slave中重新选举一个master)
  • 集群(Cluster)是为了解决单机Redis容量有限的问题,将数据按一定的规则分配到多台机器,提高并发量

Redis哨兵主要功能

  • (1)集群监控:负责监控Redis master和slave进程是否正常工作
  • (2)消息通知:如果某个Redis实例有故障,那么哨兵负责发送消息作为报警通知给管理员
  • (3)故障转移:如果master node挂掉了,会自动转移到slave node上
  • (4)配置中心:如果故障转移发生了,通知client客户端新的master地址

5、附录:默认配置文件redis.conf详解

# Redis配置文件样例

# Note on units: when memory size is needed, it is possible to specifiy
# it in the usual form of 1k 5GB 4M and so forth:
#
# 1k => 1000 bytes
# 1kb => 1024 bytes
# 1m => 1000000 bytes
# 1mb => 1024*1024 bytes
# 1g => 1000000000 bytes
# 1gb => 1024*1024*1024 bytes
#
# units are case insensitive so 1GB 1Gb 1gB are all the same.

# Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程
# 启用守护进程后,Redis会把pid写到一个pidfile中,在/var/run/redis.pid
daemonize no

# 当Redis以守护进程方式运行时,Redis默认会把pid写入/var/run/redis.pid文件,可以通过pidfile指定
pidfile /var/run/redis.pid

# 指定Redis监听端口,默认端口为6379
# 如果指定0端口,表示Redis不监听TCP连接
port 6379

# 绑定的主机地址
# 你可以绑定单一接口,如果没有绑定,所有接口都会监听到来的连接
# bind 127.0.0.1

# Specify the path for the unix socket that will be used to listen for
# incoming connections. There is no default, so Redis will not listen
# on a unix socket when not specified.
#
# unixsocket /tmp/redis.sock
# unixsocketperm 755

# 当客户端闲置多长时间后关闭连接,如果指定为0,表示关闭该功能
timeout 0

# 指定日志记录级别,Redis总共支持四个级别:debug、verbose、notice、warning,默认为verbose
# debug (很多信息, 对开发/测试比较有用)
# verbose (many rarely useful info, but not a mess like the debug level)
# notice (moderately verbose, what you want in production probably)
# warning (only very important / critical messages are logged)
loglevel verbose

# 日志记录方式,默认为标准输出,如果配置为redis为守护进程方式运行,而这里又配置为标准输出,则日志将会发送给/dev/null
logfile stdout

# To enable logging to the system logger, just set 'syslog-enabled' to yes,
# and optionally update the other syslog parameters to suit your needs.
# syslog-enabled no

# Specify the syslog identity.
# syslog-ident redis

# Specify the syslog facility.  Must be USER or between LOCAL0-LOCAL7.
# syslog-facility local0

# 设置数据库的数量,默认数据库为0,可以使用select <dbid>命令在连接上指定数据库id
# dbid是从0到‘databases’-1的数目
databases 16

################################ SNAPSHOTTING  #################################
# 指定在多长时间内,有多少次更新操作,就将数据同步到数据文件,可以多个条件配合
# Save the DB on disk:
#
#   save <seconds> <changes>
#
#   Will save the DB if both the given number of seconds and the given
#   number of write operations against the DB occurred.
#
#   满足以下条件将会同步数据:
#   900秒(15分钟)内有1个更改
#   300秒(5分钟)内有10个更改
#   60秒内有10000个更改
#   Note: 可以把所有“save”行注释掉,这样就取消同步操作了

save 900 1
save 300 10
save 60 10000

# 指定存储至本地数据库时是否压缩数据,默认为yes,Redis采用LZF压缩,如果为了节省CPU时间,可以关闭该选项,但会导致数据库文件变的巨大
rdbcompression yes

# 指定本地数据库文件名,默认值为dump.rdb
dbfilename dump.rdb

# 工作目录.
# 指定本地数据库存放目录,文件名由上一个dbfilename配置项指定
# 
# Also the Append Only File will be created inside this directory.
# 
# 注意,这里只能指定一个目录,不能指定文件名
dir ./

################################# REPLICATION #################################

# 主从复制。使用slaveof从 Redis服务器复制一个Redis实例。注意,该配置仅限于当前slave有效
# so for example it is possible to configure the slave to save the DB with a
# different interval, or to listen to another port, and so on.
# 设置当本机为slav服务时,设置master服务的ip地址及端口,在Redis启动时,它会自动从master进行数据同步
# slaveof <masterip> <masterport>


# 当master服务设置了密码保护时,slav服务连接master的密码
# 下文的“requirepass”配置项可以指定密码
# masterauth <master-password>

# When a slave lost the connection with the master, or when the replication
# is still in progress, the slave can act in two different ways:
#
# 1) if slave-serve-stale-data is set to 'yes' (the default) the slave will
#    still reply to client requests, possibly with out of data data, or the
#    data set may just be empty if this is the first synchronization.
#
# 2) if slave-serve-stale data is set to 'no' the slave will reply with
#    an error "SYNC with master in progress" to all the kind of commands
#    but to INFO and SLAVEOF.
#
slave-serve-stale-data yes

# Slaves send PINGs to server in a predefined interval. It's possible to change
# this interval with the repl_ping_slave_period option. The default value is 10
# seconds.
#
# repl-ping-slave-period 10

# The following option sets a timeout for both Bulk transfer I/O timeout and
# master data or ping response timeout. The default value is 60 seconds.
#
# It is important to make sure that this value is greater than the value
# specified for repl-ping-slave-period otherwise a timeout will be detected
# every time there is low traffic between the master and the slave.
#
# repl-timeout 60

################################## SECURITY ###################################

# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
# 设置Redis连接密码,如果配置了连接密码,客户端在连接Redis时需要通过auth <password>命令提供密码,默认关闭
# requirepass foobared

# Command renaming.
#
# It is possilbe to change the name of dangerous commands in a shared
# environment. For instance the CONFIG command may be renamed into something
# of hard to guess so that it will be still available for internal-use
# tools but not available for general clients.
#
# Example:
#
# rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52
#
# It is also possilbe to completely kill a command renaming it into
# an empty string:
#
# rename-command CONFIG ""

################################### LIMITS ####################################

# 设置同一时间最大客户端连接数,默认无限制,Redis可以同时打开的客户端连接数为Redis进程可以打开的最大文件描述符数,
# 如果设置maxclients 0,表示不作限制。当客户端连接数到达限制时,Redis会关闭新的连接并向客户端返回max Number of clients reached错误信息
# maxclients 128

# Don't use more memory than the specified amount of bytes.
# When the memory limit is reached Redis will try to remove keys with an
# EXPIRE set. It will try to start freeing keys that are going to expire
# in little time and preserve keys with a longer time to live.
# Redis will also try to remove objects from free lists if possible.
#
# If all this fails, Redis will start to reply with errors to commands
# that will use more memory, like SET, LPUSH, and so on, and will continue
# to reply to most read-only commands like GET.
#
# WARNING: maxmemory can be a good idea mainly if you want to use Redis as a
# 'state' server or cache, not as a real DB. When Redis is used as a real
# database the memory usage will grow over the weeks, it will be obvious if
# it is going to use too much memory in the long run, and you'll have the time
# to upgrade. With maxmemory after the limit is reached you'll start to get
# errors for write operations, and this may even lead to DB inconsistency.
# 指定Redis最大内存限制,Redis在启动时会把数据加载到内存中,达到最大内存后,Redis会先尝试清除已到期或即将到期的Key,
# 当此方法处理后,仍然到达最大内存设置,将无法再进行写入操作,但仍然可以进行读取操作。
# Redis新的vm机制,会把Key存放内存,Value会存放在swap区
# maxmemory <bytes>

# MAXMEMORY POLICY: how Redis will select what to remove when maxmemory
# is reached? You can select among five behavior:
# 
# volatile-lru -> remove the key with an expire set using an LRU algorithm
# allkeys-lru -> remove any key accordingly to the LRU algorithm
# volatile-random -> remove a random key with an expire set
# allkeys->random -> remove a random key, any key
# volatile-ttl -> remove the key with the nearest expire time (minor TTL)
# noeviction -> don't expire at all, just return an error on write operations
# 
# Note: with all the kind of policies, Redis will return an error on write
#       operations, when there are not suitable keys for eviction.
#
#       At the date of writing this commands are: set setnx setex append
#       incr decr rpush lpush rpushx lpushx linsert lset rpoplpush sadd
#       sinter sinterstore sunion sunionstore sdiff sdiffstore zadd zincrby
#       zunionstore zinterstore hset hsetnx hmset hincrby incrby decrby
#       getset mset msetnx exec sort
#
# The default is:
#
# maxmemory-policy volatile-lru

# LRU and minimal TTL algorithms are not precise algorithms but approximated
# algorithms (in order to save memory), so you can select as well the sample
# size to check. For instance for default Redis will check three keys and
# pick the one that was used less recently, you can change the sample size
# using the following configuration directive.
#
# maxmemory-samples 3

############################## APPEND ONLY MODE ###############################

# 
# Note that you can have both the async dumps and the append only file if you
# like (you have to comment the "save" statements above to disable the dumps).
# Still if append only mode is enabled Redis will load the data from the
# log file at startup ignoring the dump.rdb file.
# 指定是否在每次更新操作后进行日志记录,Redis在默认情况下是异步的把数据写入磁盘,如果不开启,可能会在断电时导致一段时间内的数据丢失。
# 因为redis本身同步数据文件是按上面save条件来同步的,所以有的数据会在一段时间内只存在于内存中。默认为no
# IMPORTANT: Check the BGREWRITEAOF to check how to rewrite the append
# log file in background when it gets too big.

appendonly no

# 指定更新日志文件名,默认为appendonly.aof
# appendfilename appendonly.aof

# The fsync() call tells the Operating System to actually write data on disk
# instead to wait for more data in the output buffer. Some OS will really flush 
# data on disk, some other OS will just try to do it ASAP.

# 指定更新日志条件,共有3个可选值:
# no:表示等操作系统进行数据缓存同步到磁盘(快)
# always:表示每次更新操作后手动调用fsync()将数据写到磁盘(慢,安全)
# everysec:表示每秒同步一次(折衷,默认值)

appendfsync everysec
# appendfsync no

# When the AOF fsync policy is set to always or everysec, and a background
# saving process (a background save or AOF log background rewriting) is
# performing a lot of I/O against the disk, in some Linux configurations
# Redis may block too long on the fsync() call. Note that there is no fix for
# this currently, as even performing fsync in a different thread will block
# our synchronous write(2) call.
#
# In order to mitigate this problem it's possible to use the following option
# that will prevent fsync() from being called in the main process while a
# BGSAVE or BGREWRITEAOF is in progress.
#
# This means that while another child is saving the durability of Redis is
# the same as "appendfsync none", that in pratical terms means that it is
# possible to lost up to 30 seconds of log in the worst scenario (with the
# default Linux settings).
# 
# If you have latency problems turn this to "yes". Otherwise leave it as
# "no" that is the safest pick from the point of view of durability.
no-appendfsync-on-rewrite no

# Automatic rewrite of the append only file.
# Redis is able to automatically rewrite the log file implicitly calling
# BGREWRITEAOF when the AOF log size will growth by the specified percentage.
# 
# This is how it works: Redis remembers the size of the AOF file after the
# latest rewrite (or if no rewrite happened since the restart, the size of
# the AOF at startup is used).
#
# This base size is compared to the current size. If the current size is
# bigger than the specified percentage, the rewrite is triggered. Also
# you need to specify a minimal size for the AOF file to be rewritten, this
# is useful to avoid rewriting the AOF file even if the percentage increase
# is reached but it is still pretty small.
#
# Specify a precentage of zero in order to disable the automatic AOF
# rewrite feature.

auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb

################################## SLOW LOG ###################################

# The Redis Slow Log is a system to log queries that exceeded a specified
# execution time. The execution time does not include the I/O operations
# like talking with the client, sending the reply and so forth,
# but just the time needed to actually execute the command (this is the only
# stage of command execution where the thread is blocked and can not serve
# other requests in the meantime).
# 
# You can configure the slow log with two parameters: one tells Redis
# what is the execution time, in microseconds, to exceed in order for the
# command to get logged, and the other parameter is the length of the
# slow log. When a new command is logged the oldest one is removed from the
# queue of logged commands.

# The following time is expressed in microseconds, so 1000000 is equivalent
# to one second. Note that a negative number disables the slow log, while
# a value of zero forces the logging of every command.
slowlog-log-slower-than 10000

# There is no limit to this length. Just be aware that it will consume memory.
# You can reclaim memory used by the slow log with SLOWLOG RESET.
slowlog-max-len 1024

################################ VIRTUAL MEMORY ###############################

### WARNING! Virtual Memory is deprecated in Redis 2.4
### The use of Virtual Memory is strongly discouraged.

### WARNING! Virtual Memory is deprecated in Redis 2.4
### The use of Virtual Memory is strongly discouraged.

# Virtual Memory allows Redis to work with datasets bigger than the actual
# amount of RAM needed to hold the whole dataset in memory.
# In order to do so very used keys are taken in memory while the other keys
# are swapped into a swap file, similarly to what operating systems do
# with memory pages.
# 指定是否启用虚拟内存机制,默认值为no,
# VM机制将数据分页存放,由Redis将访问量较少的页即冷数据swap到磁盘上,访问多的页面由磁盘自动换出到内存中
# 把vm-enabled设置为yes,根据需要设置好接下来的三个VM参数,就可以启动VM了
vm-enabled no
# vm-enabled yes

# This is the path of the Redis swap file. As you can guess, swap files
# can't be shared by different Redis instances, so make sure to use a swap
# file for every redis process you are running. Redis will complain if the
# swap file is already in use.
#
# Redis交换文件最好的存储是SSD(固态硬盘)
# 虚拟内存文件路径,默认值为/tmp/redis.swap,不可多个Redis实例共享
# *** WARNING *** if you are using a shared hosting the default of putting
# the swap file under /tmp is not secure. Create a dir with access granted
# only to Redis user and configure Redis to create the swap file there.
vm-swap-file /tmp/redis.swap

# With vm-max-memory 0 the system will swap everything it can. Not a good
# default, just specify the max amount of RAM you can in bytes, but it's
# better to leave some margin. For instance specify an amount of RAM
# that's more or less between 60 and 80% of your free RAM.
# 将所有大于vm-max-memory的数据存入虚拟内存,无论vm-max-memory设置多少,所有索引数据都是内存存储的(Redis的索引数据就是keys)
# 也就是说当vm-max-memory设置为0的时候,其实是所有value都存在于磁盘。默认值为0
vm-max-memory 0

# Redis swap文件分成了很多的page,一个对象可以保存在多个page上面,但一个page上不能被多个对象共享,vm-page-size是要根据存储的数据大小来设定的。
# 建议如果存储很多小对象,page大小最后设置为32或64bytes;如果存储很大的对象,则可以使用更大的page,如果不确定,就使用默认值
vm-page-size 32

# 设置swap文件中的page数量由于页表(一种表示页面空闲或使用的bitmap)是存放在内存中的,在磁盘上每8个pages将消耗1byte的内存
# swap空间总容量为 vm-page-size * vm-pages
#
# With the default of 32-bytes memory pages and 134217728 pages Redis will
# use a 4 GB swap file, that will use 16 MB of RAM for the page table.
#
# It's better to use the smallest acceptable value for your application,
# but the default is large in order to work in most conditions.
vm-pages 134217728

# Max number of VM I/O threads running at the same time.
# This threads are used to read/write data from/to swap file, since they
# also encode and decode objects from disk to memory or the reverse, a bigger
# number of threads can help with big objects even if they can't help with
# I/O itself as the physical device may not be able to couple with many
# reads/writes operations at the same time.
# 设置访问swap文件的I/O线程数,最后不要超过机器的核数,如果设置为0,那么所有对swap文件的操作都是串行的,可能会造成比较长时间的延迟,默认值为4
vm-max-threads 4

############################### ADVANCED CONFIG ###############################

# Hashes are encoded in a special way (much more memory efficient) when they
# have at max a given numer of elements, and the biggest element does not
# exceed a given threshold. You can configure this limits with the following
# configuration directives.
# 指定在超过一定的数量或者最大的元素超过某一临界值时,采用一种特殊的哈希算法
hash-max-zipmap-entries 512
hash-max-zipmap-value 64

# Similarly to hashes, small lists are also encoded in a special way in order
# to save a lot of space. The special representation is only used when
# you are under the following limits:
list-max-ziplist-entries 512
list-max-ziplist-value 64

# Sets have a special encoding in just one case: when a set is composed
# of just strings that happens to be integers in radix 10 in the range
# of 64 bit signed integers.
# The following configuration setting sets the limit in the size of the
# set in order to use this special memory saving encoding.
set-max-intset-entries 512

# Similarly to hashes and lists, sorted sets are also specially encoded in
# order to save a lot of space. This encoding is only used when the length and
# elements of a sorted set are below the following limits:
zset-max-ziplist-entries 128
zset-max-ziplist-value 64

# Active rehashing uses 1 millisecond every 100 milliseconds of CPU time in
# order to help rehashing the main Redis hash table (the one mapping top-level
# keys to values). The hash table implementation redis uses (see dict.c)
# performs a lazy rehashing: the more operation you run into an hash table
# that is rhashing, the more rehashing "steps" are performed, so if the
# server is idle the rehashing is never complete and some more memory is used
# by the hash table.
# 
# The default is to use this millisecond 10 times every second in order to
# active rehashing the main dictionaries, freeing memory when possible.
#
# If unsure:
# use "activerehashing no" if you have hard latency requirements and it is
# not a good thing in your environment that Redis can reply form time to time
# to queries with 2 milliseconds delay.
# 指定是否激活重置哈希,默认为开启
activerehashing yes

################################## INCLUDES ###################################

# 指定包含其他的配置文件,可以在同一主机上多个Redis实例之间使用同一份配置文件,而同时各实例又拥有自己的特定配置文件
# include /path/to/local.conf
# include /path/to/other.conf
0

评论区