codis实现redis分片和在线扩展
Codis是一个开源的分布式redis解决方案,由以下四个组件组成:codis-proxy:后端redis的代理,本事实现了redis协议。codis-config:是codis的管理配置工具,用于..
Codis是一个开源的分布式redis解决方案,由以下四个组件组成:
codis-proxy:后端redis的代理,本事实现了redis协议。
codis-config:是codis的管理配置工具,用于增删redis server、操作数据迁移。
zookeeper:用来存放数据路由表和codis-proxy节点的元信息,codis-config发起的命令都会通过 zookeeper同步到各个存活的codis-proxy。
codis-server:codis项目维护的一个 redis 分支, 基于 2.8.13 开发, 加入了 slot 的支持和原子的数据迁移指令,如果要使用codis的在线扩展迁移功能的话必须使用codis自带的codis-server,codis不支持对原生redis做在线的扩展和数据迁移。
节点规划:
codis-proxy
192.168.0.100:19000
codis-config
192.168.0.100
codis-server1
192.168.0.100:6381
codis-server2192.168.0.100:6382
codis-server3192.168.0.100:6380
codis-server4192.168.0.100:6383
1、安装配置
a、首先安装golang
## 解压下载好的go软件包 $ tar xf/opt/go1.4.2.linux-amd64.tar.gz -C/usr/local/ ## 配置环境变量 在.bash_profile中加入go的相关环境变量,并使新的环境变量生效 PATH=$PATH:$HOME/bin exportGOROOT=/usr/local/go exportGOOS=linux exportGOARCH=amd64 exportGOBIN=$GOROOT/bin exportGOPATH=/opt/mygo PATH=$PATH:$GOBIN export PATH ##编写一个 测试程序验证go是否安装成功 $ cat hello.go package main import"fmt" func main() { fmt.Printf("hello, world\n") } ## 运行测试程序,输出hello,word说明go已经安装成功 $ go run hello.go hello, worldb、安装codis
## 安装工具软件 $ yum install gccmake git -y ## 下载codis,会下载到环境变量GOPATH指定的目录 $ go get github.com/wandoulabs/codis#ssdb-server config packagegithub.com/wandoulabs/codis imports github.com/wandoulabs/codis imports github.com/wandoulabs/codis: nobuildable Go source files in /opt/mygo/src/github.com/wandoulabs/codis ## 下载完成后进入/opt/mygo/src/github.com/wandoulabs/codis $ cd /opt/mygo/src/github.com/wandoulabs/codis ## 执行安装命令 $ ./bootstrap.sh ## 这一步时间稍久,会下载一些文件目录到本地,耐心等待即可c、安装zookeeper
## 解压zookeeper $ tar xfzookeeper-3.4.6.tar.gz -C /opt/ ## 进入zk的安装目录,修改配置文件 $ cd /opt/zookeeper-3.4.6 $ cp conf/zoo_sample.cfgconf/zoo.cfg $ vim conf/zoo.cfg tickTime=2000 initLimit=10 syncLimit=5 dataDir=/data/zookeeper dataLogDir=/opt/zookeeper-3.4.6/logs clientPort=2181 ## 创建所需要的目录 mkdir /data/zookeeper -p mkdir /opt/zookeeper-3.4.6/logs ## 启动zk $ /opt/zookeeper-3.4.6/bin/zkServer.sh start JMX enabled bydefault Using config:/opt/zookeeper-3.4.6/bin/../conf/zoo.cfg Starting zookeeper... STARTED ## 验证是否启动成功,若2181端口被监听,则表示成功 $ netstat-alnut|grep 2181 tcp 0 0 :::2181 :::* LISTENd、配置codis
## codis中配置zk的信息 $ cd /opt/mygo/src/github.com/wandoulabs/codis/sample/ ## 修改config.ini $ vim config.ini zk=localhost:2181 product=codis_proxy proxy_id=proxy_1 net_timeout=5 dashboard_addr=localhost:18087 coordinator=zookeeper2、启动codis
a、
:
b、初始化slots
##进入相关目录 $ cd /opt/mygo/src/github.com/wandoulabs/codis/sample/ ## 执行初始化 $ ../bin/codis-config slot init { "msg": "OK", "ret": 0 }c、启动codis-server
## codis自带有一个启动脚本,会启动两个codis-server,监听端口为6381和6382 [root@mgs02 sample]#./start_redis.sh sleep 3s [6294] 09 Apr 14:28:02.796* Increased maximum number of open files to 10032 (it was originally set to1024). _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 2.8.13 (1e823fd3/1) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in stand alone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6381 | `-._ `._ / _.-' | PID: 6294 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' [6294] 09 Apr14:28:02.814 # Server started, Redis version 2.8.13 [6294] 09 Apr14:28:02.814 # WARNING overcommit_memory is set to 0! Background save may failunder low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to/etc/sysctl.conf and then reboot or run the command 'sysctlvm.overcommit_memory=1' for this to take effect. [6294] 09 Apr14:28:02.814 * The server is now ready to accept connections on port 6381 [6295] 09 Apr14:28:02.795 * Increased maximum number of open files to 10032 (it wasoriginally set to 1024). _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 2.8.13 (1e823fd3/1) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in stand alone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6382 | `-._ `._ / _.-' | PID: 6295 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' [6295] 09 Apr14:28:02.811 # Server started, Redis version 2.8.13 [6295] 09 Apr14:28:02.811 # WARNING overcommit_memory is set to 0! Background save may failunder low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to/etc/sysctl.conf and then reboot or run the command 'sysctlvm.overcommit_memory=1' for this to take effect. [6295] 09 Apr 14:28:02.812 * The server is nowready to accept connections on port 6382
Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

On CentOS systems, you can limit the execution time of Lua scripts by modifying Redis configuration files or using Redis commands to prevent malicious scripts from consuming too much resources. Method 1: Modify the Redis configuration file and locate the Redis configuration file: The Redis configuration file is usually located in /etc/redis/redis.conf. Edit configuration file: Open the configuration file using a text editor (such as vi or nano): sudovi/etc/redis/redis.conf Set the Lua script execution time limit: Add or modify the following lines in the configuration file to set the maximum execution time of the Lua script (unit: milliseconds)

Use the Redis command line tool (redis-cli) to manage and operate Redis through the following steps: Connect to the server, specify the address and port. Send commands to the server using the command name and parameters. Use the HELP command to view help information for a specific command. Use the QUIT command to exit the command line tool.

Redis counter is a mechanism that uses Redis key-value pair storage to implement counting operations, including the following steps: creating counter keys, increasing counts, decreasing counts, resetting counts, and obtaining counts. The advantages of Redis counters include fast speed, high concurrency, durability and simplicity and ease of use. It can be used in scenarios such as user access counting, real-time metric tracking, game scores and rankings, and order processing counting.

There are two types of Redis data expiration strategies: periodic deletion: periodic scan to delete the expired key, which can be set through expired-time-cap-remove-count and expired-time-cap-remove-delay parameters. Lazy Deletion: Check for deletion expired keys only when keys are read or written. They can be set through lazyfree-lazy-eviction, lazyfree-lazy-expire, lazyfree-lazy-user-del parameters.

In Debian systems, readdir system calls are used to read directory contents. If its performance is not good, try the following optimization strategy: Simplify the number of directory files: Split large directories into multiple small directories as much as possible, reducing the number of items processed per readdir call. Enable directory content caching: build a cache mechanism, update the cache regularly or when directory content changes, and reduce frequent calls to readdir. Memory caches (such as Memcached or Redis) or local caches (such as files or databases) can be considered. Adopt efficient data structure: If you implement directory traversal by yourself, select more efficient data structures (such as hash tables instead of linear search) to store and access directory information
