redis使用教程及java客户端连接访问

redis使用教程

1.了解NOSOL

什么是nosql(not only sql)不仅仅是sql

百度百科

1).Nosql产生的背景

适应当前互联网环境的高并发,高负荷环境

2).Nosql数据模型

KV键值 Bson 树形 列族

3).Nosql数据库的四大分类

a.KV键值对

1.新浪:BerkeleyDB+Redis

2.美团:redis+tair

3.阿里、百度:memcache+redis

b.文档型数据库

1.couchDB

2.MongoDB:一个基于分布式文件存储的数据库

c.列存储数据库

1.Cassandra HBase

2.分布式文件系统

d.图关系数据库

专注于构建关系图谱

1.Neo4j

2.InfoGrid

4).CAP+BASE

传统sql的传统的ACID分别是什么?

A(Atomicity)原子性 C( consistency) 一致性

I(Isolation) 独立性 D(durability) 持久性

Nosql的CAP

C:( consistency) 强一致性

A:(Availability)可用

P : ( partition tolerance) 分区容错

5).BASE

基本可用 ( Basically Availavble)

软状态 ( soft state)

最终一致(Eventually onsistent)

2.redis使用

1).redis能干嘛?

内存存储和持久化:redis支持异步将内存中的数据写到硬盘上,同时不影响继续服务取最新N个数据的操作,如:可以将最新的10条评论的ID放在redis的List集合里面模拟类似于httpSession这种需要设置过期时间的功能。

发布、订阅消息系统

定时器、计数器

2).redis安装

1.yum安装
	yum update
	如果无法连接到网络
	vi /etc/sysconfig/network-scripts/ifcfg-ens33  ====》》将ONBOOT:no修改为ONBOOT:yes
	yum install -y redis
2.安装包安装
	tar -zxvf redis-3.0.0.tar.gz
	使用gcc编译器安装
	没有gcc编译器使用 yum install -y gcc
	进入redis使用 make 
	最后使用 make install 安装路径

3).redis启动

####a 修改配置文件

################################# 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.
#通过使用默认的redis不能直接运行一个守护 ,如果你需要这样做就设置为yes,redis将在进程中写入一个时候守#护进程,简而言之,就是创建一个守护进程,也就是后台进程
daemonize yes

b 启动redis

1.查看是否启动redis服务
	ps -ef|grep redis
2.进入redis安装目录
	cd usr/local/redis
3.启动redis
	./bin/redis-service redis.conf
4.连接到redis
	./bin/redis-cli -p 6379
5.关闭redis
	shutdown
	或者 ./bin/redis-cli -p 6379 shutdown

redis安装目录在这里插入图片描述

redis启动成功
在这里插入图片描述

连接到redis

在这里插入图片描述

4).redis基础知识

a 杂项基础知识

redis安装bin目录下的文件
在这里插入图片描述

1.redis启动状态下
	./redis-benchmark #redis自带的测试工具,能够测试出当前redis的效率
2.连接到redis
	redis-cli -p 6379
3.redis简单使用
	1.查看当前redis的键值数目
		keys * 或者 dbsize
	2.redis仓库
		redis默认设置为16个仓库,默认从0开始
		select 仓库编码   #切换仓库
	3.清除键值
		flashdb 清除当前仓库的键值
		flashall 清除所有仓库的键值

切换仓库
在这里插入图片描述

b redis常用五大数据类型

redis文档手册

#####1 key-value

keys * #查看有多少key
set key keyValue #给某一个键设置值
get key #获取某一个键的值
move key 仓库 #将当前仓库的键值移动到指定仓库
expire key time #设置值的过期时间,单位秒
ttl key #查看还有多少秒过期 -1表示永不过期,-2表示已过期
del key #删除
2 String

对字符串进行操作

3 List

list是一个链表集合,能够往左边插入数据,也可以往右边插入数据

List集合展示是从左往右,即最左边的是第一个元素

1.往左边插入数据   #一个一个添加,并不是添加一串 类似于砌墙
	lpush key [value...] 
	eg: lpush list1 0 1 2 3 4
		"4"
		"3"
		"2"
		"1"
		"0"
2.往右边
	rpush key [value ...]
	eg:	rpush list2 0 1 2 3 4 5
		"0"
		"1"
		"2"
		"3"
		"4"
		"5"
3.查看List的元素
	lrange key start end #start表示开始位置 end表示结束位置 end=-1表示查看全部
4.删除一个元素
	lpop  key  #删除最左边的元素
	rpop  key  #删除最右边的元素
5.其他
	lindex key index # 给其他的一个start 表示下标号
	lset  key  index value #给指定下标的设置一个值 index 表示下标
	
4 set

与java类似,存储不同的值

1.新增set
	sadd set1 0 1 2 3
2.查看set中的元素
	smember set1
3.删除set中的一个元素
	srem key value #移除set中的一个value

5 hash

与java hashMap相似

1.新增
	hset key field value   #给某一个键的某一个属性赋值
	hmset key filed1 value field2 value2 #给多个字段赋值
2.查看
	hget key field #查看指定键的指定属性
	hmget key field1 field2 ... #查看多个属性
	hgetall key  #查看所有元素
	hexist key feild  #判断一个元素是否存在
	hkeys key  #查看key中所有的属性
	hvals key #查看key中所有的属性的值
	

3.redis配置文件

1).include

可以包含其他redis的配置文件,效果与jsp相似

################################## 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

3). GENERAL

redis的通用配置

################################# 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 no  ##是否后台运行

# If you run Redis from upstart or systemd, Redis can interact with your
# supervision tree. Options:
#   supervised no      - no supervision interaction
#   supervised upstart - signal upstart by putting Redis into SIGSTOP mode
#   supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
#   supervised auto    - detect upstart or systemd method based on
#                        UPSTART_JOB or NOTIFY_SOCKET environment variables
# Note: these supervision methods only signal "process is ready."
#       They do not enable continuous liveness pings back to your supervisor.
supervised no

# If a pid file is specified, Redis writes it where specified at startup
# and removes it at exit.
#
# When the server runs non daemonized, no pid file is created if none is
# specified in the configuration. When the server is daemonized, the pid file
# is used even if not specified, defaulting to "/var/run/redis.pid".
#
# Creating a pid file is best effort: if Redis is not able to create it
# nothing bad happens, the server will start and run normally.
pidfile /var/run/redis_6379.pid  ##运行线程的pid路径

# 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' 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

# By default Redis shows an ASCII art logo only when started to log to the
# standard output and if the standard output is a TTY. Basically this means
# that normally a logo is displayed only in interactive sessions.
#
# However it is possible to force the pre-4.0 behavior and always show a
# ASCII art logo in startup logs by setting the following option to yes.
always-show-logo yes

4持久化

RDB 与 AOF两种持久化方式

1).RDB

在指定的时间间隔内将内存中的数据集快照写入磁盘,它恢复时是将快照文件直接读到内存中

工作流程:

​ redis会单独创建(fork) 一个子进程来进行持久化,会先将数据写入到一个临时文件,待持久化过程都结束了,在用这个临时文件替换上次持久化好的文件,整改过程中,主进程是不进行任何IO操作的,这就确保了极高的性能,如果需要进行大规模数据的恢复,且对于数据恢复的完整性不是非常敏感,那么RDB方式要比AOF方式更加的高效,RDB的缺点是最后一次持久化的后的数据可能丢失。

fork?

​ fork的作用是复制一个与当前进程一样的进程,新进程的所有数据(变量、环境、程序计数器等),与原进程一致,但是是一个全新的进程,并作为原进程的子进程
在这里插入图片描述

################################ 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 ""   ##使用这个指令将停止保存

save 900 1
save 300 10
save 60 10000

# By default Redis will stop accepting writes if RDB snapshots are enabled
# (at least one save point) and the latest background save failed.
# This will make the user aware (in a hard way) that data is not persisting
# on disk properly, otherwise chances are that no one will notice and some
# disaster will happen.
#
# If the background saving process will start working again Redis will
# automatically allow writes again.
#
# However if you have setup your proper monitoring of the Redis server
# and persistence, you may want to disable this feature so that Redis will
# continue to work as usual even if there are problems with disk,
# permissions, and so forth.
stop-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.
rdbcompression yes

# Since version 5 of RDB a CRC64 checksum is placed at the end of the file.
# This makes the format more resistant to corruption but there is a performance
# hit to pay (around 10%) when saving and loading RDB files, so you can disable it
# for maximum performances.
#
# RDB files created with checksum disabled have a checksum of zero that will
# tell the loading code to skip the check.
rdbchecksum yes

# The filename where to dump the DB
dbfilename dump.rdb #默认名称

# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir ./

如何恢复?

​ 直接将备份好的dump.rdb文件复制到redis安装目录重新启动即可

优缺点

​ 优点:1.适合大规模恢复数据。2.对数据完整性和一致性要求不高

​ 缺点:1.间隔时间做一次备份,如果中途中断,最后一次没有备份的数据将丢失

2.fork的时候,每一次备份的数据量将是上一次的两倍

2).AOF

  • AOF 则以协议文本的方式,将所有对数据库进行过写入的命令(及其参数)记录到 AOF 文件,以此达到记录数据库状态的目的。

    ############################## 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).
    #
    # The Append Only File is an alternative persistence mode that provides
    # much better durability. For instance using the default data fsync policy
    # (see later in the config file) Redis can lose just one second of writes in a
    # dramatic event like a server power outage, or a single write if something
    # wrong with the Redis process itself happens, but the operating system is
    # still running correctly.
    #
    # AOF and RDB persistence can be enabled at the same time without problems.
    # If the AOF is enabled on startup Redis will load the AOF, that is the file
    # with the better durability guarantees.
    #
    # Please check http://redis.io/topics/persistence for more information.
    
    appendonly no
    
    # The name of the append only file (default: "appendonly.aof")
    
    appendfilename "appendonly.aof"
    
    # The fsync() call tells the Operating System to actually write data on disk
    # instead of waiting for more data in the output buffer. Some OS will really flush
    # data on disk, some other OS will just try to do it ASAP.
    #
    # Redis supports three different modes:
    #
    # no: don't fsync, just let the OS flush the data when it wants. Faster.
    # always: fsync after every write to the append only log. Slow, Safest.
    # everysec: fsync only one time every second. Compromise.
    #
    # The default is "everysec", as that's usually the right compromise between
    # speed and data safety. It's up to you to understand if you can relax this to
    # "no" that will let the operating system flush the output buffer when
    # it wants, for better performances (but if you can live with the idea of
    # some data loss consider the default persistence mode that's snapshotting),
    # or on the contrary, use "always" that's very slow but a bit safer than
    # everysec.
    #
    # More details please check the following article:
    # http://antirez.com/post/redis-persistence-demystified.html
    #
    # If unsure, use "everysec".
    
    # appendfsync always
    appendfsync everysec
    # appendfsync no
    
    # When the AOF fsync policy is set to always or everysec, and a background
    # saving process (a background save or AOF log background rewriting) is
    # performing a lot of I/O against the disk, in some Linux configurations
    # Redis may block too long on the fsync() call. Note that there is no fix for
    # this currently, as even performing fsync in a different thread will block
    # our synchronous write(2) call.
    #
    # In order to mitigate this problem it's possible to use the following option
    # that will prevent fsync() from being called in the main process while a
    # BGSAVE or BGREWRITEAOF is in progress.
    #
    # This means that while another child is saving, the durability of Redis is
    # the same as "appendfsync none". In practical terms, this means that it is
    # possible to lose up to 30 seconds of log in the worst scenario (with the
    # default Linux settings).
    #
    # If you have latency problems turn this to "yes". Otherwise leave it as
    # "no" that is the safest pick from the point of view of durability.
    
    no-appendfsync-on-rewrite no
    
    # Automatic rewrite of the append only file.
    # Redis is able to automatically rewrite the log file implicitly calling
    # BGREWRITEAOF when the AOF log size grows by the specified percentage.
    #
    # This is how it works: Redis remembers the size of the AOF file after the
    # latest rewrite (if no rewrite has happened since the restart, the size of
    # the AOF at startup is used).
    #
    # This base size is compared to the current size. If the current size is
    # bigger than the specified percentage, the rewrite is triggered. Also
    # you need to specify a minimal size for the AOF file to be rewritten, this
    # is useful to avoid rewriting the AOF file even if the percentage increase
    # is reached but it is still pretty small.
    #
    # Specify a percentage of zero in order to disable the automatic AOF
    # rewrite feature.
    
    auto-aof-rewrite-percentage 100
    auto-aof-rewrite-min-size 64mb
    
    # An AOF file may be found to be truncated at the end during the Redis
    # startup process, when the AOF data gets loaded back into memory.
    # This may happen when the system where Redis is running
    # crashes, especially when an ext4 filesystem is mounted without the
    # data=ordered option (however this can't happen when Redis itself
    # crashes or aborts but the operating system still works correctly).
    #
    # Redis can either exit with an error when this happens, or load as much
    # data as possible (the default now) and start if the AOF file is found
    # to be truncated at the end. The following option controls this behavior.
    #
    # If aof-load-truncated is set to yes, a truncated AOF file is loaded and
    # the Redis server starts emitting a log to inform the user of the event.
    # Otherwise if the option is set to no, the server aborts with an error
    # and refuses to start. When the option is set to no, the user requires
    # to fix the AOF file using the "redis-check-aof" utility before to restart
    # the server.
    #
    # Note that if the AOF file will be found to be corrupted in the middle
    # the server will still exit with an error. This option only applies when
    # Redis will try to read more data from the AOF file but not enough bytes
    # will be found.
    aof-load-truncated yes
    
    # When rewriting the AOF file, Redis is able to use an RDB preamble in the
    # AOF file for faster rewrites and recoveries. When this option is turned
    # on the rewritten AOF file is composed of two different stanzas:
    #
    #   [RDB file][AOF tail]
    #
    # When loading Redis recognizes that the AOF file starts with the "REDIS"
    # string and loads the prefixed RDB file, and continues loading the AOF
    # tail.
    #
    # This is currently turned off by default in order to avoid the surprise
    # of a format change, but will at some point be used as the default.
    aof-use-rdb-preamble no
    

总结:

AOF是的配置会优先于rdb加载,最多损失了一到两秒的数据

5.redis事务管理

可以一次执行多个命令,本质是一组命令的集合,一个事务中,所有的命令都会被序列化,按顺序的串行话执行而不会被其他命令插入,不许加塞

redis事物命令

​ 1.discard

​ 取消事务,放弃执行事务块内的所有命令

​ 2.exec

​ 执行所有事务块内的命令,如commit

​ 3.multi

​ 标记一个事务块的开始,如begintranslation

​ 4.unwatch

​ 取消watch命令对所有key的监视

​ 5.watch

​ 监视一个(或多个)key,如果在事物执行之前这些key被其他命令所改动,那么事物将会被打断

1. multi #开始一个事务
2. exec #提交事务
3. discard #放弃事务,
4  watch  #监听事务,当监听的那个键值在提交之前出现变化,那么这个事务就会被打断
5  unwatch  #放弃事务监听

在这里插入图片描述

redis主从复制

其他redis作为从机复制主机的键值,一般遵守一主二从,从机只有读取的工程,不能写入数据,从机选择主机

salvof ip port

6.java连接redis

1).使用java客户端连接

使用maven创建,使用jedis

<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>

 Jedis jedis=new Jedis("192.168.26.129",6379);//通过jedis连接redis客户端
 jedis.auth("houxiaobo");//连接密码
 //jedis API和redis客户端指令相同

2).使用spring-data-redis构建

maven配置


	<properties>
		<!-- redis 版本 -->
		<redis.version>2.9.0</redis.version>
		<spring.redis.version>2.1.0.RELEASE</spring.redis.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>${redis.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-redis</artifactId>
			<version>${spring.redis.version}</version>
		</dependency>
	</dependencies>

redis配置参数

# Redis settings  
redis.host=192.168.26.129  #ip
redis.port=6379   #端口
redis.pass=houxiaobo #密码
redis.dbIndex=0  #redis仓库
redis.expiration=3000  #键值有效期
redis.maxIdle=300  
redis.maxActive=600  
redis.maxWait=1000  
redis.testOnBorrow=true

spring-redis.xml配置参数

<!--配置jedis连接池 --> 
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"  
        p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}"  p:pool-config-ref="poolConfig"/>  
<!-- 使用redis模板---作用效果类似于hibernateTemplate-->      
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">  
        <property name="connectionFactory"   ref="connectionFactory" />  
 </bean>  

java使用

		 ClassPathXmlApplicationContext context=new 				                     ClassPathXmlApplicationContext("spring-redis.xml");
        StringRedisTemplate redis=(StringRedisTemplate)context.getBean("redisTemplate");
        redis.setEnableTransactionSupport(true);//设置redis支持事物
        redis.multi();
        ValueOperations<String, String> opsForValue = redis.opsForValue();//操作普通键值
        opsForValue.set("java", "java_test");
        redis.exec();

猜你喜欢

转载自blog.csdn.net/qq_38318330/article/details/107778355