Java项目实战 - Redis安装和集群搭建

Redis

Redis是用C语言开发的一个开源的高性能键值对(key-value)数据库。它通过提供多种键值数据类型来适应不同场景下的存储需求,目前为止Redis支持的键值数据类型如下:

  • 字符串类型
  • 散列类型
  • 列表类型
  • 集合类型
  • 有序集合类型

详细内容,参考:Redis

安装

安装还是很简单的,如下:

1
2
3
4
$ wget http://download.redis.io/releases/redis-4.0.10.tar.gz
$ tar xzf redis-4.0.10.tar.gz
$ cd redis-4.0.10
$ make

运行

  1. 直接启动

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    [root@Automation redis-4.0.10]# src/redis-server 
    9833:C 13 Jun 23:58:18.056 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
    9833:C 13 Jun 23:58:18.056 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=9833, just started
    9833:C 13 Jun 23:58:18.056 # Warning: no config file specified, using the default config. In order to specify a config file use src/redis-server /path/to/redis.conf
    9833:M 13 Jun 23:58:18.058 * Increased maximum number of open files to 10032 (it was originally set to 1024).
    _._
    _.-``__ ''-._
    _.-`` `. `_. ''-._ Redis 4.0.10 (00000000/0) 64 bit
    .-`` .-```. ```\/ _.,_ ''-._
    ( ' , .-` | `, ) Running in standalone mode
    |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
    | `-._ `._ / _.-' | PID: 9833
    `-._ `-._ `-./ _.-' _.-'

    |`-._`-._ `-.__.-' _.-'_.-'|
    | `-._`-._ _.-'_.-' | http://redis.io
    `-._ `-._`-.__.-'_.-' _.-'
    |`-._`-._ `-.__.-' _.-'_.-'|

    | `-._`-._ _.-'_.-' |
    `-._ `-._`-.__.-'_.-' _.-'
    `-._ `-.__.-' _.-'
    `-._ _.-'
    `-.__.-'

  2. 指定配置文件启动
    把redis的解压缩目录的redis.conf文件复制一份到/usr/local/redis/bin目录下

    1
    2
    3
    4
    [root@Automation redis-4.0.10]# src/redis-server redis.conf 
    12033:C 14 Jun 00:15:48.382 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
    12033:C 14 Jun 00:15:48.383 # Redis version=4.0.10, bits=64, commit=00000000, modified=0, pid=12033, just started
    12033:C 14 Jun 00:15:48.383 # Configuration loaded
  3. 验证

    1
    2
    3
    4
    5
    6
    [root@Automation redis-4.0.10]# src/redis-cli 
    127.0.0.1:6379> set a 10
    OK
    127.0.0.1:6379> get a
    "10"
    127.0.0.1:6379>
  4. 关闭

    1
    [root@Automation redis-4.0.10]# src/redis-cli shutdown

集群搭建

需要安装以下环境

  • ruby的环境:yum install ruby
  • rubygems组件:yum install rubygems
  • redis和ruby的接口:gem install redis

在同一台机器上搭建环境

  1. /usr/local/下创建文件夹redis-cluster
  2. 为每个实例创建一个文件夹,共6个

    1
    2
    [root@Automation redis-cluster]# ls
    7001 7002 7003 7004 7005 7006
  3. 把redis安装目录bin Copy到6个文件夹

  4. 修改每个实例
    1. Port 7001 - 7006
    2. 屏蔽限制本地访问,在bind 127.0.0.1之前加#
    3. protected-mode后的yes改为no
    4. daemonize后的no改为yes
    5. 去掉cluster-enabled前面的#
    6. 去掉cluster-node-timeout前面的#
  5. 启动6个实例 - 可以建个脚本一并启动
  6. 验证启动
    redis-cluster]# ps -ef | grep redis
    1
    root     24891     1  0 00:05 ?        00:00:00 ./redis-server 127.0.0.1:7001 [cluster]
    root     24896     1  0 00:05 ?        00:00:00 ./redis-server 127.0.0.1:7002 [cluster]
    root     24901     1  0 00:05 ?        00:00:00 ./redis-server 127.0.0.1:7003 [cluster]
    root     24906     1  0 00:05 ?        00:00:00 ./redis-server 127.0.0.1:7004 [cluster]
    root     24911     1  0 00:05 ?        00:00:00 ./redis-server 127.0.0.1:7005 [cluster]
    root     24913     1  0 00:05 ?        00:00:00 ./redis-server 127.0.0.1:7006 [cluster]
    root     24921 23221  0 00:05 pts/2    00:00:00 grep redis

创建集群

  1. 创建
    把这个脚本(redis-trib.rb)从解压目录下复制到/usr/local/redis-cluster。

    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
    [root@Automation redis-cluster]# ./redis-trib.rb create --replicas 1 10.24.33.147:7001 10.24.33.147:7002 10.24.33.147:7003 10.24.33.147:7004 10.24.33.147:7005  10.24.33.147:7006
    >>> Creating cluster
    >>> Performing hash slots allocation on 6 nodes...
    Using 3 masters:
    10.24.33.147:7001
    10.24.33.147:7002
    10.24.33.147:7003
    Adding replica 10.24.33.147:7005 to 10.24.33.147:7001
    Adding replica 10.24.33.147:7006 to 10.24.33.147:7002
    Adding replica 10.24.33.147:7004 to 10.24.33.147:7003
    >>> Trying to optimize slaves allocation for anti-affinity
    [WARNING] Some slaves are in the same host as their master
    M: 6d9cab72763d1a1fe85be04b6448998b88ea18a2 10.24.33.147:7001
    slots:0-5460 (5461 slots) master
    M: c7693a9c9e1189196481f65153ed41ca9a6fe449 10.24.33.147:7002
    slots:5461-10922 (5462 slots) master
    M: e3c3977d52165dcb589f59d16a713888abdc0c56 10.24.33.147:7003
    slots:10923-16383 (5461 slots) master
    S: 089b61bc451920a7a0cba996a4a430b8a8805c03 10.24.33.147:7004
    replicates e3c3977d52165dcb589f59d16a713888abdc0c56
    S: f3e9541424af4fcad212456d2f5ae5b6a3563525 10.24.33.147:7005
    replicates 6d9cab72763d1a1fe85be04b6448998b88ea18a2
    S: 0c16f9babaa8def8b721bcd82e43d9258f3f68d9 10.24.33.147:7006
    replicates c7693a9c9e1189196481f65153ed41ca9a6fe449
    Can I set the above configuration? (type 'yes' to accept): yes
    >>> Nodes configuration updated
    >>> Assign a different config epoch to each node
    >>> Sending CLUSTER MEET messages to join the cluster
    Waiting for the cluster to join.....
    >>> Performing Cluster Check (using node 10.24.33.147:7001)
    M: 6d9cab72763d1a1fe85be04b6448998b88ea18a2 10.24.33.147:7001
    slots:0-5460 (5461 slots) master
    1 additional replica(s)
    S: f3e9541424af4fcad212456d2f5ae5b6a3563525 10.24.33.147:7005
    slots: (0 slots) slave
    replicates 6d9cab72763d1a1fe85be04b6448998b88ea18a2
    M: e3c3977d52165dcb589f59d16a713888abdc0c56 10.24.33.147:7003
    slots:10923-16383 (5461 slots) master
    1 additional replica(s)
    S: 0c16f9babaa8def8b721bcd82e43d9258f3f68d9 10.24.33.147:7006
    slots: (0 slots) slave
    replicates c7693a9c9e1189196481f65153ed41ca9a6fe449
    S: 089b61bc451920a7a0cba996a4a430b8a8805c03 10.24.33.147:7004
    slots: (0 slots) slave
    replicates e3c3977d52165dcb589f59d16a713888abdc0c56
    M: c7693a9c9e1189196481f65153ed41ca9a6fe449 10.24.33.147:7002
    slots:5461-10922 (5462 slots) master
    1 additional replica(s)
    [OK] All nodes agree about slots configuration.
    >>> Check for open slots...
    >>> Check slots coverage...
    [OK] All 16384 slots covered.
  2. 验证

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    [root@Automation 7001]# redis-cli -h 10.24.33.147 -p 7001 -c
    10.24.33.147:7001> CLUSTER NODES
    f3e9541424af4fcad212456d2f5ae5b6a3563525 10.24.33.147:7005@17005 slave 6d9cab72763d1a1fe85be04b6448998b88ea18a2 0 1528994341234 5 connected
    e3c3977d52165dcb589f59d16a713888abdc0c56 10.24.33.147:7003@17003 master - 0 1528994340000 3 connected 10923-16383
    0c16f9babaa8def8b721bcd82e43d9258f3f68d9 10.24.33.147:7006@17006 slave c7693a9c9e1189196481f65153ed41ca9a6fe449 0 1528994339000 6 connected
    6d9cab72763d1a1fe85be04b6448998b88ea18a2 10.24.33.147:7001@17001 myself,master - 0 1528994338000 1 connected 0-5460
    089b61bc451920a7a0cba996a4a430b8a8805c03 10.24.33.147:7004@17004 slave e3c3977d52165dcb589f59d16a713888abdc0c56 0 1528994339221 4 connected
    c7693a9c9e1189196481f65153ed41ca9a6fe449 10.24.33.147:7002@17002 master - 0 1528994340228 2 connected 5461-10922
    10.24.33.147:7001> set a 100
    -> Redirected to slot [15495] located at 10.24.33.147:7003
    OK
    10.24.33.147:7003> get a
    "100"
唐胡璐 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
分享创造价值,您的支持将鼓励我继续前行!