用java实现etcd分布式锁_ETCD 分布式锁实现逻辑

https://github.com/coreos/etcd/blob/master/Documentation/api.md

Atomic Compare-and-Swap

etcd can be used as a centralized coordination service in a cluster, and CompareAndSwap (CAS) is the most basic operation used to build a distributed lock service.

This command will set the value of a key only if the client-provided conditions are equal to the current conditions.

The current comparable conditions are:

prevValue - checks the previous value of the key.

prevIndex - checks the previous modifiedIndex of the key.

prevExist - checks existence of the key: if prevExist is true, it is an update request; if prevExist is false, it is acreate request.

Here is a simple example. Let's create a key-value pair first: foo=one.

curl http://127.0.0.1:2379/v2/keys/foo -XPUT -d value=one

Now let's try some invalid CompareAndSwap commands.

Trying to set this existing key with prevExist=false fails as expected:

curl http://127.0.0.1:2379/v2/keys/foo?prevExist=false -XPUT -d value=three

The error code explains the problem:

{

"cause": "/foo",

"errorCode": 105,

"index": 39776,

"message": "Key already exists"

}

Now let's provide a prevValue parameter:

curl http://127.0.0.1:2379/v2/keys/foo?prevValue=two -XPUT -d value=three

This will try to compare the previous value of the key and the previous value we provided. If they are equal, the value of the key will change to three.

{

"cause": "[two != one]",

"errorCode": 101,

"index": 8,

"message": "Compare failed"

}

which means CompareAndSwap failed. cause explains why the test failed. Note: the condition prevIndex=0 always passes.

Let'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java实现Redis分布式可以通过使用Redis的SETNX命令来实现。SETNX命令可以在Redis中设置一个键值对,但只有在键不存在的情况下才会设置成功。因此,可以将某个键作为的标识,当某个线程成功执行SETNX命令并获得时,其他线程执行SETNX命令时会失败,从而实现分布式的效果。在释放时,可以使用Redis的DEL命令来删除对应的键。 另外,Java中也可以使用显式(Lock)来实现分布式。通过使用ReentrantLock类,可以在代码中显式地加和解。在同一个线程中,当外层方法获取后,再进入内层方法时会自动获取,不会因为之前已经获取过而阻塞。这种可重入的特性可以一定程度上避免死的发生。 总结起来,Java实现Redis分布式可以通过使用Redis的SETNX命令或者使用显式(Lock)来实现。具体的实现方式可以根据实际需求和场景选择。\[2\]\[3\] #### 引用[.reference_title] - *1* *3* [Java --- redis7实现分布式](https://blog.csdn.net/qq_46093575/article/details/130661856)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Redis分布式的正确实现方式(Java版)](https://blog.csdn.net/zth_killer/article/details/106853052)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值