serf 简单使用
1. 介绍
// 以下为官方介绍,说白了就是进行系统的集群节点管理
Serf uses an efficient gossip protocol to solve three major problems:
Membership: Serf maintains cluster membership lists and is able to execute custom handler scripts when that membership changes. For example, Serf can maintain the list of web servers for a load balancer and notify that load balancer whenever a node comes online or goes offline.
Failure detection and recovery: Serf automatically detects failed nodes within seconds, notifies the rest of the cluster, and executes handler scripts allowing you to handle these events. Serf will attempt to recover failed nodes by reconnecting to them periodically.
Custom event propagation: Serf can broadcast custom events and queries to the cluster. These can be used to trigger deploys, propagate configuration, etc. Events are simply fire-and-forget broadcast, and Serf makes a best effort to deliver messages in the face of offline nodes or network partitions. Queries provide a simple realtime request/response mechanism.
2. 使用
// 测试的是连个节点的模式
a. download
https://www.serf.io/downloads.html
b. start up
serf agent 实际使用为 serf agent -node=node-name -bind=bindaddress
c. join cluster
serf join nodebindaddress
d. members
serf members
e. leave cluster
stop the serf agent
3. 扩展
// event handler
foo.sh - The script "foo.sh" will be invoked for any/every event.
member-join=foo.sh - The script "foo.sh" will only be invoked for the "member-join" event.
member-join,member-leave=foo.sh - The script "foo.sh" will be invoked for either member-join or member-leave events. Any combination of events may be specified in this way.
user=foo.sh - The script "foo.sh" will be invoked for all user events.
user:deploy=foo.sh - The script "foo.sh" will be invoked only for "deploy" user events.
query=foo.sh - The script "foo.sh" will be invoked for all queries.
query:load=uptime - The uptime command will be invoked only for "load" queries.
4. 实际案例
// web-site loadbalance
https://github.com/hashicorp/serf/tree/master/demo/web-load-balancer
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)