javascript基础
This commit is contained in:
19
code/Distributed-Lock/src/main/java/com/lock/RedisLock.java
Normal file
19
code/Distributed-Lock/src/main/java/com/lock/RedisLock.java
Normal file
@ -0,0 +1,19 @@
|
||||
package com.lock;
|
||||
|
||||
import org.redisson.Redisson;
|
||||
import org.redisson.api.RLock;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.redisson.config.Config;
|
||||
import org.redisson.config.TransportMode;
|
||||
|
||||
public class RedisLock {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Config config = new Config();
|
||||
config.setTransportMode(TransportMode.EPOLL);
|
||||
config.useSingleServer().setAddress("redis://127.0.0.1:7181");
|
||||
RedissonClient client = Redisson.create(config);
|
||||
RLock mylock = client.getLock("mylock");
|
||||
mylock.lock();
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package com.lock;
|
||||
|
||||
import org.apache.curator.RetryPolicy;
|
||||
import org.apache.curator.framework.CuratorFramework;
|
||||
import org.apache.curator.framework.CuratorFrameworkFactory;
|
||||
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
|
||||
import org.apache.curator.retry.ExponentialBackoffRetry;
|
||||
|
||||
public class ZooKeeperLock {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
|
||||
CuratorFramework client = CuratorFrameworkFactory.newClient("hadoop001:2181", retryPolicy);
|
||||
client.start();
|
||||
InterProcessMutex mutex = new InterProcessMutex(client, "/curator/lock");
|
||||
mutex.acquire();
|
||||
mutex.release();
|
||||
client.close();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user