Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【Q020】如何实现一个分布式锁 #21

Open
shfshanyue opened this issue Nov 9, 2019 · 2 comments
Open

【Q020】如何实现一个分布式锁 #21

shfshanyue opened this issue Nov 9, 2019 · 2 comments

Comments

@shfshanyue
Copy link
Owner

No description provided.

@zhangxiaokun
Copy link

mysql,redis,zk redis效率较高

@shfshanyue
Copy link
Owner Author

shfshanyue commented Nov 12, 2019

多节点部署就会产生分布式问题,分布式锁由两个词组成,分布式

  1. 分布式: 解决分布式问题就要找一个大家都能够访问到的中介,比如 RedisConsulZookeeper
  2. 锁: 解决原子问题,当不存在时便加锁,不存在加锁 要作为原子进行操作

以下是一个 redis 实现的操作

# EX 100:100s 的过期时间
# NX: 如果不存在 User:10086,设置成功返回 OK,否则返回 nil,也就是说 100s 之内只有第一次操作返回 OK
set User:10086 Random:shanyue EX 100 NX

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants