Redis入門:分佈式存儲
要完成數據的分片存儲,需要多個redis實例。
1 多個REDIS實例
前面的單個redis節點實例的啓動時默認配置端口號6379。
1.1 配置文件
配置文件位置:/redis根目錄/redis.conf。
修改配置文件,使用vim命令進行編輯:
vim redis.conf
1.1.1 內存分配
一個redis實例默認佔用所有物理內存,在實際使用中需要限制大小。這裏爲了簡化操作,不做內存佔用的配置,使用默認即可。
從下面這一部分中,就能看出內存配置的方式和配置文件的使用方式,如果要使用配置文件,需要在啓動的時候將配置文件作爲啓動命令的第一個參數。
內存配置寫法不同代表的大小也不同。參照11行到16行。
以下爲本段配置內容詳情:
1 # Redis configuration file example.
2 #
3 # Note that in order to read the configuration file, Redis must be
4 # started with the file path as first argument:
5 #
6 # ./redis-server /path/to/redis.conf
7
8 # Note on units: when memory size is needed, it is possible to specify
9 # it in the usual form of 1k 5GB 4M and so forth:
10 #
11 # 1k => 1000 bytes
12 # 1kb => 1024 bytes
13 # 1m => 1000000 bytes
14 # 1mb => 1024*1024 bytes
15 # 1g => 1000000000 bytes
16 # 1gb => 1024*1024*1024 bytes
17 #
18 # units are case insensitive so 1GB 1Gb 1gB are all the same.
19
1.1.2 綁定ip
這個版本的配置文件中,綁定IP的示例從60行到62行都是。
綁定的配置在75行。
以下爲本段配置內容詳情:
46 ################################## NETWORK #####################################
47
48 # By default, if no "bind" configuration directive is specified, Redis listens
49 # for connections from all available network interfaces on the host machine.
50 # It is possible to listen to just one or multiple selected interfaces using
51 # the "bind" configuration directive, followed by one or more IP addresses.
52 # Each address can be prefixed by "-", which means that redis will not fail to
53 # start if the address is not available. Being not available only refers to
54 # addresses that does not correspond to any network interfece. Addresses that
55 # are already in use will always fail, and unsupported protocols will always BE
56 # silently skipped.
57 #
58 # Examples:
59 #
60 # bind 192.168.1.100 10.0.0.1 # listens on two specific IPv4 addresses
61 # bind 127.0.0.1 ::1 # listens on loopback IPv4 and IPv6
62 # bind * -::* # like the default, all available interfaces
63 #
64 # ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
65 # internet, binding to all the interfaces is dangerous and will expose the
66 # instance to everybody on the internet. So by default we uncomment the
67 # following bind directive, that will force Redis to listen only on the
68 # IPv4 and IPv6 (if available) loopback interface addresses (this means Redis
69 # will only be able to accept client connections from the same host that it is
70 # running on).
71 #
72 # IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
73 # JUST COMMENT OUT THE FOLLOWING LINE.
74 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
75 bind 127.0.0.1 -::1
76
1.1.3 保護模式
保護模式不啓動,在實際工作中爲了安全是要啓動的,做測試的過程爲了避免麻煩,可以不啓動。
配置項在94行,只需要更改值即可,yes表示開啓,no表示不開啓。
以下爲本段配置詳情:
90 # By default protected mode is enabled. You should disable it only if
91 # you are sure you want clients from other hosts to connect to Redis
92 # even if no authentication is configured, nor a specific set of interfaces
93 # are explicitly listed using the "bind" directive.
94 protected-mode no
1.1.4 默認端口
6379是默認端口(要啓動其他的redis實例需要修改端口)。
98行爲配置信息,port後爲要使用的端口號。
以下是本段配置信息:
96 # Accept connections on the specified port, default is 6379 (IANA #815344).
97 # If port 0 is specified Redis will not listen on a TCP socket.
98 port 6379
1.1.5 連接超時
當客戶端空閒時間達到一小時,就會自動斷開連接。0秒錶示不啓用超時配置。
119行爲此項目配置內容,此項的配置單位爲秒,如果啓用配置爲對應的秒數即可。
以下爲本段配置內容詳情,這裏本人配置了1分鐘:
118 # Close the connection after a client is idle for N seconds (0 to disable)
119 # timeout 0
120 timeout 60
1.1.6 後臺運行
daemonize設置成yes讓redis服務器啓動由守護進程管理,使其後臺執行,不佔用控制檯。其配置在257行。
內容如下:
252 ################################# GENERAL #####################################
253
254 # By default Redis does not run as a daemon. Use 'yes' if you need it.
255 # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
256 # When Redis is supervised by upstart or systemd, this parameter has no impact.
257 daemonize yes
1.1.7 pid文件
對應不同的redis實例,pid的文件名稱需要和端口同名。配置在289行。
內容如下:
277 # If a pid file is specified, Redis writes it where specified at startup
278 # and removes it at exit.
279 #
280 # When the server runs non daemonized, no pid file is created if none is
281 # specified in the configuration. When the server is daemonized, the pid file
282 # is used even if not specified, defaulting to "/var/run/redis.pid".
283 #
284 # Creating a pid file is best effort: if Redis is not able to create it
285 # nothing bad happens, the server will start and run normally.
286 #
287 # Note that on modern Linux systems "/run/redis.pid" is more conforming
288 # and should be used instead.
289 pidfile /var/run/redis_6379.pid
1.1.8 持久化規則
- save 900 1 當900秒以內,至少有1條數據變動,可使flush保存數據到文件。
- save 300 10 當300秒以內,至少10條數據變動,保存文件。
- save 60 10000 當60秒內,有10000條數據變動,保存文件。
保持默認規則即可,也可按照實際需求進行配置。
本人這裏配置了第三種,具體內容如下:
360 ################################ SNAPSHOTTING ################################
361
362 # Save the DB to disk.
363 #
364 # save <seconds> <changes>
365 #
366 # Redis will save the DB if both the given number of seconds and the given
367 # number of write operations against the DB occurred.
368 #
369 # Snapshotting can be completely disabled with a single empty string argument
370 # as in following example:
371 #
372 # save ""
373 #
374 # Unless specified otherwise, by default Redis will save the DB:
375 # * After 3600 seconds (an hour) if at least 1 key changed
376 # * After 300 seconds (5 minutes) if at least 100 keys changed
377 # * After 60 seconds if at least 10000 keys changed
378 #
379 # You can set these explicitly by uncommenting the three following lines.
380 #
381 # save 3600 1
382 # save 300 100
383 save 60 10000
1.2 其他實例
將剛配置好的配置文件複製2份,分別修改端口號和pid文件名即可。
cp redis.conf redis6380.conf
cp redis.conf redis6381.conf
將拷貝的文件中只修改與端口有關內容port和pid的文件名。
2 啓動REDIS多實例
啓動多實例的命令如下:
redis-server redis.conf(指定啓動文件)
啓動另外兩個節點:
#redis-server redis6380.conf
#redis-server redis6381.conf
#ps -ef|grep redis
root 2822 1 0 15:53 ? 00:00:00 redis-server 127.0.0.1:6379
root 2836 1 0 15:53 ? 00:00:00 redis-server 127.0.0.1:6380
root 2848 1 0 15:53 ? 00:00:00 redis-server 127.0.0.1:6381
root 2868 1697 0 15:53 pts/1 00:00:00 grep --color=auto redis
3 登錄客戶端
指定端口登錄客戶端redis-cli -p [端口號]
#默認登錄
[root@lk7 bin]# redis-cli
127.0.0.1:6379> exit
[root@lk7 bin]# redis-cli -p 6379
127.0.0.1:6379> exit
[root@lk7 bin]# redis-cli -p 6380
127.0.0.1:6380> exit
[root@lk7 bin]# redis-cli -p 6381
127.0.0.1:6381> exit
6380和6381會共享6379的dump.db文件,所以不同的節點實例在同一個機器上運行時,可以修改dump.db指定端口文件。