用java实现etcd分布式锁_etcdsync 一个golang的ectd分布式锁实现

etcdsync

a95c20c28c057c5c.jpg

介绍

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")

}

}

使用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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值