设为首页 收藏本站
查看: 989|回复: 0

[经验分享] [爬虫]抓取知乎百万用户信息之Redis篇

[复制链接]

尚未签到

发表于 2017-12-20 16:04:37 | 显示全部楼层 |阅读模式
DSC0000.png 点击我前往Github查看源代码   别忘记star

  本项目github地址:https://github.com/wangqifan/ZhiHu     
  Redis安装
  Redis官方并没有推出windows版本,人家觉得linux已经够了,开发windows版本影响开发进度,还好微软有一个团队维持着Redis的windows版本,网上有很多介绍Redis安装的博客,大多数是敲各种命令行。这里有Redis的msi版本,只需要像安装普通软件一样点击下一步,下一步即可地址:https://github.com/MSOpenTech/redis/releases/download/win-3.2.100/Redis-x64-3.2.100.msi
  RRedis配置
  Redis配置文件详解 http://www.cnblogs.com/kreo/p/4423362.html
  找到Redis.windowserver.conf
DSC0001.png

  这里要注意的两点:1.远程连接
  

#  
# ~~~ WARNING ~~~ If the computer running Redis is directly exposed to the
  
# internet, binding to all the interfaces is dangerous and will expose the
  
# instance to everybody on the internet. So by default we uncomment the
  
# following bind directive, that will force Redis to listen only into
  
# the IPv4 lookback interface address (this means Redis will be able to
  
# accept connections only from clients running into the same computer it
  
# is running).
  
#
  
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
  
# JUST COMMENT THE FOLLOWING LINE.
  
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  
bind 0.0.0.0
  
将bind 127.0.0.1 修改成bind 0.0.0.0这样redis可以接受远程连接
  

  内存限制
  

# NOTE: since Redis uses the system paging file to allocate the heap memory,  
# the Working Set memory usage showed by the Windows Task Manager or by other
  
# tools such as ProcessExplorer will not always be accurate. For example, right
  
# after a background save of the RDB or the AOF files, the working set value
  
# may drop significantly. In order to check the correct amount of memory used
  
# by the redis-server to store the data, use the INFO client command. The INFO
  
# command shows only the memory used to store the redis data, not the extra
  
# memory used by the Windows process for its own requirements. Th3 extra amount
  
# of memory not reported by the INFO command can be calculated subtracting the
  
# Peak Working Set reported by the Windows Task Manager and the used_memory_peak
  
# reported by the INFO command.
  
#
  
maxmemory 2000mb
  
这里可以修改最大内存,建议放大点Redis比较还是吃内存的
  

  连接Reids类的封装
  Redis的C#驱动ServiceStack.Redis使用NuGet进行安装,由于这个类库已经商业化了,在4.0版本开始限制数量,每小时不得超过6000次,建议安装3.9版本
  在这个爬虫系统中,开始时候我只使用一台电脑装Redis,后来发现这台电脑特别卡,后来换成三台电脑装Redis,一个负责hash表,一个负责UrlNext队列和Urltoken队列,一台负责User队列,由于实验室的电脑非常老旧,还是很卡。最后又加持2台电脑,实验室三台电脑负责hash表,我的电脑负责User队列,征用学妹电脑用作任务队列。
  这个类命名为RedisCore
  Ip地址列表
  

public static List<string> ips = new List<string>()  

  {
  

"59.74.169.54",  

"59.74.169.57",  

"59.74.169.52",  

"59.74.169.58",  

"59.74.169.39"  

  };
  

  对插入队列的封装。
  Redis队列是有list这个数据结构实现的,从右边插入,左边弹出就可以实现队列
  插入
  

public static bool PushIntoList(int type, string key, string value)  

  {
  

bool Result = false;  

using (RedisClient Redis = new RedisClient(ips[type - 1], 6379))  

  {
  

  Redis.ConnectTimeout
= 2000;  

  Result
= Redis.RPush(key, Encoding.UTF8.GetBytes(value)) > 0;  

  }
  

return Result;  

  }
  

  注意这个非托管资源要手动释放
  弹出
  

public static string PopFromList(int type, string key)  {
string result = string.Empty;try  {
using (RedisClient Redis = new RedisClient(ips[type - 1], 6379))  {
  Redis.ConnectTimeout
= 2000;  result
= Encoding.UTF8.GetString(Redis.LPop(key));  }
  }
catch  {
  }
return result;  }
  

  Hash表有三个电脑,到底放到那一台,首先对key进行hash运算,取绝对值,对3取余,为0 就放到3号机器,为1放到4号机器,为2 放到5号机器
  ,如果hash表已经存在就会插入失败返回false,不存在插入成功返回true
  

public static bool InsetIntoHash(int type, string hashid, string key, string value)  {
bool result = false;try  {
using (RedisClient Redis = new RedisClient(ips[type - 1], 6379))  {
  Redis.ConnectTimeout
= 2000;  result
= Redis.SetEntryInHashIfNotExists(hashid, key, value);  }
  }
catch { }  

return result;  }
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-426101-1-1.html 上篇帖子: Spring Data操作Redis详解 下篇帖子: 使用Zabbix官方模板监控Redis运行状况
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表