ããä¸ãæ¶æ¯éåæ¦è¿°
ããæ¶æ¯éåä¸é´ä»¶æ¯åå¸å¼ç³»ç»ä¸éè¦çç»ä»¶ï¼ä¸»è¦è§£å³åºç¨è¦åï¼å¼æ¥æ¶æ¯ï¼æµéåéçé®é¢ãå®ç°é«æ§è½ï¼é«å¯ç¨ï¼å¯ä¼¸ç¼©åæç»ä¸è´æ§æ¶æãæ¯å¤§ååå¸å¼ç³»ç»ä¸å¯ç¼ºå°çä¸é´ä»¶ã
ããç®åå¨ç产ç¯å¢ï¼ä½¿ç¨è¾å¤çæ¶æ¯éåæActiveMQï¼RabbitMQï¼ZeroMQï¼Kafkaï¼MetaMQï¼RocketMQçã
ããäºãæ¶æ¯éååºç¨åºæ¯
ãã以ä¸ä»ç»æ¶æ¯éåå¨å®é
åºç¨ä¸å¸¸ç¨ç使ç¨åºæ¯ãå¼æ¥å¤çï¼åºç¨è§£è¦ï¼æµéåéåæ¶æ¯é讯å个åºæ¯ã
ãã2.1å¼æ¥å¤ç
ããåºæ¯è¯´æï¼ç¨æ·æ³¨ååï¼éè¦å注åé®ä»¶å注åçä¿¡ãä¼ ç»çåæ³æ两ç§1.串è¡çæ¹å¼ï¼2.并è¡æ¹å¼ã
ããï¼1ï¼ä¸²è¡æ¹å¼ï¼å°æ³¨åä¿¡æ¯åå
¥æ°æ®åºæååï¼åé注åé®ä»¶ï¼ååé注åçä¿¡ã以ä¸ä¸ä¸ªä»»å¡å
¨é¨å®æåï¼è¿åç»å®¢æ·ç«¯ãï¼æ¶æKKQï¼466097527ï¼æ¬¢è¿å å
¥ï¼
ããï¼2ï¼å¹¶è¡æ¹å¼ï¼å°æ³¨åä¿¡æ¯åå
¥æ°æ®åºæååï¼åé注åé®ä»¶çåæ¶ï¼åé注åçä¿¡ã以ä¸ä¸ä¸ªä»»å¡å®æåï¼è¿åç»å®¢æ·ç«¯ãä¸ä¸²è¡çå·®å«æ¯ï¼å¹¶è¡çæ¹å¼å¯ä»¥æé«å¤ççæ¶é´ã
ããå设ä¸ä¸ªä¸å¡èç¹æ¯ä¸ªä½¿ç¨50毫ç§éï¼ä¸èèç½ç»çå
¶ä»å¼éï¼å串è¡æ¹å¼çæ¶é´æ¯150毫ç§ï¼å¹¶è¡çæ¶é´å¯è½æ¯100毫ç§ã
ããå 为CPUå¨åä½æ¶é´å
å¤çç请æ±æ°æ¯ä¸å®çï¼å设CPU1ç§å
ååéæ¯100次ãå串è¡æ¹å¼1ç§å
CPUå¯å¤çç请æ±éæ¯7次ï¼1000/150ï¼ã并è¡æ¹å¼å¤çç请æ±éæ¯10次ï¼1000/100ï¼ã
ããå°ç»ï¼å¦ä»¥ä¸æ¡ä¾æè¿°ï¼ä¼ ç»çæ¹å¼ç³»ç»çæ§è½ï¼å¹¶åéï¼ååéï¼ååºæ¶é´ï¼ä¼æç¶é¢ãå¦ä½è§£å³è¿ä¸ªé®é¢å¢ï¼
ããå¼å
¥æ¶æ¯éåï¼å°ä¸æ¯å¿
é¡»çä¸å¡é»è¾ï¼å¼æ¥å¤çãæ¹é åçæ¶æå¦ä¸ï¼
ããæç
§ä»¥ä¸çº¦å®ï¼ç¨æ·çååºæ¶é´ç¸å½äºæ¯æ³¨åä¿¡æ¯åå
¥æ°æ®åºçæ¶é´ï¼ä¹å°±æ¯50毫ç§ã注åé®ä»¶ï¼åéçä¿¡åå
¥æ¶æ¯éååï¼ç´æ¥è¿åï¼å æ¤åå
¥æ¶æ¯éåçé度å¾å¿«ï¼åºæ¬å¯ä»¥å¿½ç¥ï¼å æ¤ç¨æ·çååºæ¶é´å¯è½æ¯50毫ç§ãå æ¤æ¶ææ¹ååï¼ç³»ç»çååéæé«å°æ¯ç§20 QPSãæ¯ä¸²è¡æé«äº3åï¼æ¯å¹¶è¡æé«äºä¸¤åã
ãã2.2åºç¨è§£è¦
ããåºæ¯è¯´æï¼ç¨æ·ä¸ååï¼è®¢åç³»ç»éè¦éç¥åºåç³»ç»ãä¼ ç»çåæ³æ¯ï¼è®¢åç³»ç»è°ç¨åºåç³»ç»çæ¥å£ãå¦ä¸å¾ï¼
ããä¼ ç»æ¨¡å¼ç缺ç¹ï¼
ãã1ï¼ åå¦åºåç³»ç»æ æ³è®¿é®ï¼å订åååºåå°å¤±è´¥ï¼ä»è导è´è®¢å失败ï¼
ãã2ï¼ è®¢åç³»ç»ä¸åºåç³»ç»è¦åï¼
ããå¦ä½è§£å³ä»¥ä¸é®é¢å¢ï¼å¼å
¥åºç¨æ¶æ¯éååçæ¹æ¡ï¼å¦ä¸å¾ï¼
ãã订åç³»ç»ï¼ç¨æ·ä¸ååï¼è®¢åç³»ç»å®ææä¹
åå¤çï¼å°æ¶æ¯åå
¥æ¶æ¯éåï¼è¿åç¨æ·è®¢åä¸åæåã
ããåºåç³»ç»ï¼è®¢é
ä¸åçæ¶æ¯ï¼éç¨æ/æ¨çæ¹å¼ï¼è·åä¸åä¿¡æ¯ï¼åºåç³»ç»æ ¹æ®ä¸åä¿¡æ¯ï¼è¿è¡åºåæä½ã
ããåå¦ï¼å¨ä¸åæ¶åºåç³»ç»ä¸è½æ£å¸¸ä½¿ç¨ãä¹ä¸å½±åæ£å¸¸ä¸åï¼å 为ä¸ååï¼è®¢åç³»ç»åå
¥æ¶æ¯éåå°±ä¸åå
³å¿å
¶ä»çåç»æä½äºãå®ç°è®¢åç³»ç»ä¸åºåç³»ç»çåºç¨è§£è¦ã
ãã2.3æµéåé
ããæµéåéä¹æ¯æ¶æ¯éåä¸ç常ç¨åºæ¯ï¼ä¸è¬å¨ç§ææå¢æ¢æ´»å¨ä¸ä½¿ç¨å¹¿æ³ã
ããåºç¨åºæ¯ï¼ç§ææ´»å¨ï¼ä¸è¬ä¼å 为æµéè¿å¤§ï¼å¯¼è´æµéæ´å¢ï¼åºç¨ææã为解å³è¿ä¸ªé®é¢ï¼ä¸è¬éè¦å¨åºç¨å端å å
¥æ¶æ¯éåã
ããå¯ä»¥æ§å¶æ´»å¨ç人æ°ï¼
ããå¯ä»¥ç¼è§£çæ¶é´å
é«æµéåå®åºç¨ï¼
ããç¨æ·ç请æ±ï¼æå¡å¨æ¥æ¶åï¼é¦å
åå
¥æ¶æ¯éåãåå¦æ¶æ¯éåé¿åº¦è¶
è¿æ大æ°éï¼åç´æ¥æå¼ç¨æ·è¯·æ±æ跳转å°é误页é¢ï¼
ããç§æä¸å¡æ ¹æ®æ¶æ¯éåä¸ç请æ±ä¿¡æ¯ï¼åååç»å¤çã
ãã2.4æ¥å¿å¤ç
ããæ¥å¿å¤çæ¯æå°æ¶æ¯éåç¨å¨æ¥å¿å¤çä¸ï¼æ¯å¦Kafkaçåºç¨ï¼è§£å³å¤§éæ¥å¿ä¼ è¾çé®é¢ãæ¶æç®åå¦ä¸ï¼
ããæ¥å¿éé客æ·ç«¯ï¼è´è´£æ¥å¿æ°æ®ééï¼å®æ¶åååå
¥Kafkaéåï¼
ããKafkaæ¶æ¯éåï¼è´è´£æ¥å¿æ°æ®çæ¥æ¶ï¼åå¨å转åï¼
ããæ¥å¿å¤çåºç¨ï¼è®¢é
并æ¶è´¹kafkaéåä¸çæ¥å¿æ°æ®ï¼
ãã以ä¸æ¯æ°æµªkafkaæ¥å¿å¤çåºç¨æ¡ä¾ï¼
ãã(1)Kafkaï¼æ¥æ¶ç¨æ·æ¥å¿çæ¶æ¯éåã
ãã(2)Logstashï¼åæ¥å¿è§£æï¼ç»ä¸æJSONè¾åºç»Elasticsearchã
ãã(3)Elasticsearchï¼å®æ¶æ¥å¿åææå¡çæ ¸å¿ææ¯ï¼ä¸ä¸ªschemalessï¼å®æ¶çæ°æ®åå¨æå¡ï¼éè¿indexç»ç»æ°æ®ï¼å
¼å
·å¼ºå¤§çæç´¢åç»è®¡åè½ã
ãã(4)Kibanaï¼åºäºElasticsearchçæ°æ®å¯è§åç»ä»¶ï¼è¶
强çæ°æ®å¯è§åè½åæ¯ä¼å¤å
¬å¸éæ©ELK stackçéè¦åå ã
ãã2.5æ¶æ¯é讯
ããæ¶æ¯é讯æ¯æï¼æ¶æ¯éåä¸è¬é½å
ç½®äºé«æçéä¿¡æºå¶ï¼å æ¤ä¹å¯ä»¥ç¨å¨çº¯çæ¶æ¯é讯ãæ¯å¦å®ç°ç¹å¯¹ç¹æ¶æ¯éåï¼æè
è天室çã
ããç¹å¯¹ç¹é讯ï¼
ãã客æ·ç«¯Aå客æ·ç«¯B使ç¨åä¸éåï¼è¿è¡æ¶æ¯é讯ã
ããè天室é讯ï¼
ãã客æ·ç«¯Aï¼å®¢æ·ç«¯Bï¼å®¢æ·ç«¯N订é
åä¸ä¸»é¢ï¼è¿è¡æ¶æ¯åå¸åæ¥æ¶ãå®ç°ç±»ä¼¼è天室ææã
ãã以ä¸å®é
æ¯æ¶æ¯éåç两ç§æ¶æ¯æ¨¡å¼ï¼ç¹å¯¹ç¹æåå¸è®¢é
模å¼ã模å为示æå¾ï¼ä¾åèã
ããä¸ãæ¶æ¯ä¸é´ä»¶ç¤ºä¾
ãã3.1çµåç³»ç»
ããæ¶æ¯éåéç¨é«å¯ç¨ï¼å¯æä¹
åçæ¶æ¯ä¸é´ä»¶ãæ¯å¦Active MQï¼Rabbit MQï¼Rocket Mqãï¼1ï¼åºç¨å°ä¸»å¹²é»è¾å¤çå®æåï¼åå
¥æ¶æ¯éåãæ¶æ¯åéæ¯å¦æåå¯ä»¥å¼å¯æ¶æ¯ç确认模å¼ãï¼æ¶æ¯éåè¿åæ¶æ¯æ¥æ¶æåç¶æåï¼åºç¨åè¿åï¼è¿æ ·ä¿éæ¶æ¯çå®æ´æ§ï¼
ããï¼2ï¼æ©å±æµç¨ï¼åçä¿¡ï¼é
éå¤çï¼è®¢é
éåæ¶æ¯ãéç¨æ¨ææçæ¹å¼è·åæ¶æ¯å¹¶å¤çã
ããï¼3ï¼æ¶æ¯å°åºç¨è§£è¦çåæ¶ï¼å¸¦æ¥äºæ°æ®ä¸è´æ§é®é¢ï¼å¯ä»¥éç¨æç»ä¸è´æ§æ¹å¼è§£å³ãæ¯å¦ä¸»æ°æ®åå
¥æ°æ®åºï¼æ©å±åºç¨æ ¹æ®æ¶æ¯éåï¼å¹¶ç»åæ°æ®åºæ¹å¼å®ç°åºäºæ¶æ¯éåçåç»å¤çã
ãã3.2æ¥å¿æ¶éç³»ç»
ããå为Zookeeper注åä¸å¿ï¼æ¥å¿æ¶é客æ·ç«¯ï¼Kafkaé群åStormé群ï¼OtherAppï¼åé¨åç»æã
ããZookeeper注åä¸å¿ï¼æåºè´è½½åè¡¡åå°åæ¥æ¾æå¡ï¼
ããæ¥å¿æ¶é客æ·ç«¯ï¼ç¨äºééåºç¨ç³»ç»çæ¥å¿ï¼å¹¶å°æ°æ®æ¨éå°kafkaéåï¼
ããåãJMSæ¶æ¯æå¡
ãã讲æ¶æ¯éåå°±ä¸å¾ä¸æJMS ãJMSï¼Java Message Service,Javaæ¶æ¯æå¡ï¼APIæ¯ä¸ä¸ªæ¶æ¯æå¡çæ å/è§èï¼å
许åºç¨ç¨åºç»ä»¶åºäºJavaEEå¹³å°å建ãåéãæ¥æ¶å读åæ¶æ¯ãå®ä½¿åå¸å¼éä¿¡è¦å度æ´ä½ï¼æ¶æ¯æå¡æ´å å¯é 以åå¼æ¥æ§ã
ããå¨EJBæ¶æä¸ï¼ææ¶æ¯beanå¯ä»¥æ ç¼çä¸JMæ¶æ¯æå¡éæãå¨J2EEæ¶æ模å¼ä¸ï¼ææ¶æ¯æå¡è
模å¼ï¼ç¨äºå®ç°æ¶æ¯ä¸åºç¨ç´æ¥ç解è¦ã
ãã4.1æ¶æ¯æ¨¡å
ããå¨JMSæ åä¸ï¼æ两ç§æ¶æ¯æ¨¡åP2Pï¼Point to Pointï¼,Publish/Subscribe(Pub/Sub)ã
ãã4.1.1 P2P模å¼
ããP2P模å¼å
å«ä¸ä¸ªè§è²ï¼æ¶æ¯éåï¼Queueï¼ï¼åéè
(Sender)ï¼æ¥æ¶è
(Receiver)ãæ¯ä¸ªæ¶æ¯é½è¢«åéå°ä¸ä¸ªç¹å®çéåï¼æ¥æ¶è
ä»éåä¸è·åæ¶æ¯ãéåä¿ççæ¶æ¯ï¼ç´å°ä»ä»¬è¢«æ¶è´¹æè¶
æ¶ã
ããP2Pçç¹ç¹
ããæ¯ä¸ªæ¶æ¯åªæä¸ä¸ªæ¶è´¹è
ï¼Consumerï¼(å³ä¸æ¦è¢«æ¶è´¹ï¼æ¶æ¯å°±ä¸åå¨æ¶æ¯éåä¸)
ããåéè
åæ¥æ¶è
ä¹é´å¨æ¶é´ä¸æ²¡æä¾èµæ§ï¼ä¹å°±æ¯è¯´å½åéè
åéäºæ¶æ¯ä¹åï¼ä¸ç®¡æ¥æ¶è
æ没ææ£å¨è¿è¡ï¼å®ä¸ä¼å½±åå°æ¶æ¯è¢«åéå°éå
ããæ¥æ¶è
å¨æåæ¥æ¶æ¶æ¯ä¹åéåéååºçæå
ããå¦æå¸æåéçæ¯ä¸ªæ¶æ¯é½ä¼è¢«æåå¤ççè¯ï¼é£ä¹éè¦P2P模å¼ãï¼æ¶æKKQï¼466097527ï¼æ¬¢è¿å å
¥ï¼
ãã4.1.2 Pub/sub模å¼
ããå
å«ä¸ä¸ªè§è²ä¸»é¢ï¼Topicï¼ï¼åå¸è
ï¼Publisherï¼ï¼è®¢é
è
ï¼Subscriberï¼ ãå¤ä¸ªåå¸è
å°æ¶æ¯åéå°Topic,ç³»ç»å°è¿äºæ¶æ¯ä¼ éç»å¤ä¸ªè®¢é
è
ã
ããPub/Subçç¹ç¹
ããæ¯ä¸ªæ¶æ¯å¯ä»¥æå¤ä¸ªæ¶è´¹è
ããåå¸è
å订é
è
ä¹é´ææ¶é´ä¸çä¾èµæ§ãé对æ个主é¢ï¼Topicï¼ç订é
è
ï¼å®å¿
é¡»å建ä¸ä¸ªè®¢é
è
ä¹åï¼æè½æ¶è´¹åå¸è
çæ¶æ¯ã
ãã为äºæ¶è´¹æ¶æ¯ï¼è®¢é
è
å¿
é¡»ä¿æè¿è¡çç¶æã
ãã为äºç¼åè¿æ ·ä¸¥æ ¼çæ¶é´ç¸å
³æ§ï¼JMSå
许订é
è
å建ä¸ä¸ªå¯æä¹
åç订é
ãè¿æ ·ï¼å³ä½¿è®¢é
è
没æ被æ¿æ´»ï¼è¿è¡ï¼ï¼å®ä¹è½æ¥æ¶å°åå¸è
çæ¶æ¯ã
ããå¦æå¸æåéçæ¶æ¯å¯ä»¥ä¸è¢«åä»»ä½å¤çãæè
åªè¢«ä¸ä¸ªæ¶æ¯è
å¤çãæè
å¯ä»¥è¢«å¤ä¸ªæ¶è´¹è
å¤ççè¯ï¼é£ä¹å¯ä»¥éç¨Pub/Sub模åã
ãã4.2æ¶æ¯æ¶è´¹
ããå¨JMSä¸ï¼æ¶æ¯ç产çåæ¶è´¹é½æ¯å¼æ¥çã对äºæ¶è´¹æ¥è¯´ï¼JMSçæ¶æ¯è
å¯ä»¥éè¿ä¸¤ç§æ¹å¼æ¥æ¶è´¹æ¶æ¯ã
ããï¼1ï¼åæ¥
ãã订é
è
ææ¥æ¶è
éè¿receiveæ¹æ³æ¥æ¥æ¶æ¶æ¯ï¼receiveæ¹æ³å¨æ¥æ¶å°æ¶æ¯ä¹åï¼æè¶
æ¶ä¹åï¼å°ä¸ç´é»å¡ï¼
ããï¼2ï¼å¼æ¥
ãã订é
è
ææ¥æ¶è
å¯ä»¥æ³¨å为ä¸ä¸ªæ¶æ¯çå¬å¨ãå½æ¶æ¯å°è¾¾ä¹åï¼ç³»ç»èªå¨è°ç¨çå¬å¨çonMessageæ¹æ³ã
ããJNDIï¼Javaå½ååç®å½æ¥å£,æ¯ä¸ç§æ åçJavaå½åç³»ç»æ¥å£ãå¯ä»¥å¨ç½ç»ä¸æ¥æ¾å访é®æå¡ãéè¿æå®ä¸ä¸ªèµæºå称ï¼è¯¥å称对åºäºæ°æ®åºæå½åæå¡ä¸çä¸ä¸ªè®°å½ï¼åæ¶è¿åèµæºè¿æ¥å»ºç«æå¿
é¡»çä¿¡æ¯ã
ããJNDIå¨JMSä¸èµ·å°æ¥æ¾å访é®åéç®æ ææ¶æ¯æ¥æºçä½ç¨ãï¼æ¶æKKQï¼466097527ï¼æ¬¢è¿å å
¥ï¼
ãã4.3JMSç¼ç¨æ¨¡å
ãã(1) ConnectionFactory
ããå建Connection对象çå·¥åï¼é对两ç§ä¸åçjmsæ¶æ¯æ¨¡åï¼åå«æQueueConnectionFactoryåTopicConnectionFactory两ç§ãå¯ä»¥éè¿JNDIæ¥æ¥æ¾ConnectionFactory对象ã
ãã(2) Destination
ããDestinationçæææ¯æ¶æ¯ç产è
çæ¶æ¯åéç®æ æè
说æ¶æ¯æ¶è´¹è
çæ¶æ¯æ¥æºã对äºæ¶æ¯ç产è
æ¥è¯´ï¼å®çDestinationæ¯æ个éåï¼Queueï¼ææ个主é¢ï¼Topicï¼;对äºæ¶æ¯æ¶è´¹è
æ¥è¯´ï¼å®çDestinationä¹æ¯æ个éåæ主é¢ï¼å³æ¶æ¯æ¥æºï¼ã
ããæ以ï¼Destinationå®é
ä¸å°±æ¯ä¸¤ç§ç±»åç对象ï¼QueueãTopicå¯ä»¥éè¿JNDIæ¥æ¥æ¾Destinationã
ãã(3) Connection
ããConnection表示å¨å®¢æ·ç«¯åJMSç³»ç»ä¹é´å»ºç«çé¾æ¥ï¼å¯¹TCP/IP socketçå
è£
ï¼ãConnectionå¯ä»¥äº§çä¸ä¸ªæå¤ä¸ªSessionãè·ConnectionFactoryä¸æ ·ï¼Connectionä¹æ两ç§ç±»åï¼QueueConnectionåTopicConnectionã
ãã(4) Session
ããSessionæ¯æä½æ¶æ¯çæ¥å£ãå¯ä»¥éè¿sessionå建ç产è
ãæ¶è´¹è
ãæ¶æ¯çãSessionæä¾äºäºå¡çåè½ãå½éè¦ä½¿ç¨sessionåé/æ¥æ¶å¤ä¸ªæ¶æ¯æ¶ï¼å¯ä»¥å°è¿äºåé/æ¥æ¶å¨ä½æ¾å°ä¸ä¸ªäºå¡ä¸ãåæ ·ï¼ä¹åQueueSessionåTopicSessionã
ãã(5) æ¶æ¯çç产è
ããæ¶æ¯ç产è
ç±Sessionå建ï¼å¹¶ç¨äºå°æ¶æ¯åéå°Destinationãåæ ·ï¼æ¶æ¯ç产è
å两ç§ç±»åï¼QueueSenderåTopicPublisherãå¯ä»¥è°ç¨æ¶æ¯ç产è
çæ¹æ³ï¼sendæpublishæ¹æ³ï¼åéæ¶æ¯ã
ãã(6) æ¶æ¯æ¶è´¹è
ããæ¶æ¯æ¶è´¹è
ç±Sessionå建ï¼ç¨äºæ¥æ¶è¢«åéå°Destinationçæ¶æ¯ã两ç§ç±»åï¼QueueReceiveråTopicSubscriberãå¯åå«éè¿sessionçcreateReceiver(Queue)æcreateSubscriber(Topic)æ¥å建ãå½ç¶ï¼ä¹å¯ä»¥sessionçcreatDurableSubscriberæ¹æ³æ¥å建æä¹
åç订é
è
ã
ãã(7) MessageListener
ããæ¶æ¯çå¬å¨ãå¦æ注åäºæ¶æ¯çå¬å¨ï¼ä¸æ¦æ¶æ¯å°è¾¾ï¼å°èªå¨è°ç¨çå¬å¨çonMessageæ¹æ³ãEJBä¸çMDBï¼Message-Driven Beanï¼å°±æ¯ä¸ç§MessageListenerã
ããæ·±å
¥å¦ä¹ JMS对ææ¡JAVAæ¶æï¼EJBæ¶ææå¾å¥½ç帮å©ï¼æ¶æ¯ä¸é´ä»¶ä¹æ¯å¤§ååå¸å¼ç³»ç»å¿
é¡»çç»ä»¶ãæ¬æ¬¡å享主è¦åå
¨å±æ§ä»ç»ï¼å
·ä½çæ·±å
¥éè¦å¤§å®¶å¦ä¹ ï¼å®è·µï¼æ»ç»ï¼é¢ä¼ã
ããäºã常ç¨æ¶æ¯éå
ããä¸è¬åç¨ç容å¨ï¼æ¯å¦WebLogicï¼JBossï¼é½æ¯æJMSæ åï¼å¼åä¸å¾æ¹ä¾¿ãä½å
è´¹çæ¯å¦Tomcatï¼Jettyçåéè¦ä½¿ç¨ç¬¬ä¸æ¹çæ¶æ¯ä¸é´ä»¶ãæ¬é¨åå
容ä»ç»å¸¸ç¨çæ¶æ¯ä¸é´ä»¶ï¼Active MQ,Rabbit MQï¼Zero MQ,Kafkaï¼ä»¥åä»ä»¬çç¹ç¹ã
ãã5.1 ActiveMQ
ããActiveMQ æ¯Apacheåºåï¼ææµè¡çï¼è½å强å²çå¼æºæ¶æ¯æ»çº¿ãActiveMQ æ¯ä¸ä¸ªå®å
¨æ¯æJMS1.1åJ2EE 1.4è§èç JMS Providerå®ç°ï¼å°½ç®¡JMSè§èåºå°å·²ç»æ¯å¾ä¹
çäºæ
äºï¼ä½æ¯JMSå¨å½ä»çJ2EEåºç¨ä¸é´ä»ç¶æ®æ¼çç¹æ®çå°ä½ã
ããActiveMQç¹æ§å¦ä¸ï¼
ããâ å¤ç§è¯è¨ååè®®ç¼å客æ·ç«¯ãè¯è¨: Java,C,C++,C#,Ruby,Perl,Python,PHPãåºç¨åè®®ï¼ OpenWire,Stomp REST,WS Notification,XMPP,AMQP
ããâ å®å
¨æ¯æJMS1.1åJ2EE 1.4è§è ï¼æä¹
åï¼XAæ¶æ¯ï¼äºå¡)
ããâ 对springçæ¯æï¼ActiveMQå¯ä»¥å¾å®¹æå
åµå°ä½¿ç¨Springçç³»ç»éé¢å»ï¼èä¸ä¹æ¯æSpring2.0çç¹æ§
ããâ éè¿äºå¸¸è§J2EEæå¡å¨ï¼å¦ Geronimo,JBoss 4,GlassFish,WebLogic)çæµè¯ï¼å
¶ä¸éè¿JCA 1.5 resource adaptorsçé
ç½®ï¼å¯ä»¥è®©ActiveMQå¯ä»¥èªå¨çé¨ç½²å°ä»»ä½å
¼å®¹J2EE 1.4 åä¸æå¡å¨ä¸
ããâ æ¯æå¤ç§ä¼ éåè®®ï¼in-VM,TCP,SSL,NIO,UDP,JGroups,JXTA
ããâ æ¯æéè¿JDBCåjournalæä¾é«éçæ¶æ¯æä¹
å
ããâ ä»è®¾è®¡ä¸ä¿è¯äºé«æ§è½çé群ï¼å®¢æ·ç«¯-æå¡å¨ï¼ç¹å¯¹ç¹
ããâ æ¯æAjax
ããâ æ¯æä¸Axisçæ´å
ããâ å¯ä»¥å¾å®¹æå¾è°ç¨å
åµJMS providerï¼è¿è¡æµè¯
ãã5.2 RabbitMQ
ããRabbitMQæ¯æµè¡çå¼æºæ¶æ¯éåç³»ç»ï¼ç¨erlangè¯è¨å¼åãRabbitMQæ¯AMQPï¼é«çº§æ¶æ¯éååè®®ï¼çæ åå®ç°ãæ¯æå¤ç§å®¢æ·ç«¯ï¼å¦ï¼PythonãRubyã.NETãJavaãJMSãCãPHPãActionScriptãXMPPãSTOMPçï¼æ¯æAJAXï¼æä¹
åãç¨äºå¨åå¸å¼ç³»ç»ä¸åå¨è½¬åæ¶æ¯ï¼å¨æç¨æ§ãæ©å±æ§ãé«å¯ç¨æ§çæ¹é¢è¡¨ç°ä¸ä¿ã
ããå 个éè¦æ¦å¿µï¼
ããBrokerï¼ç®åæ¥è¯´å°±æ¯æ¶æ¯éåæå¡å¨å®ä½ã
ããExchangeï¼æ¶æ¯äº¤æ¢æºï¼å®æå®æ¶æ¯æä»ä¹è§åï¼è·¯ç±å°åªä¸ªéåã
ããQueueï¼æ¶æ¯éåè½½ä½ï¼æ¯ä¸ªæ¶æ¯é½ä¼è¢«æå
¥å°ä¸ä¸ªæå¤ä¸ªéåã
ããBindingï¼ç»å®ï¼å®çä½ç¨å°±æ¯æexchangeåqueueæç
§è·¯ç±è§åç»å®èµ·æ¥ã
ããRouting Keyï¼è·¯ç±å
³é®åï¼exchangeæ ¹æ®è¿ä¸ªå
³é®åè¿è¡æ¶æ¯æéã
ããvhostï¼èæ主æºï¼ä¸ä¸ªbrokeréå¯ä»¥å¼è®¾å¤ä¸ªvhostï¼ç¨ä½ä¸åç¨æ·çæéå离ã
ããproducerï¼æ¶æ¯ç产è
ï¼å°±æ¯æéæ¶æ¯çç¨åºã
ããconsumerï¼æ¶æ¯æ¶è´¹è
ï¼å°±æ¯æ¥åæ¶æ¯çç¨åºã
ããchannelï¼æ¶æ¯ééï¼å¨å®¢æ·ç«¯çæ¯ä¸ªè¿æ¥éï¼å¯å»ºç«å¤ä¸ªchannelï¼æ¯ä¸ªchannel代表ä¸ä¸ªä¼è¯ä»»å¡ã
ããæ¶æ¯éåç使ç¨è¿ç¨ï¼å¦ä¸ï¼
ããï¼1ï¼å®¢æ·ç«¯è¿æ¥å°æ¶æ¯éåæå¡å¨ï¼æå¼ä¸ä¸ªchannelã
ããï¼2ï¼å®¢æ·ç«¯å£°æä¸ä¸ªexchangeï¼å¹¶è®¾ç½®ç¸å
³å±æ§ã
ããï¼3ï¼å®¢æ·ç«¯å£°æä¸ä¸ªqueueï¼å¹¶è®¾ç½®ç¸å
³å±æ§ã
ããï¼4ï¼å®¢æ·ç«¯ä½¿ç¨routing keyï¼å¨exchangeåqueueä¹é´å»ºç«å¥½ç»å®å
³ç³»ã
ããï¼5ï¼å®¢æ·ç«¯æéæ¶æ¯å°exchangeã
ããexchangeæ¥æ¶å°æ¶æ¯åï¼å°±æ ¹æ®æ¶æ¯çkeyåå·²ç»è®¾ç½®çbindingï¼è¿è¡æ¶æ¯è·¯ç±ï¼å°æ¶æ¯æéå°ä¸ä¸ªæå¤ä¸ªéåéã
ãã5.3 ZeroMQ
ããå·ç§°å²ä¸æå¿«çæ¶æ¯éåï¼å®å®é
类似äºSocketçä¸ç³»åæ¥å£ï¼ä»è·Socketçåºå«æ¯ï¼æ®éçsocketæ¯ç«¯å°ç«¯çï¼1:1çå
³ç³»ï¼ï¼èZMQå´æ¯å¯ä»¥Nï¼M çå
³ç³»ï¼äººä»¬å¯¹BSDå¥æ¥åçäºè§£è¾å¤çæ¯ç¹å¯¹ç¹çè¿æ¥ï¼ç¹å¯¹ç¹è¿æ¥éè¦æ¾å¼å°å»ºç«è¿æ¥ãéæ¯è¿æ¥ãéæ©åè®®ï¼TCP/UDPï¼åå¤çé误çï¼èZMQå±è½äºè¿äºç»èï¼è®©ä½ çç½ç»ç¼ç¨æ´ä¸ºç®åãZMQç¨äºnodeä¸nodeé´çéä¿¡ï¼nodeå¯ä»¥æ¯ä¸»æºæè
æ¯è¿ç¨ã
ããå¼ç¨å®æ¹ç说æ³ï¼ âZMQ(以ä¸ZeroMQç®ç§°ZMQ)æ¯ä¸ä¸ªç®å好ç¨çä¼ è¾å±ï¼åæ¡æ¶ä¸æ ·çä¸ä¸ªsocket libraryï¼ä»ä½¿å¾Socketç¼ç¨æ´å ç®åãç®æ´åæ§è½æ´é«ãæ¯ä¸ä¸ªæ¶æ¯å¤çéååºï¼å¯å¨å¤ä¸ªçº¿ç¨ãå
æ ¸å主æºçä¹é´å¼¹æ§ä¼¸ç¼©ãZMQçæç¡®ç®æ æ¯âæ为æ åç½ç»åè®®æ çä¸é¨åï¼ä¹åè¿å
¥Linuxå
æ ¸âãç°å¨è¿æªçå°å®ä»¬çæåãä½æ¯ï¼å®æ çæ¯æå
·åæ¯çã并ä¸æ¯äººä»¬æ´å éè¦çâä¼ ç»âBSDå¥æ¥åä¹ä¸çä¸ å±å°è£
ãZMQ让ç¼åé«æ§è½ç½ç»åºç¨ç¨åºæ为ç®ååæ趣ãâ
ããç¹ç¹æ¯ï¼
ããé«æ§è½ï¼éæä¹
åï¼
ãã跨平å°ï¼æ¯æLinuxãWindowsãOS Xçã
ããå¤è¯è¨æ¯æï¼ CãC++ãJavaã.NETãPythonç30å¤ç§å¼åè¯è¨ã
ããå¯åç¬é¨ç½²æéæå°åºç¨ä¸ä½¿ç¨ï¼
ããå¯ä½ä¸ºSocketéä¿¡åºä½¿ç¨ã
ããä¸RabbitMQç¸æ¯ï¼ZMQ并ä¸åæ¯ä¸ä¸ªä¼ ç»æä¹ä¸çæ¶æ¯éåæå¡å¨ï¼äºå®ä¸ï¼å®ä¹æ ¹æ¬ä¸æ¯ä¸ä¸ªæå¡å¨ï¼æ´åä¸ä¸ªåºå±çç½ç»é讯åºï¼å¨Socket APIä¹ä¸åäºä¸å±å°è£
ï¼å°ç½ç»é讯ãè¿ç¨é讯å线ç¨é讯æ½è±¡ä¸ºç»ä¸çAPIæ¥å£ãæ¯æâRequest-Reply âï¼âPublisher-Subscriberâï¼âParallel Pipelineâä¸ç§åºæ¬æ¨¡ååæ©å±æ¨¡åã
ããZeroMQé«æ§è½è®¾è®¡è¦ç¹ï¼
ãã1ãæ éçéå模å
ãã对äºè·¨çº¿ç¨é´ç交äºï¼ç¨æ·ç«¯åsessionï¼ä¹é´çæ°æ®äº¤æ¢éépipeï¼éç¨æ éçéåç®æ³CASï¼å¨pipe两端注åæå¼æ¥äºä»¶ï¼å¨è¯»æè
åæ¶æ¯å°pipeçæ¶ï¼ä¼èªå¨è§¦å读åäºä»¶ã
ãã2ãæ¹éå¤ççç®æ³
ãã对äºä¼ ç»çæ¶æ¯å¤çï¼æ¯ä¸ªæ¶æ¯å¨åéåæ¥æ¶çæ¶åï¼é½éè¦ç³»ç»çè°ç¨ï¼è¿æ ·å¯¹äºå¤§éçæ¶æ¯ï¼ç³»ç»çå¼éæ¯è¾å¤§ï¼zeroMQ对äºæ¹éçæ¶æ¯ï¼è¿è¡äºéåºæ§çä¼åï¼å¯ä»¥æ¹éçæ¥æ¶ååéæ¶æ¯ã
ãã3ãå¤æ ¸ä¸ç线ç¨ç»å®ï¼æ é¡»CPUåæ¢
ããåºå«äºä¼ ç»çå¤çº¿ç¨å¹¶å模å¼ï¼ä¿¡å·éæè
临çåºï¼ zeroMQå
åå©ç¨å¤æ ¸çä¼å¿ï¼æ¯ä¸ªæ ¸ç»å®è¿è¡ä¸ä¸ªå·¥ä½è
线ç¨ï¼é¿å
å¤çº¿ç¨ä¹é´çCPUåæ¢å¼éã
ãã5.4 Kafka
ããKafkaæ¯ä¸ç§é«ååéçåå¸å¼åå¸è®¢é
æ¶æ¯ç³»ç»ï¼å®å¯ä»¥å¤çæ¶è´¹è
è§æ¨¡çç½ç«ä¸çææå¨ä½æµæ°æ®ã è¿ç§å¨ä½ï¼ç½é¡µæµè§ï¼æç´¢åå
¶ä»ç¨æ·çè¡å¨ï¼æ¯å¨ç°ä»£ç½ç»ä¸ç许å¤ç¤¾ä¼åè½çä¸ä¸ªå
³é®å ç´ ã è¿äºæ°æ®é常æ¯ç±äºååéçè¦æ±èéè¿å¤çæ¥å¿åæ¥å¿èåæ¥è§£å³ã 对äºåHadoopçä¸æ ·çæ¥å¿æ°æ®å离线åæç³»ç»ï¼ä½åè¦æ±å®æ¶å¤ççéå¶ï¼è¿æ¯ä¸ä¸ªå¯è¡ç解å³æ¹æ¡ãKafkaçç®çæ¯éè¿Hadoopç并è¡å è½½æºå¶æ¥ç»ä¸çº¿ä¸å离线çæ¶æ¯å¤çï¼ä¹æ¯ä¸ºäºéè¿é群æºæ¥æä¾å®æ¶çæ¶è´¹ã
ããKafkaæ¯ä¸ç§é«ååéçåå¸å¼åå¸è®¢é
æ¶æ¯ç³»ç»ï¼æå¦ä¸ç¹æ§ï¼
ããéè¿O(1)çç£çæ°æ®ç»ææä¾æ¶æ¯çæä¹
åï¼è¿ç§ç»æ对äºå³ä½¿æ°ä»¥TBçæ¶æ¯åå¨ä¹è½å¤ä¿æé¿æ¶é´ç稳å®æ§è½ãï¼æ件追å çæ¹å¼åå
¥æ°æ®ï¼è¿æçæ°æ®å®æå é¤ï¼
ããé«ååéï¼å³ä½¿æ¯é常æ®éç硬件Kafkaä¹å¯ä»¥æ¯ææ¯ç§æ°ç¾ä¸çæ¶æ¯ã
ããæ¯æéè¿Kafkaæå¡å¨åæ¶è´¹æºé群æ¥ååºæ¶æ¯ã
ããæ¯æHadoop并è¡æ°æ®å è½½ã
ããKafkaç¸å
³æ¦å¿µ
ããBroker
ããKafkaé群å
å«ä¸ä¸ªæå¤ä¸ªæå¡å¨ï¼è¿ç§æå¡å¨è¢«ç§°ä¸ºbroker[5]
ããTopic
ããæ¯æ¡åå¸å°Kafkaé群çæ¶æ¯é½æä¸ä¸ªç±»å«ï¼è¿ä¸ªç±»å«è¢«ç§°ä¸ºTopicãï¼ç©çä¸ä¸åTopicçæ¶æ¯åå¼åå¨ï¼é»è¾ä¸ä¸ä¸ªTopicçæ¶æ¯è½ç¶ä¿åäºä¸ä¸ªæå¤ä¸ªbrokerä¸ä½ç¨æ·åªéæå®æ¶æ¯çTopicå³å¯ç产ææ¶è´¹æ°æ®èä¸å¿
å
³å¿æ°æ®åäºä½å¤ï¼
ããPartition
ããParitionæ¯ç©çä¸çæ¦å¿µï¼æ¯ä¸ªTopicå
å«ä¸ä¸ªæå¤ä¸ªPartition.
ããProducer
ããè´è´£åå¸æ¶æ¯å°Kafka broker
ããConsumer
ããæ¶æ¯æ¶è´¹è
ï¼åKafka broker读åæ¶æ¯ç客æ·ç«¯ã
ããConsumer Group
ããæ¯ä¸ªConsumerå±äºä¸ä¸ªç¹å®çConsumer Groupï¼å¯ä¸ºæ¯ä¸ªConsumeræå®group nameï¼è¥ä¸æå®group nameåå±äºé»è®¤çgroupï¼ã
ããä¸è¬åºç¨å¨å¤§æ°æ®æ¥å¿å¤çæ对å®æ¶æ§ï¼å°é延è¿ï¼ï¼å¯é æ§ï¼å°é丢æ°æ®ï¼è¦æ±ç¨ä½çåºæ¯ä½¿ç¨ã
温馨提示:内容为网友见解,仅供参考