发表于: 2017-10-16 21:58:22
1 905
一、今日完成
1.安装与配置redis
首次启动redis,会给出生如下提醒:
25788:M 16 Oct 21:44:37.884 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
25788:M 16 Oct 21:44:37.884 # Server initialized
25788:M 16 Oct 21:44:37.884 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
25788:M 16 Oct 21:44:37.885 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
需要对/etc/rc.local和/etc/sysctl.conf进行参数配置,涉及到backlog、redis overcommit_memory与透明大页等知识点的了解,参考http://blog.csdn.net/a491857321/article/details/52006376。
2.学习redis的主要数据类型
Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合)。
(1)string是redis最基本的类型,你可以理解成与Memcached一模一样的类型,一个key对应一个value。
string类型是二进制安全的。意思是redis的string可以包含任何数据。比如jpg图片或者序列化的对象 。
string类型是Redis最基本的数据类型,一个键最大能存储512MB。
redis 127.0.0.1:6379> SET name "runoob"
OK
redis 127.0.0.1:6379> GET name
"runoob"
使用了 Redis 的 SET 和 GET 命令。键为 name,对应的值为 runoob。
(2)Redis hash 是一个键名对集合。
Redis hash是一个string类型的field和value的映射表,hash特别适合用于存储对象。
127.0.0.1:6379> HMSET user:1 username runoob password runoob points 200
OK
127.0.0.1:6379> HGETALL user:1
1) "username"
2) "runoob"
3) "password"
4) "runoob"
5) "points"
6) "200"
hash 数据类型存储了包含用户脚本信息的用户对象。 使用 Redis HMSET, HGETALL 命令,user:1 为键值。
(3)Redis 列表是简单的字符串列表,按照插入顺序排序。你可以添加一个元素到列表的头部(左边)或者尾部(右边)。
redis 127.0.0.1:6379> lpush runoob redis
(integer) 1
redis 127.0.0.1:6379> lpush runoob mongodb
(integer) 2
redis 127.0.0.1:6379> lpush runoob rabitmq
(integer) 3
redis 127.0.0.1:6379> lrange runoob 0 10
1) "rabitmq"
2) "mongodb"
3) "redis"
redis 127.0.0.1:6379>
(4)Redis的Set是string类型的无序集合。集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是O(1)。
(5)Redis zset 和 set 一样也是string类型元素的集合,且不允许重复的成员。
不同的是每个元素都会关联一个double类型的分数。redis正是通过分数来为集合中的成员进行从小到大的排序。zset的成员是唯一的,但分数(score)却可以重复。
3.练习redis的性能测试
命令:redis-benchmark [option] [option value]
可选参数如下所示
1 -h 指定服务器主机名 127.0.0.1
2 -p 指定服务器端口 6379
3 -s 指定服务器 socket
4 -c 指定并发连接数 50
5 -n 指定请求数 10000
6 -d 以字节的形式指定 SET/GET 值的数据大小 2
7 -k 1=keep alive 0=reconnect 1
8 -r SET/GET/INCR 使用随机 key, SADD 使用随机值
9 -P 通过管道传输 <numreq> 请求 1
10 -q 强制退出 redis。仅显示 query/sec 值
11 --csv 以 CSV 格式输出
12 -l 生成循环,永久执行测试
13 -t 仅运行以逗号分隔的测试命令列表。
14 -I Idle 模式。仅打开 N 个 idle 连接并等待。
设置主机为 127.0.0.1,端口号为 6379,执行的命令为 set,lpush,请求数为 10000,通过 -q 参数让结果只显示每秒执行的请求数。
redis-benchmark -h 127.0.0.1 -p 6379 -t set,lpush -n 100000 -q
SET: 146198.83 requests per second
LPUSH: 145560.41 requests per second
4.在Java中使用redis
(1)连接到 redis 服务
import redis.clients.jedis.Jedis;
public class RedisJava {
public static void main(String[] args) {
//连接本地的 Redis 服务
Jedis jedis = new Jedis("localhost");
System.out.println("Connection to server sucessfully");
//查看服务是否运行
System.out.println("Server is running: "+jedis.ping());
}
}
(2)Redis Java String
import redis.clients.jedis.Jedis;
public class RedisStringJava {
public static void main(String[] args) {
//连接本地的 Redis 服务
Jedis jedis = new Jedis("localhost");
System.out.println("Connection to server sucessfully");
//设置 redis 字符串数据
jedis.set("w3ckey", "Redis tutorial");
// 获取存储的数据并输出
System.out.println("Stored string in redis:: "+ jedis.get("w3ckey"));
}
}
(3)Redis Java List
import redis.clients.jedis.Jedis;
public class RedisListJava {
public static void main(String[] args) {
//连接本地的 Redis 服务
Jedis jedis = new Jedis("localhost");
System.out.println("Connection to server sucessfully");
//存储数据到列表中
jedis.lpush("tutorial-list", "Redis");
jedis.lpush("tutorial-list", "Mongodb");
jedis.lpush("tutorial-list", "Mysql");
// 获取存储的数据并输出
List<String> list = jedis.lrange("tutorial-list", 0 ,5);
for(int i=0; i<list.size(); i++) {
System.out.println("Stored string in redis:: "+list.get(i));
}
}
}
(4)Redis Java Keys
import redis.clients.jedis.Jedis;
public class RedisKeyJava {
public static void main(String[] args) {
//连接本地的 Redis 服务
Jedis jedis = new Jedis("localhost");
System.out.println("Connection to server sucessfully");
// 获取数据并输出
List<String> list = jedis.keys("*");
for(int i=0; i<list.size(); i++) {
System.out.println("List of stored keys:: "+list.get(i));
}
}
}
二、明日计划
1.练习使用JMeter和Memcache进行压测
2.部署服务器,练习nginx的负责均衡功能
三、遇到问题
暂无。
四、收获
以上。
禅道:http://task.ptteng.com/zentao/task-view-10379.html
计划在本周五完成并提交任务6.
评论