概述
接上一篇博文(https://my.oschina.net/u/2342969/blog/995598)的架构图,将分以下三步完成架构优化工作
- redis主从集群搭建
- nginx主备高可用环境搭建
- servicemix集群搭建
本文先写第一步 redis主从集群搭建,如有错误或者哪里不清楚的,欢迎大家批评指正。
下一篇将写redis哨兵在应用中的使用
环境准备
- centOs:6.5
-
redis:2.8.10
背景
公司现redis有两个应用场景:
- 业务缓存
- session共享
这两个场景均是单点架构,有一台redis实例挂掉,就会影响到应用正常使用,so 出现了redis主从集群搭建工作,既然有了redis集群环境,那应用就要支持集群,主节点挂掉,应用就需要使用最新的主节点继续工作。
redis主从集群搭建
本文使用一台虚拟机通过修改redis端口进行模拟redis的一主两从集群环境
环境介绍
master : 127.0.0.1:6377
slave1 : 127.0.0.1:6378
slave2 : 127.0.0.1:6379
资源准备
下载redis-2.8.10(https://github.com/antirez/redis/releases/tag/2.8.10)
Windows:选择下载zip包
linux:选择下载tar.gz包(本文使用此包)
安装redis
- 将redis-2.8.10.tar.gz上传到服务器(本文上传到了/opt目录下)并解压
cd /opt
tar -xzvf redis-2.8.10.tar.gz
- 进入redis解压文件夹
cd redis-2.8.10
- 在解压文件夹下进行源码安装redis
make
- 如图安装完毕后,可以执行make test进行测试
cd src && make test
执行结果如下图:通过此命令可以知道redis还有那些依赖需要升级等信息
上图不解决不影响使用。
- 测试redis使用
set test llan
get test
执行结果如下图:
到此,redis安装完毕并且可以使用
redis基本使用
主要目录介绍
简单操作redis
- 启动redis
- 进入%redis%/src 目录
- 启动redis
./redis-server ../redis.conf
- 连接客户端
./redis-cli
or
./redis-cli -p 6379
- 关闭redis实例
在客户端模式下执行命令
- 退出客户端
配置redis主从
- 公共配置
- 将daemonize no修改为 daemonize yes ,目的是使redis以守护进程运行
- master配置
- 根据自己redis安装路径执行命令复制一份redis配置文件出来,命令如下: cp /opt/redis-2.8.10/redis.conf /opt/redis-2.8.10/redis6377.conf
- 编辑redis6377.conf文件(命令:vim /opt/redis-2.8.10/redis6377.conf)
- port 6379修改为port 6377
- pidfile /var/run/redis.pid 修改为pidfile /var/run/redis6377.pid(不在同一台机器可以不修改)
- dbfilename dump.rdb修改为dbfilename dump6377.rdb(不在同一台机器可以不修改)
- appendonly no修改为appendonly yes(启用AOF持久化,不开启的话,可以不修改)
最终master配置文件如下:
# Redis configuration file example
# Note on units: when memory size is needed, it is possible to specify
# it in the usual form of 1k 5GB 4M and so forth:
#
# 1k => 1000 bytes
# 1kb => 1024 bytes
# 1m => 1000000 bytes
# 1mb => 1024*1024 bytes
# 1g => 1000000000 bytes
# 1gb => 1024*1024*1024 bytes
#
# units are case insensitive so 1GB 1Gb 1gB are all the same.
################################## INCLUDES ###################################
# Include one or more other config files here. This is useful if you
# have a standard template that goes to all Redis server but also need
# to customize a few per-server settings. Include files can include
# other files, so use this wisely.
#
# Notice option "include" won't be rewritten by command "CONFIG REWRITE"
# from admin or Redis Sentinel. Since Redis always uses the last processed
# line as value of a configuration directive, you'd better put includes
# at the beginning of this file to avoid overwriting config change at runtime.
#
# If instead you are interested in using includes to override configuration
# options, it is better to use include as the last line.
#
# include /path/to/local.conf
# include /path/to/other.conf
################################ GENERAL #####################################
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize yes
# When running daemonized, Redis writes a pid file in /var/run/redis.pid by
# default. You can specify a custom pid file location here.
pidfile /var/run/redis6377.pid
# Accept connections on the specified port, default is 6379.
# If port 0 is specified Redis will not listen on a TCP socket.
port 6377
# TCP listen() backlog.
#
# In high requests-per-second environments you need an high backlog in order
# to avoid slow clients connections issues. Note that the Linux kernel
# will silently truncate it to the value of /proc/sys/net/core/somaxconn so
# make sure to raise both the value of somaxconn and tcp_max_syn_backlog
# in order to get the desired effect.
tcp-backlog 511
# By default Redis listens for connections from all the network interfaces
# available on the server. It is possible to listen to just one or multiple
# interfaces using the "bind" configuration directive, followed by one or
# more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1
# Specify the path for the Unix socket that will be used to listen for
# incoming connections. There is no default, so Redis will not listen
# on a unix socket when not specified.
#
# unixsocket /tmp/redis.sock
# unixsocketperm 755
# Close the connection after a client is idle for N seconds (0 to disable)
timeout 0
# TCP keepalive.
#
# If non-zero, use SO_KEEPALIVE to send TCP ACKs to clients in absence
# of communication. This is useful for two reasons:
#
# 1) Detect dead peers.
# 2) Take the connection alive from the point of view of network
# equipment in the middle.
#
# On Linux, the specified value (in seconds) is the period used to send ACKs.
# Note that to close the connection the double of the time is needed.
# On other kernels the period depends on the kernel configuration.
#
# A reasonable value for this option is 60 seconds.
tcp-keepalive 0
# Specify the server verbosity level.
# This can be one of:
# debug (a lot of information, useful for development/testing)
# verbose (many rarely useful info, but not a mess like the debug level)
# notice (moderately verbose, what you want in production probably)
# warning (only very important / critical messages are logged)
loglevel notice
# Specify the log file name. Also the empty string can be used to force
# Redis to log on the standard output. Note that if you use standard
# output for logging but daemonize, logs will be sent to /dev/null
logfile ""
# To enable logging to the system logger, just set 'syslog-enabled'