redis 负载均衡 集群配置
redis 官网 http://redis.io/
中文网站 http://redis.cn/
谷歌代码的redis项目 https://code.google.com/p/redis/
http://www.oschina.net/p/redis/
在oschina.net的介绍:
Redis 是一个高性能的key-value数据库。 redis的出现,很大程度补偿了memcached这类keyvalue存储的不足,在部 分场合可以对关系数据库起到很好的补充作用。它提供了Python,Ruby,Erlang,PHP客户端,使用很方便。 性能测试结果: SET操作每秒钟 次,GET操作每秒钟 次,服务器配置如下: Linux 2.6, Xeon X3320 .5Ghz. stackoverflow 网站使用 Redis 做为缓存服务器。
我要做的事情就是,在多台linux服务器中,部署redis,由于redis的集群功能没有完全编写好,见:http://redis.io/topics/cluster-spec
目前 redis的最新版本是:redis-2.6.13.tar.gz
但是redis可以做一个主从复制的设置,见redis.conf的 复制【REPLICATION】 部分注释和设置
################################# REPLICATION #################################
# Master-Slave replication. Use slaveof to make a Redis instance a copy of
# another Redis server. Note that the configuration is local to the 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.
#
# slaveof <masterip> <masterport> # If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the slave to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the slave request.
#
# masterauth <master-password> # When a slave loses its 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 date 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
# You can configure a slave instance to accept writes or not. Writing against
# a slave instance may be useful to store some ephemeral data (because data
# written on a slave will be easily deleted after resync with the master) but
# may also cause problems if clients are writing to it because of a
# misconfiguration.
#
# Since Redis 2.6 by default slaves are read-only.
#
# Note: read only slaves are not designed to be exposed to untrusted clients
# on the internet. It's just a protection layer against misuse of the instance.
# Still a read only slave exports by default all the administrative commands
# such as CONFIG, DEBUG, and so forth. To a limited extend you can improve
# security of read only slaves using 'rename-command' to shadow all the
# administrative / dangerous commands.
slave-read-only 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 # Disable TCP_NODELAY on the slave socket after SYNC?
#
# If you select "yes" Redis will use a smaller number of TCP packets and
# less bandwidth to send data to slaves. But this can add a delay for
# the data to appear on the slave side, up to 40 milliseconds with
# Linux kernels using a default configuration.
#
# If you select "no" the delay for data to appear on the slave side will
# be reduced but more bandwidth will be used for replication.
#
# By default we optimize for low latency, but in very high traffic conditions
# or when the master and slaves are many hops away, turning this to "yes" may
# be a good idea.
repl-disable-tcp-nodelay no # The slave priority is an integer number published by Redis in the INFO output.
# It is used by Redis Sentinel in order to select a slave to promote into a
# master if the master is no longer working correctly.
# A slave with a low priority number is considered better for promotion, so
# for instance if there are three slaves with priority 10, 100, 25 Sentinel will
# pick the one wtih priority 10, that is the lowest.
#
# However a special priority of 0 marks the slave as not able to perform the
# role of master, so a slave with priority of 0 will never be selected by
# Redis Sentinel for promotion.
#
# By default the priority is 100.
slave-priority
看了上面的默认配置,其实也很容易理解,修改下就可以配置主从复制了,有 slaveof <masterip> <masterport> ,就成了 从服务器 ,没有就是 主服务器。
还有 主服务器 安全的设置
################################## SECURITY ###################################
# Require clients to issue AUTH <PASSWORD> before processing any other
# commands. This might be useful in environments in which you do not trust
# others with access to the host running redis-server.
#
# This should stay commented out for backward compatibility and because most
# people do not need auth (e.g. they run their own servers).
#
# 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.
#
# requirepass foobared
# Command renaming.
#
# It is possible to change the name of dangerous commands in a shared
# environment. For instance the CONFIG command may be renamed into something
# hard to guess so that it will still be available for internal-use tools
# but not available for general clients.
#
# Example:
#
# rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52
#
# It is also possible to completely kill a command by renaming it into
# an empty string:
#
# rename-command CONFIG ""
#
# Please note that changing the name of commands that are logged into the
# AOF file or transmitted to slaves may cause problems.
对了,redis的编译安装非常简单,下载redis-2.6.13.tar.gz后,
tar xvf redis-2.6.13.tar.gz
cd redis-2.6.13
make && make install
将会把redis-server、redis-cli、redis-benchmark、redis-check-aof、redis-check-dump 五个文件复制到 /usr/local/bin/下
[root@localhost ~]# ll /usr/local/bin
总用量
-rwxr-xr-x. root root 6月 : redis-benchmark
-rwxr-xr-x. root root 6月 : redis-check-aof
-rwxr-xr-x. root root 6月 : redis-check-dump
-rwxr-xr-x. root root 6月 : redis-cli
-rwxr-xr-x. root root 6月 : redis-server
[root@localhost ~]#
然后将源码中的 redis.conf 复制到 /etc/redis.conf
再制作一个 init.d 的启动脚本:
#!/usr/bin/env bash
#
# redis start up the redis server daemon
#
# chkconfig: 345 99 99
# description: redis service in /etc/init.d/redis \
# chkconfig --add redis or chkconfig --list redis \
# service redis start or service redis stop
# processname: redis-server
# config: /etc/redis.conf PATH=/usr/local/bin:/sbin:/usr/bin:/bin REDISPORT=
EXEC=/usr/local/bin/redis-server
REDIS_CLI=/usr/local/bin/redis-cli PIDFILE=/var/run/redis.pid
CONF="/etc/redis.conf"
#make sure some dir exist
if [ ! -d /var/lib/redis ] ;then
mkdir -p /var/lib/redis
mkdir -p /var/log/redis
fi case "$1" in
status)
ps -A|grep redis
;;
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
if [ "$?"="0" ]
then
echo "Redis is running..."
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$REDIS_CLI -p $REDISPORT SHUTDOWN
while [ -x ${PIDFILE} ]
do
echo "Waiting for Redis to shutdown ..."
sleep
done
echo "Redis stopped"
fi
;;
restart|force-reload)
${} stop
${} start
;;
*)
echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&
exit
esac
将上面内容复制到 /etc/init.d/redis
chkconfig --add redis
chkconfig --list redis
service redis start
开启了服务,对了忘记了 /etc/redis.conf里面可以把 daemonize no 修改为
daemonize yes
就可以默认在后台执行redis-server了。
这就是主服务器,从服务器,配置一样,只不过 修改/etc/redis.conf 中
slaveof <masterip>
然后开启从服务器的redis服务。
测试
#主服务器
redis-cli -p set hello world #从服务器 redis-cli -p get hello
"world" #主服务器
redis-cli -p set hello world2 #从服务器 redis-cli -p get hello
"world2" redis-cli -p set hello world
(error) READONLY You can't write against a read only slave. #成功 配置主从redis 服务器。好简单啊。
由于配置中有一条 从服务器 是只读的,所以从服务器 没法设置数据,只可以读取数据。
redis 负载均衡 集群配置的更多相关文章
- Apache+tomcat+mod_jk+centos6.2负载均衡集群配置--转载
转载地址:http://blog.163.com/chenhui_java/blog/static/17267249420128101191860/ 注: 由于长期受转载毒害,所以本人日志均是原创:其 ...
- 1.Apache+Tomcat负载均衡+集群配置
1.本文Apache+Tomcat集群配置 基于最新的Apache和Tomcat,具体是2011年4月20日最新的Tomcat和Apache集群和负载均衡配置. 准备环境 Apache Apache是 ...
- LVS+Keepalived+Nginx+Tomcat高可用负载均衡集群配置(DR模式,一个VIP,多个端口)
一.概述 LVS作用:实现负载均衡 Keepalived作用:监控集群系统中各个服务节点的状态,HA cluster. 配置LVS有两种方式: 1. 通过ipvsadm命令行方式配置 2. 通过Red ...
- haproxy + keepalived + mycat 高可用与负载均衡集群配置 centos7
架构如上,但是其实keepalived.haproxy.Mycat都可以多台(比如keepalived.haproxy.Mycat各3台,3台keepalived抢占vip,然后抢到vip的hapro ...
- Linux平台上搭建apache+tomcat负载均衡集群
传统的Java Web项目是通过tomcat来运行和发布的.但在实际的企业应用环境中,采用单一的tomcat来维持项目的运行是不现实的.tomcat 处理能力低,效率低,承受并发小(1000左右).当 ...
- nginx集群:nginx配置负载均衡集群(nginx1.18.0)
一,nginx的负载均衡集群的特点: 1,nginx集群和lvs的不同? lvs集群:工作在第4层(传输层) nginx集群:工作在第7层(应用层) lvs集群:性能更强 nginx集群:功能更强:可 ...
- windows配置nginx实现负载均衡集群
windows配置nginx实现负载均衡集群2014-08-20 09:44:40 来源:www.abcde.cn 评论:0 点击:617 网上大部分关于nginx负载均衡集群的教程都是lin ...
- LB负载均衡集群及NAT模式配置
一.LB(load balance)负载均衡集群 负载均衡集群常用的有: 1.软件实现的 nginx(工作在OSI第七层应用层) lvs+keepalived(工作在OSI第四层传输层) 2.硬件实现 ...
- windows配置nginx实现负载均衡集群 -请求分流
windows配置nginx实现负载均衡集群 一.windows上安装nginx 1.下载nginx的windows版本http://nginx.org/en/download.html 2.把压缩文 ...
随机推荐
- 如何使用DSP的cache(转)
C6747在执行一块算法的执行时间在114ms左右,需求要20ms以下.6000属于分层存储器体系架构,内部RAM跟CPU不同频运行,只有cache使能才跟CPU同频.可能是cache没打开.下面转载 ...
- 洛谷P3398 仓鼠找sugar [LCA]
题目传送门 仓鼠找sugar 题目描述 小仓鼠的和他的基(mei)友(zi)sugar住在地下洞穴中,每个节点的编号为1~n.地下洞穴是一个树形结构.这一天小仓鼠打算从从他的卧室(a)到餐厅(b),而 ...
- 跟厂长学PHP7内核(六):变量之zval
记得网上流传甚广的段子"PHP是世界上最好的语言",暂且不去讨论是否言过其实,但至少PHP确实有独特优势的,比如它的弱类型,即只需要$符号即可声明变量,使得PHP入手门槛极低,成为 ...
- jenkins自动构建部署
环境 centos7 tomcat8.5.37 maven3.3.9 jdk8 git1.8.3.1 安装jdk,tomcat,maven,git(环境变量,配置文件什么的自行百度) ...
- python基础-UDP、进程、进程池、paramike模块
1 基于UDP套接字1.1 介绍 udp是无连接的,是数据报协议,先启动哪端都不会报错 udp服务端 import socket sk = socket() #创建一个服务器的套接字 sk.bind( ...
- Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip dfs 按位考虑
E. Mahmoud and a xor trip 题目连接: http://codeforces.com/contest/766/problem/E Description Mahmoud and ...
- JAVA GC 图解
http://www.cnblogs.com/hnrainll/archive/2013/11/06/3410042.html http://www.blogjava.net/ldwblog/arch ...
- java入门学习(十四)运算语句for
循环可用来重复执行一条语句或者含有多条语句的语句块.在大多数程序中都会需要重复执行一块语句. for 循环的基本语法是: for (表达式1:表达式2:表达式3) { 若干语句 } for语句由关键字 ...
- WinForm通用自动更新器AutoUpdater项目实战
一.项目背景介绍 最近单位开发一个项目,其中需要用到自动升级功能.因为自动升级是一个比较常用的功能,可能会在很多程序中用到,于是,我就想写一个自动升级的组件,在应用程序中,只需要引用这个自动升级组件, ...
- Mustache.js语法
看了Mustache的github,学学此中的语法,做个笔记 1.简单的变量调换:{{name}} 1 var data = { "name": "Willy" ...