Zookeeper实现分布式锁代码实例
@RestController
@RequestMapping("/zookeeper")
public class ZooKeeperController {
@Autowired
private CuratorFramework zkClient;
@Autowired
private ZooKeeperImpl zooKeeper;
/**
* zookeeper 获取节点下的数据
*
* post请求: http://localhost:8082/zookeeper/makeOrder?path=/task
*
* @param path
* @return
*/
@PostMapping("/getData")
public String getData(@RequestParam String path) {
byte[] bytes = null;
try {
bytes = zkClient.getData().forPath(path);
} catch (Exception e) {
e.printStackTrace();
}
String str = new String(bytes);
return str;
}
@PostMapping("/create")
public String create(@RequestParam String path) {
try {
zkClient.create().forPath(path);
} catch (Exception e) {
e.printStackTrace();
}
return "success";
}
@PostMapping("/delete")
public String delete(@RequestParam String path) {
try {
zkClient.delete().forPath(path);
} catch (Exception e) {
e.printStackTrace();
}
return "success";
}
@PostMapping("/setData")
public String setData(@RequestParam(value = "path") String path, @RequestParam(value = "data") String data) {
try {
zkClient.setData().forPath(path, data.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
return "success";
}
@PostMapping("/check")
public String check(@RequestParam(value = "path") String path) {
Stat stat = null;
try {
stat = zkClient.checkExists().forPath(path);
} catch (Exception e) {
e.printStackTrace();
}
return "stat" + stat;
}
@PostMapping("/children")
public String children(@RequestParam(value = "path") String path) {
List
try {
children = zkClient.getChildren().forPath(path);
} catch (Exception e) {
e.printStackTrace();
}
return "children" + children;
}
@PostMapping("/watch")
public String watch(@RequestParam(value = "path") String path) {
Stat stat = null;
try {
stat = zkClient.checkExists().watched().forPath(path);
} catch (Exception e) {
e.printStackTrace();
}
return "watch " + stat;
}
/**
* zookeeper分布式锁
*
* @param product
* @return
*/
@PostMapping("/makeOrder")
public String makeOrder(@RequestParam(value = "product") String product) {
zooKeeper.makeOrder(product);
return "success";
}
}
- .NET Core系列之MemoryCache 初识
- 007手机一键Root(安机网一键Root) v3.0 官方最新版 一键ROOT您的Android手机
- 12306密码被盗了怎么办?12306密码外泄解决方法
- 12个字的qq网名
- 150M迷你型无线路由器怎么设置?
- 192.168.1.1打不开怎么办?路由器192.168.1.1打不开的原因以及解决办法
- 2011年电子报合订本 电子报 编辑部 中文 PDF版 [84M]
- 2015年1月15日小米新旗舰发布会现场图文直播
- 2016.3.1vivo Xplay5新品发布会现场视频直播 优酷直播
- 2016华为P9发布会视频直播地址 4月15日华为P9国行发布会直播