zookeeper 分布式barrier


zookeeper 分布式barrier

 

分布式barrier可在分布式环境中控制进程同步

 

*************************

相关类

 

DistributrdBarrier

public class DistributedBarrier {

*******************************
构造方法

    public DistributedBarrier(CuratorFramework client, String barrierPath) {

*******************************
常用方法

    public synchronized void setBarrier() throws Exception {

    public synchronized void removeBarrier() throws Exception {

    public synchronized void waitOnBarrier() throws Exception {
    public synchronized boolean waitOnBarrier(long maxWait, TimeUnit unit) throws Exception { 

 

DistributedDoubleBarrier

public class DistributedDoubleBarrier {

****************************
构造方法

    public DistributedDoubleBarrier(CuratorFramework client, String barrierPath, int memberQty) {

******************************
常用方法

    public void enter() throws Exception {  
                             //一直等待,满足条件后继续执行
    public boolean enter(long maxWait, TimeUnit unit) throws Exception {
                             //最长等待maxWait后继续执行

    public synchronized void leave() throws Exception {
                             //一直阻塞,满足条件后离开
    public synchronized boolean leave(long maxWait, TimeUnit unit) throws Exception {
                             //最长到时间自动离开

 

*************************

使用示例  DistributedBarrier

 

*********************

config 层

 

@Configuration
public class ZookeeperConfig {

    @Value("${zookeeper.connecting-string}")
    private String connectingString;

    @Bean
    public CuratorFramework initCuratorFramework(){
        CuratorFramework curatorFramework= CuratorFrameworkFactory.builder()
                .connectString(connectingString)
                .sessionTimeoutMs(5000)
                .retryPolicy(new ExponentialBackoffRetry(1000,3,3000))
                .namespace("test")
                .build();
        curatorFramework.start();

        return curatorFramework;
    }
}

 

********************

controller 层

 

@RestController
public class HelloController {

    @Autowired
    private CuratorFramework curatorFramework;

    private String barrierPath="/barrier";

    @RequestMapping("/get2")
    public String test(){
        DistributedBarrier barrier=new DistributedBarrier(curatorFramework,barrierPath);
        ExecutorService executorService=Executors.newFixedThreadPool(5);

        for(int i=0;i<5;i++){
            executorService.submit(()->{

                try{
                    barrier.setBarrier();
                    System.out.println(Thread.currentThread().getId()+" 设置barrier: "+System.currentTimeMillis());
                    Thread.sleep(1000);
                    barrier.waitOnBarrier();
                    System.out.println(Thread.currentThread().getId()+" 在barrier上等待: "+System.currentTimeMillis());
                }catch (Exception e){
                    e.printStackTrace();
                }
            });
        }

        try{
            Thread.sleep(2000);
            System.out.println(Thread.currentThread().getId()+" 移除barrier: "+System.currentTimeMillis());
            barrier.removeBarrier();
        }catch (Exception e){
            e.printStackTrace();
        }

        return "success";
    }
}

 

****************

控制台输出

 

40 设置barrier: 1569199694093
42 设置barrier: 1569199694098
44 设置barrier: 1569199694101
43 设置barrier: 1569199694106
41 设置barrier: 1569199694107
26 移除barrier: 1569199696082
40 在barrier上等待: 1569199696096
41 在barrier上等待: 1569199696098
43 在barrier上等待: 1569199696100
44 在barrier上等待: 1569199696104
42 在barrier上等待: 1569199696107

 

 

*************************

使用示例 DistriburedDoubleBarrier

 

********************

config 层

 

@Configuration
public class ZookeeperConfig {

    @Value("${zookeeper.connecting-string}")
    private String connectingString;

    @Bean
    public CuratorFramework initCuratorFramework(){
        CuratorFramework curatorFramework= CuratorFrameworkFactory.builder()
                .connectString(connectingString)
                .sessionTimeoutMs(5000)
                .retryPolicy(new ExponentialBackoffRetry(1000,3,3000))
                .namespace("test")
                .build();
        curatorFramework.start();

        return curatorFramework;
    }
}

 

********************

controller 层

 

@RestController
public class HelloController {

    @Autowired
    private CuratorFramework curatorFramework;

    private String doubleBarrier="/doubleBarrier";

    @RequestMapping("/get3")
    public String test2(){
        ExecutorService executorService=Executors.newFixedThreadPool(5);

        for(int i=0;i<5;i++){
            executorService.submit(()->{
                DistributedDoubleBarrier barrier=new DistributedDoubleBarrier(curatorFramework,doubleBarrier,5);

                try{
                    System.out.println(Thread.currentThread().getId()+" 即将进入barrier:"+System.currentTimeMillis());
                    barrier.enter();
                    Thread.sleep(2000);
                    System.out.println(Thread.currentThread().getId()+" 进入barrier: "+System.currentTimeMillis());
                }catch (Exception e){
                    e.printStackTrace();
                }finally {
                    try{
                        Thread.sleep(2000);
                        barrier.leave();
                        System.out.println(Thread.currentThread().getId()+" 退出barrier: "+System.currentTimeMillis());
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            });
        }

        return "success";
    }
}

 

****************

控制台输出

 

40 即将进入barrier:1569199586907
44 即将进入barrier:1569199586907
42 即将进入barrier:1569199586907
43 即将进入barrier:1569199586907
41 即将进入barrier:1569199586908
41 进入barrier: 1569199588969
40 进入barrier: 1569199588969
43 进入barrier: 1569199588969
42 进入barrier: 1569199588969
44 进入barrier: 1569199588969
41 退出barrier: 1569199591023
40 退出barrier: 1569199591027
42 退出barrier: 1569199591027
44 退出barrier: 1569199591027
43 退出barrier: 1569199591027

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值