{"id":16660465,"url":"https://github.com/killme2008/lighthouse","last_synced_at":"2025-04-09T18:43:21.644Z","repository":{"id":32287865,"uuid":"35862694","full_name":"killme2008/lighthouse","owner":"killme2008","description":"leader election and node register/discover/balance in a service cluster by zookeeper in clojure","archived":false,"fork":false,"pushed_at":"2015-07-30T08:53:45.000Z","size":160,"stargazers_count":6,"open_issues_count":0,"forks_count":4,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-04-14T16:06:36.039Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Clojure","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"epl-1.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/killme2008.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-05-19T06:08:21.000Z","updated_at":"2022-03-03T11:23:29.000Z","dependencies_parsed_at":"2022-08-07T17:15:46.838Z","dependency_job_id":null,"html_url":"https://github.com/killme2008/lighthouse","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/killme2008%2Flighthouse","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/killme2008%2Flighthouse/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/killme2008%2Flighthouse/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/killme2008%2Flighthouse/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/killme2008","download_url":"https://codeload.github.com/killme2008/lighthouse/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248090462,"owners_count":21046094,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":[],"created_at":"2024-10-12T10:29:23.108Z","updated_at":"2025-04-09T18:43:21.623Z","avatar_url":"https://github.com/killme2008.png","language":"Clojure","funding_links":[],"categories":[],"sub_categories":[],"readme":"# lighthouse\n\nA Clojure library designed to leader election and node register/discover/balance in a service cluster by zookeeper.\n\n## Usage\n\nAdd dependency in your project:\n\n```clj\n[lighthouse \"0.1.0\"]\n```\n\nCreate a [curator](http://curator.apache.org/) framework client:\n\n```clj\n(require '[lighthouse.zk :as zk])\n(def cli (zk/mk-client \"localhost:2181\"))\n```\n\n\n### Service register/discover/balance\n\nIn a service node:\n\n```clj\n(require '[lighthouse.discover :refer :all])\n\n;;register a node to zookeeper\n(register cli \"user-service\")\n```\n\nEvery service node can register itself to zookeeper by above code.\n\nThe default registerd node id and data is the node's machine hostname,but you can special them by:\n\n```clj\n(def node (register cli \"user-service\" :id \"node-1\" :data \"192.168.1.20:8080\"))\n```\n\nWhen you shutdown the node, you can unregister it by `(unregister node)`.\n\nIn a service consumer, you can create a balancer for `user-service`:\n\n```clj\n(require '[lighthouse.discover :refer :all])\n\n(def user-service-balancer (create-service-balancer cli \"user-service\"))\n```\n\nThen You can get an alive `user-service` node:\n\n```\n(user-service-balancer)\n```\n\nIt returns a node value map: `{:id \"node-1\" :data \"192.168.1.20:8080\"}`。\n\nThe default balance type is `:round-robin`,you can create other balancer type:\n\n```\n(def user-service-balancer (create-service-balancer cli \"user-service\" :balancer-type :random))\n(def user-service-balancer (create-service-balancer cli \"user-service\" :balancer-type :hash))\n```\n\nWhen you use `:hash` strategy, you must pass a key to balancer for hashing:\n\n```clj\n(user-service-balancer \"a\")\n(user-service-balancer \"b\")\n;;pass a default value that returned when can not find an alive node.\n(user-service-balancer \"a\" {:id \"default-node\" :data \"node1.ip\"})\n```\nStop the balancer:\n\n```clj\n(stop-service-balancer user-service-balancer)\n```\n\n### Leader election\n\nEvery node trys to elect a leader:\n\n```clj\n(require '[lighthouse.leader :refer :all])\n\n(start-election cli \"/leader_election\"\n  (fn [cli path id]\n    (println id \"got leadership.\"))\n  (fn [cli path id]\n    (println id \"released leadership.\"))\n  :id \"node-1\")\n```\n\nEach node pass it's id,if not present, the default id is the node's matchine hostname.\n\n```clj\n(start-election client path on-aquired on-released \u0026 {:keys [id]})\n```\n\nWhen the node got the leadership, the `on-aquired` would be called with curator client,path and itself id.\nAnd when the node released the leadership, the `on-released` would be called with curator client,path and itself id.\n\nThe `start-election` returns a resettable promise, you can deliver it a true value to stop the election, or a false value to\nrelease the leadership but still be in queue for next election:\n\n```clj\n(def p (start-election ......))\n\n;;release the leadership,but still be in queue for next election\n(deliver p false)\n;;stop current node in election\n(deliver p true)\n```\n\nWhen election starts, you can get current leader or participants:\n\n```clj\n(get-leader cli \"/leader_election\")\n(get-participants cli \"/leader_election\")\n```\n\nStop current node in election:\n\n```\n(stop)\n```\n\n\n## License\n\nCopyright © 2015 dennis zhuang\n\nDistributed under the Eclipse Public License either version 1.0 or (at\nyour option) any later version.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkillme2008%2Flighthouse","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkillme2008%2Flighthouse","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkillme2008%2Flighthouse/lists"}