etcdsync 一个golang的ectd分布式锁实现

Golang 归档:201901
普通
浏览:131
2019-01-12 21:58:42
etcdsync 一个golang的ectd分布式锁实现

项目地址:https://github.com/zieckey/etcdsync

etcdsync

介绍

etcdsync is a distributed lock library in Go using etcd. It easy to use like sync.Mutex.
In fact, there are many similar implementation which are all obsolete depending on library github.com/coreos/go-etcd/etcd which is official marked deprecated, and the usage is a little bit complicated. Otherwise this library is very very simple. The usage is simple, the code is simple.

安装

go get -u github.com/zieckey/etcdsync

demo

package main

import (
    "log"
    "os"

    "github.com/zieckey/etcdsync"
)

func main() {
    m, err := etcdsync.New("/mylock", 10, []string{"http://127.0.0.1:2379"})
    if m == nil || err != nil {
        log.Printf("etcdsync.New failed")
        return
    }
    err = m.Lock()
    if err != nil {
        log.Printf("etcdsync.Lock failed")
    } else {
        log.Printf("etcdsync.Lock OK")
    }

    log.Printf("Get the lock. Do something here.")

    err = m.Unlock()
    if err != nil {
        log.Printf("etcdsync.Unlock failed")
    } else {
        log.Printf("etcdsync.Unlock OK")
    }
}

注意
需要跑一个ectd在 http://localhost:2379

使用docker跑一个etcd

docker run \
  -p 2379:2379 \
  -p 4001:4001 \
  --name etcd \
  -v /usr/share/ca-certificates/:/etc/ssl/certs \
  quay.io/coreos/etcd:v2.3.0-alpha.1 \
  -listen-client-urls http://0.0.0.0:2379,http://0.0.0.0:4001
注意事项
  • 本文地址:https://22v.net/article/10/
  • 转载本文时,请注明转载自“SamBlog”的字样。
  • 如此文章有损您的合法权益,请使用页面底部的邮箱与我取得联系。
分类目录
文章归档
友情站点