{"id":48680759,"url":"https://github.com/luohaha/MyPaxos","last_synced_at":"2026-04-26T20:00:45.282Z","repository":{"id":90140969,"uuid":"79129994","full_name":"luohaha/MyPaxos","owner":"luohaha","description":"My multi-paxos service implement :-)","archived":false,"fork":false,"pushed_at":"2022-08-25T02:41:39.000Z","size":90,"stargazers_count":144,"open_issues_count":5,"forks_count":45,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-05-13T05:41:10.426Z","etag":null,"topics":["multi-paxos","paxos"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/luohaha.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-01-16T15:08:40.000Z","updated_at":"2025-04-13T15:17:27.000Z","dependencies_parsed_at":null,"dependency_job_id":"366613b1-75e6-43c1-a43d-84a32a112102","html_url":"https://github.com/luohaha/MyPaxos","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/luohaha/MyPaxos","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luohaha%2FMyPaxos","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luohaha%2FMyPaxos/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luohaha%2FMyPaxos/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luohaha%2FMyPaxos/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/luohaha","download_url":"https://codeload.github.com/luohaha/MyPaxos/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luohaha%2FMyPaxos/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32310804,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-26T19:15:34.056Z","status":"ssl_error","status_checked_at":"2026-04-26T19:15:15.467Z","response_time":129,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["multi-paxos","paxos"],"created_at":"2026-04-11T01:00:36.061Z","updated_at":"2026-04-26T20:00:45.276Z","avatar_url":"https://github.com/luohaha.png","language":"Java","funding_links":[],"categories":["分布式开发"],"sub_categories":["Spring Cloud框架"],"readme":"# MyPaxos\n\nMy multi-paxos service implement :-)  \n\n## 说明\n\n这是对multi-paxos协议服务的实现，并提供了给了使用者可以拓展的简单接口，以及简单的客户端。本实现有如下特性：\n\n* 实现了multi-paxos中连续同一leader提交时，优化协议流程，将prepare和accept流程，优化到只有accept流程。\n* 实现了节点崩溃恢复的机制。\n* 提供了简单的易用的拓展接口给使用者，使用者可以基于此实现基于paxos服务的系统。\n* 提供了分组功能，单个节点上可以设置多个虚拟分组，各个分组之间逻辑独立。\n\n## paxos协议的简单说明\n\nPaxos算法解决的问题是在一个可能发生上述异常的分布式系统中如何就某个值达成一致，保证不论发生以上任何异常，都不会破坏决议的一致性。一个典型的场景是，在一个分布式数据库系统中，如果各节点的初始状态一致，每个节点都执行相同的操作序列，那么他们最后能得到一个一致的状态。为保证每个节点执行相同的命令序列，需要在每一条指令上执行一个“一致性算法”以保证每个节点看到的指令一致。  \n\npaxos协议中有三种角色：\n* proposer : 提案的发起者\n* accepter : 提案的接受者\n* learner : 提案的学习者\n\npaxos协议保证在每一轮的提案中，只要某一个提案被大于半数的accepter接受，本轮的提案也就生效了，不会再被修改和破坏。具体的算法说明可以看[维基百科](https://zh.wikipedia.org/wiki/Paxos%E7%AE%97%E6%B3%95)。\n\n## 使用\n\n使用MyPaxos协议服务，需要下面几步：\n* 实现提交成功时需要执行的回调函数`PaxosCallback`\n* 在各个节点上修改配置文件，启动paxos服务器\n* 启动客户端，执行提交请求\n\n我在这里使用MyPaxos来实现一个分布式的简单kv存储。\n\n* 配置文件信息\n\n```json\n{\n    \"nodes\" : [{\n        // 节点1，服务器端口为33333\n        \"id\" : 1,\n        \"host\" : \"localhost\",\n        \"port\" : 33333\n    },\n    {\n        \"id\" : 2,\n        \"host\" : \"localhost\",\n        \"port\" : 33334\n    },\n    {\n        \"id\" : 3,\n        \"host\" : \"localhost\",\n        \"port\" : 33335\n    }],\n    \"myid\" : 1,    //本节点的id\n    \"timeout\" : 1000, //通信超时\n    \"learningInterval\" : 1000, // learner的学习时间间隔\n    \"dataDir\" : \"./dataDir/\", // 数据持久化的位置，用于崩溃恢复\n    \"enableDataPersistence\" : false // 是否开启数据持久化功能\n}\n```\n\n* 提交成功后的回调函数\n\n```java\npublic class KvCallback implements PaxosCallback {\n\t/**\n\t * 使用map来保存key与value映射\n\t */\n\tprivate Map\u003cString, String\u003e kv = new HashMap\u003c\u003e();\n\tprivate Gson gson = new Gson();\n\n\t@Override\n\tpublic void callback(String msg) {\n\t\t/**\n\t\t * 一共提供了三种动作： get : 获取 put : 添加 delete : 删除\n\t\t */\n\t\tMsgBean bean = gson.fromJson(msg, MsgBean.class);\n\t\tswitch (bean.getType()) {\n\t\tcase \"get\":\n\t\t\tSystem.out.println(kv.get(bean.getKey()));\n\t\t\tbreak;\n\t\tcase \"put\":\n\t\t\tkv.put(bean.getKey(), bean.getValue());\n\t\t\tSystem.out.println(\"ok\");\n\t\t\tbreak;\n\t\tcase \"delete\":\n\t\t\tkv.remove(bean.getKey());\n\t\t\tSystem.out.println(\"ok\");\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tbreak;\n\t\t}\n\t}\n\n}\n```\n\n* 消息格式\n\n```java\npublic class MsgBean {\n\t\n\tprivate String type;\n\tprivate String key;\n\tprivate String value;\n\tpublic MsgBean(String type, String key, String value) {\n\t\tsuper();\n\t\tthis.type = type;\n\t\tthis.key = key;\n\t\tthis.value = value;\n\t}\n\tpublic String getType() {\n\t\treturn type;\n\t}\n\tpublic void setType(String type) {\n\t\tthis.type = type;\n\t}\n\tpublic String getKey() {\n\t\treturn key;\n\t}\n\tpublic void setKey(String key) {\n\t\tthis.key = key;\n\t}\n\tpublic String getValue() {\n\t\treturn value;\n\t}\n\tpublic void setValue(String value) {\n\t\tthis.value = value;\n\t}\n\n}\n```\n\n* 启动服务器\n\n```java\npublic class ServerTest {\n\tpublic static void main(String[] args) {\n\t\tMyPaxos server = new MyPaxos(\"./conf/conf.json\");\n\t\t// 设置分组group1\n\t\tserver.setGroupId(1, new KvCallback());\n\t\t// 设置分组group2\n\t\tserver.setGroupId(2, new KvCallback());\n\t\ttry {\n\t\t\tserver.start();\n\t\t} catch (IOException | InterruptedException e) {\n\t\t\t// TODO Auto-generated catch block\n\t\t\te.printStackTrace();\n\t\t}\n\t}\n}\n```\n\n* 启动客户端，并发送请求\n\n```java\npublic class ClientTest {\n\tpublic static void main(String[] args) {\n\t\tMyPaxosClient client = new MyPaxosClient(\"localhost\", 33333);\n\t\ttry {\n\t\t    // 发往group1\n\t\t\tclient.submit(new Gson().toJson(new MsgBean(\"put\", \"name\", \"Mike\")), 1);\n\t\t\t// 发往group2\n\t\t\tclient.submit(new Gson().toJson(new MsgBean(\"put\", \"name\", \"Neo\")), 2);\n\t\t\t// 发往group1\n\t\t\tclient.submit(new Gson().toJson(new MsgBean(\"get\", \"name\", \"\")), 1);\n\t\t} catch (IOException e) {\n\t\t\t// TODO Auto-generated catch block\n\t\t\te.printStackTrace();\n\t\t}\n\t}\n}\n```\n\n* 结果\n\n节点1\n\n```\npaxos server-1 start...\nok\nok\nMike\n```\n\n节点2\n\n```\npaxos server-2 start...\nok\nok\nMike\n```\n\n节点3\n\n```\npaxos server-3 start...\nok\nok\nMike\n```\n\n## 参考文献和资料\n\n* Lamport, Leslie. \"The part-time parliament.\" ACM Transactions on Computer Systems (TOCS) 16.2 (1998): 133-169.\n* Lamport, Leslie. \"Paxos made simple.\" ACM Sigact News 32.4 (2001): 18-25.\n* Primi, Marco. Paxos made code. Diss. University of Lugano, 2009.\n* Chandra, Tushar D., Robert Griesemer, and Joshua Redstone. \"Paxos made live: an engineering perspective.\" Proceedings of the twenty-sixth annual ACM symposium on Principles of distributed computing. ACM, 2007.\n* [微信自研生产级paxos类库PhxPaxos实现原理介绍](http://mp.weixin.qq.com/s?__biz=MzI4NDMyNTU2Mw==\u0026mid=2247483695\u0026idx=1\u0026sn=91ea422913fc62579e020e941d1d059e#rd)\n* [Paxos理论介绍(1): 朴素Paxos算法理论推导与证明](https://zhuanlan.zhihu.com/p/21438357?refer=lynncui)\n* [Paxos理论介绍(2): Multi-Paxos与Leader](https://zhuanlan.zhihu.com/p/21466932?refer=lynncui)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluohaha%2FMyPaxos","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fluohaha%2FMyPaxos","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluohaha%2FMyPaxos/lists"}