redis: 初步使用&集群搭建

  • redis命令行
  • idea 读写redis
  • redis集群搭建

下载redis最新版,中文官网 http://www.redis.cn/download.html

wget http://download.redis.io/releases/redis-5.0.0.tar.gz
tar xzf redis-5.0.0.tar.gz
cd redis-5.0.0
make

1,命令行

库操作: 0-15号
显示当前的库存量 dbsize
删除当前库所有数据 flushdb
选择一个库:默认0 select 1
-------- -------
查看redis 状态 info
保存所有库的数据 save
删除所有库的数据 flushall
数据管理: key操作
显示当前的库所有key keys *
添加新数据key set key1 aaaa
查询某key的数据 get key1
key重命名 rename key1 key2
查询某key是否存在 exsists key1
移动数据到指定的库 move key1 2

2,idea读写redis

添加maven依赖,使用scala编写程序

 	<dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
import java.io._
import redis.clients.jedis.Jedis

object RedisTest2 {
  def main(args: Array[String]): Unit = {
    t1()
    t3()
  }

  def t1(): Unit = {
    //读取图片
    val br = new BufferedInputStream(new FileInputStream("/home/wang/rm.png"))
    val buf = new Array[Byte](1024)
    var len=0
    //转存到redis
    val jedis = new Jedis("s101",6379)
    val out = new ByteArrayOutputStream()

    while(({len=br.read(buf) ; len } )!= -1){
      out.write(buf, 0, len)
    }
    out.close()
    val bytes = out.toByteArray
    jedis.set("jpg".getBytes(),bytes)

    //关闭资源
    out.close()
    jedis.close()
  }

  def t3(): Unit = {
    val jedis = new Jedis("s101", 6379)
    val bytes = jedis.get("jpg".getBytes())
    val out = new BufferedOutputStream(new FileOutputStream("a3.png"))
    out.write(bytes)
    //关闭资源
    out.close()
    jedis.close()
  }
}

3,redis集群搭建

a,进入redis解压目录: 单机模拟集群创建

cd /soft/redis-5.0.0/

#创建目录: 存放不同的配置文件
mkdir -p cluster-conf/{1,2,3,4,5,6}
for i in {1..6}
do
	cp redis.conf  cluster-conf/$i/redis.conf
	sed -i "[email protected] [email protected] [email protected]"  cluster-conf/$i/redis.conf
	sed -i "[email protected] [email protected] [email protected]"  cluster-conf/$i/redis.conf
	sed -i "[email protected] [email protected] [email protected]"  cluster-conf/$i/redis.conf 
	sed -i "[email protected] [email protected] [email protected]" cluster-conf/$i/redis.conf
	sed -i "[email protected] /var/run/[email protected] /var/run/[email protected]"  cluster-conf/$i/redis.conf 
done

b,开启所有节点,并初始化集群

for i in {1..6}
do
	/soft/redis-5.0.0/src/redis-server /soft/redis-5.0.0/cluster-conf/$i/redis.conf
done

redis: 初步使用&集群搭建
查看集群管理帮助:
redis: 初步使用&集群搭建
初始化集群所有节点

src/redis-cli --cluster create \
192.168.56.111:7006 \
192.168.56.111:7001 \
192.168.56.111:7002 \
192.168.56.111:7003 \
192.168.56.111:7004 \
192.168.56.111:7005 \
--cluster-replicas  1

redis: 初步使用&集群搭建
redis: 初步使用&集群搭建