分布式锁功能:

在分布式场景中,我们为了保证数据的一致性,经常在程序运行的某一个点,需要进行同步操作,(java提供synchronized或者Reentrantlock实现),

使用curator基于zookeeper的特性提供的分布式锁来处理分布式场景的数据一致性,zookeeper本身的分布式是有写问题的,这里推荐使用Curator框架的

InterProcessMutex来实现。

 package bjsxt.curator.lock;

 import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.CountDownLatch; 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 Lock2 { /** zookeeper地址 */
static final String CONNECT_ADDR = "192.168.2.2:2181";
/** session超时时间 */
static final int SESSION_OUTTIME = 5000;// ms static int count = 10; public static void genarNo() {
try {
count--;
System.out.println(count);
} finally { }
} public static void main(String[] args) throws Exception { // 1 重试策略:初试时间为1s 重试10次
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 10);
// 2 通过工厂创建连接
CuratorFramework cf = CuratorFrameworkFactory.builder()
.connectString(CONNECT_ADDR).sessionTimeoutMs(SESSION_OUTTIME)
.retryPolicy(retryPolicy)
// .namespace("super")
.build();
// 3 开启连接
cf.start(); // 4 分布式锁
final InterProcessMutex lock = new InterProcessMutex(cf, "/super");
// final ReentrantLock reentrantLock = new ReentrantLock();
final CountDownLatch countdown = new CountDownLatch(1); for (int i = 0; i < 10; i++) {
new Thread(new Runnable() {
@Override
public void run() {
try {
countdown.await();
// 加锁
lock.acquire();
// reentrantLock.lock();
// -------------业务处理开始
// genarNo();
SimpleDateFormat sdf = new SimpleDateFormat(
"HH:mm:ss|SSS");
System.out.println(sdf.format(new Date()));
// System.out.println(System.currentTimeMillis());
// -------------业务处理结束
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 释放
lock.release();
// reentrantLock.unlock();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}, "t" + i).start();
}
Thread.sleep(100);
countdown.countDown(); }
}

分布式计数器功能

分布式计数器,在单JVM中,我们可以通过AtomicInteger这种经典的方式实现,但是在分布式的场景下,就需要利用Curator框架的DistributedAtomicInteger来实现

 package bjsxt.curator.atomicinteger;

 import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.atomic.AtomicValue;
import org.apache.curator.framework.recipes.atomic.DistributedAtomicInteger;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.retry.RetryNTimes; public class CuratorAtomicInteger { /** zookeeper地址 */
static final String CONNECT_ADDR = "192.168.2.2:2181";
/** session超时时间 */
static final int SESSION_OUTTIME = 5000;// ms public static void main(String[] args) throws Exception { // 1 重试策略:初试时间为1s 重试10次
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 10);
// 2 通过工厂创建连接
CuratorFramework cf = CuratorFrameworkFactory.builder()
.connectString(CONNECT_ADDR).sessionTimeoutMs(SESSION_OUTTIME)
.retryPolicy(retryPolicy).build();
// 3 开启连接
cf.start();
// cf.delete().forPath("/super"); // 4 使用DistributedAtomicInteger
DistributedAtomicInteger atomicIntger = new DistributedAtomicInteger(
cf, "/super", new RetryNTimes(3, 1000)); AtomicValue<Integer> value = atomicIntger.add(1);
System.out.println(value.succeeded());
System.out.println(value.postValue()); // 最新值
System.out.println(value.preValue()); // 原始值 }
}

Curator框架,让一些很困难的问题,简单化了

Curator场景应用的更多相关文章

  1. Zookeeper 系列(五)Curator API

    Zookeeper 系列(五)Curator API 一.Curator 使用 Curator 框架中使用链式编程风格,易读性更强,使用工程方法创建连接对象使用. (1) CuratorFramewo ...

  2. Zookeeper开源客户端框架Curator简介

    Curator是Netflix开源的一套ZooKeeper客户端框架. Netflix在使用ZooKeeper的过程中发现ZooKeeper自带的客户端太底层, 应用方在使用的时候需要自己处理很多事情 ...

  3. Curator leader 选举(一)

    要想使用Leader选举功能,需要添加recipes包,可以在maven中添加如下依赖: <dependency> <groupId>org.apache.curator< ...

  4. Zookeeper开源客户端框架Curator简介[转]

    Curator是Netflix开源的一套ZooKeeper客户端框架. Netflix在使用ZooKeeper的过程中发现ZooKeeper自带的客户端太底层, 应用方在使用的时候需要自己处理很多事情 ...

  5. twitter storm源码走读之1 -- nimbus启动场景分析

    欢迎转载,转载时请注明作者徽沪一郎及出处,谢谢. 本文详细介绍了twitter storm中的nimbus节点的启动场景,分析nimbus是如何一步步实现定义于storm.thrift中的servic ...

  6. zookeeper典型应用场景之一:master选举

    对于zookeeper这种东西,仅仅知道怎么安装是远远不够的,至少要对其几个典型的应用场景进行了解,才能比较全面的知道zk究竟能干啥,怎么玩儿,以后的日子里才能知道这货如何能为我所用.于是,有了如下的 ...

  7. Service Discovery with Apache Curator

    Curator的介绍 Curator就是Zookeeper的一个客户端工具(不知道Zookeeper的同学可以到http://www.ibm.com/developerworks/cn/opensou ...

  8. 通过Curator操作Zookeeper的简单例子代码

    Curator主要解决了三类问题: 一个是ZooKeeper client与ZooKeeper server之间的连接处理; 一个是提供了一套Fluent风格的操作API; 一个是ZooKeeper各 ...

  9. zookeeper curator选主(Leader)

    在分布式系统设计中,选主是一个常见的场景.选主是一个这样的过程,通过选主,主节点被选择出来控制其他节点或者是分配任务. 选主算法要满足的几个特征: 1)各个节点均衡的获得成为主节点的权利,一旦主节点被 ...

随机推荐

  1. lucas 模板 洛古模板题

    #include<bits/stdc++.h> #define int long long using namespace std; ; int a[maxn]; int quick(in ...

  2. glog学习(一):glog的编译及demo

    windows平台: 1.下载glog代码.下载地址:https://github.com/google/glog 2.使用cmake工具,获得对应的工程文件sln. 3.打开sln文件,生成对应的l ...

  3. oracle错误汇总1

    这是遇见的第一个整个库正常,但某张表查询报错的情况 某张表数据可以查,但一排序查就报错 select * from acct_daily_bak; select * from acct_daily_b ...

  4. 我发起并创立了一个 C 语言编译器 开源项目 InnerC

    本文是 VMBC / D#  项目 的 系列文章, 有关 VMBC / D# ,  见 <我发起并创立了一个 VMBC 的 子项目 D#>(以下简称 <D#>)  https: ...

  5. 1.1.10 word2010图片另存为存到图片库里,怎么修改之

    1. 打开一个文档,在图片上右击,图片另存为,在弹出的[保存文件]对话框中. 2.单击左上角的[组织]下拉列表"属性"按钮,在弹出的[图片 属性]对话框中,单击[包含文件夹]按钮, ...

  6. QQ群成员发言次数统计(正则表达式版)

    1.先将QQ群的消息记录以.txt文件格式导出来,保存路径及名称自己定义(在本文我导出到Y盘,命名为test.txt) 2.程序如下: data statistics1; if _n_=1 then ...

  7. 变量、&连接、函数与过程、按下某键

    1.变量的声明 常量 信息 为 文本型 = "你好,世界!" '这是常量 变量 结果 为 整数型 '这是程序集变量,在程序集的开头定义即可 变量 测试 为 整数型 = '可以直接给 ...

  8. WINDOWS NT操作系统的注册表文件

    WINDOWS NT操作系统的注册表文件 WINDOWS NT注册表文件分为系统文件和用户文件两类. 系统设置和缺少用户 配置数据存放在系统C:\Windows\System32\config文件夹下 ...

  9. 常量&字符编码

    day1 name='Nod Chen' name2=name print('My name is ',name,name2) name='Luna zhou' print(name,name2) _ ...

  10. 内训--2小时 Word精髓

    企业内部使用Word最常见就是用来写产品手册与合同,产品手册是书版(即可以出版,与买到的书类似),合同重点是修订.Word的精髓在于样式,或者说在企业办公使用,学会样式就可以了.什么域.宏.VBA几乎 ...