redis是什么
Redis是一个开源的、高性能的Key-Value存储系统,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set 有序集合)、hash(哈希类型)。Redis的所有数据都是保存在内存中,然后不定期的通过异步方式保存到磁盘上(这称为“半持久化模式”);也可以把每一次数据变化都写入到一个appendonly file(aof)里面(这称为“全持久化模式”)
持久化方式
RDB(适用灾难恢复,内容紧凑,保存redis在某个时间点数据集)和AOF(追加操作日志文件,根据使用的fsync策略,体积大,速度慢于RDB)
分区
分区是将数据分割成多个 Redis 实例,使每个实例将只包含键子集的过程。
项目中使用
集群搭建
进入redis安装包路径:
cd /usr/local/src/redis/redis-3.0.7/src
6个虚拟机之间互相通信
./redis-trib.rb create --replicas 1 192.168.246.44:6379 192.168.246.44:6380 192.168.246.44:6381 192.168.246.44:6382 192.168.246.44:6383 192.168.246.44:6384 192.168.22.251:6380
添加redis服务
pom中添加依赖,redis封装在tool里面,配置文件在easyui中
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${jedis.version}</version>
</dependency>
添加目录applicationContext-redis.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<!-- 配置redis客户端集群版 -->
<bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
<constructor-arg>
<set>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.22.251"/>
<constructor-arg name="port" value="6380"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.22.251"/>
<constructor-arg name="port" value="6381"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.22.251"/>
<constructor-arg name="port" value="6382"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.22.252"/>
<constructor-arg name="port" value="6380"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.22.252"/>
<constructor-arg name="port" value="6381"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.22.252"/>
<constructor-arg name="port" value="6382"/>
</bean>
</set>
</constructor-arg>
</bean>
</beans><bean id="jedisClientCluster" class="com.tgb.itoo.tool.redisUtil.JedisClientCluster"/>
controller或service使用
JedisCluster jediscluster;
public JedisCluster getJediscluster() {
return jediscluster;
}
public void setJediscluster(JedisCluster jediscluster) {
this.jediscluster = jediscluster;
}
添加:
@RequestMapping("test1")
public void test1(HttpServletRequest request, HttpServletResponse response) {
//添加缓存(String)
jediscluster.set("key", "value");
}
@RequestMapping("test3")
public void test3(HttpServletRequest request, HttpServletResponse response) {
//查询缓存(String)
String string = jediscluster.get("key");
}
JedisCluster jediscluster =null;
public BuildTypeBeanImpl(){
//在执行此实例化的时候就会完成所有注入
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-redis.xml");
jediscluster = (JedisCluster)ctx.getBean("jedisCluster");
}
主从复制
Redis启动成功后,可以看到启动的端口是6379看这个可以通过指定配置文件修改,我们就来制定以下配置文件,设置6个同样的redis服务,三主三从,redis集群采用了数据分片的技术。我们可以是6个虚拟机,6个IP,也可以是1个虚拟机,同一个IP,不同的端口号。Demo中采用的是一个虚拟机,不同端口的方式。
1、如下路径创建6个文件夹:6379,6380,6381,6382,6383,6384
2、复制redis.cof到6个文件夹:
cp redis.conf /usr/local/redis/myredis/6379
3、同理,复制redis-server到6个文件夹,复制redis-check-aof到6个文件夹(如下文件都复制过去)
4、配置6个redis.conf
port 6379
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
port 6380
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
……剩下的一样,端口号不一样
5、启动6个redis服务,指向各自conf
6379 文件夹下: ./redis-server ./redis.conf
6380 文件夹下: ./redis-server ./redis.conf
……
查看启动的redis服务进程状态: ps –ef|grep redis
与memcached相比
与memcached类似,为了保证效率,数据都是缓存在内存中。redis可以用来做存储具有持久化功能,memcached用来做缓存。Redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并在此基础上实现master-slave(主从)同步。