redis的使用

来源:互联网 发布:批处理软件 编辑:程序博客网 时间:2024/06/06 01:54

最近一直在学java后台,看的是黑马的视频,这里复习一下redis服务器,下面是黑马视频的笔记,加以修改。

redis的作用:为了解决高并发、高可用、高可扩展,大数据存储等一系列问题而产生的数据库解决方案,就是NoSql。NoSql,叫非关系型数据库,它的全名Not only sql。它不能替代关系型数据库,只能作为关系型数据库的一个良好补充。redis是以键值对存储数据的。

典型应用: 内容缓存,主要用于处理大量数据的高访问负载。 
数据模型: 一系列键值对
优势: 快速查询
劣势: 存储的数据缺少结构化


1.redis的安装
准备工作:因为redis是用c语言开发的,先安装c语言环境
[root@itheima ~]# yum install gcc-c++
这个步骤需要联网,重网上下载东西的。

第一步把下载的redis安装包上传到linux服务器

然后在linux的用户文件夹下找到压缩包。


第二步解压压缩文件
[root@itheima ~]# tar -zxf redis-3.0.0.tar.gz


第三步编译redis源码
先进入解压出来的文件夹,然后编译
[root@itheima ~]# cd redis-3.0.0
[root@itheima redis-3.0.0]# make


第四步安装redis,安装到/usr/local/redis这个文件夹下
[root@itheima redis-3.0.0]# make install PREFIX=/usr/local/redis
如果安装成功了,这个文件夹下会有一个bin的文件夹



2.redis的启动
两种启动方式:
2.1 前端启动
进入安装的文件夹下(/usr/local/redis 这个文件夹)
[root@itheima bin]# ./redis-server

前端启动的关闭
强制关闭:Ctrl+c
正常关闭:[root@itheima bin]# ./redis-cli shutdown

启动界面:

前端启动的问题
一旦客户端关闭,则redis服务也停掉。


2.2 后端启动
第一步:需要将redis解压之后的源码包中的redis.conf文件拷贝到bin目录下
[root@itheima bin]# cp /root/redis-3.0.0/redis.conf ./

第二步:修改redis.conf文件,将daemonize改为yes
先要使用vim redis.conf,然后输入i,然后将no改成yes,然后按ESC键,然后输入:wq,就可以保存



第三步:使用命令后端启动redis
[root@itheima bin]# ./redis-server redis.conf

第四步:查看是否启动成功
ps -aux | qrep redis


关闭后端启动的方式:

强制关闭:[root@itheima bin]# kill -9 5071
正常关闭:[root@itheima bin]# ./redis-cli shutdown

在项目中,建议使用正常关闭。
因为redis作为缓存来使用的话,将数据存储到内存中,如果使用正常关闭,则会将内存数据持久化到本地之后,再关闭。

如果是强制关闭,则不会进行持久化操作,可能会造成部分数据的丢失。



3.redis客户端的启动
在redis的安装目录的bin文件夹下输入命令
[root@itheima bin]# ./redis-cli -h 127.0.0.1 -p 6379

默认启动:[root@itheima bin]# ./redis-cli
使用默认配置:默认的ip【127.0.0.1】,默认的port【6379】
- 关闭
Ctrl+c
127.0.0.1:6379> quit



4.redis持久化方案
持久化保证了即使redis服务重启也不会丢失数据,因为redis服务重启后会将硬盘上持久化的数据恢复到内存中

4.1 Rdb方式
在redis.conf文件中添加如下
save 900 1  
save 300 10
save 60 10000
解释: 如果修改了一次数据库,900秒存储一下。如果修改了10次,300秒存储一次。如果修改了10000次,60秒存储一次。

在redis.conf中可以指定持久化文件存储的目录

dump.rdb这个文件

Rdb问题:
一旦redis非法关闭,那么会丢失最后一次持久化之后的数据。
如果数据不重要,则不必要关心。
如果数据不能允许丢失,那么要使用aof方式。


4.2 Aof方式:
aof方式是,修改一次数据库,就将操作的记录存储到aof持久化文件中。

开启aof持久化方式:
将redis.conf中的appendonly改为yes,即开启aof方式的持久化方案。

Aof文件存储的名称

在使用aof和rdb方式时,如果redis重启,则数据从aof文件加载。
实际项目中,aof也是要开启



5.redis集群(准备工作)
搭建ruby
1.安装ruby
[root@itheima bin2]# yum install ruby
[root@itheima bin2]# yum install rubygems

2.把gem文件上传到linux系统中

然后到这个目录下,安装ruby和redis接口
[root@itheima ~]# gem install redis-3.0.0.gem

3.把redis解压的文件夹里src目录下的redis-trib.rb这个文件,拷贝到redis安装目录下的redis-cluster这个文件夹下(这个文件夹需要自己创建)
1) 先去redis安装目录下把redis-cluster这个文件夹创建。
[root@itheima src]# cd /usr/local/redis19/
[root@itheima redis19]# mkdir redis-cluster

2) 接下来去redis解压缩src目录下,把redis-trib.rb拷贝到redis-cluster这个文件夹里
[root@itheima redis19]# cd /root/redis-3.0.0/src/
[root@itheima src]# cp redis-trib.rb /usr/local/redis19/redis-cluster

4.查看是否拷贝成功




6.搭建集群
搭建集群最少也得需要3台主机,如果每台主机再配置一台从机的话,则最少需要6台机器。(这里就用一台虚拟机,不同的主机也就是ip或者端口不同而已,改下各个的端口就可以模拟一下了)
端口设计如下:7001-7006

第一步:复制出一个7001机器
[root@itheima redis]# cp bin ./redis-cluster/7001 –r

第二步:如果存在持久化文件,则删除
[root@itheima 7001]# rm -rf appendonly.aof dump.rdb

第三步:设置集群参数,修改redis.conf文件这段本来是注释掉的(用#),去掉#
,修改端口


第四步:复制出7002-7006机器
[root@itheima redis-cluster]# cp 7001/ 7002 -r
[root@itheima redis-cluster]# cp 7001/ 7003 -r
[root@itheima redis-cluster]# cp 7001/ 7004 -r
[root@itheima redis-cluster]# cp 7001/ 7005 -r
[root@itheima redis-cluster]# cp 7001/ 7006 –r

第五步:修改7002-7006机器的端口(重复第三步,挨个修改7002~7006里面的端口)

第七步:启动7001-7006这六台机器
创建一个脚本
[root@itheima redis-cluster]# vim start-all.sh


第八步:修改start-all.sh文件的权限,并且执行脚本
[root@itheima redis-cluster]# chmod u+x start-all.sh

[root@itheima redis-cluster]# ./start-all.sh

第九步:创建集群
[root@itheima redis-cluster]# ./redis-trib.rb create --replicas 1 192.168.72.128:7001 192.168.72.128:7002 192.168.72.128:7003 192.168.72.128:7004 192.168.72.128:7005 192.168.72.128:7006
这里的ip地址,是你虚拟机的ip地址

第十步:连接集群
[root@itheima 7001]# ./redis-cli -h 192.168.242.137 -p 7001 –c

只要连接上了一个集群,所有的集群就都连接上了




7.jedis连接集群
7.1 设置防火墙
[root@itheima redis-cluster]# vim /etc/sysconfig/iptables-A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT# Firewall configuration written by system-config-firewall# Manual customization of this file is not recommended.*filter:INPUT ACCEPT [0:0]:FORWARD ACCEPT [0:0]:OUTPUT ACCEPT [0:0]-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT-A INPUT -p icmp -j ACCEPT-A INPUT -i lo -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 7001 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 7002 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 7003 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 7004 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 7005 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 7006 -j ACCEPT-A INPUT -m state --state NEW -m tcp -p tcp --dport 7007 -j ACCEPT-A INPUT -j REJECT --reject-with icmp-host-prohibited-A FORWARD -j REJECT --reject-with icmp-host-prohibitedCOMMIT~~~~"/etc/sysconfig/iptables" 23L, 1146C 已写入[root@itheima redis-cluster]# service iptables restartiptables:清除防火墙规则: [确定]iptables:将链设置为政策 ACCEPT:filter [确定]iptables:正在卸载模块: [确定]iptables:应用防火墙规则: [确定][root@itheima redis-cluster]#

7.2 代码

添加jar包





运行结果:



8.spring配置连接集群
思路:创建一个redis操作的接口。分别创建两个实现类对应redis 的单机版和集群版。当使用单机版redis时,配置单机版的实现类,当使用集群版本的时候,配置集群版的实现类。

测试类:


第一步:导入jedis的jar包
 <dependency>                <groupId>redis.clients</groupId>                <artifactId>jedis</artifactId></dependency>


第二步:jedisClient接口
public interface JedisClient {     String set(String key, String value);     String get(String key);     /**      * hashmap设置值,map里面有一个名字key,然后里面是一个一个的键值对      *      * @param key      *            map的名字      * @param item      *            键      * @param value      *            值      */     Long hset(String key, String item, String value);     /**      * hashmap取值      */     String hget(String key, String item);     Long incr(String key);     Long decr(String key);     Long expire(String key, int second);     Long ttl(String key);}

单机版和集群版的两个实现类

1.单机版:
package com.taotao.rest.component.impl;import org.springframework.beans.factory.annotation.Autowired;import com.taotao.rest.component.JedisClient;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisCommands;import redis.clients.jedis.JedisPool;public class JedisClientSingle implements JedisClient {    @Autowired    private JedisPool jedisPool;    @Override    public String set(String key, String value) {        Jedis jedis = jedisPool.getResource();        String result = jedis.set(key, value);        return result;    }    @Override    public String get(String key) {        Jedis jedis = jedisPool.getResource();        String result = jedis.get(key);        return result;    }    @Override    public Long hset(String key, String item, String value) {        Jedis jedis = jedisPool.getResource();        Long result = jedis.hset(key, item, value);        return result;    }    @Override    public String hget(String key, String item) {        Jedis jedis = jedisPool.getResource();        String result = jedis.hget(key, item);        return result;    }    @Override    public Long incr(String key) {        Jedis jedis = jedisPool.getResource();        Long result = jedis.incr(key);        return result;    }    @Override    public Long decr(String key) {        Jedis jedis = jedisPool.getResource();        Long result = jedis.decr(key);        return result;    }    @Override    public Long expire(String key, int second) {        Jedis jedis = jedisPool.getResource();        Long result = jedis.expire(key, second);        return result;    }    @Override    public Long ttl(String key) {        Jedis jedis = jedisPool.getResource();        Long result = jedis.ttl(key);        return result;    }}

2.集群版
package com.taotao.rest.component.impl;import org.springframework.beans.factory.annotation.Autowired;import com.taotao.rest.component.JedisClient;import redis.clients.jedis.JedisCluster;import redis.clients.jedis.JedisPool;public class JedisClientCluster implements JedisClient {     @Autowired     private JedisCluster cluster;     @Override     public String set(String key, String value) {           return cluster.set(key, value);     }     @Override     public String get(String key) {           return cluster.get(key);     }     @Override     public Long hset(String key, String item, String value) {           return cluster.hset(key, item, value);     }     @Override     public String hget(String key, String item) {           return cluster.hget(key, item);     }     @Override     public Long incr(String key) {           return cluster.incr(key);     }     @Override     public Long decr(String key) {           return cluster.decr(key);     }     @Override     public Long expire(String key, int second) {           return cluster.expire(key, second);     }     @Override     public Long ttl(String key) {           return cluster.ttl(key);     }}


第二步:spring配置
<!-- 配置包扫描器,扫描@Service主键的类 -->     <context:component-scan base-package="com.taotao.rest.service" />     <!-- 配置连接池 -->     <bean class="redis.clients.jedis.JedisPool" id="jedisPool">           <constructor-arg name="host" value="192.168.72.128"></constructor-arg>           <constructor-arg name="port" value="6379"></constructor-arg>     </bean>     <!-- 单机版redis -->     <bean id="jedisClientSingle" class="com.taotao.rest.component.impl.JedisClientSingle" />     <bean class="redis.clients.jedis.JedisCluster" id="jedisCluster">           <constructor-arg>                <set>                     <bean class="redis.clients.jedis.HostAndPort">                           <constructor-arg name="host" value="192.168.72.128"></constructor-arg>                           <constructor-arg name="port" value="7001"></constructor-arg>                     </bean>                     <bean class="redis.clients.jedis.HostAndPort">                           <constructor-arg name="host" value="192.168.72.128"></constructor-arg>                           <constructor-arg name="port" value="7002"></constructor-arg>                     </bean>                     <bean class="redis.clients.jedis.HostAndPort">                           <constructor-arg name="host" value="192.168.72.128"></constructor-arg>                           <constructor-arg name="port" value="7003"></constructor-arg>                     </bean>                     <bean class="redis.clients.jedis.HostAndPort">                           <constructor-arg name="host" value="192.168.72.128"></constructor-arg>                           <constructor-arg name="port" value="7004"></constructor-arg>                     </bean>                     <bean class="redis.clients.jedis.HostAndPort">                           <constructor-arg name="host" value="192.168.72.128"></constructor-arg>                           <constructor-arg name="port" value="7005"></constructor-arg>                     </bean>                     <bean class="redis.clients.jedis.HostAndPort">                           <constructor-arg name="host" value="192.168.72.128"></constructor-arg>                           <constructor-arg name="port" value="7006"></constructor-arg>                     </bean>                </set>           </constructor-arg>     </bean>     <bean class="com.taotao.rest.component.impl.JedisClientCluster"           id="jedisClientCluster" />
上面是单机版和集群版都配置了,项目使用哪个,就把另一个配置注释掉即可

测试代码:
   @Test     public void springJedis(){           ApplicationContext applicationContext = new                     ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml");           JedisClient jedis = applicationContext.getBean(JedisClient.class);           jedis.set("client", "111111");           String result = jedis.get("client");           System.out.println("result: " + result);     }

以上
原创粉丝点击