作者:拖拉机
n
作者:じ☆ve宝贝
如图,新建的maven项目不能新建package  这是因为Java是普通的文件夹,要设置为 
作者:微信小助手
<h2><strong><span style="font-size: 14px;letter-spacing: 1px;">系统架构中,为什么会存在单点?</span></strong></h2> <p><span style="font-size: 14px;letter-spacing: 1px;">(1)存在</span><span style="font-size: 14px;letter-spacing: 1px;color: rgb(255, 76, 0);">设计缺陷</span><span style="font-size: 14px;letter-spacing: 1px;">,出现了单点;</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">(2)能大大简化系统设计,</span><span style="font-size: 14px;letter-spacing: 1px;color: rgb(255, 76, 0);">有意为之</span><span style="font-size: 14px;letter-spacing: 1px;"><span style="font-size: 14px;letter-spacing: 1px;">,设置单点</span>;</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;"> </span></p> <p><strong><span style="font-size: 14px;letter-spacing: 1px;">典型互联网高可用架构,哪些地方可能存在潜在单点?</span></strong></p> <p><img data-ratio="0.6257928118393234" data-s="300,640" src="/upload/686b9cef6ad8ab7190e02dfb17718d4f.png" data-type="png" data-w="473"><br><span style="font-size: 14px;letter-spacing: 1px;">典型互联网高可用架构:</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">(1)<strong>端</strong>,通过DNS,由域名拿到nginx的外网IP;</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">(2)<strong>反向代理</strong>,</span><span style="font-size: 14px;letter-spacing: 1px;color: rgb(255, 76, 0);">nginx</span><span style="font-size: 14px;letter-spacing: 1px;">是后端入口;</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">(3)<strong>站点应用</strong>,典型的是tomcat或者apache;</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">(4)<strong>服务</strong>,典型的是dubbo提供RPC服务调用;</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">(5)<strong>数据层</strong>,典型的是</span><span style="font-size: 14px;letter-spacing: 1px;color: rgb(255, 76, 0);">读写分离</span><span style="font-size: 14px;letter-spacing: 1px;">的db架构;</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;"><br></span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">在这个互联网架构中,<strong>站点、服务、数据库的从库</strong>都容易通过</span><span style="font-size: 14px;letter-spacing: 1px;color: rgb(255, 76, 0);">冗余</span><span style="font-size: 14px;letter-spacing: 1px;">的方式来保证高可用,但:</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">(1)</span><span style="font-size: 14px;letter-spacing: 1px;color: rgb(255, 76, 0);">nginx</span><span style="font-size: 14px;letter-spacing: 1px;">是一个潜在的单点;</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">(2)数据库</span><span style="font-size: 14px;letter-spacing: 1px;color: rgb(255, 76, 0);">写库</span><span style="font-size: 14px;letter-spacing: 1px;">也是一个潜在的单点;</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;"> </span></p> <p><strong><span style="font-size: 14px;letter-spacing: 1px;">哪些例子,因为设计需要,有意设置的单点?</span></strong></p> <p><span style="font-size: 14px;letter-spacing: 1px;">先看GFS</span><span style="letter-spacing: 1px;font-size: 12px;">(Google File System)</span><span style="font-size: 14px;letter-spacing: 1px;">架构的例子:</span></p> <p><img data-ratio="0.36235955056179775" data-s="300,640" src="/upload/9f1a43be824c19b9d4319df91cca5bbf.png" data-type="png" data-w="356"><br><span style="font-size: 14px;letter-spacing: 1px;">GFS的系统架构里主要有这么几种角色:</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">(1)client,就是发起文件读写的调用端;</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">(2)master,这是一个单点服务,它有全局视野,掌握文件元信息;</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">(3)chunk-server,实际存储文件的服务器;</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;"><br></span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">在GFS系统里,master是一个单点服务。</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;"><br></span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">Map-reduce系统里也有类似的角色,协调<span style="font-size: 14px;letter-spacing: 1px;">全局</span>的master就是单点,它的存在,能够</span><span style="font-size: 14px;letter-spacing: 1px;color: rgb(255, 76, 0);">大大的简化系统架构设计</span><span style="font-size: 14px;letter-spacing: 1px;">。</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;"> </span></p> <p><strong><span style="font-size: 14px;letter-spacing: 1px;">不管是设计缺陷,还是有意为之,像nginx,db-master,GFS-master这样的单点服务,会存在什么问题呢?</span></strong></p> <p><span style="font-size: 14px;letter-spacing: 1px;">两个大问题:</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">(1)</span><span style="font-size: 14px;letter-spacing: 1px;color: rgb(255, 76, 0);">高可用问题</span><span style="font-size: 14px;letter-spacing: 1px;">:单点一旦发生故障,服务就会受到影响;</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">(2)</span><span style="font-size: 14px;letter-spacing: 1px;color: rgb(255, 76, 0);">性能瓶颈</span><span style="font-size: 14px;letter-spacing: 1px;">:单点不具备良好的扩展性,</span><span style="font-size: 14px;letter-spacing: 1px;color: rgb(255, 76, 0);">单点的性能上限往往就是整个系统的性能上限</span><span style="font-size: 14px;letter-spacing: 1px;">;</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;"> </span></p> <p><strong><span style="font-size: 14px;letter-spacing: 1px;">“高可用”问题通常怎么优化?</span></strong></p> <p><span style="font-size: 14px;letter-spacing: 1px;">shadow-master是一种很常见的解决单点高可用问题的技术方案。</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;"><br></span></p> <p><span style="font-size: 14px;letter-spacing: 1px;"><span style="font-size: 14px;letter-spacing: 1px;">shadow-master</span>,顾名思义,它只是单点master的一个shadow(影子):</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">(1)master工作时,shadow-master只备份;</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">(2)master出现故障时,shadow-master会自动变成master,继续提供服务;</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;"><br></span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">shadow-master它能够解决高可用的问题,并且故障的转移是自动的,不需要人工介入,但不足是它使</span><span style="font-size: 14px;letter-spacing: 1px;color: rgb(255, 76, 0);">资源的利用率降为了50%</span><span style="font-size: 14px;letter-spacing: 1px;">,业内经常使用keepalived+vip的方式实现这类单点的高可用。</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;"> </span></p> <p><img data-ratio="0.3502673796791444" data-s="300,640" src="/upload/d417f3cf07727c25df0d2533595879f9.png" data-type="png" data-w="374"><br><span style="font-size: 14px;letter-spacing: 1px;">以GFS的master为例,master正常时:</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">(1)client会连接正常的master,shadow-master不对外提供服务;</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">(2)master与shadow-master之间有一种存活探测机制;</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">(3)master与shadow-master有相同的虚IP</span><span style="font-size: 14px;letter-spacing: 1px;">;</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;"> </span></p> <p><img data-ratio="0.3582887700534759" data-s="300,640" src="/upload/659ecd0833de69c85f0603ab60e235dd.png" data-type="png" data-w="374"><br><span style="font-size: 14px;letter-spacing: 1px;">当发现master异常时:</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">shadow-master会自动顶上成为master,虚IP机制可以保证这个过程对调用方是透明的。</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;"> </span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">除了GFS与MapReduce系统中的主控master,nginx和数据库的</span><span style="font-size: 14px;letter-spacing: 1px;color: rgb(255, 76, 0);">主库</span><span style="font-size: 14px;letter-spacing: 1px;">master亦可用类似的方式来保证高可用:</span></p> <p><img data-ratio="0.37037037037037035" data-s="300,640" src="/upload/f3e7dccb4c312984bbe6dae322187a5c.png" data-type="png" data-w="351"><br><span style="font-size: 14px;letter-spacing: 1px;">(1)两个主库设置相互同步的双主模式;</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">(2)平时只有一个主库提供服务;</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">(3)异常时,虚IP漂移到另一个主库,shadow-master变成主库继续提供服务;</span></p> <p><br></p> <p><span style="font-size: 14px;letter-spacing: 1px;">关于高可用,更多详细的内容,可参考《<a href="http://mp.weixin.qq.com/s?__biz=MjM5ODYxMDA5OQ==&mid=2651959728&idx=1&sn=933227840ec8cdc35d3a33ae3fe97ec5&chksm=bd2d046c8a5a8d7a13551124af36bedf68f7a6e31f6f32828678d2adb108b86b7e08c678f22f&scene=21#wechat_redirect" target="_blank">究竟啥才是互联网架构“高可用”</a>》。</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;"> </span></p> <h2><strong><span style="font-size: 14px;letter-spacing: 1px;">“性能瓶颈”问题通常怎么优化?</span></strong></h2> <p><span style="font-size: 14px;letter-spacing: 1px;">有时候,单点设计是有意为之,此时单点的性能(例如GFS中的master)有可能成为系统的瓶颈,那么,</span><span style="font-size: 14px;letter-spacing: 1px;color: rgb(255, 76, 0);">减少与单点的交互</span><span style="font-size: 14px;letter-spacing: 1px;">,便成了存在单点的系统优化的核心方向。</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;"><br></span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">如何来减少与单点的交互,有两种常见的方法:</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">(1)</span><span style="font-size: 14px;letter-spacing: 1px;color: rgb(255, 76, 0);">批量写</span><span style="font-size: 14px;letter-spacing: 1px;">;</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">(2)</span><span style="font-size: 14px;letter-spacing: 1px;color: rgb(255, 76, 0);">客户端缓存</span><span style="font-size: 14px;letter-spacing: 1px;">;</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;"><br></span></p> <p><strong><span style="font-size: 14px;letter-spacing: 1px;">如何利用“批量写”减少与单点的交互,提升整体性能?</span></strong></p> <p><span style="font-size: 14px;letter-spacing: 1px;">举一个单点“ID生成器”的例子,很多公司会利用数据库的auto-inc-id,来作为一个严格递增的ID生成工具。</span></p> <p><img data-ratio="1.375" data-s="300,640" src="/upload/6d9c93700ca2f7e250e027d426c6703b.png" data-type="png" data-w="112"><br><span style="font-size: 14px;letter-spacing: 1px;">其交互流程是:</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">(1)调用方需要ID;</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">(2)插入记录,利用auto-inc-id来生成和返回ID;</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;"><br></span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">此时,ID<span style="font-size: 14px;letter-spacing: 1px;">生成</span>的并发上限,取决于单点数据库的插入性能上限。</span><br></p> <p><span style="font-size: 14px;letter-spacing: 1px;"><br></span></p> <p><strong><span style="font-size: 14px;letter-spacing: 1px;">如何利用“<span style="font-size: 14px;letter-spacing: 1px;">批量写”</span>提升性能呢?</span></strong></p> <p><span style="font-size: 14px;letter-spacing: 1px;"> </span><img data-ratio="1.3565217391304347" data-s="300,640" src="/upload/10727a90b6c92f0bfb96e08d4bb871d1.png" data-type="png" data-w="115"></p> <p><span style="font-size: 14px;letter-spacing: 1px;">优化如下:</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">(1)增加一个服务,每次从DB拿出100个id;</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">(2)调用方需要ID;</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">(3)服务直接返回100个id中的1个,100个分配完,再访问DB;</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;"><br></span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">这样一来,每分配100个才会写数据库一次,分配id的性能提升了100倍。</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;"> </span></p> <p><span style="font-size: 14px;letter-spacing: 1px;"><strong style="white-space: normal;"><span style="font-size: 14px;letter-spacing: 1px;">如何利用“客户端缓存”减少与单点的交互,提升整体性能?</span></strong></span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">还是举GFS文件系统的栗子。</span><br></p> <p><img data-ratio="0.28225806451612906" data-s="300,640" src="/upload/af321e718bff4aefd73ff2bc4520785d.png" data-type="png" data-w="496"><br><span style="font-size: 14px;letter-spacing: 1px;">GFS文件读取的流程如下:</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">(1)GFS的调用客户端client要访问shenjian.txt,先查询本地缓存,miss了;</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">(2)client访问master问说文件在哪里,master告诉client在chunk3上;</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">(3)client把shenjian.txt存放在chunk3上记录到本地的缓存,然后进行文件的读写操作;</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">(4)未来client要访问文件,从本地缓存中查找到对应的记录,就不用再请求master了,可以直接访问chunk-server;</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;"> </span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">这类缓存的命中非常非常高,在99%以上(因为文件的自动迁移是小概率事件),这样与master的交互次数就降低了100倍。</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;"><br></span></p> <p><strong><span style="font-size: 14px;letter-spacing: 1px;">批量写,客户端缓存,对性能的提升也有极限,单点性能优化还有没有其他方法?</span></strong></p> <p><span style="font-size: 14px;letter-spacing: 1px;">无论怎么批量写,客户端缓存,单点毕竟是单机,还是有性能上限的。</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;"><br></span></p> <p><span style="font-size: 14px;letter-spacing: 1px;color: rgb(255, 76, 0);">水平扩展</span><span style="font-size: 14px;letter-spacing: 1px;">,才能够无限的提升系统性能。</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;"><br></span></p> <p><strong><span style="font-size: 14px;letter-spacing: 1px;">以nginx为例,如何来进行水平扩展呢?</span></strong></p> <p><img data-ratio="0.25" data-s="300,640" src="/upload/644a1d266060910efda091ca3e04aa1c.png" data-type="png" data-w="404"><br><span style="font-size: 14px;letter-spacing: 1px;">第一步的DNS解析,只能返回一个nginx外网IP么?</span></p> <p><br></p> <p><img data-ratio="0.22870662460567823" data-s="300,640" src="/upload/f7657393a47bde2a3e2f1156dcff8ea0.png" data-type="png" data-w="634"><br><span style="font-size: 14px;letter-spacing: 1px;">通过</span><span style="font-size: 14px;letter-spacing: 1px;color: rgb(255, 76, 0);">DNS轮询</span><span style="font-size: 14px;letter-spacing: 1px;">,在DNS-server,一个域名可以配置多个IP,每次DNS解析请求,轮询返回不同的IP,就能实现nginx的水平扩展,扩充负载均衡层的整体性能。</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;"> </span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">数据库单点写库也是同样的道理,在数据量很大的情况下,可以通过水平拆分,来提升写入性能。</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;"> </span></p> <p><span style="font-size: 14px;letter-spacing: 1px;"><span style="font-size: 14px;letter-spacing: 1px;">关于性能扩展,更多详细的内容,可参考</span><span style="max-width: 100%;color: rgb(51, 51, 51);font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 14px;letter-spacing: 1px;text-align: justify;background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;">《</span><a href="http://mp.weixin.qq.com/s?__biz=MjM5ODYxMDA5OQ==&mid=2651959830&idx=1&sn=ce1c5a58caed227d7dfdbc16d6e1cea4&chksm=bd2d07ca8a5a8edc45cc45c4787cc72cf4c8b96fb43d2840c7ccd44978036a7d39a03dd578b5&scene=21#wechat_redirect" target="_blank" style="color: rgb(87, 107, 149);-webkit-tap-highlight-color: rgba(0, 0, 0, 0);max-width: 100%;font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 14px;letter-spacing: 1px;text-align: justify;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;">究竟啥才是互联网架构“可扩展”</a><span style="max-width: 100%;color: rgb(51, 51, 51);font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 14px;letter-spacing: 1px;text-align: justify;background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;">》</span><span style="font-size: 14px;letter-spacing: 1px;">。</span></span></p> <p><span style="font-size: 14px;letter-spacing: 1px;"> </span></p> <h2><strong><span style="font-size: 14px;letter-spacing: 1px;">总结</span></strong></h2> <p><span style="font-size: 14px;letter-spacing: 1px;">今天的内容很多,希望行文有逻辑:</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">(1)<strong>单点系统存在的问题</strong>:</span><span style="font-size: 14px;letter-spacing: 1px;color: rgb(255, 76, 0);">可用性问题</span><span style="font-size: 14px;letter-spacing: 1px;">,</span><span style="font-size: 14px;letter-spacing: 1px;color: rgb(255, 76, 0);">性能瓶颈问题</span><span style="font-size: 14px;letter-spacing: 1px;">;</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">(2)<strong>shadow-master</strong>是一种常见</span><span style="font-size: 14px;letter-spacing: 1px;color: rgb(255, 76, 0);">高可用</span><span style="font-size: 14px;letter-spacing: 1px;">方案;</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">(3)<strong>减少与单点的交互</strong>,是</span><span style="font-size: 14px;letter-spacing: 1px;color: rgb(255, 76, 0);">单点系统优化的核心方向</span><span style="font-size: 14px;letter-spacing: 1px;">,常见方法有:<strong>批量写</strong>,<strong>客户端缓存</strong>;</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;">(4)<strong>水平扩展</strong>,才能做到理论上的</span><span style="font-size: 14px;letter-spacing: 1px;color: rgb(255, 76, 0);">无限性能</span><span style="font-size: 14px;letter-spacing: 1px;">;</span></p> <p><span style="font-size: 14px;letter-spacing: 1px;"><br></span></p> <p style="max-width: 100%;min-height: 1em;color: rgb(51, 51, 51);font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 17px;letter-spacing: 0.544px;text-align: justify;white-space: normal;background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(255, 76, 0);letter-spacing: 1px;font-size: 14px;box-sizing: border-box !important;word-wrap: break-word !important;">思路</span><span style="max-width: 100%;letter-spacing: 1px;font-size: 14px;box-sizing: border-box !important;word-wrap: break-word !important;">比结论重要。</span><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"></p> <p style="max-width: 100%;box-sizing: border-box;min-height: 1em;color: rgb(51, 51, 51);font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 17px;letter-spacing: 0.544px;white-space: normal;text-align: center;background-color: rgb(255, 255, 255);line-height: normal;word-wrap: break-word !important;"><span style="max-width: 100%;box-sizing: border-box;word-wrap: break-word;letter-spacing: normal;"><strong style="max-width: 100%;box-sizing: border-box;word-wrap: break-word;"><span style="max-width: 100%;box-sizing: border-box;word-wrap: break-word;font-size: 12px;"><img width="auto" data-ratio="1" data-type="jpeg" data-w="250" data-s="300,640" class="" src="/upload/70c6338917b0840a1d6c89c07c078365.jpg" style="max-width: 677px;box-sizing: border-box;word-wrap: break-word;visibility: visible !important;width: 130px !important;"></span></strong></span></p> <p style="max-width: 100%;box-sizing: border-box;min-height: 1em;color: rgb(51, 51, 51);font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 17px;letter-spacing: 0.544px;white-space: normal;text-align: center;background-color: rgb(255, 255, 255);line-height: normal;word-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box;word-wrap: break-word;"><span style="max-width: 100%;box-sizing: border-box;word-wrap: break-word;font-size: 12px;letter-spacing: 1px;">架构师之路</span></strong><span style="max-width: 100%;box-sizing: border-box;word-wrap: break-word;font-size: 12px;letter-spacing: 1px;">-分享</span><span style="max-width: 100%;box-sizing: border-box;word-wrap: break-word;color: rgb(255, 76, 0);font-size: 12px;letter-spacing: 1px;">可落地</span><span style="max-width: 100%;box-sizing: border-box;word-wrap: break-word;font-size: 12px;letter-spacing: 1px;">的技术文章</span></p> <p style="max-width: 100%;box-sizing: border-box;min-height: 1em;color: rgb(51, 51, 51);font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 17px;letter-spacing: 0.544px;text-align: justify;white-space: normal;background-color: rgb(255, 255, 255);word-wrap: break-word !important;"><span style="max-width: 100%;box-sizing: border-box;word-wrap: break-word;font-size: 14px;letter-spacing: 1px;"><br style="max-width: 100%;box-sizing: border-box;word-wrap: break-word;"></span></p> <p style="max-width: 100%;box-sizing: border-box;min-height: 1em;color: rgb(51, 51, 51);font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 17px;letter-spacing: 0.544px;text-align: justify;white-space: normal;background-color: rgb(255, 255, 255);word-wrap: break-word !important;"><span style="max-width: 100%;box-sizing: border-box;word-wrap: break-word;font-size: 14px;letter-spacing: 1px;">推荐阅读:</span></p> <p style="max-width: 100%;box-sizing: border-box;min-height: 1em;color: rgb(51, 51, 51);font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 17px;letter-spacing: 0.544px;text-align: justify;white-space: normal;background-color: rgb(255, 255, 255);word-wrap: break-word !important;"><span style="max-width: 100%;box-sizing: border-box;word-wrap: break-word;font-size: 14px;letter-spacing: 1px;"><span style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">《</span><a href="http://mp.weixin.qq.com/s?__biz=MjM5ODYxMDA5OQ==&mid=2651960408&idx=1&sn=8f4825caf8183d248c06b87e6bf28a20&chksm=bd2d01848a5a88925c263a24c15ff9c92e17f5486ca27d65c5905590f135d6c241ffe88f214a&scene=21#wechat_redirect" target="_blank" style="color: rgb(87, 107, 149);-webkit-tap-highlight-color: rgba(0, 0, 0, 0);max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">究竟啥才是互联网架构“一致性”</a><span style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">》</span></span></p> <p style="max-width: 100%;box-sizing: border-box;min-height: 1em;color: rgb(51, 51, 51);font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 17px;letter-spacing: 0.544px;text-align: justify;white-space: normal;background-color: rgb(255, 255, 255);word-wrap: break-word !important;"><span style="max-width: 100%;box-sizing: border-box;word-wrap: break-word;font-size: 14px;letter-spacing: 1px;"><span style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">《</span><a href="http://mp.weixin.qq.com/s?__biz=MjM5ODYxMDA5OQ==&mid=2651959728&idx=1&sn=933227840ec8cdc35d3a33ae3fe97ec5&chksm=bd2d046c8a5a8d7a13551124af36bedf68f7a6e31f6f32828678d2adb108b86b7e08c678f22f&scene=21#wechat_redirect" target="_blank" style="color: rgb(87, 107, 149);-webkit-tap-highlight-color: rgba(0, 0, 0, 0);max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">究竟啥才是互联网架构“高可用”</a><span style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">》</span></span></span></p> <p style="max-width: 100%;box-sizing: border-box;min-height: 1em;color: rgb(51, 51, 51);font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 17px;letter-spacing: 0.544px;text-align: justify;white-space: normal;background-color: rgb(255, 255, 255);word-wrap: break-word !important;"><span style="max-width: 100%;box-sizing: border-box;word-wrap: break-word;font-size: 14px;letter-spacing: 1px;"><span style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">《</span><a href="http://mp.weixin.qq.com/s?__biz=MjM5ODYxMDA5OQ==&mid=2651959830&idx=1&sn=ce1c5a58caed227d7dfdbc16d6e1cea4&chksm=bd2d07ca8a5a8edc45cc45c4787cc72cf4c8b96fb43d2840c7ccd44978036a7d39a03dd578b5&scene=21#wechat_redirect" target="_blank" style="color: rgb(87, 107, 149);-webkit-tap-highlight-color: rgba(0, 0, 0, 0);max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">究竟啥才是互联网架构“可扩展”</a><span style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">》</span></span></span></span></p>
作者:じ☆ve宝贝
# Redis 安装步骤 #### 准备工作 (1)确保系统安装zlib,否则gem install会报(no such file to load -- zlib) 下载:zlib-1.2.6.tar ./configure make make install (2)安装ruby1.9.2 cd /文件夹路径/ruby ./configure //指定编译后的路径 make make install (3)安装 rubygems-1.8.16.tgz cd /文件夹路径/rubygems sudo ruby setup.rb (4)安装redis-3.0.0.gem #由于源的原因,可能下载失败,就手动下载下来安装 #download地址:http://rubygems.org/gems/redis/versions/3.0.0 gem install -l /文件路径/redis-3.0.0.gem #### 正式安装 (1)下载官方提供的压缩包。(注意:一定要在linux解压,否则出错) (2)使用make编译.c文件 (32位系统 查看方法getconf LONG_BIT 使用 make CFLAGS="-march=i686") 使用 make install 可以将 redis-server redis-cli redis-benchmark redis-check-dump redis-check-aof 安装在 usr/local/bin下 直接使用 (3)把redis.conf中的配置修改一下 daemonize yes #是否以后台daemon方式运行 pidfile /usr/local/redis/var/redis.pid #pid文件位置 port 6379 #监听的端口号 timeout 300 #请求超时时间 loglevel debug #log信息级别 logfile /usr/local/redis/var/redis.log #log文件位置 databases 16 #开启数据库的数量 save 900 1 #保存快照的频率,第一个*表示多长时间,第二个*表示执行多少次写操作。在一定时间内执行一定数量的写操作时,自动保存快照。可设置多个条件。 save 300 10 # 在5分钟内 执行写的操作超过10次 自动保存快照 save 60 10000 # rdbcompression yes # 是否使用压缩 dbfilename dump.rdb #数据快照文件名(只是文件名,不包括目录) dir /usr/local/redis/var/ #数据快照的保存目录(这个是目录) appendonly no #是否开启appendonlylog,开启的话每次写操作会记一条log,这会提高数据抗风险能力,但影响效率。 appendfsync always #appendonlylog如何同步到磁盘(三个选项,分别是每次写都强制调用fsync、每秒启用一次fsync、不调用fsync等待系统自己同步) glueoutputbuf yes # shareobjects no # shareobjectspoolsize 1024 # ####启动 Redis服务 1. src/redis-server 启动redis服务 2. 存值redis-cli set mykey somevalue 3. 取值redis-cli get mykey 4. 关闭服务redis-cli shutdown redis-cli -p 7002 shutdown 关闭端口号为7002的redis服务端 redis-cli -p 7002 debug segfault 让这个节点崩溃 (如果没有提示的话 加上./就行了 例如:./redis-cli shutdown ./意思是:当前文件夹) ####Windows版 Redis服务 启动 redis-server.exe redis.window.conf //会按照你redis.window.conf 配置启动 使用 redis-cli.exe -h 192.168.79.110 -p 6379 //启动windos版的客户端 ####Redis主从配置 //在丛机位置配置 打开redis.conf 添加上如下的内容 OK slaveof 192.168.223.153 6379 # slaveof(奴隶) master(主人)的ip master的端口 ####Redis集群配置 要让集群正常运作至少需要三个主节点和三个从节点 #####1.打开redis.conf 修改如下的内容 OK(最节俭的配置) ``` port 7000 #端口号 cluster-enabled yes #打开实例的集群模式 cluster-config-file nodes-"名字不要重复".conf #设定了保存节点配置文件的路径, 默认值为 nodes.conf (节点配置文件无须人为修改, 它由 Redis 集群在启动时创建, 并在有需要时自动进行更新。) cluster-node-timeout 5000 #连接超时 appendonly yes #是否开启appendonlylog,开启的话每次写操作会记一条log,这会提高数据抗风险能力,但影响效率。 ``` #####2.安装必要的插件 一、zlib安装zlib-1.2.6.tar.gz ./configure make make install 二、 Ruby 安装ruby-1.8.7-p174.tar.gz 1)得到Ruby安装包 wget ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.7-p174.tar.gz 安装的1.8.7版本 2) 解压安装 tar -xzvf ruby-1.8.7-p174.tar.gz cd ruby-1.8.7-p174 ./configure make make install 通过以上步骤就已经安装好了ruby 3) 验证 ruby –v 输出:ruby 1.8.7 (2009-06-12 patchlevel 174) [i686-linux] 这样表示ruby 安装成功了 三、 RubyGems 安装ruby安装成共rubygems-1.8.5.tgz 1) 得到rubygems包 wget http://rubyforge.org/frs/download.php/73882/rubygems-1.4.2.tgz 2) 安装 tar –xzvf rubygems-1.4.2.tgz cd rubygems-1.4.2 ruby setup.rb 3) 验证安装成功: gem –v 输出:1.4.2 那么就rubygems就安装成功了 三、redis的第三方接口!既然ruby程序要访问redis数据库,总得有个连接接口。 1) gem install redis 到此位置 ok #####3.启动集群程序 安装好的软件在 /usr/local/bin下 安装完成后使用redis-trib.rb来创建集群 1)启动各个节点 进入redis安装的文件 使用 src/redis-server redis.conf 来启动redis (注意:不能直接在该终端继续开启,一个终端在没有关闭重新打开的情况下只能开启一个redis,建议不要关闭重新打开一个终端) 2)创建集群 redis-trib.rb create --replicas 1 192.168.223.153:7000 192.168.223.153:7001 192.168.223.153:7002 192.168.223.153:7003 192.168.223.153:7004 192.168.223.153:7005 redis-trib.rb 程序的命令是 create , 这表示我们希望创建一个新的集群。 选项 --replicas 1 表示我们希望为集群中的每个主节点创建一个从节点。 之后跟着的其他参数则是实例的地址列表, 我们希望程序使用这些地址所指示的实例来创建新集群。 3)启动集群 redis-cli -c -p 7000 4)检查集群状态 redis-trib.rb check 127.0.0.1:7001 5)添加新master(主人)节点 a)首先启动redis端口及配置文件src/redis-server redis.conf b)添加新的节点redis-trib.rb add-node 127.0.0.1:7006 127.0.0.1:7000 第一个ip是新增加的 第二个是已经存在的 (注意:官方add-node没有横线,可能版本不同,大家可以 redis-trib.rb help 来了解里面的命令) c)为新增加节点添加槽数: redis-trib.rb reshard 127.0.0.1:7006 #根据提示选择要迁移的slot(槽)数量(ps:这里选择500) How many slots do you want to move (from 1 to 16384)? 500 #选择要接受这些slot的node-id What is the receiving node ID? f51e26b5d5ff74f85341f06f28f125b7254e61bf (7006端口的nodeid) #选择slot来源: #all表示从所有的master重新分配, #或者数据要提取slot的master节点id,最后用done结束 Please enter all the source node IDs. Type 'all' to use all the nodes as source nodes for the hash slots. Type 'done' once you entered all the source nodes IDs. Source node #1:all #打印被移动的slot后,输入yes开始移动slot以及对应的数据. #Do you want to proceed with the proposed reshard plan (yes/no)? yes #结束 (哈希槽:一个 Redis 集群包含 16384 个哈希槽(hash slot), 数据库中的每个键都属于这 16384 个哈希槽的其中一个) 6)添加slave(奴隶)节点 a)b)和上面相同 c) 输入redis-cli -p 7006 连接上新节点shell ,输入命令:cluster replicate 对应master的node-id 如果我们打算让新节点(7006)成为 127.0.0.1:7005 的从节点, 那么我们只要用客户端连接上新节点 cluster replicate 2b9ebcbd627ff0fd7a7bbcc5332fb09e72788835 (主节点 127.0.0.1:7005 的节点 ID ) 注意: note:在线添加slave 时,需要dump(将整个储存装置记录在另一储存装置中)整个master进程,并传递到slave,再由 slave加载rdb文件到内存,rdb传输过程中Master可能无法提供服务,整个过程消耗大量io,小心操作 7)对负载均衡的处理 对于负载/数据不均匀的情况,可以在线reshard slot来解决,方法与添加新master的reshard一样,只是需要reshard的master节点是老节点 8)删除一个slave(奴隶)节点 #redis-trib del-node ip地址:端口号 '<节点的id(启动服务的时候在图的上面 I'f51e26b5d5ff74f85341f06f28f125b7254e61bf>' redis-trib.rb del-node 127.0.0.1:7006 'f51e26b5d5ff74f85341f06f28f125b7254e61bf' 9)删除一个master(主人)节点 a)删除master节点之前首先要使用reshard移除master的全部slot(哈希槽的数据),然后再删除当前节点(目前只能把被删除 master的slot迁移到一个节点上) b)为删除的节点移动槽数: #把127.0.0.1:7006当前master迁移到127.0.0.1:7000上 redis-trib.rb reshard 127.0.0.1:7000 #根据提示选择要迁移的slot数量(ps:这里选择500) How many slots do you want to move (from 1 to 16384)? 500(被删除master的所有slot数量) #选择要接受这些slot的node-id(10.10.34.14:6380) What is the receiving node ID? c4a31c852f81686f6ed8bcd6d1b13accdc947fd2 #(ps:127.0.0.1:7000的node-id) Please enter all the source node IDs. Type 'all' to use all the nodes as source nodes for the hash slots. Type 'done' once you entered all the source nodes IDs. Source node #1:f51e26b5d5ff74f85341f06f28f125b7254e61bf#(被删除master的node-id,也就是7006端口号的nodeid) Source node #2:done #打印被移动的slot后,输入yes开始移动slot以及对应的数据. #Do you want to proceed with the proposed reshard plan (yes/no)? yes c)删除该空节点 redis-trib.rb del-node 127.0.0.1:7006 'f51e26b5d5ff74f85341f06f28f125b7254e61bf' 10)获得节点的运行 ID redis-cli -p 7001 cluster nodes | grep myself #查看自己的id redis-cli -p 7001 cluster nodes | grep master #查看所有的主节点id 测试 redis-cli -h 192.168.223.153 -p 7000//进入 端口号为的7000节点 redis-cli -c -p 7000 //进入集群 错误总结 1. 问题: [ERR]Node 127.0.0.1:7006 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0. 描述: ``` 删除了一个节点,我再一次添加,他会提示错误! ``` 解决: cluster-config-file nodes-"名字不要重复".conf #设定了保存节点配置文件的路径)把这个路径里node-xxx.conf删除掉就行 如果还是报错换个生成后的路径/usr/xx/nodes-xx.conf 或者删除该redis文件夹 重新生成! 2. 问题: ERROR: Invalid configuration for cluster creation. Redis Cluster requires at least 3 master nodes. This is not possible with 4 nodes and 1 replicas per node. At least 6 nodes are required. 描述: redis集群启动个数不够 解决: Redis官说,至少3+3 3. 问题 Either the node already knows other nodes 描述:由于单机测试,产生垃圾数据 解决: db和log下能清空的都清空了。 linux centOS6.5不支持中文解决? 1)# vi /etc/sysconfig/i18n 修改LANG="en_US.UTF-8"为LANG="zh_CN.UTF-8"保存 2) # export LC_ALL=zh_CN.UTF-8 OK 重新启动电脑即可 有中文乱码问题? http://www.cnblogs.com/ylqmf/archive/2011/09/05/2167458.html
作者:じ☆ve宝贝
### 第一、安装邮件发送工具mailx 这里我选择的是mailx,Centos6默认会安装,如果没有安装的话,请使用语句进行安装。 ``` yum install -y mailx ``` ### 第二、查看是否有其他的邮件发送工具,进行关闭。 ``` service sendmail stop chkconfig sendmail off service postfix stop chkconfig postfix off ``` ### 第三、配置Zabbix服务端外部邮箱 打开文件vim /etc/mail.rc 如果没有的话,有可能是 /etc/nail.rc,在最后加内容如下: ``` set sendcharsets=iso-8859-1,utf-8 #设置发送支持的编码 set from=xxx@studyjava.cn #在邮件头中可以看到的发送人 set smtp=smtp.studyjava.cn #smtp服务器 set smtp-auth-user=xxx@studyjava.cn #发送邮件的用户 set smtp-auth-password=xxx #邮件发送人密码 ``` 第四、测试发送邮件是否可用 ``` echo "测试本地发送邮件" | mailx -s "文件正文内容" zsljava@163.com ``` 发送成功 ### 第五、编辑zabbix-server配置文件,启用脚本执行路径,编写发送邮件脚本 ``` vim /etc/zabbix/zabbix_server.conf AlertScriptsPath=/usr/lib/zabbix/alertscripts/ #启用选项 ``` **编写脚本** vim /usr/lib/zabbix/alertscripts/sendmail.sh ``` #!/bin/bash export LANG=en_US.UTF-8 to=$1 subject=$2 echo "$3" > /tmp/zabbix_mailbody_$$ dos2unix /tmp/zabbix_mailbody_$$ mail -s "$subject" "$to" < /tmp/zabbix_mailbody_$$ rm -f /tmp/zabbix_mailbody_$$ ``` 上面涉及3个参数,$1:收件人邮箱 $2:邮件标题 $3:邮件正文 **给脚本设置权限** ``` chmod +x /usr/lib/zabbix/alertscripts/sendmail.sh #赋予执行权限 chown zabbix:zabbix /usr/lib/zabbix/alertscripts/sendmail.sh #属主属组为zabbix用户 service zabbix-server restart ``` **测试发送邮件的脚本** ``` /usr/lib/zabbix/alertscripts/sendmail.sh zsljava@163.com test 12346 ``` ### 第六、在zabbix的web界面上配置发送告警 1、点击配置 –> 报警媒介类型 –> 创建媒介类型  2、$1,$2,$3分别表示发送脚本中的$1,$2,$3,配置好后保存,点击更新  3、给用户添加报警媒介 点击 管理–> 用户 –> Admin用户,或者自己另创建用户  4、添加媒介  5、编辑添加,填写后点击 已启用 –> 更新  6、创建触发告警动作  填写动作选项  填写操作,步骤1-0表示无限制,步骤持续时间表示多久发生一次告警  填写恢复操作  以上操作完成后点击更新 ### 第七、测试阶段 将一台添加到 Template OS Linux 模板的服务器进行关闭客户端 ``` service zabbix-agent stop 或者 /bin/systemctl stop zabbix-agent.service ``` 登录到zabbix Web界面,看触发告警  触发成功,并显示发送告警成功,查看邮件 启动服务器zabbix客户端查看是否会发生恢复邮件 ``` service zabbix-agent start 或者 /bin/systemctl start zabbix-agent.service ``` 接收到恢复告警邮件,查看web界面是否触发问题已取消 服务器均正常。 到此,zabbix邮件告警就结束了。
作者:じ☆ve宝贝
``` Packet for query is too large (1204 > 1024). You can change this value on the server by setting the max_allowed_packet' variable. ``` mysql插入或更新字段有大数据时(大于1M),会出现该错误! mysql默认加载的数据文件不超过1M,可以通过更改mysql的配置文件my.cnf(Linux,或windows的my.ini)来更改这一默认值,从而达到插入大数据的目的。 解决方案 ``` # The MySQL server [mysqld] max_allowed_packet = 4M就可以了 ``` 在[mysqld]后加上max_allowed_packet = 4M就可以了
作者:微信小助手
<h3 style="box-sizing: border-box;margin-bottom: 1rem;color: rgb(21, 153, 87);line-height: 1.35;font-size: 20px;white-space: normal;font-family: Menlo, Monaco, "Source Code Pro", Consolas, Inconsolata, "Ubuntu Mono", "DejaVu Sans Mono", "Courier New", "Droid Sans Mono", "Hiragino Sans GB", 微软雅黑, monospace !important;"><strong><span style="font-size: 16px;color: rgb(61, 170, 214);">点击上方蓝字即可关注我,如果觉得我的文章不错的话,建议将该公众号置顶/星标(点击作者蓝字然后点击右上角),有惊喜哦!</span></strong></h3> <p><br></p> <h3 style="box-sizing: border-box;margin-bottom: 1rem;color: rgb(21, 153, 87);line-height: 1.35;font-size: 20px;white-space: normal;font-family: Menlo, Monaco, "Source Code Pro", Consolas, Inconsolata, "Ubuntu Mono", "DejaVu Sans Mono", "Courier New", "Droid Sans Mono", "Hiragino Sans GB", 微软雅黑, monospace !important;">主要内容</h3> <ol style="" class=" list-paddingleft-2"> <li><p><span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">redis 简介</span></p></li> <li><p><span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">为什么要用 redis /为什么要用缓存</span></p></li> <li><p><span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">为什么要用 redis 而不用 map/guava 做缓存?</span></p></li> <li><p><span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">redis 和 memcached 的区别</span></p></li> <li><p><span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">redis 常见数据结构以及使用场景分析</span></p></li> <li><p><span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">redis 设置过期时间</span></p></li> <li><p><span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">redis 内存淘汰机制</span></p></li> <li><p><span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">redis 持久化机制(怎么保证 redis 挂掉之后再重启数据可以进行恢复)</span></p></li> <li><p><span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">redis 事务</span></p></li> <li><p><span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">缓存雪崩和缓存穿透问题解决方案</span></p></li> <li><p><span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">如何解决 Redis 的并发竞争 Key 问题</span></p></li> <li><p><span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;">如何保证缓存与数据库双写时的数据一致性?</span></p></li> </ol> <h3 style="box-sizing: border-box;margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(21, 153, 87);line-height: 1.35;font-size: 20px;white-space: normal;font-family: Menlo, Monaco, "Source Code Pro", Consolas, Inconsolata, "Ubuntu Mono", "DejaVu Sans Mono", "Courier New", "Droid Sans Mono", "Hiragino Sans GB", 微软雅黑, monospace !important;">redis 简介</h3> <p>简单来说 redis 就是一个数据库,不过与传统数据库不同的是 redis 的数据是存在内存中的,所以存写速度非常快,因此 redis 被广泛应用于缓存方向。另外,redis 也经常用来做分布式锁。redis 提供了多种数据类型来支持不同的业务场景。除此之外,redis 支持事务 、持久化、LUA脚本、LRU驱动事件、多种集群方案。</p> <p><span style="color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;font-size: 16px;white-space: pre-line;"><span style="color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;font-size: 16px;white-space: pre-line;"><br></span></span></p> <h3 style="box-sizing: border-box;margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(21, 153, 87);line-height: 1.35;font-size: 20px;white-space: normal;font-family: Menlo, Monaco, "Source Code Pro", Consolas, Inconsolata, "Ubuntu Mono", "DejaVu Sans Mono", "Courier New", "Droid Sans Mono", "Hiragino Sans GB", 微软雅黑, monospace !important;">为什么要用 redis /为什么要用缓存</h3> <p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;font-size: 16px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">主要从“高性能”和“高并发”这两点来看待这个问题。</p> <p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;font-size: 16px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;"><strong style="box-sizing: border-box;color: rgb(0, 0, 0);">高性能:</strong></p> <p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;font-size: 16px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">假如用户第一次访问数据库中的某些数据。这个过程会比较慢,因为是从硬盘上读取的。将该用户访问的数据存在数缓存中,这样下一次再访问这些数据的时候就可以直接从缓存中获取了。操作缓存就是直接操作内存,所以速度相当快。如果数据库中的对应数据改变的之后,同步改变缓存中相应的数据即可!</p> <p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;font-size: 16px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;"><img class="" data-ratio="0.6433470507544582" src="/upload/9d23d1226203e548b0e02175bc4ffaaa.png" data-type="png" data-w="729" style="box-sizing: border-box;border-width: 2px;border-style: solid;border-color: rgb(238, 238, 238);border-radius: 6px;"></p> <p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;font-size: 16px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;"><strong style="box-sizing: border-box;color: rgb(0, 0, 0);">高并发:</strong></p> <p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;font-size: 16px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">直接操作缓存能够承受的请求是远远大于直接访问数据库的,所以我们可以考虑把数据库中的部分数据转移到缓存中去,这样用户的一部分请求会直接到缓存这里而不用经过数据库。</p> <p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;font-size: 16px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;"><img class="" data-ratio="0.8465703971119134" src="/upload/7c45f5a09ec0562e54d4cb032759add5.png" data-type="png" data-w="554" style="box-sizing: border-box;border-width: 2px;border-style: solid;border-color: rgb(238, 238, 238);border-radius: 6px;"></p> <h3 style="box-sizing: border-box;margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(21, 153, 87);line-height: 1.35;font-size: 20px;white-space: normal;font-family: Menlo, Monaco, "Source Code Pro", Consolas, Inconsolata, "Ubuntu Mono", "DejaVu Sans Mono", "Courier New", "Droid Sans Mono", "Hiragino Sans GB", 微软雅黑, monospace !important;">为什么要用 redis 而不用 map/guava 做缓存?</h3> <blockquote style="box-sizing: border-box;margin-top: 1em;margin-bottom: 1.2em;padding: 15px 15px 15px 1rem;color: rgb(129, 145, 152);border-left-width: 6px;border-left-color: rgb(220, 230, 240);font-size: 14px;line-height: 22px;background: rgb(242, 247, 251);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;white-space: normal;"> <p style="box-sizing: border-box;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);">下面的内容来自 segmentfault 一位网友的提问,地址:https://segmentfault.com/q/1010000009106416</p> </blockquote> <p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;font-size: 16px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">缓存分为本地缓存和分布式缓存。以 Java 为例,使用自带的 map 或者 guava 实现的是本地缓存,最主要的特点是轻量以及快速,生命周期随着 jvm 的销毁而结束,并且在多实例的情况下,每个实例都需要各自保存一份缓存,缓存不具有一致性。</p> <p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;font-size: 16px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">使用 redis 或 memcached 之类的称为分布式缓存,在多实例的情况下,各实例共用一份缓存数据,缓存具有一致性。缺点是需要保持 redis 或 memcached服务的高可用,整个程序架构上较为复杂。</p> <h3 style="box-sizing: border-box;margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(21, 153, 87);line-height: 1.35;font-size: 20px;white-space: normal;font-family: Menlo, Monaco, "Source Code Pro", Consolas, Inconsolata, "Ubuntu Mono", "DejaVu Sans Mono", "Courier New", "Droid Sans Mono", "Hiragino Sans GB", 微软雅黑, monospace !important;">redis 和 memcached 的区别</h3> <p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;font-size: 16px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">对于 redis 和 memcached 我总结了下面四点。现在公司一般都是用 redis 来实现缓存,而且 redis 自身也越来越强大了!</p> <ol style="" class=" list-paddingleft-2"> <li><p><span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;"><strong style="box-sizing: border-box;color: rgb(0, 0, 0);">redis支持更丰富的数据类型(支持更复杂的应用场景)</strong>:Redis不仅仅支持简单的k/v类型的数据,同时还提供list,set,zset,hash等数据结构的存储。memcache支持简单的数据类型,String。</span></p></li> <li><p><span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;"><strong style="box-sizing: border-box;color: rgb(0, 0, 0);">Redis支持数据的持久化,可以将内存中的数据保持在磁盘中,重启的时候可以再次加载进行使用,而Memecache把数据全部存在内存之中。</strong></span></p></li> <li><p><span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;"><strong style="box-sizing: border-box;color: rgb(0, 0, 0);">集群模式</strong>:memcached没有原生的集群模式,需要依靠客户端来实现往集群中分片写入数据;但是 redis 目前是原生支持 cluster 模式的.</span></p></li> <li><p><span style="box-sizing: border-box;color: rgb(74, 74, 74);line-height: 22px;font-size: 16px;"><strong style="box-sizing: border-box;color: rgb(0, 0, 0);">Memcached是多线程,非阻塞IO复用的网络模型;Redis使用单线程的多路 IO 复用模型。</strong></span></p></li> </ol> <blockquote style="box-sizing: border-box;margin-top: 1em;margin-bottom: 1.2em;padding: 15px 15px 15px 1rem;color: rgb(129, 145, 152);border-left-width: 6px;border-left-color: rgb(220, 230, 240);font-size: 14px;line-height: 22px;background: rgb(242, 247, 251);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;white-space: normal;"> <p style="box-sizing: border-box;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);">来自网络上的一张图,这里分享给大家!</p> </blockquote> <p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;font-size: 16px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;"><img class="" data-ratio="0.6086956521739131" src="/upload/d0cc3706880fa86a191297677ea4be61.jpg" data-type="jpeg" data-w="621" style="box-sizing: border-box;border-width: 2px;border-style: solid;border-color: rgb(238, 238, 238);border-radius: 6px;"></p> <h3 style="box-sizing: border-box;margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(21, 153, 87);line-height: 1.35;font-size: 20px;white-space: normal;font-family: Menlo, Monaco, "Source Code Pro", Consolas, Inconsolata, "Ubuntu Mono", "DejaVu Sans Mono", "Courier New", "Droid Sans Mono", "Hiragino Sans GB", 微软雅黑, monospace !important;">redis 常见数据结构以及使用场景分析</h3> <h4 style="box-sizing: border-box;margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(21, 153, 87);line-height: 1.35;font-size: 18px;white-space: normal;font-family: Menlo, Monaco, "Source Code Pro", Consolas, Inconsolata, "Ubuntu Mono", "DejaVu Sans Mono", "Courier New", "Droid Sans Mono", "Hiragino Sans GB", 微软雅黑, monospace !important;">1. String</h4> <blockquote style="box-sizing: border-box;margin-top: 1em;margin-bottom: 1.2em;padding: 15px 15px 15px 1rem;color: rgb(129, 145, 152);border-left-width: 6px;border-left-color: rgb(220, 230, 240);font-size: 14px;line-height: 22px;background: rgb(242, 247, 251);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;white-space: normal;"> <p style="box-sizing: border-box;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);"><strong style="box-sizing: border-box;color: rgb(0, 0, 0);">常用命令:</strong> set,get,decr,incr,mget 等。</p> </blockquote> <p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;font-size: 16px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">String数据结构是简单的key-value类型,value其实不仅可以是String,也可以是数字。 常规key-value缓存应用; 常规计数:微博数,粉丝数等。</p> <h4 style="box-sizing: border-box;margin-top: 1.5rem;margin-bottom: 1rem;color: rgb(21, 153, 87);line-height: 1.35;font-size: 18px;white-space: normal;font-family: Menlo, Monaco, "Source Code Pro", Consolas, Inconsolata, "Ubuntu Mono", "DejaVu Sans Mono", "Courier New", "Droid Sans Mono", "Hiragino Sans GB", 微软雅黑, monospace !important;">2.Hash</h4> <blockquote style="box-sizing: border-box;margin-top: 1em;margin-bottom: 1.2em;padding: 15px 15px 15px 1rem;color: rgb(129, 145, 152);border-left-width: 6px;border-left-color: rgb(220, 230, 240);font-size: 14px;line-height: 22px;background: rgb(242, 247, 251);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;white-space: normal;"> <p style="box-sizing: border-box;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);"><strong style="box-sizing: border-box;color: rgb(0, 0, 0);">常用命令:</strong> hget,hset,hgetall 等。</p> </blockquote> <p style="box-sizing: border-box;margin-top: 15px;margin-bottom: 15px;font-size: 16px;white-space: pre-line;line-height: 30px;color: rgb(74, 74, 74);font-family: Avenir, -apple-system-font, 微软雅黑, sans-serif;">Hash 是一个 string 类型的 field 和 value 的映射表,hash 特别适合用于存储对象,后续操作的时候,你可以直接仅仅修改这个对象中的某个字段的值。 比如我们可以Hash数据结构来存储用户信息,商品信息等等。比如下面我就用 hash 类型
作者:じ☆ve宝贝
将字符串中的.(点号)全部去掉,在java中算是很简单的一个操作的,用replaceall可以解决。就随手写了个String mt=str.replaceAll(".","");结果到测试时候才发现得到的结果是个空的字符串 原因:因为.也算是正则的一个元素所以要先转义一下 改成 ` String mt=str.replaceAll("\\.",""); ` 好了,大功告成了
作者:微信小助手
<p>本文转载自公众号 打杂的ZRJ</p> <h2 style="margin-top: 1.6em;margin-bottom: 1.6em;font-weight: bold;font-size: 1.4em;color: inherit;font-variant-numeric: normal;font-variant-east-asian: normal;white-space: normal;max-width: 100%;font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 2px;text-align: justify;widows: 1;word-spacing: 2px;line-height: inherit;background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;font-size: inherit;color: inherit;line-height: inherit;box-sizing: border-box !important;word-wrap: break-word !important;">引言</span></h2> <h3 style="margin-top: 1.6em;margin-bottom: 1.6em;font-weight: bold;font-size: 1.3em;color: inherit;font-variant-numeric: normal;font-variant-east-asian: normal;white-space: normal;max-width: 100%;font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 2px;text-align: justify;widows: 1;word-spacing: 2px;line-height: inherit;border-bottom: 2px solid rgb(239, 112, 96);background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;"><span style="margin-right: 3px;padding: 3px 10px 1px;max-width: 100%;font-size: inherit;line-height: inherit;display: inline-block;font-weight: normal;color: rgb(255, 255, 255);border-top-right-radius: 3px;border-top-left-radius: 3px;background: rgb(239, 112, 96);box-sizing: border-box !important;word-wrap: break-word !important;">为什么写这篇文章?</span><span style="max-width: 100%;display: inline-block;vertical-align: bottom;border-bottom: 36px solid rgb(239, 235, 233);border-right: 20px solid transparent;box-sizing: border-box !important;word-wrap: break-word !important;"></span></h3> <p style="margin-top: 1.7em;margin-bottom: 1.7em;color: inherit;font-size: inherit;font-variant-numeric: normal;font-variant-east-asian: normal;white-space: normal;max-width: 100%;min-height: 1em;font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 2px;line-height: inherit;text-align: justify;widows: 1;word-spacing: 2px;background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;">大家当年在学MySQL的时候,为了能够迅速就业,一般是学习一下MySQL的基本语法,差不多就出山找工作了。水平稍微好一点的童鞋呢还会懂一点存储过程的编写,又或者是懂一点索引的创建和使用。但是呢,基本上大家都忽略了对底层知识的学习。为什么呢?因为工作中很少用到嘛。然后呢,市面上流传的大部分这种底层的知识,又比较偏运维,研发懂这么多意义也不是太大,很多知识可能这辈子都不会用到。<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">因此,我整理了一部分相关的知识,希望大家有所收获。</p> <h3 style="margin-top: 1.6em;margin-bottom: 1.6em;font-weight: bold;font-size: 1.3em;color: inherit;font-variant-numeric: normal;font-variant-east-asian: normal;white-space: normal;max-width: 100%;font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 2px;text-align: justify;widows: 1;word-spacing: 2px;line-height: inherit;border-bottom: 2px solid rgb(239, 112, 96);background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;"><span style="margin-right: 3px;padding: 3px 10px 1px;max-width: 100%;font-size: inherit;line-height: inherit;display: inline-block;font-weight: normal;color: rgb(255, 255, 255);border-top-right-radius: 3px;border-top-left-radius: 3px;background: rgb(239, 112, 96);box-sizing: border-box !important;word-wrap: break-word !important;">研发究竟要懂哪些?</span><span style="max-width: 100%;display: inline-block;vertical-align: bottom;border-bottom: 36px solid rgb(239, 235, 233);border-right: 20px solid transparent;box-sizing: border-box !important;word-wrap: break-word !important;"></span></h3> <p style="margin-top: 1.7em;margin-bottom: 1.7em;color: inherit;font-size: inherit;font-variant-numeric: normal;font-variant-east-asian: normal;white-space: normal;max-width: 100%;min-height: 1em;font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 2px;line-height: inherit;text-align: justify;widows: 1;word-spacing: 2px;background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;">主要分为两个部分</p> <ul class=" list-paddingleft-2" style=""> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;word-wrap: break-word !important;"><code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;max-width: 100%;font-size: inherit;line-height: inherit;overflow-wrap: break-word;border-radius: 4px;color: rgb(248, 35, 117);background: rgb(248, 248, 248);box-sizing: border-box !important;word-wrap: break-word !important;">binlog</code>的相关概念</p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;word-wrap: break-word !important;">怎么解析<code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;max-width: 100%;font-size: inherit;line-height: inherit;overflow-wrap: break-word;border-radius: 4px;color: rgb(248, 35, 117);background: rgb(248, 248, 248);box-sizing: border-box !important;word-wrap: break-word !important;">binlog</code></p></li> </ul> <p style="margin-top: 1.7em;margin-bottom: 1.7em;color: inherit;font-size: inherit;font-variant-numeric: normal;font-variant-east-asian: normal;white-space: normal;max-width: 100%;min-height: 1em;font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 2px;line-height: inherit;text-align: justify;widows: 1;word-spacing: 2px;background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;">计划分上下两个部分来叙述。上部分讲述<strong style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(233, 105, 0);box-sizing: border-box !important;word-wrap: break-word !important;">binlog的相关概念</strong>这部分的知识,我们不需要像运维懂的那么深,我会列举一些常见概念和常见配置,大家匆匆扫一眼,有个概念即可。这样大家以后和运维讨论问题的时候,也不会一脸的懵逼。正所谓</p> <blockquote style="margin-top: 1em;margin-bottom: 1em;padding: 15px 15px 15px 1rem;border-left-width: 5px;border-left-color: rgb(239, 112, 96);font-variant-numeric: normal;font-variant-east-asian: normal;white-space: normal;max-width: 100%;font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 2px;text-align: justify;widows: 1;word-spacing: 2px;line-height: inherit;font-size: 0.9em;overflow: auto;overflow-wrap: normal;word-break: normal;background: rgb(239, 235, 233);box-sizing: border-box !important;word-wrap: break-word !important;"> <p style="max-width: 100%;min-height: 1em;font-size: inherit;color: inherit;line-height: inherit;box-sizing: border-box !important;word-wrap: break-word !important;"><strong style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(233, 105, 0);box-sizing: border-box !important;word-wrap: break-word !important;">懵逼树上懵逼果,懵逼树下你和我。 </strong></p> <p style="max-width: 100%;min-height: 1em;font-size: inherit;color: inherit;line-height: inherit;box-sizing: border-box !important;word-wrap: break-word !important;"><strong style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(233, 105, 0);box-sizing: border-box !important;word-wrap: break-word !important;">懵逼树前排排坐,一人一个懵逼果。</strong></p> </blockquote> <p style="margin-top: 1.7em;margin-bottom: 1.7em;color: inherit;font-size: inherit;font-variant-numeric: normal;font-variant-east-asian: normal;white-space: normal;max-width: 100%;min-height: 1em;font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 2px;line-height: inherit;text-align: justify;widows: 1;word-spacing: 2px;background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;">博主一个人默默的把懵逼果收走独享就好,各位读者还是懂点基本概念,以后方便和运维沟通。下半部分讲<strong style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(233, 105, 0);box-sizing: border-box !important;word-wrap: break-word !important;">怎么解析binlog</strong>。</p> <p style="margin-top: 1.7em;margin-bottom: 1.7em;color: inherit;font-size: inherit;font-variant-numeric: normal;font-variant-east-asian: normal;white-space: normal;max-width: 100%;min-height: 1em;font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 2px;line-height: inherit;text-align: justify;widows: 1;word-spacing: 2px;background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;">另外,这篇文章是给研发大大看的,可能有些概念我理解的也不对,请运维大大轻喷。</p> <h2 style="margin-top: 1.6em;margin-bottom: 1.6em;font-weight: bold;font-size: 1.4em;color: inherit;font-variant-numeric: normal;font-variant-east-asian: normal;white-space: normal;max-width: 100%;font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 2px;text-align: justify;widows: 1;word-spacing: 2px;line-height: inherit;background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;font-size: inherit;color: inherit;line-height: inherit;box-sizing: border-box !important;word-wrap: break-word !important;">正文</span></h2> <p style="margin-top: 1.7em;margin-bottom: 1.7em;color: inherit;font-size: inherit;font-variant-numeric: normal;font-variant-east-asian: normal;white-space: normal;max-width: 100%;min-height: 1em;font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 2px;line-height: inherit;text-align: justify;widows: 1;word-spacing: 2px;background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;">记得我的"一个定义,两个误解,三个用途,四个常识"</p> <h3 style="margin-top: 1.6em;margin-bottom: 1.6em;font-weight: bold;font-size: 1.3em;color: inherit;font-variant-numeric: normal;font-variant-east-asian: normal;white-space: normal;max-width: 100%;font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 2px;text-align: justify;widows: 1;word-spacing: 2px;line-height: inherit;border-bottom: 2px solid rgb(239, 112, 96);background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;"><span style="margin-right: 3px;padding: 3px 10px 1px;max-width: 100%;font-size: inherit;line-height: inherit;display: inline-block;font-weight: normal;color: rgb(255, 255, 255);border-top-right-radius: 3px;border-top-left-radius: 3px;background: rgb(239, 112, 96);box-sizing: border-box !important;word-wrap: break-word !important;">一个定义</span><span style="max-width: 100%;display: inline-block;vertical-align: bottom;border-bottom: 36px solid rgb(239, 235, 233);border-right: 20px solid transparent;box-sizing: border-box !important;word-wrap: break-word !important;"></span></h3> <p style="margin-top: 1.7em;margin-bottom: 1.7em;color: inherit;font-size: inherit;font-variant-numeric: normal;font-variant-east-asian: normal;white-space: normal;max-width: 100%;min-height: 1em;font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 2px;line-height: inherit;text-align: justify;widows: 1;word-spacing: 2px;background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;">先从定义开始讲起</p> <blockquote style="margin-top: 1em;margin-bottom: 1em;padding: 15px 15px 15px 1rem;border-left-width: 5px;border-left-color: rgb(239, 112, 96);font-variant-numeric: normal;font-variant-east-asian: normal;white-space: normal;max-width: 100%;font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 2px;text-align: justify;widows: 1;word-spacing: 2px;line-height: inherit;font-size: 0.9em;overflow: auto;overflow-wrap: normal;word-break: normal;background: rgb(239, 235, 233);box-sizing: border-box !important;word-wrap: break-word !important;"> <p style="max-width: 100%;min-height: 1em;font-size: inherit;color: inherit;line-height: inherit;box-sizing: border-box !important;word-wrap: break-word !important;"><strong style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(233, 105, 0);box-sizing: border-box !important;word-wrap: break-word !important;">binlog是记录所有数据库表结构变更(例如CREATE、ALTER TABLE…)以及表数据修改(INSERT、UPDATE、DELETE…)的二进制日志。</strong></p> <p style="max-width: 100%;min-height: 1em;font-size: inherit;color: inherit;line-height: inherit;box-sizing: border-box !important;word-wrap: break-word !important;"><strong style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(233, 105, 0);box-sizing: border-box !important;word-wrap: break-word !important;"><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;"></strong></p> <p style="max-width: 100%;min-height: 1em;font-size: inherit;color: inherit;line-height: inherit;box-sizing: border-box !important;word-wrap: break-word !important;"><strong style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(233, 105, 0);box-sizing: border-box !important;word-wrap: break-word !important;">binlog不会记录SELECT和SHOW这类操作,因为这类操作对数据本身并没有修改,但你可以通过查询通用日志来查看MySQL执行过的所有语句。</strong></p> </blockquote> <p style="margin-top: 1.7em;margin-bottom: 1.7em;color: inherit;font-size: inherit;font-variant-numeric: normal;font-variant-east-asian: normal;white-space: normal;max-width: 100%;min-height: 1em;font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 2px;line-height: inherit;text-align: justify;widows: 1;word-spacing: 2px;background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;">多说一句,如果<code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;max-width: 100%;font-size: inherit;line-height: inherit;overflow-wrap: break-word;border-radius: 4px;color: rgb(248, 35, 117);background: rgb(248, 248, 248);box-sizing: border-box !important;word-wrap: break-word !important;">update</code>操作没有造成数据变化,也是会记入<code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;max-width: 100%;font-size: inherit;line-height: inherit;overflow-wrap: break-word;border-radius: 4px;color: rgb(248, 35, 117);background: rgb(248, 248, 248);box-sizing: border-box !important;word-wrap: break-word !important;">binlog</code>。</p> <h3 style="margin-top: 1.6em;margin-bottom: 1.6em;font-weight: bold;font-size: 1.3em;color: inherit;font-variant-numeric: normal;font-variant-east-asian: normal;white-space: normal;max-width: 100%;font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 2px;text-align: justify;widows: 1;word-spacing: 2px;line-height: inherit;border-bottom: 2px solid rgb(239, 112, 96);background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;"><span style="margin-right: 3px;padding: 3px 10px 1px;max-width: 100%;font-size: inherit;line-height: inherit;display: inline-block;font-weight: normal;color: rgb(255, 255, 255);border-top-right-radius: 3px;border-top-left-radius: 3px;background: rgb(239, 112, 96);box-sizing: border-box !important;word-wrap: break-word !important;">两个误解</span><span style="max-width: 100%;display: inline-block;vertical-align: bottom;border-bottom: 36px solid rgb(239, 235, 233);border-right: 20px solid transparent;box-sizing: border-box !important;word-wrap: break-word !important;"></span></h3> <p style="margin-top: 1.7em;margin-bottom: 1.7em;color: inherit;font-size: inherit;font-variant-numeric: normal;font-variant-east-asian: normal;white-space: normal;max-width: 100%;min-height: 1em;font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 2px;line-height: inherit;text-align: justify;widows: 1;word-spacing: 2px;background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;"><strong style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(233, 105, 0);box-sizing: border-box !important;word-wrap: break-word !important;">误解一:binlog只是一类记录操作内容的日志文件</strong><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;">因为<code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;max-width: 100%;font-size: inherit;line-height: inherit;overflow-wrap: break-word;border-radius: 4px;color: rgb(248, 35, 117);background: rgb(248, 248, 248);box-sizing: border-box !important;word-wrap: break-word !important;">binlog</code>称之为二进制日志,很多研发会把这个<strong style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(233, 105, 0);box-sizing: border-box !important;word-wrap: break-word !important;">二进制日志</strong>和我们平时在代码里写的<strong style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(233, 105, 0);box-sizing: border-box !important;word-wrap: break-word !important;">代码日志</strong>联系在一起。因为我们的<strong style="max-width: 100%;font-size: inherit;line-height: inherit;color: rgb(233, 105, 0);box-sizing: border-box !important;word-wrap: break-word !important;">代码日志</strong>,只有一类记录操作容的文件,并不包含索引文件。然而,这个二进制日志包括两类文件:</p> <ul class=" list-paddingleft-2" style=""> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;font-size: inherit;color: inherit;line-height: inherit;box-sizing: border-box !important;word-wrap: break-word !important;">索引文件(文件名后缀为.index)用于记录哪些日志文件正在被使用</span></p></li> <li><p style="max-width: 100%;min-height: 1em;box-sizing: border-box !important;word-wrap: break-word !important;"><span style="max-width: 100%;font-size: inherit;color: inherit;line-height: inherit;box-sizing: border-box !important;word-wrap: break-word !important;">日志文件(文件名后缀为.00000*)记录数据库所有的DDL和DML(除了数据查询语句)语句事件。</span></p></li> </ul> <p style="margin-top: 1.7em;margin-bottom: 1.7em;color: inherit;font-size: inherit;font-variant-numeric: normal;font-variant-east-asian: normal;white-space: normal;max-width: 100%;min-height: 1em;font-family: -apple-system-font, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;letter-spacing: 2px;line-height: inherit;text-align: justify;widows: 1;word-spacing: 2px;background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;">这么说可能还有一点抽象,假设文件<code style="margin-right: 2px;margin-left: 2px;padding: 2px 4px;max-width: 100%;font-size: inherit;line-height: inherit;overflow-wrap: break-word;border-radius: 4px;color: rgb(248, 35, 117);background: rgb(248, 248, 248);box-sizing: border-box !important;word-wrap: break-word !important;">my.cnf</code>中有这么三条配置</p> <pre style="margi
作者:不要哭啦
如果 Nginx 配置了空主机头,那么任意域名解析指向到服务器IP,都可以访问站点,为了防止域名解析恶意指向主机,可以将 Nginx 默认的空主机头禁止,方法是通过修改 Nginx 的主配置文件 nginx.conf ,使其主机头返回错误信息 500 1. 直接屏蔽未绑定域名的虚拟主机访问,返回500错误(这个错误信息可以自定义): ``` server { listen 80; return 500; } ``` 2. 可以做一个URL重写,把访问的流量导入到需要的网站,比如说网站的主页,配置的时候http://www.yourdomain.com替换成需要的URL即可: ``` server { listen 80 default; rewrite ^(.*) http://www.yourdomain.com permanent; } ``` 3. 禁止空主机头的同时也禁止通过IP访问,可以写成: ``` server { listen 80 default; server_name _; return 500; } ```