Redis在百度百科里的解释:Redis是一个开源的使用ANSI  C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API,包括C#、Java、PHP等等,甚至连Javascript都做了很好的封装。

可见Redis的数据是持久化的,可以分担一个项目中的部分业务,Redis的数据是存储在服务器内存当中的,这样可以极大的加快访问速度,因为内存的读取速度远远超过磁盘和数据库,这在很大程序的上解决了大并发的困惑;同时Redis和Memcached不同,会按照设定的规律定期将数据同步到磁盘数据库,这样在Redis服务器宕机时也能保证数据的完整性。

大家都知道Redis在全球最大的用户是新浪微博,新浪有最大的Redis集群,新浪最开始使用Memcached与Mysql配合,后来新浪又增加了Redis,而不是弃用Memcached,新浪加入Redis集群的初衷:

1、Memcached存在命中率问题,这样大量请求会发送到Mysql,对于新浪微博这么大的并发无疑是一个灾难,Redis不存在命中率问题

2、Redis相对于Memcached支持更多的数据类型,如string(字符串)、list(链表)、set(集合)、zset(有序集合)和hash(哈希类型)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的

3、Redis主从集群配置技术容易实现,维护成本低,项目复杂度低

4、Redis的持久化存储从某种程度上解决了Cache宕机带来的雪崩现象

当然,新浪并没有弃用Memcached+Mysql,用户的全量信息是在此结构中的,使用Redis带来的问题是,数据是持久化的,这是相当占用内存的,成本不可小觑。

Redis(2.8.19目前最新稳定版本)的安装:

1.下载:

http://pan.baidu.com/s/1jGvJxBo

2.安装

解压下载文件:

[root@jhq0229 src]# tar xzvf redis-stable.tar.gz

编译安装:

[root@jhq0229 src]# cd redis-stable

[root@jhq0229 redis-stable]# make

[root@jhq0229 redis-stable]# make  PREFIX=/usr/local install

配置:

[root@jhq0229 redis-stable]# mkdir /etc/redis

[root@jhq0229 redis-stable]# cp redis.conf /etc/redis/

创建Redis数据及日志存放目录:

[root@jhq0229 redis-stable]# mkdir /data/redis

Redis配置文件详解:

daemonize:如果需要在后台运行,把该项心为yes
pidfile:配置多个pid的地址,默认在/var/run/redis.pid
bind:绑定ip,设置后只接受来自该ip的请求
port:监听端口,默认为6379
timeout:设置客户端连接时的超时时间,单位为秒
loglevel:分为4级,debug、verbose、notice、warning
logfile:配置log文件地址
databases:设置数据库的个数,默认使用的数据库为0
save:设置redis进行数据库镜像的频率
rdbcompression:在进行镜像备份时,是否进行压缩
Dbfilename:镜像备份文件的文件名
Dir:数据库镜像备份的文件放置路径
Slaveof:设置数据库为其他数据库的从数据库
Masterauth:主数据库连接需要的密码验证
Requirepass:设置登录登录时需要使用的密码
Maxclients:限制同时连接的客户数量
Maxmemory:设置redis能够使用的最大内存
Appendonly:开启append only模式
Appendfsync:设置对appendonly.aof文件同步的频率
vm-enabled:是否开启虚拟内存支持
vm-swap-file:设置虚拟内存的交换文件路径
vm-max-memory:设置redis使用的最大物理内存大小
vm-page-size:设置虚拟内存的页大小
vm-pages:设置交换文件的总的page数量
vm-max-threads:设置VMIO同时使用的线程数量
Glueoutputbuf:把小的输出缓存存放在一起
hash-max-zipmap-entries:设置hash的临界值
Activerehashing:重新hash

配置自己的Redis集群(若不配置集群可以只配置Master):

[root@jhq0229 redis-stable]# vim /etc/redis/redis.conf

我的Redis主从配置:

主:192.168.1.18              认证密码:01130229

从:192.168.1.16

主Redis(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 servers 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.
#以后台进行运行,需指定pid
pidfile /var/run/redis.pid# Accept connections on the specified port, default is 6379.
# If port 0 is specified Redis will not listen on a TCP socket.
port 6379# 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 192.168.1.18# Close the connection after a client is idle for N seconds (0 to disable)
#客户端闲置N秒后关闭连接
timeout 30tcp-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#记录日志的文件名称
logfile "redlog"# To enable logging to the system logger, just set 'syslog-enabled' to yes,
# and optionally update the other syslog parameters to suit your needs.
# syslog-enabled no# Specify the syslog identity.
# syslog-ident redis# Specify the syslog facility. Must be USER or between LOCAL0-LOCAL7.
# syslog-facility local0# Set the number of databases. The default database is DB 0, you can select
# a different one on a per-connection basis using SELECT <dbid> where
# dbid is a number between 0 and 'databases'-1
#可用数据库数量
databases 16################################ SNAPSHOTTING  ################################
#
# Save the DB on disk:
#
#   save <seconds> <changes>
#
#   Will save the DB if both the given number of seconds and the given
#   number of write operations against the DB occurred.
#
#   In the example below the behaviour will be to save:
#   after 900 sec (15 min) if at least 1 key changed
#   after 300 sec (5 min) if at least 10 keys changed
#   after 60 sec if at least 10000 keys changed
#
#   Note: you can disable saving completely by commenting out all "save" lines.
#
#   It is also possible to remove all the previously configured save
#   points by adding a save directive with a single empty string argument
#   like in the following example:
#
#   save ""
#当有一条数据更新,900秒后同步数据到磁盘数据库
save 900 1
#当有10条数据更新,300秒后同步数据到磁盘数据库
save 300 10
save 60 10000stop-writes-on-bgsave-error yes# Compress string objects using LZF when dump .rdb databases?
# For default that's set to 'yes' as it's almost always a win.
# If you want to save some CPU in the saving child set it to 'no' but
# the dataset will likely be bigger if you have compressible values or keys.
#当dump .rdb的时候是否压缩数据对象,默认值为yes
rdbcompression yesrdbchecksum yes# The filename where to dump the DB
# 磁盘数据库文件名称
dbfilename myredis.rdb#本地数据库存放路径
dir /data/redis/################################# REPLICATION #################################
#Redis主从复制配置#若当前服务为slave,在此处设置主服务的ip及端口
# slaveof <masterip> <masterport>#若当前服务为slave,设置主服务的认证密码
# masterauth <master-password>slave-serve-stale-data yes#若当前为slave服务,设置slave是否为只读服务
#slave-read-only yes# Replication SYNC strategy: disk or socket.
#
# -------------------------------------------------------
# WARNING: DISKLESS REPLICATION IS EXPERIMENTAL CURRENTLY
# -------------------------------------------------------
#
# New slaves and reconnecting slaves that are not able to continue the replication
# process just receiving differences, need to do what is called a "full
# synchronization". An RDB file is transmitted from the master to the slaves.
# The transmission can happen in two different ways:
#
# 1) Disk-backed: The Redis master creates a new process that writes the RDB
#                 file on disk. Later the file is transferred by the parent
#                 process to the slaves incrementally.
# 2) Diskless: The Redis master creates a new process that directly writes the
#              RDB file to slave sockets, without touching the disk at all.
#
# With disk-backed replication, while the RDB file is generated, more slaves
# can be queued and served with the RDB file as soon as the current child producing
# the RDB file finishes its work. With diskless replication instead once
# the transfer starts, new slaves arriving will be queued and a new transfer
# will start when the current one terminates.
#
# When diskless replication is used, the master waits a configurable amount of
# time (in seconds) before starting the transfer in the hope that multiple slaves
# will arrive and the transfer can be parallelized.
#
# With slow disks and fast (large bandwidth) networks, diskless replication
# works better.
repl-diskless-sync norepl-diskless-sync-delay 5# 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# repl-timeout 60repl-disable-tcp-nodelay noslave-priority 100################################## 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 01130229################################### LIMITS ##################################### Set the max number of connected clients at the same time. By default
# this limit is set to 10000 clients, however if the Redis server is not
# able to configure the process file limit to allow for the specified limit
# the max number of allowed clients is set to the current file limit
# minus 32 (as Redis reserves a few file descriptors for internal uses).
#
# Once the limit is reached Redis will close all the new connections sending
# an error 'max number of clients reached'.# Don't use more memory than the specified amount of bytes.
# When the memory limit is reached Redis will try to remove keys
# according to the eviction policy selected (see maxmemory-policy).
#
# If Redis can't remove keys according to the policy, or if the policy is
# set to 'noeviction', Redis will start to reply with errors to commands
# that would use more memory, like SET, LPUSH, and so on, and will continue
# to reply to read-only commands like GET.
#
# This option is usually useful when using Redis as an LRU cache, or to set
# a hard memory limit for an instance (using the 'noeviction' policy).
#
# WARNING: If you have slaves attached to an instance with maxmemory on,
# the size of the output buffers needed to feed the slaves are subtracted
# from the used memory count, so that network problems / resyncs will
# not trigger a loop where keys are evicted, and in turn the output
# buffer of slaves is full with DELs of keys evicted triggering the deletion
# of more keys, and so forth until the database is completely emptied.
#
# In short... if you have slaves attached it is suggested that you set a lower
# limit for maxmemory so that there is some free RAM on the system for slave
# output buffers (but this is not needed if the policy is 'noeviction').
# maxmemory <bytes># MAXMEMORY POLICY: how Redis will select what to remove when maxmemory
# is reached. You can select among five behaviors:
#
# volatile-lru -> remove the key with an expire set using an LRU algorithm
# allkeys-lru -> remove any key according to the LRU algorithm
# volatile-random -> remove a random key with an expire set
# allkeys-random -> remove a random key, any key
# volatile-ttl -> remove the key with the nearest expire time (minor TTL)
# noeviction -> don't expire at all, just return an error on write operations
#
# Note: with any of the above policies, Redis will return an error on write
#       operations, when there are no suitable keys for eviction.
#
#       At the date of writing these commands are: set setnx setex append
#       incr decr rpush lpush rpushx lpushx linsert lset rpoplpush sadd
#       sinter sinterstore sunion sunionstore sdiff sdiffstore zadd zincrby
#       zunionstore zinterstore hset hsetnx hmset hincrby incrby decrby
#       getset mset msetnx exec sort
#
# The default is:
#
# maxmemory-policy volatile-lru# LRU and minimal TTL algorithms are not precise algorithms but approximated
# algorithms (in order to save memory), so you can select as well the sample
#
# maxmemory-samples 3############################## APPEND ONLY MODE ################################ By default Redis asynchronously dumps the dataset on disk. This mode is
# good enough in many applications, but an issue with the Redis process or
# a power outage may result into a few minutes of writes lost (depending on
# the configured save points).
#
appendonly no# The name of the append only file (default: "appendonly.aof")#更新日志文件名称#
#
# If unsure, use "everysec".
# appendfsync always
appendfsync everysec
# appendfsync nono-appendfsync-on-rewrite noauto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mbaof-load-truncated yes################################ LUA SCRIPTING  ################################ Max execution time of a Lua script in milliseconds.
#
# If the maximum execution time is reached Redis will log that a script is
# still in execution after the maximum allowed time and will start to
# reply to queries with an error.
#
lua-time-limit 5000################################## SLOW LOG #################################### The Redis Slow Log is a system to log queries that exceeded a specified
# execution time. The execution time does not include the I/O operations
# like talking with the client, sending the reply and so forth,
# but just the time needed to actually execute the command (this is the only
# stage of command execution where the thread is blocked and can not serve
# other requests in the meantime).
slowlog-log-slower-than 10000slowlog-max-len 128################################ LATENCY MONITOR ############################### The Redis latency monitoring subsystem samples different operations
# at runtime in order to collect data related to possible sources of
# latency of a Redis instance.
#latency-monitor-threshold 0############################# Event notification ##############################notify-keyspace-events ""############################### ADVANCED CONFIG ################################ Hashes are encoded using a memory efficient data structure when they have a
# small number of entries, and the biggest entry does not exceed a given
# threshold. These thresholds can be configured using the following directives.
hash-max-ziplist-entries 512
hash-max-ziplist-value 64list-max-ziplist-entries 512
list-max-ziplist-value 64set-max-intset-entries 512zset-max-ziplist-entries 128
zset-max-ziplist-value 64hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10aof-rewrite-incremental-fsync yes

从Redis(Slave)配置,配置与Master类似,不一致内容如下:

#绑定IP地址
bind 192.168.1.16#若当前服务为slave,在此处设置主服务的ip及端口
slaveof 192.168.1.18 6379#若当前服务为slave,设置主服务的认证密码
masterauth 01130229

配置Redis开机启动:

[root@jhq0229 redis-stable]# vim /etc/init.d/redis

内容如下:

#!/bin/bash
#
# redis - this script starts and stops the redis-server daemon
#
# chkconfig:   - 80 12
# description:  Redis is a persistent key-value database
# processname: redis-server
# config:      /etc/redis/redis.conf
# pidfile:     /var/run/redis.pidsource /etc/init.d/functionsBIN="/usr/local/bin"
CONFIG="/etc/redis/redis.conf"
PIDFILE="/var/run/redis.pid"### Read configuration
[ -r "$SYSCONFIG" ] && source "$SYSCONFIG"RETVAL=0
prog="redis-server"
desc="Redis Server"start() {if [ -e $PIDFILE ];thenecho "$desc already running...."exit 1fiecho -n $"Starting $desc: "daemon $BIN/$prog $CONFIGRETVAL=$?echo[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$progreturn $RETVAL
}stop() {echo -n $"Stop $desc: "killproc $progRETVAL=$?echo[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog $PIDFILEreturn $RETVAL
}restart() {stopstart
}case "$1" instart)start;;stop)stop;;restart)restart;;condrestart)[ -e /var/lock/subsys/$prog ] && restartRETVAL=$?;;status)status $progRETVAL=$?;;*)echo $"Usage: $0 {start|stop|restart|condrestart|status}"RETVAL=1
esacexit $RETVAL

[root@jhq0229 redis-stable]# chmod +x /etc/init.d/redis

[root@jhq0229 redis-stable]# chkconfig redis on

补充配置,配置停止服务前同步数据到磁盘:

[root@jhq0229 redis-stable]# vim /etc/sysctl.conf

进行如下修改:

vm.overcommit_memory=1

应用修改:

sysctl -p

启动Redis:

[root@jhq0229 redis-stable]# service redis start

主从Redis的安装和配置只是配置文件不同,其他的均一样,一般主从配好就可以测试主从操作了,主server上set,从server马上就可以get到了。

最后,奉上公司同事辛勤总结的Redis学习资料。希望可以帮到你。

下载地址:    http://pan.baidu.com/s/1qWLV87a

PPT内容版权归公司所有。

分布式业务Redis安装与集群配置相关推荐

  1. ubuntu18.04下hadoop安装与集群配置

    ubuntu18.04下hadoop安装与集群配置 hadoop安装_单机/伪分布式配置 环境 创建hadoop用户 更新apt 安装SSH.配置SSH无密码登陆 安装Java环境 安装 Hadoop ...

  2. zoomkeeper java使用_Zoonkeeper安装、集群配置 [Mac]

    Zoonkeeper安装.集群配置 [Mac] 1. 下载Zoonkeeper 进去后选择你需要的版本,然后下载对应的压缩文件到本地,比如我选择的是当前最新的版本 zookeeper-3.5.4-be ...

  3. Redis主从复制和集群配置系列之四

    非常感谢  http://blog.csdn.net/dc_726/article/details/48552531 Redis技术学习 https://www.itkc8.com 全面剖析Redis ...

  4. 基于redis的cas集群配置(转)

    1.cas ticket统一存储 做cas集群首先需要将ticket拿出来,做统一存储,以便每个节点访问到的数据一致.官方提供基于memcached的方案,由于项目需要,需要做计入redis,根据官方 ...

  5. redis三:集群配置

    文章目录 redis集群配置 主从复制 哨兵模式 集群搭建 redis集群配置 不熟悉redis请先看:redis6学习 主从复制 主从模式指的是使用一个redis实例作为主机,其余的实例作为备份机. ...

  6. redis安装、集群搭建等

    1 Redis介绍 1.1 什么是NoSql 为了解决高并发.高可扩展.高可用.大数据存储问题而产生的数据库解决方案,就是NoSql数据库. NoSQL,泛指非关系型的数据库,NoSQL即Not-On ...

  7. Redis安装及集群部署

    文章目录 一.redis编译安装 二 .redis主从复制 三. redis哨兵模式 1.配置sentinel 2 开启哨兵模式 3 观察效果 四. redis的集群cluster 1. 集群构建 1 ...

  8. 基于CentOS的分布式文件系统GlusterFS安装、集群搭建和运维

    一.准备工作 1.1 glusterfs的发展史 2011年10月04日,红帽收购Gluster. 2014年4月30日,红帽公司以1.75亿美元的价格将Ceph的企业级产品提供商Inktank收入囊 ...

  9. NOSQL 之 cassadra 安装与集群配置

    cassadra 安装 1.下载: apache-cassandra-2.0.6-bin.tar.gz jdk-8-linux-x64.rpm 2.先更新jdk rpm -ivh jdk-8-linu ...

最新文章

  1. android realm删除对象,Android Realm-从服务访问Realm对象
  2. STM32 基础系列教程 33 - Lwip_tcp_client
  3. 如何在jupyter notebook中运行markdown文件(脚本、代码)
  4. android LinkedList的基本用法
  5. nvidia控制面板点了没反应win7_为什么没有nvidia控制面板_win7没有nvidia控制面板怎么找回-系统城...
  6. 实验1 熟悉实验环境
  7. 又把锅甩给中国!苹果和解给了300多亿,但高通业绩预测并不乐观
  8. 程序员面试金典——17.8最大连续数列和
  9. 基于JAVA个人饮食营养管理信息系统计算机毕业设计源码+系统+mysql数据库+lw文档+部署
  10. 关于flash播放器不为人知的四大点
  11. matlab中floor函数,Matlab基本函数-floor函数
  12. 中国拳手徐灿将战世界拳王:有信心把金腰带带回祖国
  13. 敏捷管理第三章《敏捷理解》
  14. 广东电大计算机绘图试题,电大CAD计算机绘图本实验1小抄参考
  15. Python数据挖掘(2)简单的分类问题
  16. POJ-3411 Paid Roads 搜索优化
  17. [基于富瀚6630平台实现U盘热插拔检测并mount]
  18. AOV网络——初了解
  19. 3D多人在线角色扮演游戏《木牛流马》概念案(一门课的半期作业)
  20. 数字内容安全实验二:数字图像复制粘贴检测

热门文章

  1. python oracle连接池_python - DBUtils 连接池减少oracle数据库的连接数
  2. 坐标轨迹计算_三坐标测量机控制系统功能介绍
  3. oracle查询约束的关联表,几种容易查询Oracle中表的约束信息
  4. frame框架左右展开收缩(上下左右,其中左右可收缩)
  5. 如何在cmd命令提示符里打开Python
  6. python语言输入中文_selenium+python 语言编写问题,在执行时无法输入中文用户名...
  7. 寄存器和存储器的区别_汇编语言 第二章 寄存器
  8. python中的zip函数详解_python中的 zip函数详解
  9. stretchblt 模糊_微软开源持续开发模糊测试工具OneFuzz
  10. 如何使用ant_从 0 开始,成为 Ant-Design Contributor