[关闭]
@cdmonkey 2016-06-23T08:44:57.000000Z 字数 5925 阅读 1289

Redis(5)Codis

Redis


codis-logo.png-23.3kB

https://github.com/CodisLabs/codis
http://www.tuicool.com/articles/m6FR7zR
http://my.oschina.net/vampire/blog/639329
http://blog.csdn.net/dc_726/article/details/47052607

原理讲解请参考笔记内容。

Install Zookeeper

zk1.png-10.2kB

首先需要在同一台主机上模拟有三个节点的“Zookeeper”集群:

  1. # Install JDK:
  2. # Install Zookeeper:
  3. [root@Node-A5 ~]# cd /opt/
  4. [root@Node-A5 opt]# mkdir zk1 zk2 zk3
  5. [root@Node-A5 opt]# echo "1" > zk1/myid
  6. [root@Node-A5 opt]# echo "2" > zk2/myid
  7. [root@Node-A5 opt]# echo "3" > zk3/myid
  8. #
  9. [root@Node-A5 tools]# tar zxvf zookeeper-3.4.8.tar.gz
  10. [root@Node-A5 tools]# mv zookeeper-3.4.8 /usr/local/zookeeper
  11. [root@Node-A5 tools]# cp /usr/local/zookeeper/conf/zoo_sample.cfg /opt/zoo.cfg
  12. [root@Node-A5 tools]# vim /opt/zoo.cfg
  13. dataDir=/tmp/zookeeper --> dataDir=/opt/zk1 # 每个节点的数据存储路径要对应自己的目录。
  14. clientPort=2181 # 因为是在同一台主机上进行模拟,所以每个节点进程对应的客户端的端口号也要不同。
  15. # 下面三个就是集群内的节点(每个节点都有唯一的ID,前一个端口是集群内的节点交换信息使用的端口,后一个端口用于重新选举)。
  16. # 下面三行在三个节点内的配置文件中是相同的。
  17. server.1=172.16.1.25:5881:6881
  18. server.2=172.16.1.25:5882:6882
  19. server.3=172.16.1.25:5883:6883
  20. -----------------
  21. [root@Node-A5 tools]# cd /opt/
  22. [root@Node-A5 opt]# cp zoo.cfg zk1/zk1.cfg
  23. [root@Node-A5 opt]# cp zoo.cfg zk2/zk2.cfg
  24. [root@Node-A5 opt]# cp zoo.cfg zk3/zk3.cfg
  25. # 针对不同的目录修改配置文件内的个性化信息,主要是客户端的端口号和数据存放目录(略)。
  26. # Startup Zookeeper:
  27. [root@Node-A5 opt]# /usr/local/zookeeper/bin/zkServer.sh start /opt/zk1/zk1.cfg
  28. [root@Node-A5 opt]# /usr/local/zookeeper/bin/zkServer.sh start /opt/zk2/zk2.cfg
  29. [root@Node-A5 opt]# /usr/local/zookeeper/bin/zkServer.sh start /opt/zk3/zk3.cfg
  30. # Check status:
  31. [root@Node-A5 opt]# /usr/local/zookeeper/bin/zkServer.sh status /opt/zk1/zk1.cfg
  32. ZooKeeper JMX enabled by default
  33. Using config: /opt/zk1/zk1.cfg
  34. Mode: follower
  35. [root@Node-A5 opt]# /usr/local/zookeeper/bin/zkServer.sh status /opt/zk2/zk2.cfg
  36. ZooKeeper JMX enabled by default
  37. Using config: /opt/zk2/zk2.cfg
  38. Mode: leader # 此处显示的节点状态为领导者,而其它节点的状态为跟随者。
  39. [root@Node-A5 opt]# /usr/local/zookeeper/bin/zkServer.sh status /opt/zk3/zk3.cfg
  40. ZooKeeper JMX enabled by default
  41. Using config: /opt/zk3/zk3.cfg
  42. Mode: follower

Install Go tools

  1. [root@Node-A5 tools]# tar zxvf go1.6.linux-amd64.tar.gz -C /opt/
  2. [root@Node-A5 tools]# vim ~/.bash_profile
  3. export GO15VENDOREXPERIMENT=0
  4. export GOROOT=/usr/local/go
  5. export GOPATH=/opt/gopath
  6. export PATH=$GOROOT/bin:$GOPATH/bin:$PATH
  7. --------------
  8. [root@Node-A5 tools]# . ~/.bash_profile
  9. [root@Node-A5 ~]# go version
  10. go version go1.6 linux/amd64
  11. # Install godep:
  12. [root@Node-A5 tools]# go get github.com/tools/godep

该路径是本机所有“Go”项目(包括项目所依赖的第三方库)的目录,而并非单纯codis的所在目录。

Install Codis

  1. # Install Codis:
  2. [root@Node-A5 ~]# go get -u -d github.com/CodisLabs/codis
  3. package github.com/CodisLabs/codis: no buildable Go source files in /opt/gopath/src/github.com/CodisLabs/codis
  4. # 以上的输出内容可予以忽略。
  5. [root@Node-A5 ~]# cd $GOPATH/src/github.com/CodisLabs/codis
  6. [root@Node-A5 codis]# make
  7. [root@Node-A5 codis]# make gotest

Deployment

  1. # Execute the instructions above all, generates three files in bin directory:
  2. # codis-config、codis-proxy、codis-server
  3. [root@Node-A5 codis]# ll bin/
  4. total 34956
  5. drwxr-xr-x 4 root root 4096 Oct 8 16:10 assets # Dashboard fils
  6. -rwxr-xr-x 1 root root 14812072 Oct 8 16:10 codis-config # Codis配置管理组件
  7. -rwxr-xr-x 1 root root 14660784 Oct 8 16:10 codis-proxy # Codis代理也就是核心组件
  8. -rwxr-xr-x 1 root root 6311107 Oct 8 16:11 codis-server # Codis使用的定制版本的Redis Server

两个可执行文件codis-configcodis-proxy不使用“-c”参数的时候,会默认读取当前目录下的config.ini文件。

1. Codis Server

  1. # Create three Redis nodes at localhost:
  2. [root@Node-A5 ~]# mkdir -p /usr/local/codis/{log,conf}
  3. [root@Node-A5 ~]# mkdir /usr/local/codis/redis_{7881,7882,7883,7884}
  4. ---
  5. [root@Node-A5 ~]# cd $GOPATH/src/github.com/CodisLabs/codis
  6. [root@Node-A5 codis]# cp -a bin/ /usr/local/codis/
  7. [root@Node-A5 codis]# cp config.ini /usr/local/codis/conf/
  8. ---
  9. [root@Node-A5 codis]# cp extern/redis-test/conf/6379.conf /usr/local/codis/redis_7881/7881.conf
  10. [root@Node-A5 codis]# vim /usr/local/codis/redis_7881/7881.conf
  11. #
  12. daemonize no --> daemonize yes
  13. pidfile /var/run/redis.pid --> pidfile /var/run/redis_7881.pid
  14. port 6379 --> port 7881
  15. logfile "" --> logfile "/usr/local/codis/log/redis_7881.log"
  16. save 900 1
  17. save 300 10
  18. save 60 10000
  19. dir ./ --> dir /usr/local/codis/redis_7881
  20. appendonly no --> appendonly yes
  21. -------------
  22. [root@Node-A5 codis]# cd /usr/local/codis/
  23. [root@Node-A5 codis]# sed 's/7881/7882/g' redis_7881/7881.conf >> redis_7882/7882.conf
  24. [root@Node-A5 codis]# sed 's/7881/7883/g' redis_7881/7881.conf >> redis_7883/7883.conf

2. Codis Proxy

  1. [root@Node-A5 ~]# cd $GOPATH/src/github.com/CodisLabs/codis
  2. [root@Node-A5 codis]# cp -a bin/ /usr/local/codis/
  3. [root@Node-A5 codis]# cp config.ini /usr/local/codis/conf/config_p1.ini
  4. [root@Node-A5 codis]# cp config.ini /usr/local/codis/conf/config_p2.ini

说明:每个“Codis Proxy”都有自己的config.ini文件,由于是于单台主机上进行的部署,因而特别说明。

Configuration file

  1. [root@Node-A5 codis]# vim /usr/local/codis/conf/config_p1.ini
  2. ##### Properties below are for dashboard and proxies
  3. # zookeeper or etcd
  4. coordinator=zookeeper
  5. # The address of zookeeper (Use comma "," for multiple instances):
  6. # 如果是集群场景,则需要写成下面的形式(每个代理该部分内容要相同):
  7. zk=172.16.1.25:2181,172.16.1.25:2182,172.16.1.25:2183
  8. product=test #产品名称,该Codis集群的名字,可以认为是命名空间,不同命名空间的Codis没有交集。
  9. dashboard_addr=172.16.1.25:18087
  10. password=123456
  11. ##### Properties below are only for proxies
  12. # Proxy will ping-pong backend redis periodly to keep-alive
  13. backend_ping_period=5
  14. # If there is no request from client for a long time, the connection will be droped. Set 0 to disable.
  15. session_max_timeout=1800
  16. # Buffer size for each client connection.
  17. session_max_bufsize=131072
  18. # Number of buffered requests for each client connection.
  19. # Make sure this is higher than the max number of requests for each pipeline request, or your client may be blocked.
  20. session_max_pipeline=1024
  21. # If proxy don't send a heartbeat in timeout seconds which is usually because proxy has high load or even no response, zk will mark this proxy offline.
  22. # A higher timeout will recude the possibility of "session expired" but clients will not know the proxy has no response in time if the proxy is down indeed.
  23. # So we highly recommend you not to change this default timeout and use Jodis(https://github.com/wandoulabs/codis/tree/master/extern/jodis)
  24. # which watches the available proxies and will skip the offline proxy or add new online proxy automatically.
  25. # If you are not using Java in client, you can DIY a zk watcher accourding to Jodis source code.
  26. zk_session_timeout=30
  27. ##### must be different for each proxy
  28. proxy_id=proxy_1

Start process

  1. # Startup Dashboard:
  2. [root@Node-A3 codis]# bin/codis-config dashboard &
  3. # Initialize slots:
  4. [root@Node-A3 codis]# bin/codis-config slot init
  5. # Startup the Codis Redis:
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注