{"id":171903,"date":"2020-02-15T08:41:10","date_gmt":"2020-02-15T00:41:10","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=171903"},"modified":"2020-02-11T15:30:48","modified_gmt":"2020-02-11T07:30:48","slug":"eureka-ap-nacos","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/eureka-ap-nacos.html","title":{"rendered":"\u600e\u6837\u7528Nacos\u5b9e\u73b0Raft\u7b97\u6cd5"},"content":{"rendered":"
\u5bfc\u8bfb<\/td>\n | \u4e3a\u4e86\u63d0\u9ad8\u7406\u89e3\u6027\uff0cRaft \u5c06\u4e00\u81f4\u6027\u7b97\u6cd5\u5206\u4e3a\u4e86\u51e0\u4e2a\u90e8\u5206\uff0c\u5305\u62ec\u9886\u5bfc\u9009\u53d6(leader selection)\u3001\u65e5\u5fd7\u590d\u5236(log replication)\u3001\u5b89\u5168(safety)\uff0c\u5e76\u4e14\u4f7f\u7528\u4e86\u66f4\u5f3a\u7684\u4e00\u81f4\u6027\u6765\u51cf\u5c11\u4e86\u5fc5\u987b\u9700\u8981\u8003\u8651\u7684\u72b6\u6001\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n \u5feb\u901f\u4e86\u89e3Raft\u7b97\u6cd5<\/strong><\/span><\/div>\n Raft \u9002\u7528\u4e8e\u4e00\u4e2a\u7ba1\u7406\u65e5\u5fd7\u4e00\u81f4\u6027\u7684\u534f\u8bae\uff0c\u76f8\u6bd4\u4e8e Paxos \u534f\u8bae Raft \u66f4\u6613\u4e8e\u7406\u89e3\u548c\u53bb\u5b9e\u73b0\u5b83\u3002\u4e3a\u4e86\u63d0\u9ad8\u7406\u89e3\u6027\uff0cRaft \u5c06\u4e00\u81f4\u6027\u7b97\u6cd5\u5206\u4e3a\u4e86\u51e0\u4e2a\u90e8\u5206\uff0c\u5305\u62ec\u9886\u5bfc\u9009\u53d6(leader selection)\u3001\u65e5\u5fd7\u590d\u5236(log replication)\u3001\u5b89\u5168(safety)\uff0c\u5e76\u4e14\u4f7f\u7528\u4e86\u66f4\u5f3a\u7684\u4e00\u81f4\u6027\u6765\u51cf\u5c11\u4e86\u5fc5\u987b\u9700\u8981\u8003\u8651\u7684\u72b6\u6001\u3002<\/p>\n \u76f8\u6bd4Paxos\uff0cRaft\u7b97\u6cd5\u7406\u89e3\u8d77\u6765\u66f4\u52a0\u76f4\u89c2\u3002<\/p>\n Raft\u7b97\u6cd5\u5c06Server\u5212\u5206\u4e3a3\u79cd\u72b6\u6001\uff0c\u6216\u8005\u4e5f\u53ef\u4ee5\u79f0\u4f5c\u89d2\u8272\uff1a<\/p>\n Leader\uff1a\u8d1f\u8d23Client\u4ea4\u4e92\u548clog\u590d\u5236\uff0c\u540c\u4e00\u65f6\u523b\u7cfb\u7edf\u4e2d\u6700\u591a\u5b58\u57281\u4e2a\u3002<\/p>\n Follower\uff1a\u88ab\u52a8\u54cd\u5e94\u8bf7\u6c42RPC\uff0c\u4ece\u4e0d\u4e3b\u52a8\u53d1\u8d77\u8bf7\u6c42RPC\u3002<\/p>\n Candidate\uff1a\u4e00\u79cd\u4e34\u65f6\u7684\u89d2\u8272\uff0c\u53ea\u5b58\u5728\u4e8eleader\u7684\u9009\u4e3e\u9636\u6bb5\uff0c\u67d0\u4e2a\u8282\u70b9\u60f3\u8981\u53d8\u6210leader\uff0c\u90a3\u4e48\u5c31\u53d1\u8d77\u6295\u7968\u8bf7\u6c42\uff0c\u540c\u65f6\u81ea\u5df1\u53d8\u6210candidate\u3002\u5982\u679c\u9009\u4e3e\u6210\u529f\uff0c\u5219\u53d8\u4e3acandidate\uff0c\u5426\u5219\u9000\u56de\u4e3afollower<\/p>\n \u72b6\u6001\u6216\u8005\u8bf4\u89d2\u8272\u7684\u6d41\u8f6c\u5982\u4e0b\uff1a<\/p>\n <\/p>\n \u5728Raft\u4e2d\uff0c\u95ee\u9898\u5206\u89e3\u4e3a\uff1a\u9886\u5bfc\u9009\u53d6\u3001\u65e5\u5fd7\u590d\u5236\u3001\u5b89\u5168\u548c\u6210\u5458\u53d8\u5316\u3002<\/p>\n \u590d\u5236\u72b6\u6001\u673a\u901a\u8fc7\u590d\u5236\u65e5\u5fd7\u6765\u5b9e\u73b0\uff1a<\/p>\n \u65e5\u5fd7\uff1a\u6bcf\u53f0\u673a\u5668\u4fdd\u5b58\u4e00\u4efd\u65e5\u5fd7\uff0c\u65e5\u5fd7\u6765\u81ea\u4e8e\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\uff0c\u5305\u542b\u4e00\u7cfb\u5217\u7684\u547d\u4ee4<\/p>\n \u72b6\u6001\u673a\uff1a\u72b6\u6001\u673a\u4f1a\u6309\u987a\u5e8f\u6267\u884c\u8fd9\u4e9b\u547d\u4ee4<\/p>\n \u4e00\u81f4\u6027\u6a21\u578b\uff1a\u5206\u5e03\u5f0f\u73af\u5883\u4e0b\uff0c\u4fdd\u8bc1\u591a\u673a\u7684\u65e5\u5fd7\u662f\u4e00\u81f4\u7684\uff0c\u8fd9\u6837\u56de\u653e\u5230\u72b6\u6001\u673a\u4e2d\u7684\u72b6\u6001\u662f\u4e00\u81f4\u7684<\/p>\n Raft\u7b97\u6cd5\u9009\u4e3b\u6d41\u7a0b<\/strong><\/span><\/div>\n Raft\u4e2d\u6709Term\u7684\u6982\u5ff5\uff0cTerm\u7c7b\u6bd4\u4e2d\u56fd\u5386\u53f2\u4e0a\u7684\u671d\u4ee3\u66f4\u66ff\uff0cRaft \u7b97\u6cd5\u5c06\u65f6\u95f4\u5212\u5206\u6210\u4e3a\u4efb\u610f\u4e0d\u540c\u957f\u5ea6\u7684\u4efb\u671f(term)\u3002<\/p>\n <\/p>\n \u9009\u4e3e\u6d41\u7a0b<\/strong><\/span><\/div>\n follower\u589e\u52a0\u5f53\u524d\u7684term\uff0c\u8f6c\u53d8\u4e3acandidate\u3002<\/p>\n candidate\u6295\u7968\u7ed9\u81ea\u5df1\uff0c\u5e76\u53d1\u9001RequestVote RPC\u7ed9\u96c6\u7fa4\u4e2d\u7684\u5176\u4ed6\u670d\u52a1\u5668\u3002<\/p>\n \u6536\u5230RequestVote\u7684\u670d\u52a1\u5668\uff0c\u5728\u540c\u4e00term\u4e2d\u53ea\u4f1a\u6309\u7167\u5148\u5230\u5148\u5f97\u6295\u7968\u7ed9\u81f3\u591a\u4e00\u4e2acandidate\u3002\u4e14\u53ea\u4f1a\u6295\u7968\u7ed9log\u81f3\u5c11\u548c\u81ea\u8eab\u4e00\u6837\u65b0\u7684candidate\u3002<\/p>\n Nacos\u4e2d\u7684CP\u4e00\u81f4\u6027<\/strong><\/span><\/div>\n Spring Cloud Alibaba Nacos \u5728 1.0.0 \u6b63\u5f0f\u652f\u6301 AP \u548c CP \u4e24\u79cd\u4e00\u81f4\u6027\u534f\u8bae\uff0c\u5176\u4e2d\u7684CP\u4e00\u81f4\u6027\u534f\u8bae\u5b9e\u73b0\uff0c\u662f\u57fa\u4e8e\u7b80\u5316\u7684 Raft \u7684 CP \u4e00\u81f4\u6027\u3002<\/p>\n \u5982\u4f55\u5b9e\u73b0Raft\u7b97\u6cd5<\/strong><\/span><\/div>\n Nacos server\u5728\u542f\u52a8\u65f6\uff0c\u4f1a\u901a\u8fc7RunningConfig.onApplicationEvent()\u65b9\u6cd5\u8c03\u7528RaftCore.init()\u65b9\u6cd5\u3002<\/p>\n \u542f\u52a8\u9009\u4e3e<\/strong><\/span><\/div>\n \r\npublic static void init() throws Exception { \r\n \r\n Loggers.RAFT.info(\"initializing Raft sub-system\"); \r\n \r\n \/\/ \u542f\u52a8Notifier\uff0c\u8f6e\u8be2Datums\uff0c\u901a\u77e5RaftListener \r\n executor.submit(notifier); \r\n \r\n \/\/ \u83b7\u53d6Raft\u96c6\u7fa4\u8282\u70b9\uff0c\u66f4\u65b0\u5230PeerSet\u4e2d \r\n peers.add(NamingProxy.getServers()); \r\n \r\n long start = System.currentTimeMillis(); \r\n \r\n \/\/ \u4ece\u78c1\u76d8\u52a0\u8f7dDatum\u548cterm\u6570\u636e\u8fdb\u884c\u6570\u636e\u6062\u590d \r\n RaftStore.load(); \r\n \r\n Loggers.RAFT.info(\"cache loaded, peer count: {}, datum count: {}, current term: {}\", \r\n peers.size(), datums.size(), peers.getTerm()); \r\n \r\n while (true) { \r\n if (notifier.tasks.size() <= 0) { \r\n break; \r\n } \r\n Thread.sleep(1000L); \r\n System.out.println(notifier.tasks.size()); \r\n } \r\n \r\n Loggers.RAFT.info(\"finish to load data from disk, cost: {} ms.\", (System.currentTimeMillis() - start)); \r\n \r\n GlobalExecutor.register(new MasterElection()); \/\/ Leader\u9009\u4e3e \r\n GlobalExecutor.register1(new HeartBeat()); \/\/ Raft\u5fc3\u8df3 \r\n GlobalExecutor.register(new AddressServerUpdater(), GlobalExecutor.ADDRESS_SERVER_UPDATE_INTERVAL_MS); \r\n \r\n if (peers.size() > 0) { \r\n if (lock.tryLock(INIT_LOCK_TIME_SECONDS, TimeUnit.SECONDS)) { \r\n initialized = true; \r\n lock.unlock(); \r\n } \r\n } else { \r\n throw new Exception(\"peers is empty.\"); \r\n } \r\n \r\n Loggers.RAFT.info(\"timer started: leader timeout ms: {}, heart-beat timeout ms: {}\", \r\n GlobalExecutor.LEADER_TIMEOUT_MS, GlobalExecutor.HEARTBEAT_INTERVAL_MS); \r\n} \r\n<\/pre>\n |